blob: 3667fb1f39610e77fc6aba798cb1fc1b52502ad6 [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"
Darrick J. Wonge9e899a2017-10-31 12:04:49 -070041#include "xfs_errortag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_quota.h"
44#include "xfs_trans_space.h"
45#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000046#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110047#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110049#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100050#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100051#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070052#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110053#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056kmem_zone_t *xfs_bmap_free_item_zone;
57
58/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110059 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110063 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Dave Chinner9e5987a72013-02-25 12:31:26 +110066void
67xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
70{
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
78
79 /*
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
84 *
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
92 */
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
99 }
Eric Sandeen152d93b2014-04-14 18:58:51 +1000100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
109 }
110 mp->m_bm_maxlevels[whichfork] = level;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113STATIC int /* error */
114xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700116 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100117 int *stat) /* success/failure */
118{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700119 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100120 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
121}
122
123STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700124xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100125 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100126 int *stat) /* success/failure */
127{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700128 cur->bc_rec.b.br_startoff = 0;
129 cur->bc_rec.b.br_startblock = 0;
130 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100131 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
132}
133
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100134/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000135 * Check if the inode needs to be converted to btree format.
136 */
137static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
138{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700139 return whichfork != XFS_COW_FORK &&
140 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000141 XFS_IFORK_NEXTENTS(ip, whichfork) >
142 XFS_IFORK_MAXEXT(ip, whichfork);
143}
144
145/*
146 * Check if the inode should be converted to extent format.
147 */
148static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
149{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700150 return whichfork != XFS_COW_FORK &&
151 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000152 XFS_IFORK_NEXTENTS(ip, whichfork) <=
153 XFS_IFORK_MAXEXT(ip, whichfork);
154}
155
156/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700157 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100158 * This either works (return 0) or gets an EFSCORRUPTED error.
159 */
160STATIC int
161xfs_bmbt_update(
162 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700163 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100164{
165 union xfs_btree_rec rec;
166
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700167 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100168 return xfs_btree_update(cur, &rec);
169}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100172 * Compute the worst-case number of indirect blocks that will be used
173 * for ip's delayed extent of length "len".
174 */
175STATIC xfs_filblks_t
176xfs_bmap_worst_indlen(
177 xfs_inode_t *ip, /* incore inode pointer */
178 xfs_filblks_t len) /* delayed extent length */
179{
180 int level; /* btree level number */
181 int maxrecs; /* maximum record count at this level */
182 xfs_mount_t *mp; /* mount structure */
183 xfs_filblks_t rval; /* return value */
184
185 mp = ip->i_mount;
186 maxrecs = mp->m_bmap_dmxr[0];
187 for (level = 0, rval = 0;
188 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
189 level++) {
190 len += maxrecs - 1;
191 do_div(len, maxrecs);
192 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700193 if (len == 1)
194 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100195 level - 1;
196 if (level == 0)
197 maxrecs = mp->m_bmap_dmxr[1];
198 }
199 return rval;
200}
201
202/*
203 * Calculate the default attribute fork offset for newly created inodes.
204 */
205uint
206xfs_default_attroffset(
207 struct xfs_inode *ip)
208{
209 struct xfs_mount *mp = ip->i_mount;
210 uint offset;
211
212 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100213 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100214 XFS_BMDR_SPACE_CALC(MINABTPTRS);
215 } else {
216 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
217 }
218
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100219 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100220 return offset;
221}
222
223/*
224 * Helper routine to reset inode di_forkoff field when switching
225 * attribute fork from local to extent format - we reset it where
226 * possible to make space available for inline data fork extents.
227 */
228STATIC void
229xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100230 xfs_inode_t *ip,
231 int whichfork)
232{
233 if (whichfork == XFS_ATTR_FORK &&
234 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a72013-02-25 12:31:26 +1100235 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
236 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
237
238 if (dfl_forkoff > ip->i_d.di_forkoff)
239 ip->i_d.di_forkoff = dfl_forkoff;
240 }
241}
242
Dave Chinner9e5987a72013-02-25 12:31:26 +1100243#ifdef DEBUG
244STATIC struct xfs_buf *
245xfs_bmap_get_bp(
246 struct xfs_btree_cur *cur,
247 xfs_fsblock_t bno)
248{
249 struct xfs_log_item_desc *lidp;
250 int i;
251
252 if (!cur)
253 return NULL;
254
255 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
256 if (!cur->bc_bufs[i])
257 break;
258 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
259 return cur->bc_bufs[i];
260 }
261
262 /* Chase down all the log items to see if the bp is there */
263 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
264 struct xfs_buf_log_item *bip;
265 bip = (struct xfs_buf_log_item *)lidp->lid_item;
266 if (bip->bli_item.li_type == XFS_LI_BUF &&
267 XFS_BUF_ADDR(bip->bli_buf) == bno)
268 return bip->bli_buf;
269 }
270
271 return NULL;
272}
273
274STATIC void
275xfs_check_block(
276 struct xfs_btree_block *block,
277 xfs_mount_t *mp,
278 int root,
279 short sz)
280{
281 int i, j, dmxr;
282 __be64 *pp, *thispa; /* pointer to block address */
283 xfs_bmbt_key_t *prevp, *keyp;
284
285 ASSERT(be16_to_cpu(block->bb_level) > 0);
286
287 prevp = NULL;
288 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
289 dmxr = mp->m_bmap_dmxr[0];
290 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
291
292 if (prevp) {
293 ASSERT(be64_to_cpu(prevp->br_startoff) <
294 be64_to_cpu(keyp->br_startoff));
295 }
296 prevp = keyp;
297
298 /*
299 * Compare the block numbers to see if there are dups.
300 */
301 if (root)
302 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
303 else
304 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
305
306 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
307 if (root)
308 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
309 else
310 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
311 if (*thispa == *pp) {
312 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
313 __func__, j, i,
314 (unsigned long long)be64_to_cpu(*thispa));
315 panic("%s: ptrs are equal in node\n",
316 __func__);
317 }
318 }
319 }
320}
321
322/*
323 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100324 * btree leaves. THis becomes prohibitively expensive for large extent count
325 * files, so don't bother with inodes that have more than 10,000 extents in
326 * them. The btree record ordering checks will still be done, so for such large
327 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100328 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100329STATIC void
330xfs_bmap_check_leaf_extents(
331 xfs_btree_cur_t *cur, /* btree cursor or null */
332 xfs_inode_t *ip, /* incore inode pointer */
333 int whichfork) /* data or attr fork */
334{
335 struct xfs_btree_block *block; /* current btree block */
336 xfs_fsblock_t bno; /* block # of "block" */
337 xfs_buf_t *bp; /* buffer for "block" */
338 int error; /* error return value */
339 xfs_extnum_t i=0, j; /* index into the extents list */
340 xfs_ifork_t *ifp; /* fork structure */
341 int level; /* btree level, for checking */
342 xfs_mount_t *mp; /* file system mount structure */
343 __be64 *pp; /* pointer to block address */
344 xfs_bmbt_rec_t *ep; /* pointer to current extent */
345 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
346 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
347 int bp_release = 0;
348
349 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
350 return;
351 }
352
Dave Chinnere3543812016-01-08 11:28:49 +1100353 /* skip large extent count inodes */
354 if (ip->i_d.di_nextents > 10000)
355 return;
356
Dave Chinner9e5987a72013-02-25 12:31:26 +1100357 bno = NULLFSBLOCK;
358 mp = ip->i_mount;
359 ifp = XFS_IFORK_PTR(ip, whichfork);
360 block = ifp->if_broot;
361 /*
362 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
363 */
364 level = be16_to_cpu(block->bb_level);
365 ASSERT(level > 0);
366 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
367 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
368 bno = be64_to_cpu(*pp);
369
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000370 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100371 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
372 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
373
374 /*
375 * Go down the tree until leaf level is reached, following the first
376 * pointer (leftmost) at each level.
377 */
378 while (level-- > 0) {
379 /* See if buf is in cur first */
380 bp_release = 0;
381 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
382 if (!bp) {
383 bp_release = 1;
384 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
385 XFS_BMAP_BTREE_REF,
386 &xfs_bmbt_buf_ops);
387 if (error)
388 goto error_norelse;
389 }
390 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100391 if (level == 0)
392 break;
393
394 /*
395 * Check this block for basic sanity (increasing keys and
396 * no duplicate blocks).
397 */
398
399 xfs_check_block(block, mp, 0, 0);
400 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
401 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100402 XFS_WANT_CORRUPTED_GOTO(mp,
403 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100404 if (bp_release) {
405 bp_release = 0;
406 xfs_trans_brelse(NULL, bp);
407 }
408 }
409
410 /*
411 * Here with bp and block set to the leftmost leaf node in the tree.
412 */
413 i = 0;
414
415 /*
416 * Loop over all leaf nodes checking that all extents are in the right order.
417 */
418 for (;;) {
419 xfs_fsblock_t nextbno;
420 xfs_extnum_t num_recs;
421
422
423 num_recs = xfs_btree_get_numrecs(block);
424
425 /*
426 * Read-ahead the next leaf block, if any.
427 */
428
429 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
430
431 /*
432 * Check all the extents to make sure they are OK.
433 * If we had a previous block, the last entry should
434 * conform with the first entry in this one.
435 */
436
437 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
438 if (i) {
439 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
440 xfs_bmbt_disk_get_blockcount(&last) <=
441 xfs_bmbt_disk_get_startoff(ep));
442 }
443 for (j = 1; j < num_recs; j++) {
444 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
445 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
446 xfs_bmbt_disk_get_blockcount(ep) <=
447 xfs_bmbt_disk_get_startoff(nextp));
448 ep = nextp;
449 }
450
451 last = *ep;
452 i += num_recs;
453 if (bp_release) {
454 bp_release = 0;
455 xfs_trans_brelse(NULL, bp);
456 }
457 bno = nextbno;
458 /*
459 * If we've reached the end, stop.
460 */
461 if (bno == NULLFSBLOCK)
462 break;
463
464 bp_release = 0;
465 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
466 if (!bp) {
467 bp_release = 1;
468 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
469 XFS_BMAP_BTREE_REF,
470 &xfs_bmbt_buf_ops);
471 if (error)
472 goto error_norelse;
473 }
474 block = XFS_BUF_TO_BLOCK(bp);
475 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100476
Dave Chinner9e5987a72013-02-25 12:31:26 +1100477 return;
478
479error0:
480 xfs_warn(mp, "%s: at error0", __func__);
481 if (bp_release)
482 xfs_trans_brelse(NULL, bp);
483error_norelse:
484 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
485 __func__, i);
486 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
487 return;
488}
489
490/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100491 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000492 * given the caller's original parameters. Specifically check the
493 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100494 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
495 */
496STATIC void
497xfs_bmap_validate_ret(
498 xfs_fileoff_t bno,
499 xfs_filblks_t len,
500 int flags,
501 xfs_bmbt_irec_t *mval,
502 int nmap,
503 int ret_nmap)
504{
505 int i; /* index to map values */
506
507 ASSERT(ret_nmap <= nmap);
508
509 for (i = 0; i < ret_nmap; i++) {
510 ASSERT(mval[i].br_blockcount > 0);
511 if (!(flags & XFS_BMAPI_ENTIRE)) {
512 ASSERT(mval[i].br_startoff >= bno);
513 ASSERT(mval[i].br_blockcount <= len);
514 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
515 bno + len);
516 } else {
517 ASSERT(mval[i].br_startoff < bno + len);
518 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
519 bno);
520 }
521 ASSERT(i == 0 ||
522 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
523 mval[i].br_startoff);
524 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
525 mval[i].br_startblock != HOLESTARTBLOCK);
526 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
527 mval[i].br_state == XFS_EXT_UNWRITTEN);
528 }
529}
530
531#else
532#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700533#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100534#endif /* DEBUG */
535
536/*
537 * bmap free list manipulation functions
538 */
539
540/*
541 * Add the extent to the list of extents to be free at transaction end.
542 * The list is maintained sorted (by block number).
543 */
544void
545xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000546 struct xfs_mount *mp,
547 struct xfs_defer_ops *dfops,
548 xfs_fsblock_t bno,
549 xfs_filblks_t len,
550 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100551{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000552 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100553#ifdef DEBUG
554 xfs_agnumber_t agno;
555 xfs_agblock_t agbno;
556
557 ASSERT(bno != NULLFSBLOCK);
558 ASSERT(len > 0);
559 ASSERT(len <= MAXEXTLEN);
560 ASSERT(!isnullstartblock(bno));
561 agno = XFS_FSB_TO_AGNO(mp, bno);
562 agbno = XFS_FSB_TO_AGBNO(mp, bno);
563 ASSERT(agno < mp->m_sb.sb_agcount);
564 ASSERT(agbno < mp->m_sb.sb_agblocks);
565 ASSERT(len < mp->m_sb.sb_agblocks);
566 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
567#endif
568 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000569
Dave Chinner9e5987a72013-02-25 12:31:26 +1100570 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000571 new->xefi_startblock = bno;
572 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000573 if (oinfo)
574 new->xefi_oinfo = *oinfo;
575 else
576 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000577 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
578 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000579 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100580}
581
582/*
583 * Inode fork format manipulation functions
584 */
585
586/*
587 * Transform a btree format file with only one leaf node, where the
588 * extents list will fit in the inode, into an extents format file.
589 * Since the file extents are already in-core, all we have to do is
590 * give up the space for the btree root and pitch the leaf block.
591 */
592STATIC int /* error */
593xfs_bmap_btree_to_extents(
594 xfs_trans_t *tp, /* transaction pointer */
595 xfs_inode_t *ip, /* incore inode pointer */
596 xfs_btree_cur_t *cur, /* btree cursor */
597 int *logflagsp, /* inode logging flags */
598 int whichfork) /* data or attr fork */
599{
600 /* REFERENCED */
601 struct xfs_btree_block *cblock;/* child btree block */
602 xfs_fsblock_t cbno; /* child block number */
603 xfs_buf_t *cbp; /* child block's buffer */
604 int error; /* error return value */
605 xfs_ifork_t *ifp; /* inode fork data */
606 xfs_mount_t *mp; /* mount point structure */
607 __be64 *pp; /* ptr to block address */
608 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000609 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100610
611 mp = ip->i_mount;
612 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700613 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100614 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
615 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
616 rblock = ifp->if_broot;
617 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
618 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
619 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
620 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
621 cbno = be64_to_cpu(*pp);
622 *logflagsp = 0;
623#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700624 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
625 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100626#endif
627 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
628 &xfs_bmbt_buf_ops);
629 if (error)
630 return error;
631 cblock = XFS_BUF_TO_BLOCK(cbp);
632 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
633 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000634 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
635 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100636 ip->i_d.di_nblocks--;
637 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
638 xfs_trans_binval(tp, cbp);
639 if (cur->bc_bufs[0] == cbp)
640 cur->bc_bufs[0] = NULL;
641 xfs_iroot_realloc(ip, -1, whichfork);
642 ASSERT(ifp->if_broot == NULL);
643 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
644 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
645 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
646 return 0;
647}
648
649/*
650 * Convert an extents-format file into a btree-format file.
651 * The new file will have a root block (in the inode) and a single child block.
652 */
653STATIC int /* error */
654xfs_bmap_extents_to_btree(
655 xfs_trans_t *tp, /* transaction pointer */
656 xfs_inode_t *ip, /* incore inode pointer */
657 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000658 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100659 xfs_btree_cur_t **curp, /* cursor returned to caller */
660 int wasdel, /* converting a delayed alloc */
661 int *logflagsp, /* inode logging flags */
662 int whichfork) /* data or attr fork */
663{
664 struct xfs_btree_block *ablock; /* allocated (child) bt block */
665 xfs_buf_t *abp; /* buffer for ablock */
666 xfs_alloc_arg_t args; /* allocation arguments */
667 xfs_bmbt_rec_t *arp; /* child record pointer */
668 struct xfs_btree_block *block; /* btree root block */
669 xfs_btree_cur_t *cur; /* bmap btree cursor */
670 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
671 int error; /* error return value */
672 xfs_extnum_t i, cnt; /* extent record index */
673 xfs_ifork_t *ifp; /* inode fork pointer */
674 xfs_bmbt_key_t *kp; /* root block key pointer */
675 xfs_mount_t *mp; /* mount structure */
676 xfs_extnum_t nextents; /* number of file extents */
677 xfs_bmbt_ptr_t *pp; /* root block address pointer */
678
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500679 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700680 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100681 ifp = XFS_IFORK_PTR(ip, whichfork);
682 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
683
684 /*
685 * Make space in the inode incore.
686 */
687 xfs_iroot_realloc(ip, 1, whichfork);
688 ifp->if_flags |= XFS_IFBROOT;
689
690 /*
691 * Fill in the root.
692 */
693 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800694 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
695 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800696 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100697 /*
698 * Need a cursor. Can't allocate until bb_level is filled in.
699 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100700 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
701 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000702 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100703 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
704 /*
705 * Convert to a btree with two levels, one record in root.
706 */
707 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
708 memset(&args, 0, sizeof(args));
709 args.tp = tp;
710 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000711 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100712 args.firstblock = *firstblock;
713 if (*firstblock == NULLFSBLOCK) {
714 args.type = XFS_ALLOCTYPE_START_BNO;
715 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000716 } else if (dfops->dop_low) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100717 args.type = XFS_ALLOCTYPE_START_BNO;
718 args.fsbno = *firstblock;
719 } else {
720 args.type = XFS_ALLOCTYPE_NEAR_BNO;
721 args.fsbno = *firstblock;
722 }
723 args.minlen = args.maxlen = args.prod = 1;
724 args.wasdel = wasdel;
725 *logflagsp = 0;
726 if ((error = xfs_alloc_vextent(&args))) {
727 xfs_iroot_realloc(ip, -1, whichfork);
728 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
729 return error;
730 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700731
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800732 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
733 xfs_iroot_realloc(ip, -1, whichfork);
734 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
735 return -ENOSPC;
736 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100737 /*
738 * Allocation can't fail, the space was reserved.
739 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100740 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800741 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100742 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
743 cur->bc_private.b.allocated++;
744 ip->i_d.di_nblocks++;
745 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
746 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
747 /*
748 * Fill in the child block.
749 */
750 abp->b_ops = &xfs_bmbt_buf_ops;
751 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800752 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
753 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500754 XFS_BTREE_LONG_PTRS);
755
Dave Chinner9e5987a72013-02-25 12:31:26 +1100756 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100757 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100758 for (cnt = i = 0; i < nextents; i++) {
759 ep = xfs_iext_get_ext(ifp, i);
760 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
761 arp->l0 = cpu_to_be64(ep->l0);
762 arp->l1 = cpu_to_be64(ep->l1);
763 arp++; cnt++;
764 }
765 }
766 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
767 xfs_btree_set_numrecs(ablock, cnt);
768
769 /*
770 * Fill in the root key and pointer.
771 */
772 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
773 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
774 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
775 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
776 be16_to_cpu(block->bb_level)));
777 *pp = cpu_to_be64(args.fsbno);
778
779 /*
780 * Do all this logging at the end so that
781 * the root is at the right level.
782 */
783 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
784 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
785 ASSERT(*curp == NULL);
786 *curp = cur;
787 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
788 return 0;
789}
790
791/*
792 * Convert a local file to an extents file.
793 * This code is out of bounds for data forks of regular files,
794 * since the file data needs to get logged so things will stay consistent.
795 * (The bmap-level manipulations are ok, though).
796 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000797void
798xfs_bmap_local_to_extents_empty(
799 struct xfs_inode *ip,
800 int whichfork)
801{
802 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
803
Darrick J. Wong60b49842016-10-03 09:11:34 -0700804 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000805 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
806 ASSERT(ifp->if_bytes == 0);
807 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
808
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000809 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000810 ifp->if_flags &= ~XFS_IFINLINE;
811 ifp->if_flags |= XFS_IFEXTENTS;
812 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
813}
814
815
Dave Chinner9e5987a72013-02-25 12:31:26 +1100816STATIC int /* error */
817xfs_bmap_local_to_extents(
818 xfs_trans_t *tp, /* transaction pointer */
819 xfs_inode_t *ip, /* incore inode pointer */
820 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
821 xfs_extlen_t total, /* total blocks needed by transaction */
822 int *logflagsp, /* inode logging flags */
823 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500824 void (*init_fn)(struct xfs_trans *tp,
825 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100826 struct xfs_inode *ip,
827 struct xfs_ifork *ifp))
828{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000829 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100830 int flags; /* logging flags returned */
831 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000832 xfs_alloc_arg_t args; /* allocation arguments */
833 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700834 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100835
836 /*
837 * We don't want to deal with the case of keeping inode data inline yet.
838 * So sending the data fork of a regular inode is invalid.
839 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100840 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100841 ifp = XFS_IFORK_PTR(ip, whichfork);
842 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000843
844 if (!ifp->if_bytes) {
845 xfs_bmap_local_to_extents_empty(ip, whichfork);
846 flags = XFS_ILOG_CORE;
847 goto done;
848 }
849
Dave Chinner9e5987a72013-02-25 12:31:26 +1100850 flags = 0;
851 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000852 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
853 XFS_IFINLINE);
854 memset(&args, 0, sizeof(args));
855 args.tp = tp;
856 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000857 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000858 args.firstblock = *firstblock;
859 /*
860 * Allocate a block. We know we need only one, since the
861 * file currently fits in an inode.
862 */
863 if (*firstblock == NULLFSBLOCK) {
864 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
865 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100866 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000867 args.fsbno = *firstblock;
868 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100869 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000870 args.total = total;
871 args.minlen = args.maxlen = args.prod = 1;
872 error = xfs_alloc_vextent(&args);
873 if (error)
874 goto done;
875
876 /* Can't fail, the space was reserved. */
877 ASSERT(args.fsbno != NULLFSBLOCK);
878 ASSERT(args.len == 1);
879 *firstblock = args.fsbno;
880 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
881
Dave Chinnerfe22d552015-01-22 09:30:06 +1100882 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100883 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100884 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100885 * The callout is responsible for logging because the remote format
886 * might differ from the local format and thus we don't know how much to
887 * log here. Note that init_fn must also set the buffer log item type
888 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100889 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000890 init_fn(tp, bp, ip, ifp);
891
Brian Fosterb7cdc662015-10-12 15:40:24 +1100892 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000893 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
894 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100895 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000896
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700897 rec.br_startoff = 0;
898 rec.br_startblock = args.fsbno;
899 rec.br_blockcount = 1;
900 rec.br_state = XFS_EXT_NORM;
901 xfs_iext_insert(ip, 0, 1, &rec, 0);
902
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
904 ip->i_d.di_nblocks = 1;
905 xfs_trans_mod_dquot_byino(tp, ip,
906 XFS_TRANS_DQ_BCOUNT, 1L);
907 flags |= xfs_ilog_fext(whichfork);
908
Dave Chinner9e5987a72013-02-25 12:31:26 +1100909done:
910 *logflagsp = flags;
911 return error;
912}
913
914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * Called from xfs_bmap_add_attrfork to handle btree format files.
916 */
917STATIC int /* error */
918xfs_bmap_add_attrfork_btree(
919 xfs_trans_t *tp, /* transaction pointer */
920 xfs_inode_t *ip, /* incore inode pointer */
921 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000922 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int *flags) /* inode logging flags */
924{
925 xfs_btree_cur_t *cur; /* btree cursor */
926 int error; /* error return value */
927 xfs_mount_t *mp; /* file system mount struct */
928 int stat; /* newroot status */
929
930 mp = ip->i_mount;
931 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
932 *flags |= XFS_ILOG_DBROOT;
933 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100934 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000935 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700937 error = xfs_bmbt_lookup_first(cur, &stat);
938 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000940 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100941 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100942 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 goto error0;
944 if (stat == 0) {
945 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000946 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 *firstblock = cur->bc_private.b.firstblock;
949 cur->bc_private.b.allocated = 0;
950 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
951 }
952 return 0;
953error0:
954 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
955 return error;
956}
957
958/*
959 * Called from xfs_bmap_add_attrfork to handle extents format files.
960 */
961STATIC int /* error */
962xfs_bmap_add_attrfork_extents(
963 xfs_trans_t *tp, /* transaction pointer */
964 xfs_inode_t *ip, /* incore inode pointer */
965 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000966 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int *flags) /* inode logging flags */
968{
969 xfs_btree_cur_t *cur; /* bmap btree cursor */
970 int error; /* error return value */
971
972 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
973 return 0;
974 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000975 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 flags, XFS_DATA_FORK);
977 if (cur) {
978 cur->bc_private.b.allocated = 0;
979 xfs_btree_del_cursor(cur,
980 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
981 }
982 return error;
983}
984
985/*
Dave Chinner1e823792013-02-11 15:58:13 +1100986 * Called from xfs_bmap_add_attrfork to handle local format files. Each
987 * different data fork content type needs a different callout to do the
988 * conversion. Some are basic and only require special block initialisation
989 * callouts for the data formating, others (directories) are so specialised they
990 * handle everything themselves.
991 *
992 * XXX (dgc): investigate whether directory conversion can use the generic
993 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500994 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
996STATIC int /* error */
997xfs_bmap_add_attrfork_local(
998 xfs_trans_t *tp, /* transaction pointer */
999 xfs_inode_t *ip, /* incore inode pointer */
1000 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001001 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int *flags) /* inode logging flags */
1003{
1004 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1007 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001008
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001009 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001011 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 dargs.dp = ip;
1013 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001014 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001015 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 dargs.whichfork = XFS_DATA_FORK;
1017 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001018 return xfs_dir2_sf_to_block(&dargs);
1019 }
1020
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001021 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001022 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1023 flags, XFS_DATA_FORK,
1024 xfs_symlink_local_to_remote);
1025
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001026 /* should only be called for types that support local format data */
1027 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001028 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001032 * Convert inode from non-attributed to attributed.
1033 * Must not be in a transaction, ip must not be locked.
1034 */
1035int /* error code */
1036xfs_bmap_add_attrfork(
1037 xfs_inode_t *ip, /* incore inode pointer */
1038 int size, /* space new attribute needs */
1039 int rsvd) /* xact may use reserved blks */
1040{
1041 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001042 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001043 xfs_mount_t *mp; /* mount structure */
1044 xfs_trans_t *tp; /* transaction pointer */
1045 int blks; /* space reservation */
1046 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001047 int logflags; /* logging flags */
1048 int error; /* error return value */
1049
1050 ASSERT(XFS_IFORK_Q(ip) == 0);
1051
1052 mp = ip->i_mount;
1053 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001054
Dave Chinner9e5987a72013-02-25 12:31:26 +11001055 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001056
1057 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1058 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1059 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001060 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001061
Dave Chinner9e5987a72013-02-25 12:31:26 +11001062 xfs_ilock(ip, XFS_ILOCK_EXCL);
1063 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1064 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1065 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001066 if (error)
1067 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001068 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001069 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001070 if (ip->i_d.di_anextents != 0) {
1071 error = -EFSCORRUPTED;
1072 goto trans_cancel;
1073 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001074 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1075 /*
1076 * For inodes coming from pre-6.2 filesystems.
1077 */
1078 ASSERT(ip->i_d.di_aformat == 0);
1079 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1080 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001081
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001082 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001083 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1084
1085 switch (ip->i_d.di_format) {
1086 case XFS_DINODE_FMT_DEV:
1087 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1088 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001089 case XFS_DINODE_FMT_LOCAL:
1090 case XFS_DINODE_FMT_EXTENTS:
1091 case XFS_DINODE_FMT_BTREE:
1092 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1093 if (!ip->i_d.di_forkoff)
1094 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1095 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1096 version = 2;
1097 break;
1098 default:
1099 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001100 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001101 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001102 }
1103
1104 ASSERT(ip->i_afp == NULL);
1105 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1106 ip->i_afp->if_flags = XFS_IFEXTENTS;
1107 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001108 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001109 switch (ip->i_d.di_format) {
1110 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001111 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001112 &logflags);
1113 break;
1114 case XFS_DINODE_FMT_EXTENTS:
1115 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001116 &dfops, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001117 break;
1118 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001119 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001120 &logflags);
1121 break;
1122 default:
1123 error = 0;
1124 break;
1125 }
1126 if (logflags)
1127 xfs_trans_log_inode(tp, ip, logflags);
1128 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001129 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001130 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1131 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001132 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001133
1134 spin_lock(&mp->m_sb_lock);
1135 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1136 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001137 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001138 }
1139 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1140 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001141 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001142 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001143 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001144 if (log_sb)
1145 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001146 }
1147
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001148 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001149 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001150 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001151 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001152 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001153 return error;
1154
1155bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001156 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001157trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001158 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001159 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001160 return error;
1161}
1162
1163/*
1164 * Internal and external extent tree search functions.
1165 */
1166
1167/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001168 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001169 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001170int
1171xfs_iread_extents(
1172 struct xfs_trans *tp,
1173 struct xfs_inode *ip,
1174 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001175{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001176 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001177 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001178 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1179 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1180 struct xfs_btree_block *block = ifp->if_broot;
1181 xfs_fsblock_t bno;
1182 struct xfs_buf *bp;
1183 xfs_extnum_t i, j;
1184 int level;
1185 __be64 *pp;
1186 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001187
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001188 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1189
1190 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1191 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1192 return -EFSCORRUPTED;
1193 }
1194
1195 ifp->if_bytes = 0;
1196 ifp->if_real_bytes = 0;
1197 xfs_iext_add(ifp, 0, nextents);
1198
Dave Chinner9e5987a72013-02-25 12:31:26 +11001199 /*
1200 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1201 */
1202 level = be16_to_cpu(block->bb_level);
1203 ASSERT(level > 0);
1204 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1205 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001206
Dave Chinner9e5987a72013-02-25 12:31:26 +11001207 /*
1208 * Go down the tree until leaf level is reached, following the first
1209 * pointer (leftmost) at each level.
1210 */
1211 while (level-- > 0) {
1212 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1213 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1214 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001215 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001216 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001217 if (level == 0)
1218 break;
1219 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1220 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001221 XFS_WANT_CORRUPTED_GOTO(mp,
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001222 XFS_FSB_SANITY_CHECK(mp, bno), out_brelse);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001223 xfs_trans_brelse(tp, bp);
1224 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001225
Dave Chinner9e5987a72013-02-25 12:31:26 +11001226 /*
1227 * Here with bp and block set to the leftmost leaf node in the tree.
1228 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001229 i = 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001230
Dave Chinner9e5987a72013-02-25 12:31:26 +11001231 /*
1232 * Loop over all leaf nodes. Copy information to the extent records.
1233 */
1234 for (;;) {
1235 xfs_bmbt_rec_t *frp;
1236 xfs_fsblock_t nextbno;
1237 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001238
1239 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001240 if (unlikely(i + num_recs > nextents)) {
1241 ASSERT(i + num_recs <= nextents);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001242 xfs_warn(ip->i_mount,
1243 "corrupt dinode %Lu, (btree extents).",
1244 (unsigned long long) ip->i_ino);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001245 XFS_CORRUPTION_ERROR(__func__,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001246 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001247 error = -EFSCORRUPTED;
1248 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001249 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001250 /*
1251 * Read-ahead the next leaf block, if any.
1252 */
1253 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1254 if (nextbno != NULLFSBLOCK)
1255 xfs_btree_reada_bufl(mp, nextbno, 1,
1256 &xfs_bmbt_buf_ops);
1257 /*
1258 * Copy records into the extent records.
1259 */
1260 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001261 for (j = 0; j < num_recs; j++, i++, frp++) {
1262 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1263 trp->l0 = be64_to_cpu(frp->l0);
1264 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001265 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001266 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001267 XFS_ERRLEVEL_LOW, mp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001268 error = -EFSCORRUPTED;
1269 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001270 }
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001271 trace_xfs_read_extent(ip, i, state, _THIS_IP_);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001272 }
1273 xfs_trans_brelse(tp, bp);
1274 bno = nextbno;
1275 /*
1276 * If we've reached the end, stop.
1277 */
1278 if (bno == NULLFSBLOCK)
1279 break;
1280 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1281 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1282 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001283 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001284 block = XFS_BUF_TO_BLOCK(bp);
1285 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001286
1287 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1288 error = -EFSCORRUPTED;
1289 goto out;
1290 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001291 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001292
1293 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001294 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001295
1296out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001297 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001298out:
1299 xfs_iext_destroy(ifp);
1300 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001301}
1302
Dave Chinner9e5987a72013-02-25 12:31:26 +11001303/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001304 * Returns the relative block number of the first unused block(s) in the given
1305 * fork with at least "len" logically contiguous blocks free. This is the
1306 * lowest-address hole if the fork has holes, else the first block past the end
1307 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001308 */
1309int /* error */
1310xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001311 struct xfs_trans *tp, /* transaction pointer */
1312 struct xfs_inode *ip, /* incore inode */
1313 xfs_extlen_t len, /* size of hole to find */
1314 xfs_fileoff_t *first_unused, /* unused block */
1315 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001316{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001317 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1318 struct xfs_bmbt_irec got;
1319 xfs_extnum_t idx = 0;
1320 xfs_fileoff_t lastaddr = 0;
1321 xfs_fileoff_t lowest, max;
1322 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001323
1324 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1325 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1326 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001327
Dave Chinner9e5987a72013-02-25 12:31:26 +11001328 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1329 *first_unused = 0;
1330 return 0;
1331 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001332
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001333 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1334 error = xfs_iread_extents(tp, ip, whichfork);
1335 if (error)
1336 return error;
1337 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001338
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001339 lowest = max = *first_unused;
1340 while (xfs_iext_get_extent(ifp, idx++, &got)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001341 /*
1342 * See if the hole before this extent will work.
1343 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001344 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001345 got.br_startoff - max >= len)
1346 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001347 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001348 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1349 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001350
Dave Chinner9e5987a72013-02-25 12:31:26 +11001351 *first_unused = max;
1352 return 0;
1353}
1354
1355/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001356 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001357 * last_block (input value) in the file.
1358 * This is not based on i_size, it is based on the extent records.
1359 * Returns 0 for local files, as they do not have extent records.
1360 */
1361int /* error */
1362xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001363 struct xfs_trans *tp, /* transaction pointer */
1364 struct xfs_inode *ip, /* incore inode */
1365 xfs_fileoff_t *last_block, /* last block */
1366 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001367{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001368 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1369 struct xfs_bmbt_irec got;
1370 xfs_extnum_t idx;
1371 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001372
Christoph Hellwig86685f72016-11-24 11:39:38 +11001373 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1374 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001375 *last_block = 0;
1376 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001377 case XFS_DINODE_FMT_BTREE:
1378 case XFS_DINODE_FMT_EXTENTS:
1379 break;
1380 default:
1381 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001382 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001383
1384 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1385 error = xfs_iread_extents(tp, ip, whichfork);
1386 if (error)
1387 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001388 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001389
Christoph Hellwigdc560152017-10-23 16:32:39 -07001390 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &idx, &got))
1391 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001392 return 0;
1393}
1394
Dave Chinner68988112013-08-12 20:49:42 +10001395int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001396xfs_bmap_last_extent(
1397 struct xfs_trans *tp,
1398 struct xfs_inode *ip,
1399 int whichfork,
1400 struct xfs_bmbt_irec *rec,
1401 int *is_empty)
1402{
1403 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1404 int error;
1405 int nextents;
1406
1407 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1408 error = xfs_iread_extents(tp, ip, whichfork);
1409 if (error)
1410 return error;
1411 }
1412
Eric Sandeen5d829302016-11-08 12:59:42 +11001413 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001414 if (nextents == 0) {
1415 *is_empty = 1;
1416 return 0;
1417 }
1418
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001419 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001420 *is_empty = 0;
1421 return 0;
1422}
1423
1424/*
1425 * Check the last inode extent to determine whether this allocation will result
1426 * in blocks being allocated at the end of the file. When we allocate new data
1427 * blocks at the end of the file which do not start at the previous data block,
1428 * we will try to align the new blocks at stripe unit boundaries.
1429 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001430 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001431 * at, or past the EOF.
1432 */
1433STATIC int
1434xfs_bmap_isaeof(
1435 struct xfs_bmalloca *bma,
1436 int whichfork)
1437{
1438 struct xfs_bmbt_irec rec;
1439 int is_empty;
1440 int error;
1441
Thomas Meyer749f24f2017-10-09 11:38:54 -07001442 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001443 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1444 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001445 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001446 return error;
1447
Dave Chinner6e708bc2013-11-22 10:41:16 +11001448 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001449 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001450 return 0;
1451 }
1452
Dave Chinner9e5987a72013-02-25 12:31:26 +11001453 /*
1454 * Check if we are allocation or past the last extent, or at least into
1455 * the last delayed allocated extent.
1456 */
1457 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1458 (bma->offset >= rec.br_startoff &&
1459 isnullstartblock(rec.br_startblock));
1460 return 0;
1461}
1462
1463/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001464 * Returns the file-relative block number of the first block past eof in
1465 * the file. This is not based on i_size, it is based on the extent records.
1466 * Returns 0 for local files, as they do not have extent records.
1467 */
1468int
1469xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001470 struct xfs_inode *ip,
1471 xfs_fileoff_t *last_block,
1472 int whichfork)
1473{
1474 struct xfs_bmbt_irec rec;
1475 int is_empty;
1476 int error;
1477
1478 *last_block = 0;
1479
1480 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1481 return 0;
1482
1483 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1484 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001485 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001486
1487 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1488 if (error || is_empty)
1489 return error;
1490
1491 *last_block = rec.br_startoff + rec.br_blockcount;
1492 return 0;
1493}
1494
1495/*
1496 * Returns whether the selected fork of the inode has exactly one
1497 * block or not. For the data fork we check this matches di_size,
1498 * implying the file's range is 0..bsize-1.
1499 */
1500int /* 1=>1 block, 0=>otherwise */
1501xfs_bmap_one_block(
1502 xfs_inode_t *ip, /* incore inode */
1503 int whichfork) /* data or attr fork */
1504{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001505 xfs_ifork_t *ifp; /* inode fork pointer */
1506 int rval; /* return value */
1507 xfs_bmbt_irec_t s; /* internal version of extent */
1508
1509#ifndef DEBUG
1510 if (whichfork == XFS_DATA_FORK)
1511 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1512#endif /* !DEBUG */
1513 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1514 return 0;
1515 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1516 return 0;
1517 ifp = XFS_IFORK_PTR(ip, whichfork);
1518 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001519 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001520 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1521 if (rval && whichfork == XFS_DATA_FORK)
1522 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1523 return rval;
1524}
1525
1526/*
1527 * Extent tree manipulation functions used during allocation.
1528 */
1529
1530/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001531 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
1533STATIC int /* error */
1534xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001535 struct xfs_bmalloca *bma,
1536 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001538 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001541 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 xfs_fileoff_t new_endoff; /* end offset of new entry */
1543 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1544 /* left is 0, right is 1, prev is 2 */
1545 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001546 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001547 xfs_filblks_t da_new; /* new count del alloc blocks used */
1548 xfs_filblks_t da_old; /* old count del alloc blocks used */
1549 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001551 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001552 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001553 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001555 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001556 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001557 ASSERT(whichfork != XFS_ATTR_FORK);
1558 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1559 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001560
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001561 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001562 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001563 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001564 ASSERT(!bma->cur ||
1565 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001566
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001567 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569#define LEFT r[0]
1570#define RIGHT r[1]
1571#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /*
1574 * Set up a bunch of variables to make the tests simpler.
1575 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001576 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001578 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ASSERT(PREV.br_startoff <= new->br_startoff);
1580 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001581
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001582 da_old = startblockval(PREV.br_startblock);
1583 da_new = 0;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /*
1586 * Set flags determining what part of the previous delayed allocation
1587 * extent is being replaced by a real allocation.
1588 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001589 if (PREV.br_startoff == new->br_startoff)
1590 state |= BMAP_LEFT_FILLING;
1591 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1592 state |= BMAP_RIGHT_FILLING;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /*
1595 * Check and set flags if this segment has a left neighbor.
1596 * Don't set contiguous if the combined extent would be too large.
1597 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001598 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001599 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001600 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001601
1602 if (isnullstartblock(LEFT.br_startblock))
1603 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001605
1606 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1607 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1608 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1609 LEFT.br_state == new->br_state &&
1610 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1611 state |= BMAP_LEFT_CONTIG;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 /*
1614 * Check and set flags if this segment has a right neighbor.
1615 * Don't set contiguous if the combined extent would be too large.
1616 * Also check for all-three-contiguous being too large.
1617 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001618 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001619 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001620 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001621
1622 if (isnullstartblock(RIGHT.br_startblock))
1623 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001625
1626 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1627 new_endoff == RIGHT.br_startoff &&
1628 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1629 new->br_state == RIGHT.br_state &&
1630 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1631 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1632 BMAP_RIGHT_FILLING)) !=
1633 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1634 BMAP_RIGHT_FILLING) ||
1635 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1636 <= MAXEXTLEN))
1637 state |= BMAP_RIGHT_CONTIG;
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 error = 0;
1640 /*
1641 * Switch out based on the FILLING and CONTIG state bits.
1642 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001643 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1644 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1645 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1646 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Filling in all of a previously delayed allocation extent.
1649 * The left and right neighbors are both contiguous with new.
1650 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001651 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001652
Christoph Hellwig0d045542017-11-03 10:34:39 -07001653 xfs_iext_remove(bma->ip, bma->idx, 2, state);
1654 bma->idx--;
1655 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001656 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001657
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001658 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1660 else {
1661 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001662 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001663 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001665 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001666 error = xfs_btree_delete(bma->cur, &i);
1667 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001669 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001670 error = xfs_btree_decrement(bma->cur, 0, &i);
1671 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001673 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001674 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001675 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto done;
1677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 break;
1679
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001680 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * Filling in all of a previously delayed allocation extent.
1683 * The left neighbor is contiguous, the right is not.
1684 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001685 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001686 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001687
1688 xfs_iext_remove(bma->ip, bma->idx, 1, state);
1689 bma->idx--;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07001690 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001691
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001692 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 rval = XFS_ILOG_DEXT;
1694 else {
1695 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001696 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001697 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001699 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001700 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001701 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto done;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 break;
1705
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001706 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /*
1708 * Filling in all of a previously delayed allocation extent.
1709 * The right neighbor is contiguous, the left is not.
1710 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001711 PREV.br_startblock = new->br_startblock;
1712 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001713
1714 bma->idx++;
1715 xfs_iext_remove(bma->ip, bma->idx, 1, state);
1716 bma->idx--;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07001717 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001718
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001719 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 rval = XFS_ILOG_DEXT;
1721 else {
1722 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001723 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001724 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001726 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001727 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001728 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto done;
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 break;
1732
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001733 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * Filling in all of a previously delayed allocation extent.
1736 * Neither the left nor right neighbors are contiguous with
1737 * the new one.
1738 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001739 PREV.br_startblock = new->br_startblock;
1740 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07001741 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001742
Darrick J. Wong60b49842016-10-03 09:11:34 -07001743 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001744 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1746 else {
1747 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001748 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001749 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001751 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001752 error = xfs_btree_insert(bma->cur, &i);
1753 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001755 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 break;
1758
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001759 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 /*
1761 * Filling in the first part of a previous delayed allocation.
1762 * The left neighbor is contiguous.
1763 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001764 old = LEFT;
1765 temp = PREV.br_blockcount - new->br_blockcount;
1766 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1767 startblockval(PREV.br_startblock));
1768
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001769 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001770
Christoph Hellwigbf999712017-11-03 10:34:38 -07001771 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001772 PREV.br_startoff += new->br_blockcount;
1773 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001774
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07001775 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001776 bma->idx--;
1777 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001778
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 rval = XFS_ILOG_DEXT;
1781 else {
1782 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001783 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001786 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001787 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001788 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto done;
1790 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001794 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /*
1796 * Filling in the first part of a previous delayed allocation.
1797 * The left neighbor is not contiguous.
1798 */
Christoph Hellwig1bfd7612017-11-03 10:34:38 -07001799 xfs_iext_update_extent(bma->ip, state, bma->idx, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001800 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1803 else {
1804 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001805 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001808 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 error = xfs_btree_insert(bma->cur, &i);
1810 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001814
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001815 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001817 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001818 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 rval |= tmp_rval;
1820 if (error)
1821 goto done;
1822 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823
1824 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001825 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001826 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001828
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001829 PREV.br_startoff = new_endoff;
1830 PREV.br_blockcount = temp;
1831 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig1bfd7612017-11-03 10:34:38 -07001832 xfs_iext_insert(bma->ip, bma->idx + 1, 1, &PREV, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 break;
1834
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001835 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /*
1837 * Filling in the last part of a previous delayed allocation.
1838 * The right neighbor is contiguous with the new allocation.
1839 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001840 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001841 RIGHT.br_startoff = new->br_startoff;
1842 RIGHT.br_startblock = new->br_startblock;
1843 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001844
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 rval = XFS_ILOG_DEXT;
1847 else {
1848 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001849 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001852 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001853 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto done;
1856 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001857
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001858 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001859 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001860 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001861
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001862 PREV.br_blockcount = temp;
1863 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001864
Christoph Hellwig0d045542017-11-03 10:34:39 -07001865 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 bma->idx++;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001867 xfs_iext_update_extent(bma->ip, state, bma->idx, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
1869
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001870 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 /*
1872 * Filling in the last part of a previous delayed allocation.
1873 * The right neighbor is not contiguous.
1874 */
Christoph Hellwig1bfd7612017-11-03 10:34:38 -07001875 xfs_iext_update_extent(bma->ip, state, bma->idx, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001876 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1879 else {
1880 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001881 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001884 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 error = xfs_btree_insert(bma->cur, &i);
1886 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001888 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001890
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001891 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001892 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001893 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001894 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 rval |= tmp_rval;
1896 if (error)
1897 goto done;
1898 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001899
1900 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001902 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001904
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001905 PREV.br_startblock = nullstartblock(da_new);
1906 PREV.br_blockcount = temp;
Christoph Hellwig1bfd7612017-11-03 10:34:38 -07001907 xfs_iext_insert(bma->ip, bma->idx, 1, &PREV, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001908
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 break;
1911
1912 case 0:
1913 /*
1914 * Filling in the middle part of a previous delayed allocation.
1915 * Contiguity is impossible here.
1916 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001917 *
1918 * We start with a delayed allocation:
1919 *
1920 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1921 * PREV @ idx
1922 *
1923 * and we are allocating:
1924 * +rrrrrrrrrrrrrrrrr+
1925 * new
1926 *
1927 * and we set it up for insertion as:
1928 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1929 * new
1930 * PREV @ idx LEFT RIGHT
1931 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001933 old = PREV;
1934
1935 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001936 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001937
1938 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001939 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001940 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001941 RIGHT.br_blockcount =
1942 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1943 RIGHT.br_startblock =
1944 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1945 RIGHT.br_blockcount));
1946
1947 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001948 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1949 PREV.br_startblock =
1950 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1951 PREV.br_blockcount));
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07001952 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001953
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001954 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001956 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001957
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1960 else {
1961 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001962 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001963 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001965 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 error = xfs_btree_insert(bma->cur, &i);
1967 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001969 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001971
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001972 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001974 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001975 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 rval |= tmp_rval;
1977 if (error)
1978 goto done;
1979 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001980
1981 da_new = startblockval(PREV.br_startblock) +
1982 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001983 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 break;
1985
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001986 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1987 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1988 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1989 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1990 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1991 case BMAP_LEFT_CONTIG:
1992 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /*
1994 * These cases are all impossible.
1995 */
1996 ASSERT(0);
1997 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001998
Darrick J. Wong9c194642016-08-03 12:16:05 +10001999 /* add reverse mapping */
2000 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2001 if (error)
2002 goto done;
2003
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002004 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002005 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002006 int tmp_logflags; /* partial log flag return val */
2007
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002008 ASSERT(bma->cur == NULL);
2009 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002010 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002011 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002012 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002013 if (error)
2014 goto done;
2015 }
2016
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002017 if (bma->cur) {
2018 da_new += bma->cur->bc_private.b.allocated;
2019 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002020 }
2021
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002022 /* adjust for changes in reserved delayed indirect blocks */
2023 if (da_new != da_old) {
2024 ASSERT(state == 0 || da_new < da_old);
2025 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2026 false);
2027 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002028
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002029 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002031 if (whichfork != XFS_COW_FORK)
2032 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return error;
2034#undef LEFT
2035#undef RIGHT
2036#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
2039/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002040 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 */
2042STATIC int /* error */
2043xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002044 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002046 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002047 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002049 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002050 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002051 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002052 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002057 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2060 /* left is 0, right is 1, prev is 2 */
2061 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002062 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002063 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002064 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002066 *logflagsp = 0;
2067
2068 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002069 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070
2071 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002072 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002073 ASSERT(!isnullstartblock(new->br_startblock));
2074
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002075 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077#define LEFT r[0]
2078#define RIGHT r[1]
2079#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /*
2082 * Set up a bunch of variables to make the tests simpler.
2083 */
2084 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002085 xfs_iext_get_extent(ifp, *idx, &PREV);
2086 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 new_endoff = new->br_startoff + new->br_blockcount;
2088 ASSERT(PREV.br_startoff <= new->br_startoff);
2089 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * Set flags determining what part of the previous oldext allocation
2093 * extent is being replaced by a newext allocation.
2094 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002095 if (PREV.br_startoff == new->br_startoff)
2096 state |= BMAP_LEFT_FILLING;
2097 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2098 state |= BMAP_RIGHT_FILLING;
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /*
2101 * Check and set flags if this segment has a left neighbor.
2102 * Don't set contiguous if the combined extent would be too large.
2103 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002104 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002105 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002106 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002107
2108 if (isnullstartblock(LEFT.br_startblock))
2109 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002111
2112 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2113 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2114 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002115 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002116 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2117 state |= BMAP_LEFT_CONTIG;
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /*
2120 * Check and set flags if this segment has a right neighbor.
2121 * Don't set contiguous if the combined extent would be too large.
2122 * Also check for all-three-contiguous being too large.
2123 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002124 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002125 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002126 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002127 if (isnullstartblock(RIGHT.br_startblock))
2128 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002130
2131 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2132 new_endoff == RIGHT.br_startoff &&
2133 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002134 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002135 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2136 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2137 BMAP_RIGHT_FILLING)) !=
2138 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2139 BMAP_RIGHT_FILLING) ||
2140 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2141 <= MAXEXTLEN))
2142 state |= BMAP_RIGHT_CONTIG;
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /*
2145 * Switch out based on the FILLING and CONTIG state bits.
2146 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002147 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2148 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2149 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2150 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /*
2152 * Setting all of a previous oldext extent to newext.
2153 * The left and right neighbors are both contiguous with new.
2154 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002155 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002156
Christoph Hellwiga6818472017-11-03 10:34:40 -07002157 xfs_iext_remove(ip, *idx, 2, state);
2158 --*idx;
2159 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002160 XFS_IFORK_NEXT_SET(ip, whichfork,
2161 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (cur == NULL)
2163 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2164 else {
2165 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002166 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2167 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002169 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002170 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002172 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002173 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002175 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002176 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002178 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002179 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002181 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002182 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002183 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 goto done;
2185 }
2186 break;
2187
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002188 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /*
2190 * Setting all of a previous oldext extent to newext.
2191 * The left neighbor is contiguous, the right is not.
2192 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002193 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002194
Christoph Hellwiga6818472017-11-03 10:34:40 -07002195 xfs_iext_remove(ip, *idx, 1, state);
2196 --*idx;
2197 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002198 XFS_IFORK_NEXT_SET(ip, whichfork,
2199 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (cur == NULL)
2201 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2202 else {
2203 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002204 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2205 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002207 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002208 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002210 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002211 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002213 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002214 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002215 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 goto done;
2217 }
2218 break;
2219
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002220 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /*
2222 * Setting all of a previous oldext extent to newext.
2223 * The right neighbor is contiguous, the left is not.
2224 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002225 PREV.br_blockcount += RIGHT.br_blockcount;
2226 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002227
2228 ++*idx;
2229 xfs_iext_remove(ip, *idx, 1, state);
2230 --*idx;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002231 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002232
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002233 XFS_IFORK_NEXT_SET(ip, whichfork,
2234 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (cur == NULL)
2236 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2237 else {
2238 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002239 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2240 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002242 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002243 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002245 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002246 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002248 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002249 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002250 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 goto done;
2252 }
2253 break;
2254
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002255 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 /*
2257 * Setting all of a previous oldext extent to newext.
2258 * Neither the left nor right neighbors are contiguous with
2259 * the new one.
2260 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002261 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002262 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (cur == NULL)
2265 rval = XFS_ILOG_DEXT;
2266 else {
2267 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002268 error = xfs_bmbt_lookup_eq(cur, new, &i);
2269 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002271 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002272 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002273 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 goto done;
2275 }
2276 break;
2277
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002278 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /*
2280 * Setting the first part of a previous oldext extent to newext.
2281 * The left neighbor is contiguous.
2282 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002283 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002284
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002285 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002286 PREV.br_startoff += new->br_blockcount;
2287 PREV.br_startblock += new->br_blockcount;
2288 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002289
Christoph Hellwiga6818472017-11-03 10:34:40 -07002290 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002291 --*idx;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002292 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (cur == NULL)
2295 rval = XFS_ILOG_DEXT;
2296 else {
2297 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002298 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002299 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002301 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002302 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002303 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002305 error = xfs_btree_decrement(cur, 0, &i);
2306 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002308 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002309 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 goto done;
2311 }
2312 break;
2313
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002314 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 /*
2316 * Setting the first part of a previous oldext extent to newext.
2317 * The left neighbor is not contiguous.
2318 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002319 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002320 PREV.br_startoff += new->br_blockcount;
2321 PREV.br_startblock += new->br_blockcount;
2322 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002323
Christoph Hellwiga6818472017-11-03 10:34:40 -07002324 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002325 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002326 XFS_IFORK_NEXT_SET(ip, whichfork,
2327 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 if (cur == NULL)
2329 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2330 else {
2331 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002332 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002333 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002335 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002336 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002337 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto done;
2339 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002340 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002342 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 }
2344 break;
2345
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002346 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 /*
2348 * Setting the last part of a previous oldext extent to newext.
2349 * The right neighbor is contiguous with the new allocation.
2350 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002351 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002352 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002353
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 RIGHT.br_startoff = new->br_startoff;
2355 RIGHT.br_startblock = new->br_startblock;
2356 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002357
2358 xfs_iext_update_extent(ip, state, *idx, &PREV);
2359 ++*idx;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002360 xfs_iext_update_extent(ip, state, *idx, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (cur == NULL)
2363 rval = XFS_ILOG_DEXT;
2364 else {
2365 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002366 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002367 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002369 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002370 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002371 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002373 error = xfs_btree_increment(cur, 0, &i);
2374 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002376 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002377 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 goto done;
2379 }
2380 break;
2381
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002382 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
2384 * Setting the last part of a previous oldext extent to newext.
2385 * The right neighbor is not contiguous.
2386 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002387 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002388 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002389
Christoph Hellwiga6818472017-11-03 10:34:40 -07002390 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002391 ++*idx;
2392 xfs_iext_insert(ip, *idx, 1, new, state);
2393
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002394 XFS_IFORK_NEXT_SET(ip, whichfork,
2395 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (cur == NULL)
2397 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2398 else {
2399 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002400 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002401 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002403 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002404 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002405 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002407 error = xfs_bmbt_lookup_eq(cur, new, &i);
2408 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002410 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002411 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002413 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415 break;
2416
2417 case 0:
2418 /*
2419 * Setting the middle part of a previous oldext extent to
2420 * newext. Contiguity is impossible here.
2421 * One extent becomes three extents.
2422 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002423 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002424 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 r[0] = *new;
2427 r[1].br_startoff = new_endoff;
2428 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002429 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002431 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002432
Christoph Hellwiga6818472017-11-03 10:34:40 -07002433 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002434 ++*idx;
2435 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2436
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002437 XFS_IFORK_NEXT_SET(ip, whichfork,
2438 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (cur == NULL)
2440 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2441 else {
2442 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002443 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002444 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002446 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002448 error = xfs_bmbt_update(cur, &r[1]);
2449 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto done;
2451 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 cur->bc_rec.b = PREV;
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);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002456 /*
2457 * Reset the cursor to the position of the new extent
2458 * we are about to insert as we can't trust it after
2459 * the previous insert.
2460 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002461 error = xfs_bmbt_lookup_eq(cur, new, &i);
2462 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002464 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002466 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002468 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470 break;
2471
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002472 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2473 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2474 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2475 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2476 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2477 case BMAP_LEFT_CONTIG:
2478 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 /*
2480 * These cases are all impossible.
2481 */
2482 ASSERT(0);
2483 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002484
Darrick J. Wong9c194642016-08-03 12:16:05 +10002485 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002486 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002487 if (error)
2488 goto done;
2489
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002490 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002491 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002492 int tmp_logflags; /* partial log flag return val */
2493
2494 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002495 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002496 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002497 *logflagsp |= tmp_logflags;
2498 if (error)
2499 goto done;
2500 }
2501
2502 /* clear out the allocated field, done with it now in any case. */
2503 if (cur) {
2504 cur->bc_private.b.allocated = 0;
2505 *curp = cur;
2506 }
2507
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002508 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002510 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return error;
2512#undef LEFT
2513#undef RIGHT
2514#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002518 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002520STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521xfs_bmap_add_extent_hole_delay(
2522 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002523 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002524 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002525 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002527 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2529 xfs_filblks_t newlen=0; /* new indirect size */
2530 xfs_filblks_t oldlen=0; /* old indirect size */
2531 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002532 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002533 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002535 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002536 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /*
2539 * Check and set flags if this segment has a left neighbor
2540 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002541 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002542 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002543 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002544
2545 if (isnullstartblock(left.br_startblock))
2546 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 /*
2550 * Check and set flags if the current (right) segment exists.
2551 * If it doesn't exist, we're converting the hole at end-of-file.
2552 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002553 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002554 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002555 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002556
2557 if (isnullstartblock(right.br_startblock))
2558 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * Set contiguity flags on the left and right neighbors.
2563 * Don't let extents get too large, even if the pieces are contiguous.
2564 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002565 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2566 left.br_startoff + left.br_blockcount == new->br_startoff &&
2567 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2568 state |= BMAP_LEFT_CONTIG;
2569
2570 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2571 new->br_startoff + new->br_blockcount == right.br_startoff &&
2572 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2573 (!(state & BMAP_LEFT_CONTIG) ||
2574 (left.br_blockcount + new->br_blockcount +
2575 right.br_blockcount <= MAXEXTLEN)))
2576 state |= BMAP_RIGHT_CONTIG;
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
2579 * Switch out based on the contiguity flags.
2580 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002581 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2582 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /*
2584 * New allocation is contiguous with delayed allocations
2585 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002586 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 */
2588 temp = left.br_blockcount + new->br_blockcount +
2589 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002590
Eric Sandeen9d87c312009-01-14 23:22:07 -06002591 oldlen = startblockval(left.br_startblock) +
2592 startblockval(new->br_startblock) +
2593 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002594 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2595 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002596 left.br_startblock = nullstartblock(newlen);
2597 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002598
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002599 xfs_iext_remove(ip, *idx, 1, state);
2600 --*idx;
2601 xfs_iext_update_extent(ip, state, *idx, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 break;
2603
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002604 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 /*
2606 * New allocation is contiguous with a delayed allocation
2607 * on the left.
2608 * Merge the new allocation with the left neighbor.
2609 */
2610 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002611
Eric Sandeen9d87c312009-01-14 23:22:07 -06002612 oldlen = startblockval(left.br_startblock) +
2613 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002614 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2615 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002616 left.br_blockcount = temp;
2617 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002618
2619 --*idx;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002620 xfs_iext_update_extent(ip, state, *idx, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 break;
2622
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002623 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /*
2625 * New allocation is contiguous with a delayed allocation
2626 * on the right.
2627 * Merge the new allocation with the right neighbor.
2628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002630 oldlen = startblockval(new->br_startblock) +
2631 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002632 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2633 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002634 right.br_startoff = new->br_startoff;
2635 right.br_startblock = nullstartblock(newlen);
2636 right.br_blockcount = temp;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002637 xfs_iext_update_extent(ip, state, *idx, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 break;
2639
2640 case 0:
2641 /*
2642 * New allocation is not contiguous with another
2643 * delayed allocation.
2644 * Insert a new entry.
2645 */
2646 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002647 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 break;
2649 }
2650 if (oldlen != newlen) {
2651 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002652 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2653 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /*
2655 * Nothing to do for disk quota accounting here.
2656 */
2657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
2660/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002661 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
2663STATIC int /* error */
2664xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002665 struct xfs_trans *tp,
2666 struct xfs_inode *ip,
2667 int whichfork,
2668 xfs_extnum_t *idx,
2669 struct xfs_btree_cur **curp,
2670 struct xfs_bmbt_irec *new,
2671 xfs_fsblock_t *first,
2672 struct xfs_defer_ops *dfops,
2673 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002675 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2676 struct xfs_mount *mp = ip->i_mount;
2677 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2681 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002682 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002683 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002684 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Christoph Hellwig6d045582017-04-11 16:45:54 -07002686 ASSERT(*idx >= 0);
2687 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002688 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002689 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002690
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002691 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /*
2694 * Check and set flags if this segment has a left neighbor.
2695 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002696 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002697 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002698 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002699 if (isnullstartblock(left.br_startblock))
2700 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 /*
2704 * Check and set flags if this segment has a current value.
2705 * Not true if we're inserting into the "hole" at eof.
2706 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002707 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002708 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002709 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002710 if (isnullstartblock(right.br_startblock))
2711 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /*
2715 * We're inserting a real allocation between "left" and "right".
2716 * Set the contiguity flags. Don't let extents get too large.
2717 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002718 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2719 left.br_startoff + left.br_blockcount == new->br_startoff &&
2720 left.br_startblock + left.br_blockcount == new->br_startblock &&
2721 left.br_state == new->br_state &&
2722 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2723 state |= BMAP_LEFT_CONTIG;
2724
2725 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2726 new->br_startoff + new->br_blockcount == right.br_startoff &&
2727 new->br_startblock + new->br_blockcount == right.br_startblock &&
2728 new->br_state == right.br_state &&
2729 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2730 (!(state & BMAP_LEFT_CONTIG) ||
2731 left.br_blockcount + new->br_blockcount +
2732 right.br_blockcount <= MAXEXTLEN))
2733 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002735 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 /*
2737 * Select which case we're in here, and implement it.
2738 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002739 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2740 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 /*
2742 * New allocation is contiguous with real allocations on the
2743 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002744 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002746 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002747
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002748 xfs_iext_remove(ip, *idx, 1, state);
2749 --*idx;
2750 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002751
Christoph Hellwig6d045582017-04-11 16:45:54 -07002752 XFS_IFORK_NEXT_SET(ip, whichfork,
2753 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2754 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002755 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002756 } else {
2757 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002758 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002759 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002760 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002761 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002762 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002763 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002764 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002765 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002766 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002767 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002768 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002769 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002770 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002771 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002772 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002776 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /*
2778 * New allocation is contiguous with a real allocation
2779 * on the left.
2780 * Merge the new allocation with the left neighbor.
2781 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002782 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002783 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002784
2785 --*idx;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002786 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002787
Christoph Hellwig6d045582017-04-11 16:45:54 -07002788 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002789 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002790 } else {
2791 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002792 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002793 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002794 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002795 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002796 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002797 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002798 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002802 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /*
2804 * New allocation is contiguous with a real allocation
2805 * on the right.
2806 * Merge the new allocation with the right neighbor.
2807 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002808 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002809
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002810 right.br_startoff = new->br_startoff;
2811 right.br_startblock = new->br_startblock;
2812 right.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002813 xfs_iext_update_extent(ip, state, *idx, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002814
Christoph Hellwig6d045582017-04-11 16:45:54 -07002815 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002816 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002817 } else {
2818 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002819 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002820 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002821 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002822 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002823 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002824 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002825 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002827 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 case 0:
2830 /*
2831 * New allocation is not contiguous with another
2832 * real allocation.
2833 * Insert a new entry.
2834 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002835 xfs_iext_insert(ip, *idx, 1, new, state);
2836 XFS_IFORK_NEXT_SET(ip, whichfork,
2837 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2838 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002839 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002840 } else {
2841 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002842 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002843 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002844 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002845 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002846 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002847 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002848 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002853
Darrick J. Wong9c194642016-08-03 12:16:05 +10002854 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002855 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002856 if (error)
2857 goto done;
2858
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002859 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002860 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002861 int tmp_logflags; /* partial log flag return val */
2862
Christoph Hellwig6d045582017-04-11 16:45:54 -07002863 ASSERT(cur == NULL);
2864 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002865 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002866 *logflagsp |= tmp_logflags;
2867 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002868 if (error)
2869 goto done;
2870 }
2871
2872 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002873 if (cur)
2874 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002875
Christoph Hellwig6d045582017-04-11 16:45:54 -07002876 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002877done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002878 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002879 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
Nathan Scottdd9f4382006-01-11 15:28:28 +11002882/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002883 * Functions used in the extent read, allocate and remove paths
2884 */
2885
2886/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002887 * Adjust the size of the new extent based on di_extsize and rt extsize.
2888 */
Dave Chinner68988112013-08-12 20:49:42 +10002889int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002890xfs_bmap_extsize_align(
2891 xfs_mount_t *mp,
2892 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2893 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2894 xfs_extlen_t extsz, /* align to this extent size */
2895 int rt, /* is this a realtime inode? */
2896 int eof, /* is extent at end-of-file? */
2897 int delay, /* creating delalloc extent? */
2898 int convert, /* overwriting unwritten extent? */
2899 xfs_fileoff_t *offp, /* in/out: aligned offset */
2900 xfs_extlen_t *lenp) /* in/out: aligned length */
2901{
2902 xfs_fileoff_t orig_off; /* original offset */
2903 xfs_extlen_t orig_alen; /* original length */
2904 xfs_fileoff_t orig_end; /* original off+len */
2905 xfs_fileoff_t nexto; /* next file offset */
2906 xfs_fileoff_t prevo; /* previous file offset */
2907 xfs_fileoff_t align_off; /* temp for offset */
2908 xfs_extlen_t align_alen; /* temp for length */
2909 xfs_extlen_t temp; /* temp for calculations */
2910
2911 if (convert)
2912 return 0;
2913
2914 orig_off = align_off = *offp;
2915 orig_alen = align_alen = *lenp;
2916 orig_end = orig_off + orig_alen;
2917
2918 /*
2919 * If this request overlaps an existing extent, then don't
2920 * attempt to perform any additional alignment.
2921 */
2922 if (!delay && !eof &&
2923 (orig_off >= gotp->br_startoff) &&
2924 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2925 return 0;
2926 }
2927
2928 /*
2929 * If the file offset is unaligned vs. the extent size
2930 * we need to align it. This will be possible unless
2931 * the file was previously written with a kernel that didn't
2932 * perform this alignment, or if a truncate shot us in the
2933 * foot.
2934 */
2935 temp = do_mod(orig_off, extsz);
2936 if (temp) {
2937 align_alen += temp;
2938 align_off -= temp;
2939 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002940
2941 /* Same adjustment for the end of the requested area. */
2942 temp = (align_alen % extsz);
2943 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002944 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002945
2946 /*
2947 * For large extent hint sizes, the aligned extent might be larger than
2948 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2949 * the length back under MAXEXTLEN. The outer allocation loops handle
2950 * short allocation just fine, so it is safe to do this. We only want to
2951 * do it when we are forced to, though, because it means more allocation
2952 * operations are required.
2953 */
2954 while (align_alen > MAXEXTLEN)
2955 align_alen -= extsz;
2956 ASSERT(align_alen <= MAXEXTLEN);
2957
Nathan Scottdd9f4382006-01-11 15:28:28 +11002958 /*
2959 * If the previous block overlaps with this proposed allocation
2960 * then move the start forward without adjusting the length.
2961 */
2962 if (prevp->br_startoff != NULLFILEOFF) {
2963 if (prevp->br_startblock == HOLESTARTBLOCK)
2964 prevo = prevp->br_startoff;
2965 else
2966 prevo = prevp->br_startoff + prevp->br_blockcount;
2967 } else
2968 prevo = 0;
2969 if (align_off != orig_off && align_off < prevo)
2970 align_off = prevo;
2971 /*
2972 * If the next block overlaps with this proposed allocation
2973 * then move the start back without adjusting the length,
2974 * but not before offset 0.
2975 * This may of course make the start overlap previous block,
2976 * and if we hit the offset 0 limit then the next block
2977 * can still overlap too.
2978 */
2979 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2980 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2981 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2982 nexto = gotp->br_startoff + gotp->br_blockcount;
2983 else
2984 nexto = gotp->br_startoff;
2985 } else
2986 nexto = NULLFILEOFF;
2987 if (!eof &&
2988 align_off + align_alen != orig_end &&
2989 align_off + align_alen > nexto)
2990 align_off = nexto > align_alen ? nexto - align_alen : 0;
2991 /*
2992 * If we're now overlapping the next or previous extent that
2993 * means we can't fit an extsz piece in this hole. Just move
2994 * the start forward to the first valid spot and set
2995 * the length so we hit the end.
2996 */
2997 if (align_off != orig_off && align_off < prevo)
2998 align_off = prevo;
2999 if (align_off + align_alen != orig_end &&
3000 align_off + align_alen > nexto &&
3001 nexto != NULLFILEOFF) {
3002 ASSERT(nexto > prevo);
3003 align_alen = nexto - align_off;
3004 }
3005
3006 /*
3007 * If realtime, and the result isn't a multiple of the realtime
3008 * extent size we need to remove blocks until it is.
3009 */
3010 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3011 /*
3012 * We're not covering the original request, or
3013 * we won't be able to once we fix the length.
3014 */
3015 if (orig_off < align_off ||
3016 orig_end > align_off + align_alen ||
3017 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003018 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003019 /*
3020 * Try to fix it by moving the start up.
3021 */
3022 if (align_off + temp <= orig_off) {
3023 align_alen -= temp;
3024 align_off += temp;
3025 }
3026 /*
3027 * Try to fix it by moving the end in.
3028 */
3029 else if (align_off + align_alen - temp >= orig_end)
3030 align_alen -= temp;
3031 /*
3032 * Set the start to the minimum then trim the length.
3033 */
3034 else {
3035 align_alen -= orig_off - align_off;
3036 align_off = orig_off;
3037 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3038 }
3039 /*
3040 * Result doesn't cover the request, fail it.
3041 */
3042 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003043 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003044 } else {
3045 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003046 /* see MAXEXTLEN handling above */
3047 ASSERT(orig_end <= align_off + align_alen ||
3048 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003049 }
3050
3051#ifdef DEBUG
3052 if (!eof && gotp->br_startoff != NULLFILEOFF)
3053 ASSERT(align_off + align_alen <= gotp->br_startoff);
3054 if (prevp->br_startoff != NULLFILEOFF)
3055 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3056#endif
3057
3058 *lenp = align_alen;
3059 *offp = align_off;
3060 return 0;
3061}
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063#define XFS_ALLOC_GAP_UNITS 4
3064
Dave Chinner68988112013-08-12 20:49:42 +10003065void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003066xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003067 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3071 xfs_mount_t *mp; /* mount point structure */
3072 int nullfb; /* true if ap->firstblock isn't set */
3073 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075#define ISVALID(x,y) \
3076 (rt ? \
3077 (x) < mp->m_sb.sb_rblocks : \
3078 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3079 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3080 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003083 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003084 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3085 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003086 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 /*
3088 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003089 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003091 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3092 !isnullstartblock(ap->prev.br_startblock) &&
3093 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3094 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003095 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 /*
3097 * Adjust for the gap between prevp and us.
3098 */
Dave Chinner3a756672011-09-18 20:40:58 +00003099 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003100 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003102 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3103 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
3105 /*
3106 * If not at eof, then compare the two neighbor blocks.
3107 * Figure out whether either one gives us a good starting point,
3108 * and pick the better one.
3109 */
3110 else if (!ap->eof) {
3111 xfs_fsblock_t gotbno; /* right side block number */
3112 xfs_fsblock_t gotdiff=0; /* right side difference */
3113 xfs_fsblock_t prevbno; /* left side block number */
3114 xfs_fsblock_t prevdiff=0; /* left side difference */
3115
3116 /*
3117 * If there's a previous (left) block, select a requested
3118 * start block based on it.
3119 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003120 if (ap->prev.br_startoff != NULLFILEOFF &&
3121 !isnullstartblock(ap->prev.br_startblock) &&
3122 (prevbno = ap->prev.br_startblock +
3123 ap->prev.br_blockcount) &&
3124 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 /*
3126 * Calculate gap to end of previous block.
3127 */
Dave Chinner3a756672011-09-18 20:40:58 +00003128 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003129 (ap->prev.br_startoff +
3130 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /*
3132 * Figure the startblock based on the previous block's
3133 * end and the gap size.
3134 * Heuristic!
3135 * If the gap is large relative to the piece we're
3136 * allocating, or using it gives us an invalid block
3137 * number, then just use the end of the previous block.
3138 */
Dave Chinner3a756672011-09-18 20:40:58 +00003139 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003141 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 prevbno += adjust;
3143 else
3144 prevdiff += adjust;
3145 /*
3146 * If the firstblock forbids it, can't use it,
3147 * must use default.
3148 */
3149 if (!rt && !nullfb &&
3150 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3151 prevbno = NULLFSBLOCK;
3152 }
3153 /*
3154 * No previous block or can't follow it, just default.
3155 */
3156 else
3157 prevbno = NULLFSBLOCK;
3158 /*
3159 * If there's a following (right) block, select a requested
3160 * start block based on it.
3161 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003162 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 /*
3164 * Calculate gap to start of next block.
3165 */
Dave Chinner3a756672011-09-18 20:40:58 +00003166 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 /*
3168 * Figure the startblock based on the next block's
3169 * start and the gap size.
3170 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003171 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 /*
3173 * Heuristic!
3174 * If the gap is large relative to the piece we're
3175 * allocating, or using it gives us an invalid block
3176 * number, then just use the start of the next block
3177 * offset by our length.
3178 */
Dave Chinner3a756672011-09-18 20:40:58 +00003179 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 ISVALID(gotbno - gotdiff, gotbno))
3181 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003182 else if (ISVALID(gotbno - ap->length, gotbno)) {
3183 gotbno -= ap->length;
3184 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 } else
3186 gotdiff += adjust;
3187 /*
3188 * If the firstblock forbids it, can't use it,
3189 * must use default.
3190 */
3191 if (!rt && !nullfb &&
3192 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3193 gotbno = NULLFSBLOCK;
3194 }
3195 /*
3196 * No next block, just default.
3197 */
3198 else
3199 gotbno = NULLFSBLOCK;
3200 /*
3201 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003202 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 */
3204 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003205 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003207 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003209 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003211#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003212}
3213
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003214static int
3215xfs_bmap_longest_free_extent(
3216 struct xfs_trans *tp,
3217 xfs_agnumber_t ag,
3218 xfs_extlen_t *blen,
3219 int *notinit)
3220{
3221 struct xfs_mount *mp = tp->t_mountp;
3222 struct xfs_perag *pag;
3223 xfs_extlen_t longest;
3224 int error = 0;
3225
3226 pag = xfs_perag_get(mp, ag);
3227 if (!pag->pagf_init) {
3228 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3229 if (error)
3230 goto out;
3231
3232 if (!pag->pagf_init) {
3233 *notinit = 1;
3234 goto out;
3235 }
3236 }
3237
Dave Chinner50adbcb2015-06-22 10:04:31 +10003238 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003239 xfs_alloc_min_freelist(mp, pag),
3240 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003241 if (*blen < longest)
3242 *blen = longest;
3243
3244out:
3245 xfs_perag_put(pag);
3246 return error;
3247}
3248
3249static void
3250xfs_bmap_select_minlen(
3251 struct xfs_bmalloca *ap,
3252 struct xfs_alloc_arg *args,
3253 xfs_extlen_t *blen,
3254 int notinit)
3255{
3256 if (notinit || *blen < ap->minlen) {
3257 /*
3258 * Since we did a BUF_TRYLOCK above, it is possible that
3259 * there is space for this request.
3260 */
3261 args->minlen = ap->minlen;
3262 } else if (*blen < args->maxlen) {
3263 /*
3264 * If the best seen length is less than the request length,
3265 * use the best as the minimum.
3266 */
3267 args->minlen = *blen;
3268 } else {
3269 /*
3270 * Otherwise we've seen an extent as big as maxlen, use that
3271 * as the minimum.
3272 */
3273 args->minlen = args->maxlen;
3274 }
3275}
3276
Nathan Scotta365bdd2006-03-14 13:34:16 +11003277STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003278xfs_bmap_btalloc_nullfb(
3279 struct xfs_bmalloca *ap,
3280 struct xfs_alloc_arg *args,
3281 xfs_extlen_t *blen)
3282{
3283 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003284 xfs_agnumber_t ag, startag;
3285 int notinit = 0;
3286 int error;
3287
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003288 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003289 args->total = ap->total;
3290
Christoph Hellwigc467c042010-02-15 23:34:42 +00003291 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3292 if (startag == NULLAGNUMBER)
3293 startag = ag = 0;
3294
Dave Chinner14b064c2011-01-27 12:16:28 +11003295 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003296 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3297 &notinit);
3298 if (error)
3299 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003300
Christoph Hellwigc467c042010-02-15 23:34:42 +00003301 if (++ag == mp->m_sb.sb_agcount)
3302 ag = 0;
3303 if (ag == startag)
3304 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003305 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003306
3307 xfs_bmap_select_minlen(ap, args, blen, notinit);
3308 return 0;
3309}
3310
3311STATIC int
3312xfs_bmap_btalloc_filestreams(
3313 struct xfs_bmalloca *ap,
3314 struct xfs_alloc_arg *args,
3315 xfs_extlen_t *blen)
3316{
3317 struct xfs_mount *mp = ap->ip->i_mount;
3318 xfs_agnumber_t ag;
3319 int notinit = 0;
3320 int error;
3321
3322 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3323 args->total = ap->total;
3324
3325 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3326 if (ag == NULLAGNUMBER)
3327 ag = 0;
3328
3329 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3330 if (error)
3331 return error;
3332
3333 if (*blen < args->maxlen) {
3334 error = xfs_filestream_new_ag(ap, &ag);
3335 if (error)
3336 return error;
3337
3338 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3339 &notinit);
3340 if (error)
3341 return error;
3342
3343 }
3344
3345 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003346
3347 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003348 * Set the failure fallback case to look in the selected AG as stream
3349 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003350 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003351 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003352 return 0;
3353}
3354
3355STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003356xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003357 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003358{
3359 xfs_mount_t *mp; /* mount point structure */
3360 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003361 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003362 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003363 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003364 xfs_alloc_arg_t args;
3365 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003366 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003367 int nullfb; /* true if ap->firstblock isn't set */
3368 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003369 int tryagain;
3370 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003371 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003372
Dave Chinnera99ebf42011-12-01 11:24:20 +00003373 ASSERT(ap->length);
3374
Nathan Scotta365bdd2006-03-14 13:34:16 +11003375 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003376
3377 /* stripe alignment for allocation is determined by mount parameters */
3378 stripe_align = 0;
3379 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3380 stripe_align = mp->m_swidth;
3381 else if (mp->m_dalign)
3382 stripe_align = mp->m_dalign;
3383
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003384 if (ap->flags & XFS_BMAPI_COWFORK)
3385 align = xfs_get_cowextsz_hint(ap->ip);
3386 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003387 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003388 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003389 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003390 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003391 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003392 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003393 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003394 }
Dave Chinner33177f052013-12-12 16:34:36 +11003395
3396
Dave Chinner0937e0f2011-09-18 20:40:57 +00003397 nullfb = *ap->firstblock == NULLFSBLOCK;
3398 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003399 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003400 if (xfs_alloc_is_userdata(ap->datatype) &&
3401 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003402 ag = xfs_filestream_lookup_ag(ap->ip);
3403 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003404 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003405 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003406 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003407 }
3408 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003409 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003410
3411 xfs_bmap_adjacent(ap);
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003414 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 * it's in the right allocation group.
3416 */
Dave Chinner3a756672011-09-18 20:40:58 +00003417 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 ;
3419 else
Dave Chinner3a756672011-09-18 20:40:58 +00003420 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 * Normal allocation, done through xfs_alloc_vextent.
3423 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003424 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003425 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003426 args.tp = ap->tp;
3427 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003428 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003429 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003430
3431 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003432 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003433 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003434 blen = 0;
3435 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003436 /*
3437 * Search for an allocation group with a single extent large
3438 * enough for the request. If one isn't found, then adjust
3439 * the minimum allocation size to the largest space found.
3440 */
Dave Chinner292378e2016-09-26 08:21:28 +10003441 if (xfs_alloc_is_userdata(ap->datatype) &&
3442 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003443 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3444 else
3445 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003446 if (error)
3447 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003448 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003449 if (xfs_inode_is_filestream(ap->ip))
3450 args.type = XFS_ALLOCTYPE_FIRST_AG;
3451 else
3452 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003453 args.total = args.minlen = ap->minlen;
3454 } else {
3455 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3456 args.total = ap->total;
3457 args.minlen = ap->minlen;
3458 }
David Chinner957d0eb2007-06-18 16:50:37 +10003459 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003460 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003461 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003462 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003463 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003464 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003465 args.prod = 1;
3466 args.mod = 0;
3467 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003468 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003469 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003470 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3471 }
3472 /*
3473 * If we are not low on available data blocks, and the
3474 * underlying logical volume manager is a stripe, and
3475 * the file offset is zero then try to allocate data
3476 * blocks on stripe unit boundary.
3477 * NOTE: ap->aeof is only set if the allocation length
3478 * is >= the stripe unit and the allocation offset is
3479 * at the end of file.
3480 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003481 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003482 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003483 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003484 atype = args.type;
3485 isaligned = 1;
3486 /*
3487 * Adjust for alignment
3488 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003489 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003490 args.minlen = blen - args.alignment;
3491 args.minalignslop = 0;
3492 } else {
3493 /*
3494 * First try an exact bno allocation.
3495 * If it fails then do a near or start bno
3496 * allocation with alignment turned on.
3497 */
3498 atype = args.type;
3499 tryagain = 1;
3500 args.type = XFS_ALLOCTYPE_THIS_BNO;
3501 args.alignment = 1;
3502 /*
3503 * Compute the minlen+alignment for the
3504 * next case. Set slop so that the value
3505 * of minlen+alignment+slop doesn't go up
3506 * between the calls.
3507 */
Dave Chinner33177f052013-12-12 16:34:36 +11003508 if (blen > stripe_align && blen <= args.maxlen)
3509 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003510 else
3511 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003512 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003513 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003514 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003515 args.minlen - 1;
3516 else
3517 args.minalignslop = 0;
3518 }
3519 } else {
3520 args.alignment = 1;
3521 args.minalignslop = 0;
3522 }
3523 args.minleft = ap->minleft;
3524 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003525 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003526 args.datatype = ap->datatype;
3527 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003528 args.ip = ap->ip;
3529
3530 error = xfs_alloc_vextent(&args);
3531 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003532 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003533
Nathan Scotta365bdd2006-03-14 13:34:16 +11003534 if (tryagain && args.fsbno == NULLFSBLOCK) {
3535 /*
3536 * Exact allocation failed. Now try with alignment
3537 * turned on.
3538 */
3539 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003540 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003541 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003542 args.minlen = nextminlen;
3543 args.minalignslop = 0;
3544 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 if ((error = xfs_alloc_vextent(&args)))
3546 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003547 }
3548 if (isaligned && args.fsbno == NULLFSBLOCK) {
3549 /*
3550 * allocation failed, so turn off alignment and
3551 * try again.
3552 */
3553 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003554 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003555 args.alignment = 0;
3556 if ((error = xfs_alloc_vextent(&args)))
3557 return error;
3558 }
3559 if (args.fsbno == NULLFSBLOCK && nullfb &&
3560 args.minlen > ap->minlen) {
3561 args.minlen = ap->minlen;
3562 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003563 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564 if ((error = xfs_alloc_vextent(&args)))
3565 return error;
3566 }
3567 if (args.fsbno == NULLFSBLOCK && nullfb) {
3568 args.fsbno = 0;
3569 args.type = XFS_ALLOCTYPE_FIRST_AG;
3570 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003571 if ((error = xfs_alloc_vextent(&args)))
3572 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003573 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003574 }
3575 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003576 /*
3577 * check the allocation happened at the same or higher AG than
3578 * the first block that was allocated.
3579 */
3580 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003581 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3582 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003583
Dave Chinner3a756672011-09-18 20:40:58 +00003584 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003585 if (*ap->firstblock == NULLFSBLOCK)
3586 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003587 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003588 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003589 if (!(ap->flags & XFS_BMAPI_COWFORK))
3590 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003591 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3592 if (ap->wasdel)
3593 ap->ip->i_delayed_blks -= args.len;
3594 /*
3595 * Adjust the disk quota also. This was reserved
3596 * earlier.
3597 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003598 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003599 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3600 XFS_TRANS_DQ_BCOUNT,
3601 (long) args.len);
3602 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003603 ap->blkno = NULLFSBLOCK;
3604 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
3606 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003607}
3608
3609/*
3610 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3611 * It figures out where to ask the underlying allocator to put the new extent.
3612 */
3613STATIC int
3614xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003615 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003616{
Dave Chinner292378e2016-09-26 08:21:28 +10003617 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3618 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003619 return xfs_bmap_rtalloc(ap);
3620 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003623/* Trim extent to fit a logical block range. */
3624void
3625xfs_trim_extent(
3626 struct xfs_bmbt_irec *irec,
3627 xfs_fileoff_t bno,
3628 xfs_filblks_t len)
3629{
3630 xfs_fileoff_t distance;
3631 xfs_fileoff_t end = bno + len;
3632
3633 if (irec->br_startoff + irec->br_blockcount <= bno ||
3634 irec->br_startoff >= end) {
3635 irec->br_blockcount = 0;
3636 return;
3637 }
3638
3639 if (irec->br_startoff < bno) {
3640 distance = bno - irec->br_startoff;
3641 if (isnullstartblock(irec->br_startblock))
3642 irec->br_startblock = DELAYSTARTBLOCK;
3643 if (irec->br_startblock != DELAYSTARTBLOCK &&
3644 irec->br_startblock != HOLESTARTBLOCK)
3645 irec->br_startblock += distance;
3646 irec->br_startoff += distance;
3647 irec->br_blockcount -= distance;
3648 }
3649
3650 if (end < irec->br_startoff + irec->br_blockcount) {
3651 distance = irec->br_startoff + irec->br_blockcount - end;
3652 irec->br_blockcount -= distance;
3653 }
3654}
3655
Brian Foster40214d12017-10-13 09:47:46 -07003656/* trim extent to within eof */
3657void
3658xfs_trim_extent_eof(
3659 struct xfs_bmbt_irec *irec,
3660 struct xfs_inode *ip)
3661
3662{
3663 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3664 i_size_read(VFS_I(ip))));
3665}
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003668 * Trim the returned map to the required bounds
3669 */
3670STATIC void
3671xfs_bmapi_trim_map(
3672 struct xfs_bmbt_irec *mval,
3673 struct xfs_bmbt_irec *got,
3674 xfs_fileoff_t *bno,
3675 xfs_filblks_t len,
3676 xfs_fileoff_t obno,
3677 xfs_fileoff_t end,
3678 int n,
3679 int flags)
3680{
3681 if ((flags & XFS_BMAPI_ENTIRE) ||
3682 got->br_startoff + got->br_blockcount <= obno) {
3683 *mval = *got;
3684 if (isnullstartblock(got->br_startblock))
3685 mval->br_startblock = DELAYSTARTBLOCK;
3686 return;
3687 }
3688
3689 if (obno > *bno)
3690 *bno = obno;
3691 ASSERT((*bno >= obno) || (n == 0));
3692 ASSERT(*bno < end);
3693 mval->br_startoff = *bno;
3694 if (isnullstartblock(got->br_startblock))
3695 mval->br_startblock = DELAYSTARTBLOCK;
3696 else
3697 mval->br_startblock = got->br_startblock +
3698 (*bno - got->br_startoff);
3699 /*
3700 * Return the minimum of what we got and what we asked for for
3701 * the length. We can use the len variable here because it is
3702 * modified below and we could have been there before coming
3703 * here if the first part of the allocation didn't overlap what
3704 * was asked for.
3705 */
3706 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3707 got->br_blockcount - (*bno - got->br_startoff));
3708 mval->br_state = got->br_state;
3709 ASSERT(mval->br_blockcount <= len);
3710 return;
3711}
3712
3713/*
3714 * Update and validate the extent map to return
3715 */
3716STATIC void
3717xfs_bmapi_update_map(
3718 struct xfs_bmbt_irec **map,
3719 xfs_fileoff_t *bno,
3720 xfs_filblks_t *len,
3721 xfs_fileoff_t obno,
3722 xfs_fileoff_t end,
3723 int *n,
3724 int flags)
3725{
3726 xfs_bmbt_irec_t *mval = *map;
3727
3728 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3729 ((mval->br_startoff + mval->br_blockcount) <= end));
3730 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3731 (mval->br_startoff < obno));
3732
3733 *bno = mval->br_startoff + mval->br_blockcount;
3734 *len = end - *bno;
3735 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3736 /* update previous map with new information */
3737 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3738 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3739 ASSERT(mval->br_state == mval[-1].br_state);
3740 mval[-1].br_blockcount = mval->br_blockcount;
3741 mval[-1].br_state = mval->br_state;
3742 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3743 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3744 mval[-1].br_startblock != HOLESTARTBLOCK &&
3745 mval->br_startblock == mval[-1].br_startblock +
3746 mval[-1].br_blockcount &&
3747 ((flags & XFS_BMAPI_IGSTATE) ||
3748 mval[-1].br_state == mval->br_state)) {
3749 ASSERT(mval->br_startoff ==
3750 mval[-1].br_startoff + mval[-1].br_blockcount);
3751 mval[-1].br_blockcount += mval->br_blockcount;
3752 } else if (*n > 0 &&
3753 mval->br_startblock == DELAYSTARTBLOCK &&
3754 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3755 mval->br_startoff ==
3756 mval[-1].br_startoff + mval[-1].br_blockcount) {
3757 mval[-1].br_blockcount += mval->br_blockcount;
3758 mval[-1].br_state = mval->br_state;
3759 } else if (!((*n == 0) &&
3760 ((mval->br_startoff + mval->br_blockcount) <=
3761 obno))) {
3762 mval++;
3763 (*n)++;
3764 }
3765 *map = mval;
3766}
3767
3768/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003769 * Map file blocks to filesystem blocks without allocation.
3770 */
3771int
3772xfs_bmapi_read(
3773 struct xfs_inode *ip,
3774 xfs_fileoff_t bno,
3775 xfs_filblks_t len,
3776 struct xfs_bmbt_irec *mval,
3777 int *nmap,
3778 int flags)
3779{
3780 struct xfs_mount *mp = ip->i_mount;
3781 struct xfs_ifork *ifp;
3782 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003783 xfs_fileoff_t obno;
3784 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003785 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003786 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003787 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003788 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003789 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003790
3791 ASSERT(*nmap >= 1);
3792 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003793 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003794 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003795
3796 if (unlikely(XFS_TEST_ERROR(
3797 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3798 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003799 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003800 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003801 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003802 }
3803
3804 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003805 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003806
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003807 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003808
3809 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003810
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003811 /* No CoW fork? Return a hole. */
3812 if (whichfork == XFS_COW_FORK && !ifp) {
3813 mval->br_startoff = bno;
3814 mval->br_startblock = HOLESTARTBLOCK;
3815 mval->br_blockcount = len;
3816 mval->br_state = XFS_EXT_NORM;
3817 *nmap = 1;
3818 return 0;
3819 }
3820
Dave Chinner5c8ed202011-09-18 20:40:45 +00003821 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3822 error = xfs_iread_extents(NULL, ip, whichfork);
3823 if (error)
3824 return error;
3825 }
3826
Christoph Hellwig334f3422016-11-24 11:39:43 +11003827 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3828 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003829 end = bno + len;
3830 obno = bno;
3831
3832 while (bno < end && n < *nmap) {
3833 /* Reading past eof, act as though there's a hole up to end. */
3834 if (eof)
3835 got.br_startoff = end;
3836 if (got.br_startoff > bno) {
3837 /* Reading in a hole. */
3838 mval->br_startoff = bno;
3839 mval->br_startblock = HOLESTARTBLOCK;
3840 mval->br_blockcount =
3841 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3842 mval->br_state = XFS_EXT_NORM;
3843 bno += mval->br_blockcount;
3844 len -= mval->br_blockcount;
3845 mval++;
3846 n++;
3847 continue;
3848 }
3849
3850 /* set up the extent map to return. */
3851 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3852 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3853
3854 /* If we're done, stop now. */
3855 if (bno >= end || n >= *nmap)
3856 break;
3857
3858 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003859 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3860 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003861 }
3862 *nmap = n;
3863 return 0;
3864}
3865
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003866/*
3867 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3868 * global pool and the extent inserted into the inode in-core extent tree.
3869 *
3870 * On entry, got refers to the first extent beyond the offset of the extent to
3871 * allocate or eof is specified if no such extent exists. On return, got refers
3872 * to the extent record that was inserted to the inode fork.
3873 *
3874 * Note that the allocated extent may have been merged with contiguous extents
3875 * during insertion into the inode fork. Thus, got does not reflect the current
3876 * state of the inode fork on return. If necessary, the caller can use lastx to
3877 * look up the updated record in the inode fork.
3878 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003879int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003880xfs_bmapi_reserve_delalloc(
3881 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003882 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003883 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003884 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003885 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003886 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003887 xfs_extnum_t *lastx,
3888 int eof)
3889{
3890 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003891 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003892 xfs_extlen_t alen;
3893 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003894 char rt = XFS_IS_REALTIME_INODE(ip);
3895 xfs_extlen_t extsz;
3896 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003897 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003898
Brian Foster974ae922016-11-28 14:57:42 +11003899 /*
3900 * Cap the alloc length. Keep track of prealloc so we know whether to
3901 * tag the inode before we return.
3902 */
3903 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003904 if (!eof)
3905 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003906 if (prealloc && alen >= len)
3907 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003908
3909 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003910 if (whichfork == XFS_COW_FORK)
3911 extsz = xfs_get_cowextsz_hint(ip);
3912 else
3913 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003914 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003915 struct xfs_bmbt_irec prev;
3916
3917 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
3918 prev.br_startoff = NULLFILEOFF;
3919
3920 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003921 1, 0, &aoff, &alen);
3922 ASSERT(!error);
3923 }
3924
3925 if (rt)
3926 extsz = alen / mp->m_sb.sb_rextsize;
3927
3928 /*
3929 * Make a transaction-less quota reservation for delayed allocation
3930 * blocks. This number gets adjusted later. We return if we haven't
3931 * allocated blocks already inside this loop.
3932 */
3933 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3934 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3935 if (error)
3936 return error;
3937
3938 /*
3939 * Split changing sb for alen and indlen since they could be coming
3940 * from different places.
3941 */
3942 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3943 ASSERT(indlen > 0);
3944
3945 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003946 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003947 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11003948 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003949 }
3950
3951 if (error)
3952 goto out_unreserve_quota;
3953
Dave Chinner0d485ad2015-02-23 21:22:03 +11003954 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003955 if (error)
3956 goto out_unreserve_blocks;
3957
3958
3959 ip->i_delayed_blks += alen;
3960
3961 got->br_startoff = aoff;
3962 got->br_startblock = nullstartblock(indlen);
3963 got->br_blockcount = alen;
3964 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003965
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003966 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003967
Brian Foster974ae922016-11-28 14:57:42 +11003968 /*
3969 * Tag the inode if blocks were preallocated. Note that COW fork
3970 * preallocation can occur at the start or end of the extent, even when
3971 * prealloc == 0, so we must also check the aligned offset and length.
3972 */
3973 if (whichfork == XFS_DATA_FORK && prealloc)
3974 xfs_inode_set_eofblocks_tag(ip);
3975 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3976 xfs_inode_set_cowblocks_tag(ip);
3977
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003978 return 0;
3979
3980out_unreserve_blocks:
3981 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003982 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003983 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11003984 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003985out_unreserve_quota:
3986 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10003987 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003988 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3989 return error;
3990}
3991
Dave Chinnercf11da92014-07-15 07:08:24 +10003992static int
3993xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10003994 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003995{
3996 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003997 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003998 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00003999 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004000 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004001
Dave Chinnera99ebf42011-12-01 11:24:20 +00004002 ASSERT(bma->length > 0);
4003
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004004 /*
4005 * For the wasdelay case, we could also just allocate the stuff asked
4006 * for in this bmap call but that wouldn't be as good.
4007 */
4008 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004009 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4010 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004011 if (bma->idx)
4012 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004013 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004014 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004015 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004016 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004017 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004018 }
4019
4020 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004021 * Set the data type being allocated. For the data fork, the first data
4022 * in the file is treated differently to all other allocations. For the
4023 * attribute fork, we only need to ensure the allocated range is not on
4024 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004025 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004026 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004027 bma->datatype = XFS_ALLOC_NOBUSY;
4028 if (whichfork == XFS_DATA_FORK) {
4029 if (bma->offset == 0)
4030 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4031 else
4032 bma->datatype |= XFS_ALLOC_USERDATA;
4033 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004034 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004035 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004036 }
4037
Dave Chinnere04426b2012-10-05 11:06:59 +10004038 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004039
4040 /*
4041 * Only want to do the alignment at the eof if it is userdata and
4042 * allocation length is larger than a stripe unit.
4043 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004044 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004045 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004046 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004047 if (error)
4048 return error;
4049 }
4050
4051 error = xfs_bmap_alloc(bma);
4052 if (error)
4053 return error;
4054
Dave Chinner29c8d172011-09-18 20:41:00 +00004055 if (bma->cur)
4056 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004057 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004058 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004059 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4060 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4061 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004062 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004063 }
4064 /*
4065 * Bump the number of extents we've allocated
4066 * in this call.
4067 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004068 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004069
Dave Chinner29c8d172011-09-18 20:41:00 +00004070 if (bma->cur)
4071 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004072 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4073
Dave Chinner963c30c2011-09-18 20:40:59 +00004074 bma->got.br_startoff = bma->offset;
4075 bma->got.br_startblock = bma->blkno;
4076 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004077 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004078
4079 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004080 * In the data fork, a wasdelay extent has been initialized, so
4081 * shouldn't be flagged as unwritten.
4082 *
4083 * For the cow fork, however, we convert delalloc reservations
4084 * (extents allocated for speculative preallocation) to
4085 * allocated unwritten extents, and only convert the unwritten
4086 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004087 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004088 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4089 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004090 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004091 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004092
Christoph Hellwigc6534242011-09-18 20:41:05 +00004093 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004094 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004095 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004096 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4097 whichfork, &bma->idx, &bma->cur, &bma->got,
4098 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004099
Christoph Hellwigc315c902011-09-18 20:41:02 +00004100 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004101 if (error)
4102 return error;
4103
4104 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004105 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4106 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4107 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004108 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004109 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004110
Dave Chinner963c30c2011-09-18 20:40:59 +00004111 ASSERT(bma->got.br_startoff <= bma->offset);
4112 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4113 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004114 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4115 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004116 return 0;
4117}
4118
Dave Chinnerb447fe52011-09-18 20:40:51 +00004119STATIC int
4120xfs_bmapi_convert_unwritten(
4121 struct xfs_bmalloca *bma,
4122 struct xfs_bmbt_irec *mval,
4123 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004124 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004125{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004126 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004127 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004128 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004129 int error;
4130
Dave Chinnerb447fe52011-09-18 20:40:51 +00004131 /* check if we need to do unwritten->real conversion */
4132 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4133 (flags & XFS_BMAPI_PREALLOC))
4134 return 0;
4135
4136 /* check if we need to do real->unwritten conversion */
4137 if (mval->br_state == XFS_EXT_NORM &&
4138 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4139 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4140 return 0;
4141
4142 /*
4143 * Modify (by adding) the state flag, if writing.
4144 */
4145 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004146 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4147 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004148 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004149 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004150 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004151 }
4152 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4153 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4154
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004155 /*
4156 * Before insertion into the bmbt, zero the range being converted
4157 * if required.
4158 */
4159 if (flags & XFS_BMAPI_ZERO) {
4160 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4161 mval->br_blockcount);
4162 if (error)
4163 return error;
4164 }
4165
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004166 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4167 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004168 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004169 /*
4170 * Log the inode core unconditionally in the unwritten extent conversion
4171 * path because the conversion might not have done so (e.g., if the
4172 * extent count hasn't changed). We need to make sure the inode is dirty
4173 * in the transaction for the sake of fsync(), even if nothing has
4174 * changed, because fsync() will not force the log for this transaction
4175 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004176 *
4177 * Note: If we're only converting cow fork extents, there aren't
4178 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004179 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004180 if (whichfork != XFS_COW_FORK)
4181 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004182 if (error)
4183 return error;
4184
4185 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004186 * Update our extent pointer, given that
4187 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4188 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004189 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004190 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004191
4192 /*
4193 * We may have combined previously unwritten space with written space,
4194 * so generate another request.
4195 */
4196 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004197 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004198 return 0;
4199}
4200
Christoph Hellwig44032802011-09-18 20:40:48 +00004201/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004202 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4203 * extent state if necessary. Details behaviour is controlled by the flags
4204 * parameter. Only allocates blocks from a single allocation group, to avoid
4205 * locking problems.
4206 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 * The returned value in "firstblock" from the first call in a transaction
4208 * must be remembered and presented to subsequent calls in "firstblock".
4209 * An upper bound for the number of blocks to be allocated is supplied to
4210 * the first call in "total"; if no allocation group has that many free
4211 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4212 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004213int
4214xfs_bmapi_write(
4215 struct xfs_trans *tp, /* transaction pointer */
4216 struct xfs_inode *ip, /* incore inode */
4217 xfs_fileoff_t bno, /* starting file offs. mapped */
4218 xfs_filblks_t len, /* length to map in file */
4219 int flags, /* XFS_BMAPI_... */
4220 xfs_fsblock_t *firstblock, /* first allocated block
4221 controls a.g. for allocs */
4222 xfs_extlen_t total, /* total blocks needed */
4223 struct xfs_bmbt_irec *mval, /* output: map values */
4224 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004225 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004227 struct xfs_mount *mp = ip->i_mount;
4228 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004229 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004230 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004231 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004232 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004233 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004234 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004235 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004238 xfs_fileoff_t orig_bno; /* original block number value */
4239 int orig_flags; /* original flags arg value */
4240 xfs_filblks_t orig_len; /* original value of len arg */
4241 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4242 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 orig_bno = bno;
4245 orig_len = len;
4246 orig_flags = flags;
4247 orig_mval = mval;
4248 orig_nmap = *nmap;
4249#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004250 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004253 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4254 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004255 ASSERT(tp != NULL ||
4256 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4257 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004258 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004259 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004260 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004261 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004262
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004263 /* zeroing is for currently only for data extents, not metadata */
4264 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4265 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4266 /*
4267 * we can allocate unwritten extents or pre-zero allocated blocks,
4268 * but it makes no sense to do both at once. This would result in
4269 * zeroing the unwritten extent twice, but it still being an
4270 * unwritten extent....
4271 */
4272 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4273 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (unlikely(XFS_TEST_ERROR(
4276 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004277 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004278 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004279 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004280 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004284 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004287
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004288 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004289
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004290 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004292 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004294 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004295 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004296 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004297 }
4298
4299 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4300 error = xfs_iread_extents(tp, ip, whichfork);
4301 if (error)
4302 goto error0;
4303 }
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 n = 0;
4306 end = bno + len;
4307 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004308
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004309 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4310 eof = true;
4311 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4312 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004313 bma.tp = tp;
4314 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004315 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004316 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004317 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004318 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004321 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004322
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004323 /* in hole or beyoned EOF? */
4324 if (eof || bma.got.br_startoff > bno) {
4325 if (flags & XFS_BMAPI_DELALLOC) {
4326 /*
4327 * For the COW fork we can reasonably get a
4328 * request for converting an extent that races
4329 * with other threads already having converted
4330 * part of it, as there converting COW to
4331 * regular blocks is not protected using the
4332 * IOLOCK.
4333 */
4334 ASSERT(flags & XFS_BMAPI_COWFORK);
4335 if (!(flags & XFS_BMAPI_COWFORK)) {
4336 error = -EIO;
4337 goto error0;
4338 }
4339
4340 if (eof || bno >= end)
4341 break;
4342 } else {
4343 need_alloc = true;
4344 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004345 } else if (isnullstartblock(bma.got.br_startblock)) {
4346 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004347 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004348
4349 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 * First, deal with the hole before the allocated space
4351 * that we found, if any.
4352 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004353 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354 bma.eof = eof;
4355 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4356 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004357 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004358 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004359
Dave Chinnera99ebf42011-12-01 11:24:20 +00004360 /*
4361 * There's a 32/64 bit type mismatch between the
4362 * allocation length request (which can be 64 bits in
4363 * length) and the bma length request, which is
4364 * xfs_extlen_t and therefore 32 bits. Hence we have to
4365 * check for 32-bit overflows and handle them here.
4366 */
4367 if (len > (xfs_filblks_t)MAXEXTLEN)
4368 bma.length = MAXEXTLEN;
4369 else
4370 bma.length = len;
4371
4372 ASSERT(len > 0);
4373 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004374 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 if (error)
4376 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004377 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004378 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004379
4380 /*
4381 * If this is a CoW allocation, record the data in
4382 * the refcount btree for orphan recovery.
4383 */
4384 if (whichfork == XFS_COW_FORK) {
4385 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4386 bma.blkno, bma.length);
4387 if (error)
4388 goto error0;
4389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004391
Dave Chinneraef9a892011-09-18 20:40:44 +00004392 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004393 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4394 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Dave Chinnerb447fe52011-09-18 20:40:51 +00004396 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004397 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004398 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004399 continue;
4400 if (error)
4401 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
Dave Chinneraef9a892011-09-18 20:40:44 +00004403 /* update the extent map to return */
4404 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4405
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 /*
4407 * If we're done, stop now. Stop when we've allocated
4408 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4409 * the transaction may get too big.
4410 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004411 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004413
4414 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004415 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004416 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4417 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 /*
4422 * Transform from btree to extents, give it cur.
4423 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004424 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004425 int tmp_logflags = 0;
4426
Dave Chinner29c8d172011-09-18 20:41:00 +00004427 ASSERT(bma.cur);
4428 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004430 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 if (error)
4432 goto error0;
4433 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004436 XFS_IFORK_NEXTENTS(ip, whichfork) >
4437 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439error0:
4440 /*
4441 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004442 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004444 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004446 bma.logflags &= ~xfs_ilog_fext(whichfork);
4447 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004449 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 /*
4451 * Log whatever the flags say, even if error. Otherwise we might miss
4452 * detecting a case where the data is changed, there's an error,
4453 * and it's not logged so we don't shutdown when we should.
4454 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004455 if (bma.logflags)
4456 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004457
Dave Chinner29c8d172011-09-18 20:41:00 +00004458 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 if (!error) {
4460 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004461 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004463 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004464 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004466 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4468 }
4469 if (!error)
4470 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4471 orig_nmap, *nmap);
4472 return error;
4473}
4474
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004475static int
4476xfs_bmapi_remap(
4477 struct xfs_trans *tp,
4478 struct xfs_inode *ip,
4479 xfs_fileoff_t bno,
4480 xfs_filblks_t len,
4481 xfs_fsblock_t startblock,
4482 struct xfs_defer_ops *dfops)
4483{
4484 struct xfs_mount *mp = ip->i_mount;
4485 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4486 struct xfs_btree_cur *cur = NULL;
4487 xfs_fsblock_t firstblock = NULLFSBLOCK;
4488 struct xfs_bmbt_irec got;
4489 xfs_extnum_t idx;
4490 int logflags = 0, error;
4491
4492 ASSERT(len > 0);
4493 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4494 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4495
4496 if (unlikely(XFS_TEST_ERROR(
4497 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4498 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004499 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004500 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4501 return -EFSCORRUPTED;
4502 }
4503
4504 if (XFS_FORCED_SHUTDOWN(mp))
4505 return -EIO;
4506
4507 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4508 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4509 if (error)
4510 return error;
4511 }
4512
4513 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4514 /* make sure we only reflink into a hole. */
4515 ASSERT(got.br_startoff > bno);
4516 ASSERT(got.br_startoff - bno >= len);
4517 }
4518
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004519 ip->i_d.di_nblocks += len;
4520 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004521
4522 if (ifp->if_flags & XFS_IFBROOT) {
4523 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4524 cur->bc_private.b.firstblock = firstblock;
4525 cur->bc_private.b.dfops = dfops;
4526 cur->bc_private.b.flags = 0;
4527 }
4528
4529 got.br_startoff = bno;
4530 got.br_startblock = startblock;
4531 got.br_blockcount = len;
4532 got.br_state = XFS_EXT_NORM;
4533
4534 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4535 &got, &firstblock, dfops, &logflags);
4536 if (error)
4537 goto error0;
4538
4539 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4540 int tmp_logflags = 0;
4541
4542 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4543 &tmp_logflags, XFS_DATA_FORK);
4544 logflags |= tmp_logflags;
4545 }
4546
4547error0:
4548 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4549 logflags &= ~XFS_ILOG_DEXT;
4550 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4551 logflags &= ~XFS_ILOG_DBROOT;
4552
4553 if (logflags)
4554 xfs_trans_log_inode(tp, ip, logflags);
4555 if (cur) {
4556 xfs_btree_del_cursor(cur,
4557 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4558 }
4559 return error;
4560}
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004563 * When a delalloc extent is split (e.g., due to a hole punch), the original
4564 * indlen reservation must be shared across the two new extents that are left
4565 * behind.
4566 *
4567 * Given the original reservation and the worst case indlen for the two new
4568 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004569 * reservation fairly across the two new extents. If necessary, steal available
4570 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4571 * ores == 1). The number of stolen blocks is returned. The availability and
4572 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004573 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004574static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004575xfs_bmap_split_indlen(
4576 xfs_filblks_t ores, /* original res. */
4577 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004578 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4579 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004580{
4581 xfs_filblks_t len1 = *indlen1;
4582 xfs_filblks_t len2 = *indlen2;
4583 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004584 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004585 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004586
4587 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004588 * Steal as many blocks as we can to try and satisfy the worst case
4589 * indlen for both new extents.
4590 */
Brian Foster75d65362017-02-13 22:48:30 -08004591 if (ores < nres && avail)
4592 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4593 ores += stolen;
4594
4595 /* nothing else to do if we've satisfied the new reservation */
4596 if (ores >= nres)
4597 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004598
4599 /*
Brian Foster75d65362017-02-13 22:48:30 -08004600 * We can't meet the total required reservation for the two extents.
4601 * Calculate the percent of the overall shortage between both extents
4602 * and apply this percentage to each of the requested indlen values.
4603 * This distributes the shortage fairly and reduces the chances that one
4604 * of the two extents is left with nothing when extents are repeatedly
4605 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004606 */
Brian Foster75d65362017-02-13 22:48:30 -08004607 resfactor = (ores * 100);
4608 do_div(resfactor, nres);
4609 len1 *= resfactor;
4610 do_div(len1, 100);
4611 len2 *= resfactor;
4612 do_div(len2, 100);
4613 ASSERT(len1 + len2 <= ores);
4614 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4615
4616 /*
4617 * Hand out the remainder to each extent. If one of the two reservations
4618 * is zero, we want to make sure that one gets a block first. The loop
4619 * below starts with len1, so hand len2 a block right off the bat if it
4620 * is zero.
4621 */
4622 ores -= (len1 + len2);
4623 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4624 if (ores && !len2 && *indlen2) {
4625 len2++;
4626 ores--;
4627 }
4628 while (ores) {
4629 if (len1 < *indlen1) {
4630 len1++;
4631 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004632 }
Brian Foster75d65362017-02-13 22:48:30 -08004633 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004634 break;
Brian Foster75d65362017-02-13 22:48:30 -08004635 if (len2 < *indlen2) {
4636 len2++;
4637 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004638 }
4639 }
4640
4641 *indlen1 = len1;
4642 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004643
4644 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004645}
4646
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004647int
4648xfs_bmap_del_extent_delay(
4649 struct xfs_inode *ip,
4650 int whichfork,
4651 xfs_extnum_t *idx,
4652 struct xfs_bmbt_irec *got,
4653 struct xfs_bmbt_irec *del)
4654{
4655 struct xfs_mount *mp = ip->i_mount;
4656 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4657 struct xfs_bmbt_irec new;
4658 int64_t da_old, da_new, da_diff = 0;
4659 xfs_fileoff_t del_endoff, got_endoff;
4660 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004661 int state = xfs_bmap_fork_to_state(whichfork);
4662 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004663 bool isrt;
4664
4665 XFS_STATS_INC(mp, xs_del_exlist);
4666
4667 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4668 del_endoff = del->br_startoff + del->br_blockcount;
4669 got_endoff = got->br_startoff + got->br_blockcount;
4670 da_old = startblockval(got->br_startblock);
4671 da_new = 0;
4672
4673 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004674 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004675 ASSERT(del->br_blockcount > 0);
4676 ASSERT(got->br_startoff <= del->br_startoff);
4677 ASSERT(got_endoff >= del_endoff);
4678
4679 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004680 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004681
4682 do_div(rtexts, mp->m_sb.sb_rextsize);
4683 xfs_mod_frextents(mp, rtexts);
4684 }
4685
4686 /*
4687 * Update the inode delalloc counter now and wait to update the
4688 * sb counters as we might have to borrow some blocks for the
4689 * indirect block accounting.
4690 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004691 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4692 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004693 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004694 if (error)
4695 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004696 ip->i_delayed_blks -= del->br_blockcount;
4697
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004698 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004699 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004700 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004701 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004702
Christoph Hellwig0173c682017-10-17 14:16:22 -07004703 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4704 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004705 /*
4706 * Matches the whole extent. Delete the entry.
4707 */
4708 xfs_iext_remove(ip, *idx, 1, state);
4709 --*idx;
4710 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004711 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004712 /*
4713 * Deleting the first part of the extent.
4714 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004715 got->br_startoff = del_endoff;
4716 got->br_blockcount -= del->br_blockcount;
4717 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4718 got->br_blockcount), da_old);
4719 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004720 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004721 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004722 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004723 /*
4724 * Deleting the last part of the extent.
4725 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004726 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4727 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4728 got->br_blockcount), da_old);
4729 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004730 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004731 break;
4732 case 0:
4733 /*
4734 * Deleting the middle of the extent.
4735 *
4736 * Distribute the original indlen reservation across the two new
4737 * extents. Steal blocks from the deleted extent if necessary.
4738 * Stealing blocks simply fudges the fdblocks accounting below.
4739 * Warn if either of the new indlen reservations is zero as this
4740 * can lead to delalloc problems.
4741 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004742 got->br_blockcount = del->br_startoff - got->br_startoff;
4743 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4744
4745 new.br_blockcount = got_endoff - del_endoff;
4746 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4747
4748 WARN_ON_ONCE(!got_indlen || !new_indlen);
4749 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4750 del->br_blockcount);
4751
4752 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004753 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004754
4755 new.br_startoff = del_endoff;
4756 new.br_state = got->br_state;
4757 new.br_startblock = nullstartblock((int)new_indlen);
4758
4759 ++*idx;
4760 xfs_iext_insert(ip, *idx, 1, &new, state);
4761
4762 da_new = got_indlen + new_indlen - stolen;
4763 del->br_blockcount -= stolen;
4764 break;
4765 }
4766
4767 ASSERT(da_old >= da_new);
4768 da_diff = da_old - da_new;
4769 if (!isrt)
4770 da_diff += del->br_blockcount;
4771 if (da_diff)
4772 xfs_mod_fdblocks(mp, da_diff, false);
4773 return error;
4774}
4775
4776void
4777xfs_bmap_del_extent_cow(
4778 struct xfs_inode *ip,
4779 xfs_extnum_t *idx,
4780 struct xfs_bmbt_irec *got,
4781 struct xfs_bmbt_irec *del)
4782{
4783 struct xfs_mount *mp = ip->i_mount;
4784 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4785 struct xfs_bmbt_irec new;
4786 xfs_fileoff_t del_endoff, got_endoff;
4787 int state = BMAP_COWFORK;
4788
4789 XFS_STATS_INC(mp, xs_del_exlist);
4790
4791 del_endoff = del->br_startoff + del->br_blockcount;
4792 got_endoff = got->br_startoff + got->br_blockcount;
4793
4794 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004795 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004796 ASSERT(del->br_blockcount > 0);
4797 ASSERT(got->br_startoff <= del->br_startoff);
4798 ASSERT(got_endoff >= del_endoff);
4799 ASSERT(!isnullstartblock(got->br_startblock));
4800
4801 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004802 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004803 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004804 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004805
Christoph Hellwig0173c682017-10-17 14:16:22 -07004806 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4807 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004808 /*
4809 * Matches the whole extent. Delete the entry.
4810 */
4811 xfs_iext_remove(ip, *idx, 1, state);
4812 --*idx;
4813 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004814 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004815 /*
4816 * Deleting the first part of the extent.
4817 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004818 got->br_startoff = del_endoff;
4819 got->br_blockcount -= del->br_blockcount;
4820 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004821 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004822 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004823 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004824 /*
4825 * Deleting the last part of the extent.
4826 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004827 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004828 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004829 break;
4830 case 0:
4831 /*
4832 * Deleting the middle of the extent.
4833 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004834 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004835 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004836
4837 new.br_startoff = del_endoff;
4838 new.br_blockcount = got_endoff - del_endoff;
4839 new.br_state = got->br_state;
4840 new.br_startblock = del->br_startblock + del->br_blockcount;
4841
4842 ++*idx;
4843 xfs_iext_insert(ip, *idx, 1, &new, state);
4844 break;
4845 }
4846}
4847
Brian Fostera9bd24a2016-03-15 11:42:46 +11004848/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004849 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004850 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004851 */
4852STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004853xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004854 xfs_inode_t *ip, /* incore inode pointer */
4855 xfs_trans_t *tp, /* current transaction pointer */
4856 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004857 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004858 xfs_btree_cur_t *cur, /* if null, not a btree */
4859 xfs_bmbt_irec_t *del, /* data to remove from extents */
4860 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004861 int whichfork, /* data or attr fork */
4862 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004863{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004864 xfs_fsblock_t del_endblock=0; /* first block past del */
4865 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004866 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004867 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004868 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004869 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004870 xfs_fileoff_t got_endoff; /* first offset past got */
4871 int i; /* temp state */
4872 xfs_ifork_t *ifp; /* inode fork pointer */
4873 xfs_mount_t *mp; /* mount structure */
4874 xfs_filblks_t nblks; /* quota/sb block count */
4875 xfs_bmbt_irec_t new; /* new record to be inserted */
4876 /* REFERENCED */
4877 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004878 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004879 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004880
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004881 mp = ip->i_mount;
4882 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004883
Dave Chinner9e5987a72013-02-25 12:31:26 +11004884 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004885 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004886 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004887 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004888 ASSERT(got.br_startoff <= del->br_startoff);
4889 del_endoff = del->br_startoff + del->br_blockcount;
4890 got_endoff = got.br_startoff + got.br_blockcount;
4891 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004892 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004893 qfield = 0;
4894 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004895
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004896 /*
4897 * If it's the case where the directory code is running with no block
4898 * reservation, and the deleted block is in the middle of its extent,
4899 * and the resulting insert of an extent would cause transformation to
4900 * btree format, then reject it. The calling code will then swap blocks
4901 * around instead. We have to do this now, rather than waiting for the
4902 * conversion to btree format, since the transaction will be dirty then.
4903 */
4904 if (tp->t_blk_res == 0 &&
4905 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4906 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4907 XFS_IFORK_MAXEXT(ip, whichfork) &&
4908 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4909 return -ENOSPC;
4910
4911 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004912 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4913 xfs_fsblock_t bno;
4914 xfs_filblks_t len;
4915
4916 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4917 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4918 bno = del->br_startblock;
4919 len = del->br_blockcount;
4920 do_div(bno, mp->m_sb.sb_rextsize);
4921 do_div(len, mp->m_sb.sb_rextsize);
4922 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4923 if (error)
4924 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004925 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004926 nblks = len * mp->m_sb.sb_rextsize;
4927 qfield = XFS_TRANS_DQ_RTBCOUNT;
4928 } else {
4929 do_fx = 1;
4930 nblks = del->br_blockcount;
4931 qfield = XFS_TRANS_DQ_BCOUNT;
4932 }
4933
4934 del_endblock = del->br_startblock + del->br_blockcount;
4935 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004936 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004937 if (error)
4938 goto done;
4939 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004940 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004941
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004942 if (got.br_startoff == del->br_startoff)
4943 state |= BMAP_LEFT_FILLING;
4944 if (got_endoff == del_endoff)
4945 state |= BMAP_RIGHT_FILLING;
4946
4947 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4948 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004949 /*
4950 * Matches the whole extent. Delete the entry.
4951 */
Christoph Hellwig35e62da2017-10-19 11:04:43 -07004952 xfs_iext_remove(ip, *idx, 1, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004953 --*idx;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004954
4955 XFS_IFORK_NEXT_SET(ip, whichfork,
4956 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4957 flags |= XFS_ILOG_CORE;
4958 if (!cur) {
4959 flags |= xfs_ilog_fext(whichfork);
4960 break;
4961 }
4962 if ((error = xfs_btree_delete(cur, &i)))
4963 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004964 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004965 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004966 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004967 /*
4968 * Deleting the first part of the extent.
4969 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004970 got.br_startoff = del_endoff;
4971 got.br_startblock = del_endblock;
4972 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004973 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11004980 goto done;
4981 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004982 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004983 /*
4984 * Deleting the last part of the extent.
4985 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004986 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07004987 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004988 if (!cur) {
4989 flags |= xfs_ilog_fext(whichfork);
4990 break;
4991 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004992 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004993 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11004994 goto done;
4995 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004996 case 0:
4997 /*
4998 * Deleting the middle of the extent.
4999 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005000 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005001
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005002 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005003 xfs_iext_update_extent(ip, state, *idx, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005004
Dave Chinner9e5987a72013-02-25 12:31:26 +11005005 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005006 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005007 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005008 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005009
Christoph Hellwige1d75532017-10-17 14:16:21 -07005010 flags |= XFS_ILOG_CORE;
5011 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005012 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005013 if (error)
5014 goto done;
5015 error = xfs_btree_increment(cur, 0, &i);
5016 if (error)
5017 goto done;
5018 cur->bc_rec.b = new;
5019 error = xfs_btree_insert(cur, &i);
5020 if (error && error != -ENOSPC)
5021 goto done;
5022 /*
5023 * If get no-space back from btree insert, it tried a
5024 * split, and we have a zero block reservation. Fix up
5025 * our state and return the error.
5026 */
5027 if (error == -ENOSPC) {
5028 /*
5029 * Reset the cursor, don't trust it after any
5030 * insert operation.
5031 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005032 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005033 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005034 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005035 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5036 /*
5037 * Update the btree record back
5038 * to the original value.
5039 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005040 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005041 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005042 goto done;
5043 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005044 * Reset the extent record back
5045 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005046 */
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005047 xfs_iext_update_extent(ip, state, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005048 flags = 0;
5049 error = -ENOSPC;
5050 goto done;
5051 }
5052 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5053 } else
5054 flags |= xfs_ilog_fext(whichfork);
5055 XFS_IFORK_NEXT_SET(ip, whichfork,
5056 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005057 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5058 ++*idx;
5059 break;
5060 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005061
5062 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005063 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5064 if (error)
5065 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005066
Dave Chinner9e5987a72013-02-25 12:31:26 +11005067 /*
5068 * If we need to, add to list of extents to delete.
5069 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005070 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005071 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5072 error = xfs_refcount_decrease_extent(mp, dfops, del);
5073 if (error)
5074 goto done;
5075 } else
5076 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5077 del->br_blockcount, NULL);
5078 }
5079
Dave Chinner9e5987a72013-02-25 12:31:26 +11005080 /*
5081 * Adjust inode # blocks in the file.
5082 */
5083 if (nblks)
5084 ip->i_d.di_nblocks -= nblks;
5085 /*
5086 * Adjust quota data.
5087 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005088 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005089 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5090
Dave Chinner9e5987a72013-02-25 12:31:26 +11005091done:
5092 *logflagsp = flags;
5093 return error;
5094}
5095
5096/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 * Unmap (remove) blocks from a file.
5098 * If nexts is nonzero then the number of extents to remove is limited to
5099 * that value. If not all extents in the block range can be removed then
5100 * *done is set.
5101 */
5102int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005103__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 xfs_trans_t *tp, /* transaction pointer */
5105 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005106 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005107 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 int flags, /* misc flags */
5109 xfs_extnum_t nexts, /* number of extents max */
5110 xfs_fsblock_t *firstblock, /* first allocated block
5111 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005112 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
5114 xfs_btree_cur_t *cur; /* bmap btree cursor */
5115 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 int error; /* error return value */
5117 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005118 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 xfs_ifork_t *ifp; /* inode fork pointer */
5120 int isrt; /* freeing in rt area */
5121 xfs_extnum_t lastx; /* last extent index used */
5122 int logflags; /* transaction logging flags */
5123 xfs_extlen_t mod; /* rt extent offset */
5124 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 int tmp_logflags; /* partial logging flags */
5126 int wasdel; /* was a delayed alloc extent */
5127 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005129 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005130 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005131 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005132 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005134 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005135
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005136 whichfork = xfs_bmapi_whichfork(flags);
5137 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 ifp = XFS_IFORK_PTR(ip, whichfork);
5139 if (unlikely(
5140 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5141 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5142 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5143 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005144 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 }
5146 mp = ip->i_mount;
5147 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005148 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005149
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005150 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 ASSERT(len > 0);
5152 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005153
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005154 /*
5155 * Guesstimate how many blocks we can unmap without running the risk of
5156 * blowing out the transaction with a mix of EFIs and reflink
5157 * adjustments.
5158 */
5159 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5160 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5161 else
5162 max_len = len;
5163
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5165 (error = xfs_iread_extents(tp, ip, whichfork)))
5166 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005167 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005168 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 return 0;
5170 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005171 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005172 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005173 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005174
Christoph Hellwigdc560152017-10-23 16:32:39 -07005175 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &lastx, &got)) {
5176 *rlen = 0;
5177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005179 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005180
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 logflags = 0;
5182 if (ifp->if_flags & XFS_IFBROOT) {
5183 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005184 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005186 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 cur->bc_private.b.flags = 0;
5188 } else
5189 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005190
5191 if (isrt) {
5192 /*
5193 * Synchronize by locking the bitmap inode.
5194 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005195 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005196 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005197 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5198 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005199 }
5200
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005202 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005203 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005205 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 * Just back up to the previous extent, if so.
5207 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005208 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 if (--lastx < 0)
5210 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005211 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 }
5213 /*
5214 * Is the last block of this extent before the range
5215 * we're supposed to delete? If so, we're done.
5216 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005217 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005219 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 break;
5221 /*
5222 * Then deal with the (possibly delayed) allocated space
5223 * we found.
5224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005226 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005227
5228 /*
5229 * Make sure we don't touch multiple AGF headers out of order
5230 * in a single transaction, as that could cause AB-BA deadlocks.
5231 */
5232 if (!wasdel) {
5233 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5234 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5235 break;
5236 prev_agno = agno;
5237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (got.br_startoff < start) {
5239 del.br_startoff = start;
5240 del.br_blockcount -= start - got.br_startoff;
5241 if (!wasdel)
5242 del.br_startblock += start - got.br_startoff;
5243 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005244 if (del.br_startoff + del.br_blockcount > end + 1)
5245 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005246
5247 /* How much can we safely unmap? */
5248 if (max_len < del.br_blockcount) {
5249 del.br_startoff += del.br_blockcount - max_len;
5250 if (!wasdel)
5251 del.br_startblock += del.br_blockcount - max_len;
5252 del.br_blockcount = max_len;
5253 }
5254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 sum = del.br_startblock + del.br_blockcount;
5256 if (isrt &&
5257 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5258 /*
5259 * Realtime extent not lined up at the end.
5260 * The extent could have been split into written
5261 * and unwritten pieces, or we could just be
5262 * unmapping part of it. But we can't really
5263 * get rid of part of a realtime extent.
5264 */
5265 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005266 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 /*
5268 * This piece is unwritten, or we're not
5269 * using unwritten extents. Skip over it.
5270 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005271 ASSERT(end >= mod);
5272 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005274 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005276 xfs_iext_get_extent(ifp, lastx,
5277 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 }
5279 continue;
5280 }
5281 /*
5282 * It's written, turn it unwritten.
5283 * This is better than zeroing it.
5284 */
5285 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005286 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 /*
5288 * If this spans a realtime extent boundary,
5289 * chop it back to the start of the one we end at.
5290 */
5291 if (del.br_blockcount > mod) {
5292 del.br_startoff += del.br_blockcount - mod;
5293 del.br_startblock += del.br_blockcount - mod;
5294 del.br_blockcount = mod;
5295 }
5296 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005297 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005298 whichfork, &lastx, &cur, &del,
5299 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 if (error)
5301 goto error0;
5302 goto nodelete;
5303 }
5304 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5305 /*
5306 * Realtime extent is lined up at the end but not
5307 * at the front. We'll get rid of full extents if
5308 * we can.
5309 */
5310 mod = mp->m_sb.sb_rextsize - mod;
5311 if (del.br_blockcount > mod) {
5312 del.br_blockcount -= mod;
5313 del.br_startoff += mod;
5314 del.br_startblock += mod;
5315 } else if ((del.br_startoff == start &&
5316 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005317 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005318 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 /*
5320 * Can't make it unwritten. There isn't
5321 * a full extent here so just skip it.
5322 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005323 ASSERT(end >= del.br_blockcount);
5324 end -= del.br_blockcount;
5325 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005326 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 continue;
5328 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005329 struct xfs_bmbt_irec prev;
5330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 /*
5332 * This one is already unwritten.
5333 * It must have a written left neighbor.
5334 * Unwrite the killed part of that one and
5335 * try again.
5336 */
5337 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005338 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005340 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 ASSERT(del.br_startblock ==
5342 prev.br_startblock + prev.br_blockcount);
5343 if (prev.br_startoff < start) {
5344 mod = start - prev.br_startoff;
5345 prev.br_blockcount -= mod;
5346 prev.br_startblock += mod;
5347 prev.br_startoff = start;
5348 }
5349 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005350 lastx--;
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 &prev, firstblock, dfops,
5354 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 if (error)
5356 goto error0;
5357 goto nodelete;
5358 } else {
5359 ASSERT(del.br_state == XFS_EXT_NORM);
5360 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005361 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005362 ip, whichfork, &lastx, &cur,
5363 &del, firstblock, dfops,
5364 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 if (error)
5366 goto error0;
5367 goto nodelete;
5368 }
5369 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005370
Brian Fosterb2706a02016-03-15 11:42:46 +11005371 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005372 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5373 &got, &del);
5374 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005375 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5376 cur, &del, &tmp_logflags, whichfork,
5377 flags);
5378 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005379 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 if (error)
5382 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005383
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005384 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005385 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 /*
5388 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005390 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005391 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005392 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005393 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005394 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 extno++;
5397 }
5398 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005399 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005400 *rlen = 0;
5401 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005402 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 /*
5405 * Convert to a btree if necessary.
5406 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005407 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005409 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 &cur, 0, &tmp_logflags, whichfork);
5411 logflags |= tmp_logflags;
5412 if (error)
5413 goto error0;
5414 }
5415 /*
5416 * transform from btree to extents, give it cur
5417 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005418 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 ASSERT(cur != NULL);
5420 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5421 whichfork);
5422 logflags |= tmp_logflags;
5423 if (error)
5424 goto error0;
5425 }
5426 /*
5427 * transform from extents to local?
5428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 error = 0;
5430error0:
5431 /*
5432 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005433 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005435 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005437 logflags &= ~xfs_ilog_fext(whichfork);
5438 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005440 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /*
5442 * Log inode even in the error case, if the transaction
5443 * is dirty we'll need to shut down the filesystem.
5444 */
5445 if (logflags)
5446 xfs_trans_log_inode(tp, ip, logflags);
5447 if (cur) {
5448 if (!error) {
5449 *firstblock = cur->bc_private.b.firstblock;
5450 cur->bc_private.b.allocated = 0;
5451 }
5452 xfs_btree_del_cursor(cur,
5453 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5454 }
5455 return error;
5456}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005457
Darrick J. Wong44535932016-10-03 09:11:29 -07005458/* Unmap a range of a file. */
5459int
5460xfs_bunmapi(
5461 xfs_trans_t *tp,
5462 struct xfs_inode *ip,
5463 xfs_fileoff_t bno,
5464 xfs_filblks_t len,
5465 int flags,
5466 xfs_extnum_t nexts,
5467 xfs_fsblock_t *firstblock,
5468 struct xfs_defer_ops *dfops,
5469 int *done)
5470{
5471 int error;
5472
5473 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5474 dfops);
5475 *done = (len == 0);
5476 return error;
5477}
5478
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005479/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005480 * Determine whether an extent shift can be accomplished by a merge with the
5481 * extent that precedes the target hole of the shift.
5482 */
5483STATIC bool
5484xfs_bmse_can_merge(
5485 struct xfs_bmbt_irec *left, /* preceding extent */
5486 struct xfs_bmbt_irec *got, /* current extent to shift */
5487 xfs_fileoff_t shift) /* shift fsb */
5488{
5489 xfs_fileoff_t startoff;
5490
5491 startoff = got->br_startoff - shift;
5492
5493 /*
5494 * The extent, once shifted, must be adjacent in-file and on-disk with
5495 * the preceding extent.
5496 */
5497 if ((left->br_startoff + left->br_blockcount != startoff) ||
5498 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5499 (left->br_state != got->br_state) ||
5500 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5501 return false;
5502
5503 return true;
5504}
5505
5506/*
5507 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5508 * hole in the file. If an extent shift would result in the extent being fully
5509 * adjacent to the extent that currently precedes the hole, we can merge with
5510 * the preceding extent rather than do the shift.
5511 *
5512 * This function assumes the caller has verified a shift-by-merge is possible
5513 * with the provided extents via xfs_bmse_can_merge().
5514 */
5515STATIC int
5516xfs_bmse_merge(
5517 struct xfs_inode *ip,
5518 int whichfork,
5519 xfs_fileoff_t shift, /* shift fsb */
5520 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005521 struct xfs_bmbt_irec *got, /* extent to shift */
5522 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005523 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005524 int *logflags, /* output */
5525 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005526{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005527 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005528 xfs_filblks_t blockcount;
5529 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005530 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005531
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005532 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005533
5534 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5535 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005536 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005537
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005538 new = *left;
5539 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005540
5541 /*
5542 * Update the on-disk extent count, the btree if necessary and log the
5543 * inode.
5544 */
5545 XFS_IFORK_NEXT_SET(ip, whichfork,
5546 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5547 *logflags |= XFS_ILOG_CORE;
5548 if (!cur) {
5549 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005550 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005551 }
5552
5553 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005554 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005555 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005556 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005557 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005558
5559 error = xfs_btree_delete(cur, &i);
5560 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005561 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005562 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005563
5564 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005565 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005566 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005567 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005568 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005569
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005570 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005571 if (error)
5572 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005573
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005574done:
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005575 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5576 current_ext - 1, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005577 xfs_iext_remove(ip, current_ext, 1, 0);
5578
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005579 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005580 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5581 if (error)
5582 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005583 memcpy(&new, got, sizeof(new));
5584 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005585 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005586}
5587
Christoph Hellwigbf806282017-10-19 11:07:34 -07005588static int
5589xfs_bmap_shift_update_extent(
5590 struct xfs_inode *ip,
5591 int whichfork,
5592 xfs_extnum_t idx,
5593 struct xfs_bmbt_irec *got,
5594 struct xfs_btree_cur *cur,
5595 int *logflags,
5596 struct xfs_defer_ops *dfops,
5597 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005598{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005599 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005600 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005601 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005602
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005603 *logflags |= XFS_ILOG_CORE;
5604
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005605 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005606
5607 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005608 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005609 if (error)
5610 return error;
5611 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5612
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005613 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005614 if (error)
5615 return error;
5616 } else {
5617 *logflags |= XFS_ILOG_DEXT;
5618 }
5619
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005620 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), idx, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005621
Darrick J. Wong9c194642016-08-03 12:16:05 +10005622 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005623 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005624 if (error)
5625 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005626 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005627}
5628
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005629int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005630xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005631 struct xfs_trans *tp,
5632 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005633 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005634 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005635 bool *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005636 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005637 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005638 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005639{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005640 int whichfork = XFS_DATA_FORK;
5641 struct xfs_mount *mp = ip->i_mount;
5642 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5643 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005644 struct xfs_bmbt_irec got, prev;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005645 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005646 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005647 int error = 0;
5648 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005649
5650 if (unlikely(XFS_TEST_ERROR(
5651 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5652 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005653 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005654 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005655 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005656 }
5657
5658 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005659 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005660
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005661 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005662
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005663 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005664 error = xfs_iread_extents(tp, ip, whichfork);
5665 if (error)
5666 return error;
5667 }
5668
Brian Fosterddb19e32014-09-23 15:38:09 +10005669 if (ifp->if_flags & XFS_IFBROOT) {
5670 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5671 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005672 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005673 cur->bc_private.b.flags = 0;
5674 }
5675
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005676 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext, &got)) {
5677 *done = true;
5678 goto del_cursor;
5679 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005680 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005681
Christoph Hellwigbf806282017-10-19 11:07:34 -07005682 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005683 if (xfs_iext_get_extent(ifp, current_ext - 1, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005684 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5685 error = -EINVAL;
5686 goto del_cursor;
5687 }
5688
Christoph Hellwigbf806282017-10-19 11:07:34 -07005689 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5690 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5691 current_ext, &got, &prev, cur,
5692 &logflags, dfops);
5693 if (error)
5694 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005695
5696 /* update got after merge */
5697 if (!xfs_iext_get_extent(ifp, current_ext, &got)) {
5698 *done = true;
5699 goto del_cursor;
5700 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005701 goto done;
5702 }
5703 } else {
5704 if (got.br_startoff < offset_shift_fsb) {
5705 error = -EINVAL;
5706 goto del_cursor;
5707 }
5708 }
5709
5710 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5711 cur, &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005712 if (error)
5713 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005714
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005715 if (!xfs_iext_get_extent(ifp, ++current_ext, &got)) {
5716 *done = true;
5717 goto del_cursor;
5718 }
5719
5720done:
Christoph Hellwigbf806282017-10-19 11:07:34 -07005721 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005722del_cursor:
5723 if (cur)
5724 xfs_btree_del_cursor(cur,
5725 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005726 if (logflags)
5727 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005728 return error;
5729}
5730
5731int
5732xfs_bmap_insert_extents(
5733 struct xfs_trans *tp,
5734 struct xfs_inode *ip,
5735 xfs_fileoff_t *next_fsb,
5736 xfs_fileoff_t offset_shift_fsb,
5737 bool *done,
5738 xfs_fileoff_t stop_fsb,
5739 xfs_fsblock_t *firstblock,
5740 struct xfs_defer_ops *dfops)
5741{
5742 int whichfork = XFS_DATA_FORK;
5743 struct xfs_mount *mp = ip->i_mount;
5744 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5745 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005746 struct xfs_bmbt_irec got, next;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005747 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005748 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005749 int error = 0;
5750 int logflags = 0;
5751
5752 if (unlikely(XFS_TEST_ERROR(
5753 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5754 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5755 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5756 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5757 return -EFSCORRUPTED;
5758 }
5759
5760 if (XFS_FORCED_SHUTDOWN(mp))
5761 return -EIO;
5762
5763 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5764
5765 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5766 error = xfs_iread_extents(tp, ip, whichfork);
5767 if (error)
5768 return error;
5769 }
5770
5771 if (ifp->if_flags & XFS_IFBROOT) {
5772 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5773 cur->bc_private.b.firstblock = *firstblock;
5774 cur->bc_private.b.dfops = dfops;
5775 cur->bc_private.b.flags = 0;
5776 }
5777
Namjae Jeona904b1c2015-03-25 15:08:56 +11005778 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005779 current_ext = xfs_iext_count(ifp) - 1;
5780 if (!xfs_iext_get_extent(ifp, current_ext, &got) ||
5781 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005782 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005783 goto del_cursor;
5784 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005785 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005786 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5787 &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005788 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005789 goto del_cursor;
5790 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005791 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005792 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005793
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005794 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005795 error = -EIO;
5796 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005797 }
5798
Christoph Hellwigbf806282017-10-19 11:07:34 -07005799 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005800 if (xfs_iext_get_extent(ifp, current_ext + 1, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005801 if (new_startoff + got.br_blockcount > next.br_startoff) {
5802 error = -EINVAL;
5803 goto del_cursor;
5804 }
5805
5806 /*
5807 * Unlike a left shift (which involves a hole punch), a right
5808 * shift does not modify extent neighbors in any way. We should
5809 * never find mergeable extents in this scenario. Check anyways
5810 * and warn if we encounter two extents that could be one.
5811 */
5812 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5813 WARN_ON_ONCE(1);
5814 }
5815
5816 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5817 cur, &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005818 if (error)
5819 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005820
5821 if (!xfs_iext_get_extent(ifp, --current_ext, &got) ||
5822 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005823 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005824 goto del_cursor;
5825 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005826
5827 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005828del_cursor:
5829 if (cur)
5830 xfs_btree_del_cursor(cur,
5831 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005832 if (logflags)
5833 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005834 return error;
5835}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005836
5837/*
5838 * Splits an extent into two extents at split_fsb block such that it is
5839 * the first block of the current_ext. @current_ext is a target extent
5840 * to be split. @split_fsb is a block where the extents is split.
5841 * If split_fsb lies in a hole or the first block of extents, just return 0.
5842 */
5843STATIC int
5844xfs_bmap_split_extent_at(
5845 struct xfs_trans *tp,
5846 struct xfs_inode *ip,
5847 xfs_fileoff_t split_fsb,
5848 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005849 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005850{
5851 int whichfork = XFS_DATA_FORK;
5852 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005853 struct xfs_bmbt_irec got;
5854 struct xfs_bmbt_irec new; /* split extent */
5855 struct xfs_mount *mp = ip->i_mount;
5856 struct xfs_ifork *ifp;
5857 xfs_fsblock_t gotblkcnt; /* new block count for got */
5858 xfs_extnum_t current_ext;
5859 int error = 0;
5860 int logflags = 0;
5861 int i = 0;
5862
5863 if (unlikely(XFS_TEST_ERROR(
5864 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5865 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005866 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005867 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5868 XFS_ERRLEVEL_LOW, mp);
5869 return -EFSCORRUPTED;
5870 }
5871
5872 if (XFS_FORCED_SHUTDOWN(mp))
5873 return -EIO;
5874
5875 ifp = XFS_IFORK_PTR(ip, whichfork);
5876 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5877 /* Read in all the extents */
5878 error = xfs_iread_extents(tp, ip, whichfork);
5879 if (error)
5880 return error;
5881 }
5882
5883 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005884 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005885 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005886 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
5887 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005888 return 0;
5889
5890 gotblkcnt = split_fsb - got.br_startoff;
5891 new.br_startoff = split_fsb;
5892 new.br_startblock = got.br_startblock + gotblkcnt;
5893 new.br_blockcount = got.br_blockcount - gotblkcnt;
5894 new.br_state = got.br_state;
5895
5896 if (ifp->if_flags & XFS_IFBROOT) {
5897 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5898 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005899 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005900 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005901 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005902 if (error)
5903 goto del_cursor;
5904 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5905 }
5906
Namjae Jeona904b1c2015-03-25 15:08:56 +11005907 got.br_blockcount = gotblkcnt;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005908 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5909 current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005910
5911 logflags = XFS_ILOG_CORE;
5912 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005913 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005914 if (error)
5915 goto del_cursor;
5916 } else
5917 logflags |= XFS_ILOG_DEXT;
5918
5919 /* Add new extent */
5920 current_ext++;
5921 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5922 XFS_IFORK_NEXT_SET(ip, whichfork,
5923 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5924
5925 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005926 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005927 if (error)
5928 goto del_cursor;
5929 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005930 error = xfs_btree_insert(cur, &i);
5931 if (error)
5932 goto del_cursor;
5933 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5934 }
5935
5936 /*
5937 * Convert to a btree if necessary.
5938 */
5939 if (xfs_bmap_needs_btree(ip, whichfork)) {
5940 int tmp_logflags; /* partial log flag return val */
5941
5942 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005943 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005944 &cur, 0, &tmp_logflags, whichfork);
5945 logflags |= tmp_logflags;
5946 }
5947
5948del_cursor:
5949 if (cur) {
5950 cur->bc_private.b.allocated = 0;
5951 xfs_btree_del_cursor(cur,
5952 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5953 }
5954
5955 if (logflags)
5956 xfs_trans_log_inode(tp, ip, logflags);
5957 return error;
5958}
5959
5960int
5961xfs_bmap_split_extent(
5962 struct xfs_inode *ip,
5963 xfs_fileoff_t split_fsb)
5964{
5965 struct xfs_mount *mp = ip->i_mount;
5966 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005967 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005968 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005969 int error;
5970
Christoph Hellwig253f4912016-04-06 09:19:55 +10005971 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5972 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5973 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005974 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005975
5976 xfs_ilock(ip, XFS_ILOCK_EXCL);
5977 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5978
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005979 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005980
5981 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005982 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983 if (error)
5984 goto out;
5985
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07005986 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987 if (error)
5988 goto out;
5989
Christoph Hellwig70393312015-06-04 13:48:08 +10005990 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005991
5992out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005993 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005994 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995 return error;
5996}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07005997
5998/* Deferred mapping is only for real extents in the data fork. */
5999static bool
6000xfs_bmap_is_update_needed(
6001 struct xfs_bmbt_irec *bmap)
6002{
6003 return bmap->br_startblock != HOLESTARTBLOCK &&
6004 bmap->br_startblock != DELAYSTARTBLOCK;
6005}
6006
6007/* Record a bmap intent. */
6008static int
6009__xfs_bmap_add(
6010 struct xfs_mount *mp,
6011 struct xfs_defer_ops *dfops,
6012 enum xfs_bmap_intent_type type,
6013 struct xfs_inode *ip,
6014 int whichfork,
6015 struct xfs_bmbt_irec *bmap)
6016{
6017 int error;
6018 struct xfs_bmap_intent *bi;
6019
6020 trace_xfs_bmap_defer(mp,
6021 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6022 type,
6023 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6024 ip->i_ino, whichfork,
6025 bmap->br_startoff,
6026 bmap->br_blockcount,
6027 bmap->br_state);
6028
6029 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6030 INIT_LIST_HEAD(&bi->bi_list);
6031 bi->bi_type = type;
6032 bi->bi_owner = ip;
6033 bi->bi_whichfork = whichfork;
6034 bi->bi_bmap = *bmap;
6035
Christoph Hellwig882d8782017-08-28 10:21:03 -07006036 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006037 if (error) {
6038 kmem_free(bi);
6039 return error;
6040 }
6041
6042 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6043 return 0;
6044}
6045
6046/* Map an extent into a file. */
6047int
6048xfs_bmap_map_extent(
6049 struct xfs_mount *mp,
6050 struct xfs_defer_ops *dfops,
6051 struct xfs_inode *ip,
6052 struct xfs_bmbt_irec *PREV)
6053{
6054 if (!xfs_bmap_is_update_needed(PREV))
6055 return 0;
6056
6057 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6058 XFS_DATA_FORK, PREV);
6059}
6060
6061/* Unmap an extent out of a file. */
6062int
6063xfs_bmap_unmap_extent(
6064 struct xfs_mount *mp,
6065 struct xfs_defer_ops *dfops,
6066 struct xfs_inode *ip,
6067 struct xfs_bmbt_irec *PREV)
6068{
6069 if (!xfs_bmap_is_update_needed(PREV))
6070 return 0;
6071
6072 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6073 XFS_DATA_FORK, PREV);
6074}
6075
6076/*
6077 * Process one of the deferred bmap operations. We pass back the
6078 * btree cursor to maintain our lock on the bmapbt between calls.
6079 */
6080int
6081xfs_bmap_finish_one(
6082 struct xfs_trans *tp,
6083 struct xfs_defer_ops *dfops,
6084 struct xfs_inode *ip,
6085 enum xfs_bmap_intent_type type,
6086 int whichfork,
6087 xfs_fileoff_t startoff,
6088 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006089 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006090 xfs_exntst_t state)
6091{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006092 xfs_fsblock_t firstfsb;
6093 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006094
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006095 /*
6096 * firstfsb is tied to the transaction lifetime and is used to
6097 * ensure correct AG locking order and schedule work item
6098 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6099 * to only making one bmap call per transaction, so it should
6100 * be safe to have it as a local variable here.
6101 */
6102 firstfsb = NULLFSBLOCK;
6103
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006104 trace_xfs_bmap_deferred(tp->t_mountp,
6105 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6106 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006107 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006108
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006109 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006110 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006111
6112 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006113 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006114 return -EIO;
6115
6116 switch (type) {
6117 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006118 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006119 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006120 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006121 break;
6122 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006123 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6124 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006125 break;
6126 default:
6127 ASSERT(0);
6128 error = -EFSCORRUPTED;
6129 }
6130
6131 return error;
6132}