blob: cad21fd0c45d5b47780bdd540f4588614a4be265 [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,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -0800403 xfs_verify_fsbno(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 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100670 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100671 xfs_ifork_t *ifp; /* inode fork pointer */
672 xfs_bmbt_key_t *kp; /* root block key pointer */
673 xfs_mount_t *mp; /* mount structure */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100674 xfs_bmbt_ptr_t *pp; /* root block address pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700675 struct xfs_iext_cursor icur;
Christoph Hellwig906abed2017-11-03 10:34:43 -0700676 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700677 xfs_extnum_t cnt = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100678
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
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700756 for_each_xfs_iext(ifp, &icur, &rec) {
Christoph Hellwig906abed2017-11-03 10:34:43 -0700757 if (isnullstartblock(rec.br_startblock))
758 continue;
759 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
760 xfs_bmbt_disk_set_all(arp, &rec);
761 cnt++;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100762 }
763 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
764 xfs_btree_set_numrecs(ablock, cnt);
765
766 /*
767 * Fill in the root key and pointer.
768 */
769 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
770 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
771 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
772 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
773 be16_to_cpu(block->bb_level)));
774 *pp = cpu_to_be64(args.fsbno);
775
776 /*
777 * Do all this logging at the end so that
778 * the root is at the right level.
779 */
780 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
781 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
782 ASSERT(*curp == NULL);
783 *curp = cur;
784 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
785 return 0;
786}
787
788/*
789 * Convert a local file to an extents file.
790 * This code is out of bounds for data forks of regular files,
791 * since the file data needs to get logged so things will stay consistent.
792 * (The bmap-level manipulations are ok, though).
793 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000794void
795xfs_bmap_local_to_extents_empty(
796 struct xfs_inode *ip,
797 int whichfork)
798{
799 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
800
Darrick J. Wong60b49842016-10-03 09:11:34 -0700801 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000802 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
803 ASSERT(ifp->if_bytes == 0);
804 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
805
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000806 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000807 ifp->if_flags &= ~XFS_IFINLINE;
808 ifp->if_flags |= XFS_IFEXTENTS;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700809 ifp->if_u1.if_root = NULL;
810 ifp->if_height = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000811 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
812}
813
814
Dave Chinner9e5987a72013-02-25 12:31:26 +1100815STATIC int /* error */
816xfs_bmap_local_to_extents(
817 xfs_trans_t *tp, /* transaction pointer */
818 xfs_inode_t *ip, /* incore inode pointer */
819 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
820 xfs_extlen_t total, /* total blocks needed by transaction */
821 int *logflagsp, /* inode logging flags */
822 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500823 void (*init_fn)(struct xfs_trans *tp,
824 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100825 struct xfs_inode *ip,
826 struct xfs_ifork *ifp))
827{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000828 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100829 int flags; /* logging flags returned */
830 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000831 xfs_alloc_arg_t args; /* allocation arguments */
832 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700833 struct xfs_bmbt_irec rec;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700834 struct xfs_iext_cursor icur;
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;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700852 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000853 memset(&args, 0, sizeof(args));
854 args.tp = tp;
855 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000856 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000857 args.firstblock = *firstblock;
858 /*
859 * Allocate a block. We know we need only one, since the
860 * file currently fits in an inode.
861 */
862 if (*firstblock == NULLFSBLOCK) {
863 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100865 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000866 args.fsbno = *firstblock;
867 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100868 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000869 args.total = total;
870 args.minlen = args.maxlen = args.prod = 1;
871 error = xfs_alloc_vextent(&args);
872 if (error)
873 goto done;
874
875 /* Can't fail, the space was reserved. */
876 ASSERT(args.fsbno != NULLFSBLOCK);
877 ASSERT(args.len == 1);
878 *firstblock = args.fsbno;
879 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
880
Dave Chinnerfe22d552015-01-22 09:30:06 +1100881 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100882 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100883 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100884 * The callout is responsible for logging because the remote format
885 * might differ from the local format and thus we don't know how much to
886 * log here. Note that init_fn must also set the buffer log item type
887 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100888 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889 init_fn(tp, bp, ip, ifp);
890
Brian Fosterb7cdc662015-10-12 15:40:24 +1100891 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
893 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100894 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000895
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700896 ifp->if_u1.if_root = NULL;
897 ifp->if_height = 0;
898
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700899 rec.br_startoff = 0;
900 rec.br_startblock = args.fsbno;
901 rec.br_blockcount = 1;
902 rec.br_state = XFS_EXT_NORM;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700903 xfs_iext_first(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -0700904 xfs_iext_insert(ip, &icur, &rec, 0);
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700905
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000906 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
907 ip->i_d.di_nblocks = 1;
908 xfs_trans_mod_dquot_byino(tp, ip,
909 XFS_TRANS_DQ_BCOUNT, 1L);
910 flags |= xfs_ilog_fext(whichfork);
911
Dave Chinner9e5987a72013-02-25 12:31:26 +1100912done:
913 *logflagsp = flags;
914 return error;
915}
916
917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * Called from xfs_bmap_add_attrfork to handle btree format files.
919 */
920STATIC int /* error */
921xfs_bmap_add_attrfork_btree(
922 xfs_trans_t *tp, /* transaction pointer */
923 xfs_inode_t *ip, /* incore inode pointer */
924 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000925 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 int *flags) /* inode logging flags */
927{
928 xfs_btree_cur_t *cur; /* btree cursor */
929 int error; /* error return value */
930 xfs_mount_t *mp; /* file system mount struct */
931 int stat; /* newroot status */
932
933 mp = ip->i_mount;
934 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
935 *flags |= XFS_ILOG_DBROOT;
936 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100937 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000938 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700940 error = xfs_bmbt_lookup_first(cur, &stat);
941 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000943 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100944 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100945 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 goto error0;
947 if (stat == 0) {
948 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000949 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951 *firstblock = cur->bc_private.b.firstblock;
952 cur->bc_private.b.allocated = 0;
953 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
954 }
955 return 0;
956error0:
957 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
958 return error;
959}
960
961/*
962 * Called from xfs_bmap_add_attrfork to handle extents format files.
963 */
964STATIC int /* error */
965xfs_bmap_add_attrfork_extents(
966 xfs_trans_t *tp, /* transaction pointer */
967 xfs_inode_t *ip, /* incore inode pointer */
968 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000969 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int *flags) /* inode logging flags */
971{
972 xfs_btree_cur_t *cur; /* bmap btree cursor */
973 int error; /* error return value */
974
975 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
976 return 0;
977 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000978 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 flags, XFS_DATA_FORK);
980 if (cur) {
981 cur->bc_private.b.allocated = 0;
982 xfs_btree_del_cursor(cur,
983 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
984 }
985 return error;
986}
987
988/*
Dave Chinner1e823792013-02-11 15:58:13 +1100989 * Called from xfs_bmap_add_attrfork to handle local format files. Each
990 * different data fork content type needs a different callout to do the
991 * conversion. Some are basic and only require special block initialisation
992 * callouts for the data formating, others (directories) are so specialised they
993 * handle everything themselves.
994 *
995 * XXX (dgc): investigate whether directory conversion can use the generic
996 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500997 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999STATIC int /* error */
1000xfs_bmap_add_attrfork_local(
1001 xfs_trans_t *tp, /* transaction pointer */
1002 xfs_inode_t *ip, /* incore inode pointer */
1003 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001004 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int *flags) /* inode logging flags */
1006{
1007 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1010 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001011
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001012 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001014 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dargs.dp = ip;
1016 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001017 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001018 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 dargs.whichfork = XFS_DATA_FORK;
1020 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001021 return xfs_dir2_sf_to_block(&dargs);
1022 }
1023
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001024 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001025 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1026 flags, XFS_DATA_FORK,
1027 xfs_symlink_local_to_remote);
1028
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001029 /* should only be called for types that support local format data */
1030 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001031 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001035 * Convert inode from non-attributed to attributed.
1036 * Must not be in a transaction, ip must not be locked.
1037 */
1038int /* error code */
1039xfs_bmap_add_attrfork(
1040 xfs_inode_t *ip, /* incore inode pointer */
1041 int size, /* space new attribute needs */
1042 int rsvd) /* xact may use reserved blks */
1043{
1044 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001045 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001046 xfs_mount_t *mp; /* mount structure */
1047 xfs_trans_t *tp; /* transaction pointer */
1048 int blks; /* space reservation */
1049 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001050 int logflags; /* logging flags */
1051 int error; /* error return value */
1052
1053 ASSERT(XFS_IFORK_Q(ip) == 0);
1054
1055 mp = ip->i_mount;
1056 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001057
Dave Chinner9e5987a72013-02-25 12:31:26 +11001058 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001059
1060 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1061 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1062 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001063 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001064
Dave Chinner9e5987a72013-02-25 12:31:26 +11001065 xfs_ilock(ip, XFS_ILOCK_EXCL);
1066 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1067 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1068 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001069 if (error)
1070 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001071 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001072 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001073 if (ip->i_d.di_anextents != 0) {
1074 error = -EFSCORRUPTED;
1075 goto trans_cancel;
1076 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001077 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1078 /*
1079 * For inodes coming from pre-6.2 filesystems.
1080 */
1081 ASSERT(ip->i_d.di_aformat == 0);
1082 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1083 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001084
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001085 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001086 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1087
1088 switch (ip->i_d.di_format) {
1089 case XFS_DINODE_FMT_DEV:
1090 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1091 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001092 case XFS_DINODE_FMT_LOCAL:
1093 case XFS_DINODE_FMT_EXTENTS:
1094 case XFS_DINODE_FMT_BTREE:
1095 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1096 if (!ip->i_d.di_forkoff)
1097 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1098 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1099 version = 2;
1100 break;
1101 default:
1102 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001103 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001104 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001105 }
1106
1107 ASSERT(ip->i_afp == NULL);
1108 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1109 ip->i_afp->if_flags = XFS_IFEXTENTS;
1110 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001111 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001112 switch (ip->i_d.di_format) {
1113 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001114 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001115 &logflags);
1116 break;
1117 case XFS_DINODE_FMT_EXTENTS:
1118 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001119 &dfops, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001120 break;
1121 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001122 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001123 &logflags);
1124 break;
1125 default:
1126 error = 0;
1127 break;
1128 }
1129 if (logflags)
1130 xfs_trans_log_inode(tp, ip, logflags);
1131 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001132 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001133 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1134 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001135 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001136
1137 spin_lock(&mp->m_sb_lock);
1138 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1139 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001140 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001141 }
1142 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1143 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001144 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001145 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001146 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001147 if (log_sb)
1148 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001149 }
1150
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001151 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001152 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001153 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001154 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001155 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001156 return error;
1157
1158bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001159 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001160trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001161 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001162 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001163 return error;
1164}
1165
1166/*
1167 * Internal and external extent tree search functions.
1168 */
1169
1170/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001171 * Read in extents from a btree-format inode.
Dave Chinner9e5987a72013-02-25 12:31:26 +11001172 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001173int
1174xfs_iread_extents(
1175 struct xfs_trans *tp,
1176 struct xfs_inode *ip,
1177 int whichfork)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001178{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001179 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001180 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001181 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1182 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1183 struct xfs_btree_block *block = ifp->if_broot;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001184 struct xfs_iext_cursor icur;
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001185 struct xfs_bmbt_irec new;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001186 xfs_fsblock_t bno;
1187 struct xfs_buf *bp;
1188 xfs_extnum_t i, j;
1189 int level;
1190 __be64 *pp;
1191 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001192
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001193 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1194
1195 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1196 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1197 return -EFSCORRUPTED;
1198 }
1199
Dave Chinner9e5987a72013-02-25 12:31:26 +11001200 /*
1201 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1202 */
1203 level = be16_to_cpu(block->bb_level);
1204 ASSERT(level > 0);
1205 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1206 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001207
Dave Chinner9e5987a72013-02-25 12:31:26 +11001208 /*
1209 * Go down the tree until leaf level is reached, following the first
1210 * pointer (leftmost) at each level.
1211 */
1212 while (level-- > 0) {
1213 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1214 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1215 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001216 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001217 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001218 if (level == 0)
1219 break;
1220 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1221 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001222 XFS_WANT_CORRUPTED_GOTO(mp,
Darrick J. Wong59f6fec2018-01-08 10:51:00 -08001223 xfs_verify_fsbno(mp, bno), out_brelse);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001224 xfs_trans_brelse(tp, bp);
1225 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001226
Dave Chinner9e5987a72013-02-25 12:31:26 +11001227 /*
1228 * Here with bp and block set to the leftmost leaf node in the tree.
1229 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001230 i = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001231 xfs_iext_first(ifp, &icur);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001232
Dave Chinner9e5987a72013-02-25 12:31:26 +11001233 /*
1234 * Loop over all leaf nodes. Copy information to the extent records.
1235 */
1236 for (;;) {
1237 xfs_bmbt_rec_t *frp;
1238 xfs_fsblock_t nextbno;
1239 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001240
1241 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001242 if (unlikely(i + num_recs > nextents)) {
1243 ASSERT(i + num_recs <= nextents);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001244 xfs_warn(ip->i_mount,
1245 "corrupt dinode %Lu, (btree extents).",
1246 (unsigned long long) ip->i_ino);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001247 XFS_CORRUPTION_ERROR(__func__,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001248 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001249 error = -EFSCORRUPTED;
1250 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001251 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001252 /*
1253 * Read-ahead the next leaf block, if any.
1254 */
1255 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1256 if (nextbno != NULLFSBLOCK)
1257 xfs_btree_reada_bufl(mp, nextbno, 1,
1258 &xfs_bmbt_buf_ops);
1259 /*
1260 * Copy records into the extent records.
1261 */
1262 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001263 for (j = 0; j < num_recs; j++, frp++, i++) {
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001264 xfs_bmbt_disk_get_all(frp, &new);
1265 if (!xfs_bmbt_validate_extent(mp, whichfork, &new)) {
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 Hellwig0254c2f2017-11-03 10:34:46 -07001271 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001272 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1273 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001274 }
1275 xfs_trans_brelse(tp, bp);
1276 bno = nextbno;
1277 /*
1278 * If we've reached the end, stop.
1279 */
1280 if (bno == NULLFSBLOCK)
1281 break;
1282 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1283 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1284 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001285 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001286 block = XFS_BUF_TO_BLOCK(bp);
1287 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001288
1289 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1290 error = -EFSCORRUPTED;
1291 goto out;
1292 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001293 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001294
1295 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001296 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001297
1298out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001299 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001300out:
1301 xfs_iext_destroy(ifp);
1302 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001303}
1304
Dave Chinner9e5987a72013-02-25 12:31:26 +11001305/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001306 * Returns the relative block number of the first unused block(s) in the given
1307 * fork with at least "len" logically contiguous blocks free. This is the
1308 * lowest-address hole if the fork has holes, else the first block past the end
1309 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001310 */
1311int /* error */
1312xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001313 struct xfs_trans *tp, /* transaction pointer */
1314 struct xfs_inode *ip, /* incore inode */
1315 xfs_extlen_t len, /* size of hole to find */
1316 xfs_fileoff_t *first_unused, /* unused block */
1317 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001318{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001319 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1320 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001321 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001322 xfs_fileoff_t lastaddr = 0;
1323 xfs_fileoff_t lowest, max;
1324 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001325
1326 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1327 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1328 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001329
Dave Chinner9e5987a72013-02-25 12:31:26 +11001330 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1331 *first_unused = 0;
1332 return 0;
1333 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001334
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001335 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1336 error = xfs_iread_extents(tp, ip, whichfork);
1337 if (error)
1338 return error;
1339 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001340
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001341 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001342 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001343 /*
1344 * See if the hole before this extent will work.
1345 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001346 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001347 got.br_startoff - max >= len)
1348 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001349 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001350 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1351 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001352
Dave Chinner9e5987a72013-02-25 12:31:26 +11001353 *first_unused = max;
1354 return 0;
1355}
1356
1357/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001358 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001359 * last_block (input value) in the file.
1360 * This is not based on i_size, it is based on the extent records.
1361 * Returns 0 for local files, as they do not have extent records.
1362 */
1363int /* error */
1364xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001365 struct xfs_trans *tp, /* transaction pointer */
1366 struct xfs_inode *ip, /* incore inode */
1367 xfs_fileoff_t *last_block, /* last block */
1368 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001369{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001370 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1371 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001372 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001373 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001374
Christoph Hellwig86685f72016-11-24 11:39:38 +11001375 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1376 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001377 *last_block = 0;
1378 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001379 case XFS_DINODE_FMT_BTREE:
1380 case XFS_DINODE_FMT_EXTENTS:
1381 break;
1382 default:
1383 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001384 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001385
1386 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1387 error = xfs_iread_extents(tp, ip, whichfork);
1388 if (error)
1389 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001390 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001391
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001392 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001393 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001394 return 0;
1395}
1396
Dave Chinner68988112013-08-12 20:49:42 +10001397int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001398xfs_bmap_last_extent(
1399 struct xfs_trans *tp,
1400 struct xfs_inode *ip,
1401 int whichfork,
1402 struct xfs_bmbt_irec *rec,
1403 int *is_empty)
1404{
1405 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001406 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001407 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001408
1409 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1410 error = xfs_iread_extents(tp, ip, whichfork);
1411 if (error)
1412 return error;
1413 }
1414
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001415 xfs_iext_last(ifp, &icur);
1416 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001417 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001418 else
1419 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001420 return 0;
1421}
1422
1423/*
1424 * Check the last inode extent to determine whether this allocation will result
1425 * in blocks being allocated at the end of the file. When we allocate new data
1426 * blocks at the end of the file which do not start at the previous data block,
1427 * we will try to align the new blocks at stripe unit boundaries.
1428 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001429 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001430 * at, or past the EOF.
1431 */
1432STATIC int
1433xfs_bmap_isaeof(
1434 struct xfs_bmalloca *bma,
1435 int whichfork)
1436{
1437 struct xfs_bmbt_irec rec;
1438 int is_empty;
1439 int error;
1440
Thomas Meyer749f24f2017-10-09 11:38:54 -07001441 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001442 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1443 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001444 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001445 return error;
1446
Dave Chinner6e708bc2013-11-22 10:41:16 +11001447 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001448 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001449 return 0;
1450 }
1451
Dave Chinner9e5987a72013-02-25 12:31:26 +11001452 /*
1453 * Check if we are allocation or past the last extent, or at least into
1454 * the last delayed allocated extent.
1455 */
1456 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1457 (bma->offset >= rec.br_startoff &&
1458 isnullstartblock(rec.br_startblock));
1459 return 0;
1460}
1461
1462/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001463 * Returns the file-relative block number of the first block past eof in
1464 * the file. This is not based on i_size, it is based on the extent records.
1465 * Returns 0 for local files, as they do not have extent records.
1466 */
1467int
1468xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001469 struct xfs_inode *ip,
1470 xfs_fileoff_t *last_block,
1471 int whichfork)
1472{
1473 struct xfs_bmbt_irec rec;
1474 int is_empty;
1475 int error;
1476
1477 *last_block = 0;
1478
1479 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1480 return 0;
1481
1482 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1483 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001484 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001485
1486 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1487 if (error || is_empty)
1488 return error;
1489
1490 *last_block = rec.br_startoff + rec.br_blockcount;
1491 return 0;
1492}
1493
1494/*
1495 * Returns whether the selected fork of the inode has exactly one
1496 * block or not. For the data fork we check this matches di_size,
1497 * implying the file's range is 0..bsize-1.
1498 */
1499int /* 1=>1 block, 0=>otherwise */
1500xfs_bmap_one_block(
1501 xfs_inode_t *ip, /* incore inode */
1502 int whichfork) /* data or attr fork */
1503{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001504 xfs_ifork_t *ifp; /* inode fork pointer */
1505 int rval; /* return value */
1506 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001507 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001508
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 Hellwigb2b17122017-11-03 10:34:43 -07001519 xfs_iext_first(ifp, &icur);
1520 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001521 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1522 if (rval && whichfork == XFS_DATA_FORK)
1523 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1524 return rval;
1525}
1526
1527/*
1528 * Extent tree manipulation functions used during allocation.
1529 */
1530
1531/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001532 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 */
1534STATIC int /* error */
1535xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001536 struct xfs_bmalloca *bma,
1537 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001539 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001542 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 xfs_fileoff_t new_endoff; /* end offset of new entry */
1544 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1545 /* left is 0, right is 1, prev is 2 */
1546 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001547 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001548 xfs_filblks_t da_new; /* new count del alloc blocks used */
1549 xfs_filblks_t da_old; /* old count del alloc blocks used */
1550 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001552 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001553 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001554 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001556 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001557 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001558 ASSERT(whichfork != XFS_ATTR_FORK);
1559 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1560 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001561
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001562 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001563 ASSERT(!bma->cur ||
1564 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001565
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001566 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568#define LEFT r[0]
1569#define RIGHT r[1]
1570#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /*
1573 * Set up a bunch of variables to make the tests simpler.
1574 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001575 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001577 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ASSERT(PREV.br_startoff <= new->br_startoff);
1579 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001580
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001581 da_old = startblockval(PREV.br_startblock);
1582 da_new = 0;
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /*
1585 * Set flags determining what part of the previous delayed allocation
1586 * extent is being replaced by a real allocation.
1587 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001588 if (PREV.br_startoff == new->br_startoff)
1589 state |= BMAP_LEFT_FILLING;
1590 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1591 state |= BMAP_RIGHT_FILLING;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Check and set flags if this segment has a left neighbor.
1595 * Don't set contiguous if the combined extent would be too large.
1596 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001597 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001598 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001599 if (isnullstartblock(LEFT.br_startblock))
1600 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001602
1603 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1604 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1605 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1606 LEFT.br_state == new->br_state &&
1607 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1608 state |= BMAP_LEFT_CONTIG;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * Check and set flags if this segment has a right neighbor.
1612 * Don't set contiguous if the combined extent would be too large.
1613 * Also check for all-three-contiguous being too large.
1614 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001615 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001616 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001617 if (isnullstartblock(RIGHT.br_startblock))
1618 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001620
1621 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1622 new_endoff == RIGHT.br_startoff &&
1623 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1624 new->br_state == RIGHT.br_state &&
1625 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1626 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1627 BMAP_RIGHT_FILLING)) !=
1628 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1629 BMAP_RIGHT_FILLING) ||
1630 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1631 <= MAXEXTLEN))
1632 state |= BMAP_RIGHT_CONTIG;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 error = 0;
1635 /*
1636 * Switch out based on the FILLING and CONTIG state bits.
1637 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001638 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1639 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1640 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1641 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /*
1643 * Filling in all of a previously delayed allocation extent.
1644 * The left and right neighbors are both contiguous with new.
1645 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001646 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001647
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001648 xfs_iext_remove(bma->ip, &bma->icur, state);
1649 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001650 xfs_iext_prev(ifp, &bma->icur);
1651 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001652 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001653
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001654 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1656 else {
1657 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001658 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001662 error = xfs_btree_delete(bma->cur, &i);
1663 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001665 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001666 error = xfs_btree_decrement(bma->cur, 0, &i);
1667 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001669 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001670 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001671 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto done;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 break;
1675
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001676 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
1678 * Filling in all of a previously delayed allocation extent.
1679 * The left neighbor is contiguous, the right is not.
1680 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001681 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001682 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001683
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001684 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001685 xfs_iext_prev(ifp, &bma->icur);
1686 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001687
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001688 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 rval = XFS_ILOG_DEXT;
1690 else {
1691 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001692 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001693 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001695 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001696 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001697 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 goto done;
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 break;
1701
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001702 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /*
1704 * Filling in all of a previously delayed allocation extent.
1705 * The right neighbor is contiguous, the left is not.
1706 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001707 PREV.br_startblock = new->br_startblock;
1708 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001709
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001710 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001711 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001712 xfs_iext_prev(ifp, &bma->icur);
1713 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001714
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 rval = XFS_ILOG_DEXT;
1717 else {
1718 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001719 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001720 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001722 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001723 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001724 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto done;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 break;
1728
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001729 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * Filling in all of a previously delayed allocation extent.
1732 * Neither the left nor right neighbors are contiguous with
1733 * the new one.
1734 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001735 PREV.br_startblock = new->br_startblock;
1736 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001737 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001738
Darrick J. Wong60b49842016-10-03 09:11:34 -07001739 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1742 else {
1743 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001744 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001747 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 error = xfs_btree_insert(bma->cur, &i);
1749 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 == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
1754
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001755 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * Filling in the first part of a previous delayed allocation.
1758 * The left neighbor is contiguous.
1759 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001760 old = LEFT;
1761 temp = PREV.br_blockcount - new->br_blockcount;
1762 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1763 startblockval(PREV.br_startblock));
1764
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001766
Christoph Hellwigbf999712017-11-03 10:34:38 -07001767 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001768 PREV.br_startoff += new->br_blockcount;
1769 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001770
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001771 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1772 xfs_iext_prev(ifp, &bma->icur);
1773 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001774
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 rval = XFS_ILOG_DEXT;
1777 else {
1778 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001779 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001780 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001782 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001783 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto done;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001789 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Filling in the first part of a previous delayed allocation.
1792 * The left neighbor is not contiguous.
1793 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001794 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001795 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1798 else {
1799 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001800 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001803 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001804 error = xfs_btree_insert(bma->cur, &i);
1805 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001809
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001810 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001811 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001812 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001813 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 rval |= tmp_rval;
1815 if (error)
1816 goto done;
1817 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001818
1819 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001820 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001821 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001824 PREV.br_startoff = new_endoff;
1825 PREV.br_blockcount = temp;
1826 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001827 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001828 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001829 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 break;
1831
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001832 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /*
1834 * Filling in the last part of a previous delayed allocation.
1835 * The right neighbor is contiguous with the new allocation.
1836 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001837 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001838 RIGHT.br_startoff = new->br_startoff;
1839 RIGHT.br_startblock = new->br_startblock;
1840 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001841
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001842 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 rval = XFS_ILOG_DEXT;
1844 else {
1845 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001846 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001850 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto done;
1853 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001854
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001855 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001856 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001857 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001858
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001859 PREV.br_blockcount = temp;
1860 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001861
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001862 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1863 xfs_iext_next(ifp, &bma->icur);
1864 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 break;
1866
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001867 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * Filling in the last part of a previous delayed allocation.
1870 * The right neighbor is not contiguous.
1871 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001872 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001873 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1876 else {
1877 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001878 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001881 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 error = xfs_btree_insert(bma->cur, &i);
1883 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001885 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001887
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001888 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001889 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001890 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001891 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 rval |= tmp_rval;
1893 if (error)
1894 goto done;
1895 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001896
1897 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001899 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001900 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001901
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001902 PREV.br_startblock = nullstartblock(da_new);
1903 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001904 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001905 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907
1908 case 0:
1909 /*
1910 * Filling in the middle part of a previous delayed allocation.
1911 * Contiguity is impossible here.
1912 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001913 *
1914 * We start with a delayed allocation:
1915 *
1916 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1917 * PREV @ idx
1918 *
1919 * and we are allocating:
1920 * +rrrrrrrrrrrrrrrrr+
1921 * new
1922 *
1923 * and we set it up for insertion as:
1924 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1925 * new
1926 * PREV @ idx LEFT RIGHT
1927 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001929 old = PREV;
1930
1931 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001932 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001933
1934 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001935 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001936 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001937 RIGHT.br_blockcount =
1938 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1939 RIGHT.br_startblock =
1940 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1941 RIGHT.br_blockcount));
1942
1943 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001944 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1945 PREV.br_startblock =
1946 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1947 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001948 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001949
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001950 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001951 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1952 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001953 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001954
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1957 else {
1958 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001959 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001962 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001963 error = xfs_btree_insert(bma->cur, &i);
1964 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001966 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001968
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001969 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001970 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001971 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001972 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 rval |= tmp_rval;
1974 if (error)
1975 goto done;
1976 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001977
1978 da_new = startblockval(PREV.br_startblock) +
1979 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 break;
1981
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001982 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1983 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1984 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1985 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1986 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1987 case BMAP_LEFT_CONTIG:
1988 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /*
1990 * These cases are all impossible.
1991 */
1992 ASSERT(0);
1993 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001994
Darrick J. Wong9c194642016-08-03 12:16:05 +10001995 /* add reverse mapping */
1996 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
1997 if (error)
1998 goto done;
1999
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002000 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002001 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002002 int tmp_logflags; /* partial log flag return val */
2003
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 ASSERT(bma->cur == NULL);
2005 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002006 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002007 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002008 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002009 if (error)
2010 goto done;
2011 }
2012
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002013 if (bma->cur) {
2014 da_new += bma->cur->bc_private.b.allocated;
2015 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002016 }
2017
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002018 /* adjust for changes in reserved delayed indirect blocks */
2019 if (da_new != da_old) {
2020 ASSERT(state == 0 || da_new < da_old);
2021 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2022 false);
2023 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002024
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002025 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002027 if (whichfork != XFS_COW_FORK)
2028 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return error;
2030#undef LEFT
2031#undef RIGHT
2032#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
2035/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002036 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038STATIC int /* error */
2039xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002040 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002042 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002043 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002045 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002046 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002047 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002048 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002053 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2056 /* left is 0, right is 1, prev is 2 */
2057 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002058 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002059 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002060 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002062 *logflagsp = 0;
2063
2064 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002065 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002066
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002067 ASSERT(!isnullstartblock(new->br_startblock));
2068
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002069 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071#define LEFT r[0]
2072#define RIGHT r[1]
2073#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Set up a bunch of variables to make the tests simpler.
2077 */
2078 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002079 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002080 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 new_endoff = new->br_startoff + new->br_blockcount;
2082 ASSERT(PREV.br_startoff <= new->br_startoff);
2083 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Set flags determining what part of the previous oldext allocation
2087 * extent is being replaced by a newext allocation.
2088 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089 if (PREV.br_startoff == new->br_startoff)
2090 state |= BMAP_LEFT_FILLING;
2091 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2092 state |= BMAP_RIGHT_FILLING;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Check and set flags if this segment has a left neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2097 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002098 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002099 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002100 if (isnullstartblock(LEFT.br_startblock))
2101 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002103
2104 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2105 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2106 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002107 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002108 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2109 state |= BMAP_LEFT_CONTIG;
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /*
2112 * Check and set flags if this segment has a right neighbor.
2113 * Don't set contiguous if the combined extent would be too large.
2114 * Also check for all-three-contiguous being too large.
2115 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002116 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002117 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002118 if (isnullstartblock(RIGHT.br_startblock))
2119 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002121
2122 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2123 new_endoff == RIGHT.br_startoff &&
2124 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002125 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002126 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2127 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2128 BMAP_RIGHT_FILLING)) !=
2129 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2130 BMAP_RIGHT_FILLING) ||
2131 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2132 <= MAXEXTLEN))
2133 state |= BMAP_RIGHT_CONTIG;
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
2136 * Switch out based on the FILLING and CONTIG state bits.
2137 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002138 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2139 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2140 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2141 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * Setting all of a previous oldext extent to newext.
2144 * The left and right neighbors are both contiguous with new.
2145 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002146 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002147
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002148 xfs_iext_remove(ip, icur, state);
2149 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002150 xfs_iext_prev(ifp, icur);
2151 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002152 XFS_IFORK_NEXT_SET(ip, whichfork,
2153 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (cur == NULL)
2155 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2156 else {
2157 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002158 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2159 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002161 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002162 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002165 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002168 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002171 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002173 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002174 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002175 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 goto done;
2177 }
2178 break;
2179
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002180 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * Setting all of a previous oldext extent to newext.
2183 * The left neighbor is contiguous, the right is not.
2184 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002185 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002186
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002187 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002188 xfs_iext_prev(ifp, icur);
2189 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002190 XFS_IFORK_NEXT_SET(ip, whichfork,
2191 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (cur == NULL)
2193 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2194 else {
2195 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002196 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2197 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002199 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002200 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002202 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002203 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002205 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002206 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002207 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto done;
2209 }
2210 break;
2211
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002212 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Setting all of a previous oldext extent to newext.
2215 * The right neighbor is contiguous, the left is not.
2216 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002217 PREV.br_blockcount += RIGHT.br_blockcount;
2218 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002219
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002220 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002221 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002222 xfs_iext_prev(ifp, icur);
2223 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002224
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002225 XFS_IFORK_NEXT_SET(ip, whichfork,
2226 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (cur == NULL)
2228 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2229 else {
2230 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002231 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2232 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002234 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002235 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002237 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002238 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002240 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002241 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto done;
2244 }
2245 break;
2246
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002247 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /*
2249 * Setting all of a previous oldext extent to newext.
2250 * Neither the left nor right neighbors are contiguous with
2251 * the new one.
2252 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002253 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002254 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (cur == NULL)
2257 rval = XFS_ILOG_DEXT;
2258 else {
2259 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002260 error = xfs_bmbt_lookup_eq(cur, new, &i);
2261 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002263 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002264 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002265 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto done;
2267 }
2268 break;
2269
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002270 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Setting the first part of a previous oldext extent to newext.
2273 * The left neighbor is contiguous.
2274 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002275 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002276
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002277 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002278 PREV.br_startoff += new->br_blockcount;
2279 PREV.br_startblock += new->br_blockcount;
2280 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002281
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002282 xfs_iext_update_extent(ip, state, icur, &PREV);
2283 xfs_iext_prev(ifp, icur);
2284 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (cur == NULL)
2287 rval = XFS_ILOG_DEXT;
2288 else {
2289 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002290 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002291 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002294 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002297 error = xfs_btree_decrement(cur, 0, &i);
2298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002300 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002301 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto done;
2303 }
2304 break;
2305
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002306 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /*
2308 * Setting the first part of a previous oldext extent to newext.
2309 * The left neighbor is not contiguous.
2310 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002311 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002312 PREV.br_startoff += new->br_blockcount;
2313 PREV.br_startblock += new->br_blockcount;
2314 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002315
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002316 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002317 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002318 XFS_IFORK_NEXT_SET(ip, whichfork,
2319 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (cur == NULL)
2321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2322 else {
2323 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002324 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002325 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002327 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002328 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002329 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 goto done;
2331 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002332 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 break;
2337
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002338 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /*
2340 * Setting the last part of a previous oldext extent to newext.
2341 * The right neighbor is contiguous with the new allocation.
2342 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002343 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002344 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002345
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002346 RIGHT.br_startoff = new->br_startoff;
2347 RIGHT.br_startblock = new->br_startblock;
2348 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002349
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002350 xfs_iext_update_extent(ip, state, icur, &PREV);
2351 xfs_iext_next(ifp, icur);
2352 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (cur == NULL)
2355 rval = XFS_ILOG_DEXT;
2356 else {
2357 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002358 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002359 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002361 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002362 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002363 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002365 error = xfs_btree_increment(cur, 0, &i);
2366 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002368 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002369 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto done;
2371 }
2372 break;
2373
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002374 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /*
2376 * Setting the last part of a previous oldext extent to newext.
2377 * The right neighbor is not contiguous.
2378 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002379 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002380 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002381
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002382 xfs_iext_update_extent(ip, state, icur, &PREV);
2383 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002384 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002385
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002386 XFS_IFORK_NEXT_SET(ip, whichfork,
2387 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (cur == NULL)
2389 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2390 else {
2391 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002392 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002393 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002395 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002396 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002397 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002399 error = xfs_bmbt_lookup_eq(cur, new, &i);
2400 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002402 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002403 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407 break;
2408
2409 case 0:
2410 /*
2411 * Setting the middle part of a previous oldext extent to
2412 * newext. Contiguity is impossible here.
2413 * One extent becomes three extents.
2414 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002415 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002416 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 r[0] = *new;
2419 r[1].br_startoff = new_endoff;
2420 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002421 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002423 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002425 xfs_iext_update_extent(ip, state, icur, &PREV);
2426 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002427 xfs_iext_insert(ip, icur, &r[1], state);
2428 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002429
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002430 XFS_IFORK_NEXT_SET(ip, whichfork,
2431 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (cur == NULL)
2433 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2434 else {
2435 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002436 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002441 error = xfs_bmbt_update(cur, &r[1]);
2442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto done;
2444 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002446 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002449 /*
2450 * Reset the cursor to the position of the new extent
2451 * we are about to insert as we can't trust it after
2452 * the previous insert.
2453 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002454 error = xfs_bmbt_lookup_eq(cur, new, &i);
2455 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002457 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002459 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002461 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 break;
2464
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002465 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2466 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2467 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2468 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2469 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2470 case BMAP_LEFT_CONTIG:
2471 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * These cases are all impossible.
2474 */
2475 ASSERT(0);
2476 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002477
Darrick J. Wong9c194642016-08-03 12:16:05 +10002478 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002479 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002480 if (error)
2481 goto done;
2482
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002483 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002484 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002485 int tmp_logflags; /* partial log flag return val */
2486
2487 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002488 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002489 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002490 *logflagsp |= tmp_logflags;
2491 if (error)
2492 goto done;
2493 }
2494
2495 /* clear out the allocated field, done with it now in any case. */
2496 if (cur) {
2497 cur->bc_private.b.allocated = 0;
2498 *curp = cur;
2499 }
2500
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002501 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002503 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return error;
2505#undef LEFT
2506#undef RIGHT
2507#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002511 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002513STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514xfs_bmap_add_extent_hole_delay(
2515 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002516 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002517 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002518 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002520 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2522 xfs_filblks_t newlen=0; /* new indirect size */
2523 xfs_filblks_t oldlen=0; /* old indirect size */
2524 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002525 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002526 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002528 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002529 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * Check and set flags if this segment has a left neighbor
2533 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002534 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002536 if (isnullstartblock(left.br_startblock))
2537 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Check and set flags if the current (right) segment exists.
2542 * If it doesn't exist, we're converting the hole at end-of-file.
2543 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002544 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002545 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002546 if (isnullstartblock(right.br_startblock))
2547 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 /*
2551 * Set contiguity flags on the left and right neighbors.
2552 * Don't let extents get too large, even if the pieces are contiguous.
2553 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002554 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2555 left.br_startoff + left.br_blockcount == new->br_startoff &&
2556 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2557 state |= BMAP_LEFT_CONTIG;
2558
2559 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2560 new->br_startoff + new->br_blockcount == right.br_startoff &&
2561 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2562 (!(state & BMAP_LEFT_CONTIG) ||
2563 (left.br_blockcount + new->br_blockcount +
2564 right.br_blockcount <= MAXEXTLEN)))
2565 state |= BMAP_RIGHT_CONTIG;
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /*
2568 * Switch out based on the contiguity flags.
2569 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002570 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2571 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * New allocation is contiguous with delayed allocations
2574 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002575 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 */
2577 temp = left.br_blockcount + new->br_blockcount +
2578 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002579
Eric Sandeen9d87c312009-01-14 23:22:07 -06002580 oldlen = startblockval(left.br_startblock) +
2581 startblockval(new->br_startblock) +
2582 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002583 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2584 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002585 left.br_startblock = nullstartblock(newlen);
2586 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002587
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002588 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002589 xfs_iext_prev(ifp, icur);
2590 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 break;
2592
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002593 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 /*
2595 * New allocation is contiguous with a delayed allocation
2596 * on the left.
2597 * Merge the new allocation with the left neighbor.
2598 */
2599 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002600
Eric Sandeen9d87c312009-01-14 23:22:07 -06002601 oldlen = startblockval(left.br_startblock) +
2602 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002603 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2604 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002605 left.br_blockcount = temp;
2606 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002607
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002608 xfs_iext_prev(ifp, icur);
2609 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 break;
2611
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002612 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /*
2614 * New allocation is contiguous with a delayed allocation
2615 * on the right.
2616 * Merge the new allocation with the right neighbor.
2617 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002619 oldlen = startblockval(new->br_startblock) +
2620 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002621 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2622 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002623 right.br_startoff = new->br_startoff;
2624 right.br_startblock = nullstartblock(newlen);
2625 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002626 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 break;
2628
2629 case 0:
2630 /*
2631 * New allocation is not contiguous with another
2632 * delayed allocation.
2633 * Insert a new entry.
2634 */
2635 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002636 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 break;
2638 }
2639 if (oldlen != newlen) {
2640 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002641 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2642 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /*
2644 * Nothing to do for disk quota accounting here.
2645 */
2646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002650 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 */
2652STATIC int /* error */
2653xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002654 struct xfs_trans *tp,
2655 struct xfs_inode *ip,
2656 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002657 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002658 struct xfs_btree_cur **curp,
2659 struct xfs_bmbt_irec *new,
2660 xfs_fsblock_t *first,
2661 struct xfs_defer_ops *dfops,
2662 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002664 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2665 struct xfs_mount *mp = ip->i_mount;
2666 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2670 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002671 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002672 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002673 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002675 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002676 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002677
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002678 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /*
2681 * Check and set flags if this segment has a left neighbor.
2682 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002683 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002684 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002685 if (isnullstartblock(left.br_startblock))
2686 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 /*
2690 * Check and set flags if this segment has a current value.
2691 * Not true if we're inserting into the "hole" at eof.
2692 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002693 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002694 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002695 if (isnullstartblock(right.br_startblock))
2696 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 /*
2700 * We're inserting a real allocation between "left" and "right".
2701 * Set the contiguity flags. Don't let extents get too large.
2702 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002703 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2704 left.br_startoff + left.br_blockcount == new->br_startoff &&
2705 left.br_startblock + left.br_blockcount == new->br_startblock &&
2706 left.br_state == new->br_state &&
2707 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2708 state |= BMAP_LEFT_CONTIG;
2709
2710 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2711 new->br_startoff + new->br_blockcount == right.br_startoff &&
2712 new->br_startblock + new->br_blockcount == right.br_startblock &&
2713 new->br_state == right.br_state &&
2714 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2715 (!(state & BMAP_LEFT_CONTIG) ||
2716 left.br_blockcount + new->br_blockcount +
2717 right.br_blockcount <= MAXEXTLEN))
2718 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002720 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /*
2722 * Select which case we're in here, and implement it.
2723 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002724 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2725 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /*
2727 * New allocation is contiguous with real allocations on the
2728 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002729 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002731 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002732
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002733 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002734 xfs_iext_prev(ifp, icur);
2735 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002736
Christoph Hellwig6d045582017-04-11 16:45:54 -07002737 XFS_IFORK_NEXT_SET(ip, whichfork,
2738 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2739 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002740 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002741 } else {
2742 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002743 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002744 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002745 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002746 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002747 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002748 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002749 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002750 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002751 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002752 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002753 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002754 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002755 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002756 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002757 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002761 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /*
2763 * New allocation is contiguous with a real allocation
2764 * on the left.
2765 * Merge the new allocation with the left neighbor.
2766 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002767 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002768 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002769
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002770 xfs_iext_prev(ifp, icur);
2771 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002772
Christoph Hellwig6d045582017-04-11 16:45:54 -07002773 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002774 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002775 } else {
2776 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002777 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002778 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002779 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002781 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002782 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002783 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002787 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /*
2789 * New allocation is contiguous with a real allocation
2790 * on the right.
2791 * Merge the new allocation with the right neighbor.
2792 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002793 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002794
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002795 right.br_startoff = new->br_startoff;
2796 right.br_startblock = new->br_startblock;
2797 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002798 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002799
Christoph Hellwig6d045582017-04-11 16:45:54 -07002800 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002801 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002802 } else {
2803 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002804 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002805 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002806 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002808 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002809 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002810 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 case 0:
2815 /*
2816 * New allocation is not contiguous with another
2817 * real allocation.
2818 * Insert a new entry.
2819 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002820 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002821 XFS_IFORK_NEXT_SET(ip, whichfork,
2822 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2823 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002824 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002825 } else {
2826 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002827 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002828 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002829 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002830 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002831 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002832 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002833 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002834 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002836 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002838
Darrick J. Wong9c194642016-08-03 12:16:05 +10002839 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002840 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002841 if (error)
2842 goto done;
2843
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002844 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002845 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002846 int tmp_logflags; /* partial log flag return val */
2847
Christoph Hellwig6d045582017-04-11 16:45:54 -07002848 ASSERT(cur == NULL);
2849 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002850 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002851 *logflagsp |= tmp_logflags;
2852 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002853 if (error)
2854 goto done;
2855 }
2856
2857 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002858 if (cur)
2859 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002860
Christoph Hellwig6d045582017-04-11 16:45:54 -07002861 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002862done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002863 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002864 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
Nathan Scottdd9f4382006-01-11 15:28:28 +11002867/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002868 * Functions used in the extent read, allocate and remove paths
2869 */
2870
2871/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002872 * Adjust the size of the new extent based on di_extsize and rt extsize.
2873 */
Dave Chinner68988112013-08-12 20:49:42 +10002874int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002875xfs_bmap_extsize_align(
2876 xfs_mount_t *mp,
2877 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2878 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2879 xfs_extlen_t extsz, /* align to this extent size */
2880 int rt, /* is this a realtime inode? */
2881 int eof, /* is extent at end-of-file? */
2882 int delay, /* creating delalloc extent? */
2883 int convert, /* overwriting unwritten extent? */
2884 xfs_fileoff_t *offp, /* in/out: aligned offset */
2885 xfs_extlen_t *lenp) /* in/out: aligned length */
2886{
2887 xfs_fileoff_t orig_off; /* original offset */
2888 xfs_extlen_t orig_alen; /* original length */
2889 xfs_fileoff_t orig_end; /* original off+len */
2890 xfs_fileoff_t nexto; /* next file offset */
2891 xfs_fileoff_t prevo; /* previous file offset */
2892 xfs_fileoff_t align_off; /* temp for offset */
2893 xfs_extlen_t align_alen; /* temp for length */
2894 xfs_extlen_t temp; /* temp for calculations */
2895
2896 if (convert)
2897 return 0;
2898
2899 orig_off = align_off = *offp;
2900 orig_alen = align_alen = *lenp;
2901 orig_end = orig_off + orig_alen;
2902
2903 /*
2904 * If this request overlaps an existing extent, then don't
2905 * attempt to perform any additional alignment.
2906 */
2907 if (!delay && !eof &&
2908 (orig_off >= gotp->br_startoff) &&
2909 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2910 return 0;
2911 }
2912
2913 /*
2914 * If the file offset is unaligned vs. the extent size
2915 * we need to align it. This will be possible unless
2916 * the file was previously written with a kernel that didn't
2917 * perform this alignment, or if a truncate shot us in the
2918 * foot.
2919 */
2920 temp = do_mod(orig_off, extsz);
2921 if (temp) {
2922 align_alen += temp;
2923 align_off -= temp;
2924 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002925
2926 /* Same adjustment for the end of the requested area. */
2927 temp = (align_alen % extsz);
2928 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002929 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002930
2931 /*
2932 * For large extent hint sizes, the aligned extent might be larger than
2933 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2934 * the length back under MAXEXTLEN. The outer allocation loops handle
2935 * short allocation just fine, so it is safe to do this. We only want to
2936 * do it when we are forced to, though, because it means more allocation
2937 * operations are required.
2938 */
2939 while (align_alen > MAXEXTLEN)
2940 align_alen -= extsz;
2941 ASSERT(align_alen <= MAXEXTLEN);
2942
Nathan Scottdd9f4382006-01-11 15:28:28 +11002943 /*
2944 * If the previous block overlaps with this proposed allocation
2945 * then move the start forward without adjusting the length.
2946 */
2947 if (prevp->br_startoff != NULLFILEOFF) {
2948 if (prevp->br_startblock == HOLESTARTBLOCK)
2949 prevo = prevp->br_startoff;
2950 else
2951 prevo = prevp->br_startoff + prevp->br_blockcount;
2952 } else
2953 prevo = 0;
2954 if (align_off != orig_off && align_off < prevo)
2955 align_off = prevo;
2956 /*
2957 * If the next block overlaps with this proposed allocation
2958 * then move the start back without adjusting the length,
2959 * but not before offset 0.
2960 * This may of course make the start overlap previous block,
2961 * and if we hit the offset 0 limit then the next block
2962 * can still overlap too.
2963 */
2964 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2965 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2966 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2967 nexto = gotp->br_startoff + gotp->br_blockcount;
2968 else
2969 nexto = gotp->br_startoff;
2970 } else
2971 nexto = NULLFILEOFF;
2972 if (!eof &&
2973 align_off + align_alen != orig_end &&
2974 align_off + align_alen > nexto)
2975 align_off = nexto > align_alen ? nexto - align_alen : 0;
2976 /*
2977 * If we're now overlapping the next or previous extent that
2978 * means we can't fit an extsz piece in this hole. Just move
2979 * the start forward to the first valid spot and set
2980 * the length so we hit the end.
2981 */
2982 if (align_off != orig_off && align_off < prevo)
2983 align_off = prevo;
2984 if (align_off + align_alen != orig_end &&
2985 align_off + align_alen > nexto &&
2986 nexto != NULLFILEOFF) {
2987 ASSERT(nexto > prevo);
2988 align_alen = nexto - align_off;
2989 }
2990
2991 /*
2992 * If realtime, and the result isn't a multiple of the realtime
2993 * extent size we need to remove blocks until it is.
2994 */
2995 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2996 /*
2997 * We're not covering the original request, or
2998 * we won't be able to once we fix the length.
2999 */
3000 if (orig_off < align_off ||
3001 orig_end > align_off + align_alen ||
3002 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003003 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003004 /*
3005 * Try to fix it by moving the start up.
3006 */
3007 if (align_off + temp <= orig_off) {
3008 align_alen -= temp;
3009 align_off += temp;
3010 }
3011 /*
3012 * Try to fix it by moving the end in.
3013 */
3014 else if (align_off + align_alen - temp >= orig_end)
3015 align_alen -= temp;
3016 /*
3017 * Set the start to the minimum then trim the length.
3018 */
3019 else {
3020 align_alen -= orig_off - align_off;
3021 align_off = orig_off;
3022 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3023 }
3024 /*
3025 * Result doesn't cover the request, fail it.
3026 */
3027 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003028 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003029 } else {
3030 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003031 /* see MAXEXTLEN handling above */
3032 ASSERT(orig_end <= align_off + align_alen ||
3033 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003034 }
3035
3036#ifdef DEBUG
3037 if (!eof && gotp->br_startoff != NULLFILEOFF)
3038 ASSERT(align_off + align_alen <= gotp->br_startoff);
3039 if (prevp->br_startoff != NULLFILEOFF)
3040 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3041#endif
3042
3043 *lenp = align_alen;
3044 *offp = align_off;
3045 return 0;
3046}
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048#define XFS_ALLOC_GAP_UNITS 4
3049
Dave Chinner68988112013-08-12 20:49:42 +10003050void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003051xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003052 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3056 xfs_mount_t *mp; /* mount point structure */
3057 int nullfb; /* true if ap->firstblock isn't set */
3058 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060#define ISVALID(x,y) \
3061 (rt ? \
3062 (x) < mp->m_sb.sb_rblocks : \
3063 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3064 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3065 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3066
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003068 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003069 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3070 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003071 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 /*
3073 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003074 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003076 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3077 !isnullstartblock(ap->prev.br_startblock) &&
3078 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3079 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003080 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 /*
3082 * Adjust for the gap between prevp and us.
3083 */
Dave Chinner3a756672011-09-18 20:40:58 +00003084 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003085 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003087 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3088 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090 /*
3091 * If not at eof, then compare the two neighbor blocks.
3092 * Figure out whether either one gives us a good starting point,
3093 * and pick the better one.
3094 */
3095 else if (!ap->eof) {
3096 xfs_fsblock_t gotbno; /* right side block number */
3097 xfs_fsblock_t gotdiff=0; /* right side difference */
3098 xfs_fsblock_t prevbno; /* left side block number */
3099 xfs_fsblock_t prevdiff=0; /* left side difference */
3100
3101 /*
3102 * If there's a previous (left) block, select a requested
3103 * start block based on it.
3104 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003105 if (ap->prev.br_startoff != NULLFILEOFF &&
3106 !isnullstartblock(ap->prev.br_startblock) &&
3107 (prevbno = ap->prev.br_startblock +
3108 ap->prev.br_blockcount) &&
3109 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 /*
3111 * Calculate gap to end of previous block.
3112 */
Dave Chinner3a756672011-09-18 20:40:58 +00003113 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003114 (ap->prev.br_startoff +
3115 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 /*
3117 * Figure the startblock based on the previous block's
3118 * end and the gap size.
3119 * Heuristic!
3120 * If the gap is large relative to the piece we're
3121 * allocating, or using it gives us an invalid block
3122 * number, then just use the end of the previous block.
3123 */
Dave Chinner3a756672011-09-18 20:40:58 +00003124 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003126 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 prevbno += adjust;
3128 else
3129 prevdiff += adjust;
3130 /*
3131 * If the firstblock forbids it, can't use it,
3132 * must use default.
3133 */
3134 if (!rt && !nullfb &&
3135 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3136 prevbno = NULLFSBLOCK;
3137 }
3138 /*
3139 * No previous block or can't follow it, just default.
3140 */
3141 else
3142 prevbno = NULLFSBLOCK;
3143 /*
3144 * If there's a following (right) block, select a requested
3145 * start block based on it.
3146 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003147 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 /*
3149 * Calculate gap to start of next block.
3150 */
Dave Chinner3a756672011-09-18 20:40:58 +00003151 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 /*
3153 * Figure the startblock based on the next block's
3154 * start and the gap size.
3155 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003156 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /*
3158 * Heuristic!
3159 * If the gap is large relative to the piece we're
3160 * allocating, or using it gives us an invalid block
3161 * number, then just use the start of the next block
3162 * offset by our length.
3163 */
Dave Chinner3a756672011-09-18 20:40:58 +00003164 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 ISVALID(gotbno - gotdiff, gotbno))
3166 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003167 else if (ISVALID(gotbno - ap->length, gotbno)) {
3168 gotbno -= ap->length;
3169 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 } else
3171 gotdiff += adjust;
3172 /*
3173 * If the firstblock forbids it, can't use it,
3174 * must use default.
3175 */
3176 if (!rt && !nullfb &&
3177 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3178 gotbno = NULLFSBLOCK;
3179 }
3180 /*
3181 * No next block, just default.
3182 */
3183 else
3184 gotbno = NULLFSBLOCK;
3185 /*
3186 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003187 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 */
3189 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003190 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003192 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003194 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003196#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003197}
3198
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003199static int
3200xfs_bmap_longest_free_extent(
3201 struct xfs_trans *tp,
3202 xfs_agnumber_t ag,
3203 xfs_extlen_t *blen,
3204 int *notinit)
3205{
3206 struct xfs_mount *mp = tp->t_mountp;
3207 struct xfs_perag *pag;
3208 xfs_extlen_t longest;
3209 int error = 0;
3210
3211 pag = xfs_perag_get(mp, ag);
3212 if (!pag->pagf_init) {
3213 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3214 if (error)
3215 goto out;
3216
3217 if (!pag->pagf_init) {
3218 *notinit = 1;
3219 goto out;
3220 }
3221 }
3222
Dave Chinner50adbcb2015-06-22 10:04:31 +10003223 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003224 xfs_alloc_min_freelist(mp, pag),
3225 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003226 if (*blen < longest)
3227 *blen = longest;
3228
3229out:
3230 xfs_perag_put(pag);
3231 return error;
3232}
3233
3234static void
3235xfs_bmap_select_minlen(
3236 struct xfs_bmalloca *ap,
3237 struct xfs_alloc_arg *args,
3238 xfs_extlen_t *blen,
3239 int notinit)
3240{
3241 if (notinit || *blen < ap->minlen) {
3242 /*
3243 * Since we did a BUF_TRYLOCK above, it is possible that
3244 * there is space for this request.
3245 */
3246 args->minlen = ap->minlen;
3247 } else if (*blen < args->maxlen) {
3248 /*
3249 * If the best seen length is less than the request length,
3250 * use the best as the minimum.
3251 */
3252 args->minlen = *blen;
3253 } else {
3254 /*
3255 * Otherwise we've seen an extent as big as maxlen, use that
3256 * as the minimum.
3257 */
3258 args->minlen = args->maxlen;
3259 }
3260}
3261
Nathan Scotta365bdd2006-03-14 13:34:16 +11003262STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003263xfs_bmap_btalloc_nullfb(
3264 struct xfs_bmalloca *ap,
3265 struct xfs_alloc_arg *args,
3266 xfs_extlen_t *blen)
3267{
3268 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003269 xfs_agnumber_t ag, startag;
3270 int notinit = 0;
3271 int error;
3272
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003273 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003274 args->total = ap->total;
3275
Christoph Hellwigc467c042010-02-15 23:34:42 +00003276 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3277 if (startag == NULLAGNUMBER)
3278 startag = ag = 0;
3279
Dave Chinner14b064c2011-01-27 12:16:28 +11003280 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003281 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3282 &notinit);
3283 if (error)
3284 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003285
Christoph Hellwigc467c042010-02-15 23:34:42 +00003286 if (++ag == mp->m_sb.sb_agcount)
3287 ag = 0;
3288 if (ag == startag)
3289 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003290 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003291
3292 xfs_bmap_select_minlen(ap, args, blen, notinit);
3293 return 0;
3294}
3295
3296STATIC int
3297xfs_bmap_btalloc_filestreams(
3298 struct xfs_bmalloca *ap,
3299 struct xfs_alloc_arg *args,
3300 xfs_extlen_t *blen)
3301{
3302 struct xfs_mount *mp = ap->ip->i_mount;
3303 xfs_agnumber_t ag;
3304 int notinit = 0;
3305 int error;
3306
3307 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3308 args->total = ap->total;
3309
3310 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3311 if (ag == NULLAGNUMBER)
3312 ag = 0;
3313
3314 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3315 if (error)
3316 return error;
3317
3318 if (*blen < args->maxlen) {
3319 error = xfs_filestream_new_ag(ap, &ag);
3320 if (error)
3321 return error;
3322
3323 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3324 &notinit);
3325 if (error)
3326 return error;
3327
3328 }
3329
3330 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003331
3332 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003333 * Set the failure fallback case to look in the selected AG as stream
3334 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003335 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003336 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003337 return 0;
3338}
3339
Darrick J. Wong751f3762018-01-25 13:58:13 -08003340/* Update all inode and quota accounting for the allocation we just did. */
3341static void
3342xfs_bmap_btalloc_accounting(
3343 struct xfs_bmalloca *ap,
3344 struct xfs_alloc_arg *args)
3345{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003346 if (ap->flags & XFS_BMAPI_COWFORK) {
3347 /*
3348 * COW fork blocks are in-core only and thus are treated as
3349 * in-core quota reservation (like delalloc blocks) even when
3350 * converted to real blocks. The quota reservation is not
3351 * accounted to disk until blocks are remapped to the data
3352 * fork. So if these blocks were previously delalloc, we
3353 * already have quota reservation and there's nothing to do
3354 * yet.
3355 */
3356 if (ap->wasdel)
3357 return;
3358
3359 /*
3360 * Otherwise, we've allocated blocks in a hole. The transaction
3361 * has acquired in-core quota reservation for this extent.
3362 * Rather than account these as real blocks, however, we reduce
3363 * the transaction quota reservation based on the allocation.
3364 * This essentially transfers the transaction quota reservation
3365 * to that of a delalloc extent.
3366 */
3367 ap->ip->i_delayed_blks += args->len;
3368 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3369 -(long)args->len);
3370 return;
3371 }
3372
3373 /* data/attr fork only */
3374 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003375 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3376 if (ap->wasdel)
3377 ap->ip->i_delayed_blks -= args->len;
3378 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3379 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3380 args->len);
3381}
3382
Christoph Hellwigc467c042010-02-15 23:34:42 +00003383STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003384xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003385 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003386{
3387 xfs_mount_t *mp; /* mount point structure */
3388 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003389 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003390 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003391 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003392 xfs_alloc_arg_t args;
3393 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003394 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003395 int nullfb; /* true if ap->firstblock isn't set */
3396 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003397 int tryagain;
3398 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003399 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003400
Dave Chinnera99ebf42011-12-01 11:24:20 +00003401 ASSERT(ap->length);
3402
Nathan Scotta365bdd2006-03-14 13:34:16 +11003403 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003404
3405 /* stripe alignment for allocation is determined by mount parameters */
3406 stripe_align = 0;
3407 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3408 stripe_align = mp->m_swidth;
3409 else if (mp->m_dalign)
3410 stripe_align = mp->m_dalign;
3411
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003412 if (ap->flags & XFS_BMAPI_COWFORK)
3413 align = xfs_get_cowextsz_hint(ap->ip);
3414 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003415 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003416 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003417 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003418 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003419 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003420 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003421 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003422 }
Dave Chinner33177f052013-12-12 16:34:36 +11003423
3424
Dave Chinner0937e0f2011-09-18 20:40:57 +00003425 nullfb = *ap->firstblock == NULLFSBLOCK;
3426 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003427 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003428 if (xfs_alloc_is_userdata(ap->datatype) &&
3429 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003430 ag = xfs_filestream_lookup_ag(ap->ip);
3431 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003432 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003433 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003434 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003435 }
3436 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003437 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003438
3439 xfs_bmap_adjacent(ap);
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003442 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 * it's in the right allocation group.
3444 */
Dave Chinner3a756672011-09-18 20:40:58 +00003445 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 ;
3447 else
Dave Chinner3a756672011-09-18 20:40:58 +00003448 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 * Normal allocation, done through xfs_alloc_vextent.
3451 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003452 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003453 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003454 args.tp = ap->tp;
3455 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003456 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003457 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003458
3459 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003460 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003461 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003462 blen = 0;
3463 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003464 /*
3465 * Search for an allocation group with a single extent large
3466 * enough for the request. If one isn't found, then adjust
3467 * the minimum allocation size to the largest space found.
3468 */
Dave Chinner292378e2016-09-26 08:21:28 +10003469 if (xfs_alloc_is_userdata(ap->datatype) &&
3470 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003471 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3472 else
3473 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003474 if (error)
3475 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003476 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003477 if (xfs_inode_is_filestream(ap->ip))
3478 args.type = XFS_ALLOCTYPE_FIRST_AG;
3479 else
3480 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003481 args.total = args.minlen = ap->minlen;
3482 } else {
3483 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3484 args.total = ap->total;
3485 args.minlen = ap->minlen;
3486 }
David Chinner957d0eb2007-06-18 16:50:37 +10003487 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003488 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003489 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003490 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003491 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003492 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003493 args.prod = 1;
3494 args.mod = 0;
3495 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003496 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003497 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003498 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3499 }
3500 /*
3501 * If we are not low on available data blocks, and the
3502 * underlying logical volume manager is a stripe, and
3503 * the file offset is zero then try to allocate data
3504 * blocks on stripe unit boundary.
3505 * NOTE: ap->aeof is only set if the allocation length
3506 * is >= the stripe unit and the allocation offset is
3507 * at the end of file.
3508 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003509 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003510 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003511 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003512 atype = args.type;
3513 isaligned = 1;
3514 /*
3515 * Adjust for alignment
3516 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003517 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003518 args.minlen = blen - args.alignment;
3519 args.minalignslop = 0;
3520 } else {
3521 /*
3522 * First try an exact bno allocation.
3523 * If it fails then do a near or start bno
3524 * allocation with alignment turned on.
3525 */
3526 atype = args.type;
3527 tryagain = 1;
3528 args.type = XFS_ALLOCTYPE_THIS_BNO;
3529 args.alignment = 1;
3530 /*
3531 * Compute the minlen+alignment for the
3532 * next case. Set slop so that the value
3533 * of minlen+alignment+slop doesn't go up
3534 * between the calls.
3535 */
Dave Chinner33177f052013-12-12 16:34:36 +11003536 if (blen > stripe_align && blen <= args.maxlen)
3537 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003538 else
3539 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003540 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003541 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003542 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003543 args.minlen - 1;
3544 else
3545 args.minalignslop = 0;
3546 }
3547 } else {
3548 args.alignment = 1;
3549 args.minalignslop = 0;
3550 }
3551 args.minleft = ap->minleft;
3552 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003553 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003554 args.datatype = ap->datatype;
3555 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003556 args.ip = ap->ip;
3557
3558 error = xfs_alloc_vextent(&args);
3559 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003560 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003561
Nathan Scotta365bdd2006-03-14 13:34:16 +11003562 if (tryagain && args.fsbno == NULLFSBLOCK) {
3563 /*
3564 * Exact allocation failed. Now try with alignment
3565 * turned on.
3566 */
3567 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003568 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003569 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003570 args.minlen = nextminlen;
3571 args.minalignslop = 0;
3572 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 if ((error = xfs_alloc_vextent(&args)))
3574 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003575 }
3576 if (isaligned && args.fsbno == NULLFSBLOCK) {
3577 /*
3578 * allocation failed, so turn off alignment and
3579 * try again.
3580 */
3581 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003582 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003583 args.alignment = 0;
3584 if ((error = xfs_alloc_vextent(&args)))
3585 return error;
3586 }
3587 if (args.fsbno == NULLFSBLOCK && nullfb &&
3588 args.minlen > ap->minlen) {
3589 args.minlen = ap->minlen;
3590 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003591 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 if ((error = xfs_alloc_vextent(&args)))
3593 return error;
3594 }
3595 if (args.fsbno == NULLFSBLOCK && nullfb) {
3596 args.fsbno = 0;
3597 args.type = XFS_ALLOCTYPE_FIRST_AG;
3598 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003599 if ((error = xfs_alloc_vextent(&args)))
3600 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003601 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602 }
3603 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003604 /*
3605 * check the allocation happened at the same or higher AG than
3606 * the first block that was allocated.
3607 */
3608 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003609 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3610 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003611
Dave Chinner3a756672011-09-18 20:40:58 +00003612 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003613 if (*ap->firstblock == NULLFSBLOCK)
3614 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003615 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003616 ap->length = args.len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003617 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003618 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003619 ap->blkno = NULLFSBLOCK;
3620 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 }
3622 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003623}
3624
3625/*
3626 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3627 * It figures out where to ask the underlying allocator to put the new extent.
3628 */
3629STATIC int
3630xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003631 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632{
Dave Chinner292378e2016-09-26 08:21:28 +10003633 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3634 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003635 return xfs_bmap_rtalloc(ap);
3636 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637}
3638
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003639/* Trim extent to fit a logical block range. */
3640void
3641xfs_trim_extent(
3642 struct xfs_bmbt_irec *irec,
3643 xfs_fileoff_t bno,
3644 xfs_filblks_t len)
3645{
3646 xfs_fileoff_t distance;
3647 xfs_fileoff_t end = bno + len;
3648
3649 if (irec->br_startoff + irec->br_blockcount <= bno ||
3650 irec->br_startoff >= end) {
3651 irec->br_blockcount = 0;
3652 return;
3653 }
3654
3655 if (irec->br_startoff < bno) {
3656 distance = bno - irec->br_startoff;
3657 if (isnullstartblock(irec->br_startblock))
3658 irec->br_startblock = DELAYSTARTBLOCK;
3659 if (irec->br_startblock != DELAYSTARTBLOCK &&
3660 irec->br_startblock != HOLESTARTBLOCK)
3661 irec->br_startblock += distance;
3662 irec->br_startoff += distance;
3663 irec->br_blockcount -= distance;
3664 }
3665
3666 if (end < irec->br_startoff + irec->br_blockcount) {
3667 distance = irec->br_startoff + irec->br_blockcount - end;
3668 irec->br_blockcount -= distance;
3669 }
3670}
3671
Brian Foster40214d12017-10-13 09:47:46 -07003672/* trim extent to within eof */
3673void
3674xfs_trim_extent_eof(
3675 struct xfs_bmbt_irec *irec,
3676 struct xfs_inode *ip)
3677
3678{
3679 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3680 i_size_read(VFS_I(ip))));
3681}
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003684 * Trim the returned map to the required bounds
3685 */
3686STATIC void
3687xfs_bmapi_trim_map(
3688 struct xfs_bmbt_irec *mval,
3689 struct xfs_bmbt_irec *got,
3690 xfs_fileoff_t *bno,
3691 xfs_filblks_t len,
3692 xfs_fileoff_t obno,
3693 xfs_fileoff_t end,
3694 int n,
3695 int flags)
3696{
3697 if ((flags & XFS_BMAPI_ENTIRE) ||
3698 got->br_startoff + got->br_blockcount <= obno) {
3699 *mval = *got;
3700 if (isnullstartblock(got->br_startblock))
3701 mval->br_startblock = DELAYSTARTBLOCK;
3702 return;
3703 }
3704
3705 if (obno > *bno)
3706 *bno = obno;
3707 ASSERT((*bno >= obno) || (n == 0));
3708 ASSERT(*bno < end);
3709 mval->br_startoff = *bno;
3710 if (isnullstartblock(got->br_startblock))
3711 mval->br_startblock = DELAYSTARTBLOCK;
3712 else
3713 mval->br_startblock = got->br_startblock +
3714 (*bno - got->br_startoff);
3715 /*
3716 * Return the minimum of what we got and what we asked for for
3717 * the length. We can use the len variable here because it is
3718 * modified below and we could have been there before coming
3719 * here if the first part of the allocation didn't overlap what
3720 * was asked for.
3721 */
3722 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3723 got->br_blockcount - (*bno - got->br_startoff));
3724 mval->br_state = got->br_state;
3725 ASSERT(mval->br_blockcount <= len);
3726 return;
3727}
3728
3729/*
3730 * Update and validate the extent map to return
3731 */
3732STATIC void
3733xfs_bmapi_update_map(
3734 struct xfs_bmbt_irec **map,
3735 xfs_fileoff_t *bno,
3736 xfs_filblks_t *len,
3737 xfs_fileoff_t obno,
3738 xfs_fileoff_t end,
3739 int *n,
3740 int flags)
3741{
3742 xfs_bmbt_irec_t *mval = *map;
3743
3744 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3745 ((mval->br_startoff + mval->br_blockcount) <= end));
3746 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3747 (mval->br_startoff < obno));
3748
3749 *bno = mval->br_startoff + mval->br_blockcount;
3750 *len = end - *bno;
3751 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3752 /* update previous map with new information */
3753 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3754 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3755 ASSERT(mval->br_state == mval[-1].br_state);
3756 mval[-1].br_blockcount = mval->br_blockcount;
3757 mval[-1].br_state = mval->br_state;
3758 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3759 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3760 mval[-1].br_startblock != HOLESTARTBLOCK &&
3761 mval->br_startblock == mval[-1].br_startblock +
3762 mval[-1].br_blockcount &&
3763 ((flags & XFS_BMAPI_IGSTATE) ||
3764 mval[-1].br_state == mval->br_state)) {
3765 ASSERT(mval->br_startoff ==
3766 mval[-1].br_startoff + mval[-1].br_blockcount);
3767 mval[-1].br_blockcount += mval->br_blockcount;
3768 } else if (*n > 0 &&
3769 mval->br_startblock == DELAYSTARTBLOCK &&
3770 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3771 mval->br_startoff ==
3772 mval[-1].br_startoff + mval[-1].br_blockcount) {
3773 mval[-1].br_blockcount += mval->br_blockcount;
3774 mval[-1].br_state = mval->br_state;
3775 } else if (!((*n == 0) &&
3776 ((mval->br_startoff + mval->br_blockcount) <=
3777 obno))) {
3778 mval++;
3779 (*n)++;
3780 }
3781 *map = mval;
3782}
3783
3784/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003785 * Map file blocks to filesystem blocks without allocation.
3786 */
3787int
3788xfs_bmapi_read(
3789 struct xfs_inode *ip,
3790 xfs_fileoff_t bno,
3791 xfs_filblks_t len,
3792 struct xfs_bmbt_irec *mval,
3793 int *nmap,
3794 int flags)
3795{
3796 struct xfs_mount *mp = ip->i_mount;
3797 struct xfs_ifork *ifp;
3798 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003799 xfs_fileoff_t obno;
3800 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003801 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003802 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003803 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003804 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003805 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003806
3807 ASSERT(*nmap >= 1);
3808 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003809 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003810 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003811
3812 if (unlikely(XFS_TEST_ERROR(
3813 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3814 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003815 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003816 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003817 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003818 }
3819
3820 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003821 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003822
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003823 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003824
3825 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003826
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003827 /* No CoW fork? Return a hole. */
3828 if (whichfork == XFS_COW_FORK && !ifp) {
3829 mval->br_startoff = bno;
3830 mval->br_startblock = HOLESTARTBLOCK;
3831 mval->br_blockcount = len;
3832 mval->br_state = XFS_EXT_NORM;
3833 *nmap = 1;
3834 return 0;
3835 }
3836
Dave Chinner5c8ed202011-09-18 20:40:45 +00003837 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3838 error = xfs_iread_extents(NULL, ip, whichfork);
3839 if (error)
3840 return error;
3841 }
3842
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003843 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003844 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003845 end = bno + len;
3846 obno = bno;
3847
3848 while (bno < end && n < *nmap) {
3849 /* Reading past eof, act as though there's a hole up to end. */
3850 if (eof)
3851 got.br_startoff = end;
3852 if (got.br_startoff > bno) {
3853 /* Reading in a hole. */
3854 mval->br_startoff = bno;
3855 mval->br_startblock = HOLESTARTBLOCK;
3856 mval->br_blockcount =
3857 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3858 mval->br_state = XFS_EXT_NORM;
3859 bno += mval->br_blockcount;
3860 len -= mval->br_blockcount;
3861 mval++;
3862 n++;
3863 continue;
3864 }
3865
3866 /* set up the extent map to return. */
3867 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3868 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3869
3870 /* If we're done, stop now. */
3871 if (bno >= end || n >= *nmap)
3872 break;
3873
3874 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003875 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003876 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003877 }
3878 *nmap = n;
3879 return 0;
3880}
3881
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003882/*
3883 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3884 * global pool and the extent inserted into the inode in-core extent tree.
3885 *
3886 * On entry, got refers to the first extent beyond the offset of the extent to
3887 * allocate or eof is specified if no such extent exists. On return, got refers
3888 * to the extent record that was inserted to the inode fork.
3889 *
3890 * Note that the allocated extent may have been merged with contiguous extents
3891 * during insertion into the inode fork. Thus, got does not reflect the current
3892 * state of the inode fork on return. If necessary, the caller can use lastx to
3893 * look up the updated record in the inode fork.
3894 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003895int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003896xfs_bmapi_reserve_delalloc(
3897 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003898 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003899 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003900 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003901 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003902 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003903 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003904 int eof)
3905{
3906 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003907 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003908 xfs_extlen_t alen;
3909 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003910 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003911 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003912
Brian Foster974ae922016-11-28 14:57:42 +11003913 /*
3914 * Cap the alloc length. Keep track of prealloc so we know whether to
3915 * tag the inode before we return.
3916 */
3917 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003918 if (!eof)
3919 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003920 if (prealloc && alen >= len)
3921 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003922
3923 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003924 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003925 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003926 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003927
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003928 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003929 prev.br_startoff = NULLFILEOFF;
3930
Shan Hai6ca30722018-01-23 13:56:11 -08003931 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003932 1, 0, &aoff, &alen);
3933 ASSERT(!error);
3934 }
3935
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003936 /*
3937 * Make a transaction-less quota reservation for delayed allocation
3938 * blocks. This number gets adjusted later. We return if we haven't
3939 * allocated blocks already inside this loop.
3940 */
3941 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003942 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003943 if (error)
3944 return error;
3945
3946 /*
3947 * Split changing sb for alen and indlen since they could be coming
3948 * from different places.
3949 */
3950 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3951 ASSERT(indlen > 0);
3952
Shan Hai6ca30722018-01-23 13:56:11 -08003953 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003954 if (error)
3955 goto out_unreserve_quota;
3956
Dave Chinner0d485ad2015-02-23 21:22:03 +11003957 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003958 if (error)
3959 goto out_unreserve_blocks;
3960
3961
3962 ip->i_delayed_blks += alen;
3963
3964 got->br_startoff = aoff;
3965 got->br_startblock = nullstartblock(indlen);
3966 got->br_blockcount = alen;
3967 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003968
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003969 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003970
Brian Foster974ae922016-11-28 14:57:42 +11003971 /*
3972 * Tag the inode if blocks were preallocated. Note that COW fork
3973 * preallocation can occur at the start or end of the extent, even when
3974 * prealloc == 0, so we must also check the aligned offset and length.
3975 */
3976 if (whichfork == XFS_DATA_FORK && prealloc)
3977 xfs_inode_set_eofblocks_tag(ip);
3978 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3979 xfs_inode_set_cowblocks_tag(ip);
3980
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003981 return 0;
3982
3983out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08003984 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003985out_unreserve_quota:
3986 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08003987 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
3988 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003989 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 Hellwigb2b17122017-11-03 10:34:43 -07004011 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004012 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004013 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004014 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004015 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004016 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004017 }
4018
4019 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004020 * Set the data type being allocated. For the data fork, the first data
4021 * in the file is treated differently to all other allocations. For the
4022 * attribute fork, we only need to ensure the allocated range is not on
4023 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004024 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004025 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004026 bma->datatype = XFS_ALLOC_NOBUSY;
4027 if (whichfork == XFS_DATA_FORK) {
4028 if (bma->offset == 0)
4029 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4030 else
4031 bma->datatype |= XFS_ALLOC_USERDATA;
4032 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004033 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004034 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004035 }
4036
Dave Chinnere04426b2012-10-05 11:06:59 +10004037 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004038
4039 /*
4040 * Only want to do the alignment at the eof if it is userdata and
4041 * allocation length is larger than a stripe unit.
4042 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004043 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004044 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004045 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004046 if (error)
4047 return error;
4048 }
4049
4050 error = xfs_bmap_alloc(bma);
4051 if (error)
4052 return error;
4053
Dave Chinner29c8d172011-09-18 20:41:00 +00004054 if (bma->cur)
4055 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004056 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004057 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004058 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4059 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4060 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004061 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004062 }
4063 /*
4064 * Bump the number of extents we've allocated
4065 * in this call.
4066 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004067 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004068
Dave Chinner29c8d172011-09-18 20:41:00 +00004069 if (bma->cur)
4070 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004071 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4072
Dave Chinner963c30c2011-09-18 20:40:59 +00004073 bma->got.br_startoff = bma->offset;
4074 bma->got.br_startblock = bma->blkno;
4075 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004076 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004077
4078 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004079 * In the data fork, a wasdelay extent has been initialized, so
4080 * shouldn't be flagged as unwritten.
4081 *
4082 * For the cow fork, however, we convert delalloc reservations
4083 * (extents allocated for speculative preallocation) to
4084 * allocated unwritten extents, and only convert the unwritten
4085 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004086 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004087 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4088 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004089 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004090 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004091
Christoph Hellwigc6534242011-09-18 20:41:05 +00004092 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004093 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004094 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004095 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004096 whichfork, &bma->icur, &bma->cur, &bma->got,
Christoph Hellwig6d045582017-04-11 16:45:54 -07004097 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004098
Christoph Hellwigc315c902011-09-18 20:41:02 +00004099 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004100 if (error)
4101 return error;
4102
4103 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004104 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4105 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4106 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004107 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004108 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004109
Dave Chinner963c30c2011-09-18 20:40:59 +00004110 ASSERT(bma->got.br_startoff <= bma->offset);
4111 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4112 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004113 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4114 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004115 return 0;
4116}
4117
Dave Chinnerb447fe52011-09-18 20:40:51 +00004118STATIC int
4119xfs_bmapi_convert_unwritten(
4120 struct xfs_bmalloca *bma,
4121 struct xfs_bmbt_irec *mval,
4122 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004123 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004124{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004125 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004126 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004127 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004128 int error;
4129
Dave Chinnerb447fe52011-09-18 20:40:51 +00004130 /* check if we need to do unwritten->real conversion */
4131 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4132 (flags & XFS_BMAPI_PREALLOC))
4133 return 0;
4134
4135 /* check if we need to do real->unwritten conversion */
4136 if (mval->br_state == XFS_EXT_NORM &&
4137 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4138 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4139 return 0;
4140
4141 /*
4142 * Modify (by adding) the state flag, if writing.
4143 */
4144 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004145 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4146 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004147 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004148 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004149 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004150 }
4151 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4152 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4153
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004154 /*
4155 * Before insertion into the bmbt, zero the range being converted
4156 * if required.
4157 */
4158 if (flags & XFS_BMAPI_ZERO) {
4159 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4160 mval->br_blockcount);
4161 if (error)
4162 return error;
4163 }
4164
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004165 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004166 &bma->icur, &bma->cur, mval, bma->firstblock,
4167 bma->dfops, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004168 /*
4169 * Log the inode core unconditionally in the unwritten extent conversion
4170 * path because the conversion might not have done so (e.g., if the
4171 * extent count hasn't changed). We need to make sure the inode is dirty
4172 * in the transaction for the sake of fsync(), even if nothing has
4173 * changed, because fsync() will not force the log for this transaction
4174 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004175 *
4176 * Note: If we're only converting cow fork extents, there aren't
4177 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004178 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004179 if (whichfork != XFS_COW_FORK)
4180 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004181 if (error)
4182 return error;
4183
4184 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004185 * Update our extent pointer, given that
4186 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4187 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004188 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004189 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004190
4191 /*
4192 * We may have combined previously unwritten space with written space,
4193 * so generate another request.
4194 */
4195 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004196 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004197 return 0;
4198}
4199
Christoph Hellwig44032802011-09-18 20:40:48 +00004200/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004201 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4202 * extent state if necessary. Details behaviour is controlled by the flags
4203 * parameter. Only allocates blocks from a single allocation group, to avoid
4204 * locking problems.
4205 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 * The returned value in "firstblock" from the first call in a transaction
4207 * must be remembered and presented to subsequent calls in "firstblock".
4208 * An upper bound for the number of blocks to be allocated is supplied to
4209 * the first call in "total"; if no allocation group has that many free
4210 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4211 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004212int
4213xfs_bmapi_write(
4214 struct xfs_trans *tp, /* transaction pointer */
4215 struct xfs_inode *ip, /* incore inode */
4216 xfs_fileoff_t bno, /* starting file offs. mapped */
4217 xfs_filblks_t len, /* length to map in file */
4218 int flags, /* XFS_BMAPI_... */
4219 xfs_fsblock_t *firstblock, /* first allocated block
4220 controls a.g. for allocs */
4221 xfs_extlen_t total, /* total blocks needed */
4222 struct xfs_bmbt_irec *mval, /* output: map values */
4223 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004224 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004226 struct xfs_mount *mp = ip->i_mount;
4227 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004228 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004229 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004230 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004231 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004232 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004233 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004234 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004237 xfs_fileoff_t orig_bno; /* original block number value */
4238 int orig_flags; /* original flags arg value */
4239 xfs_filblks_t orig_len; /* original value of len arg */
4240 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4241 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
4243 orig_bno = bno;
4244 orig_len = len;
4245 orig_flags = flags;
4246 orig_mval = mval;
4247 orig_nmap = *nmap;
4248#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004249 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004252 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4253 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004254 ASSERT(tp != NULL ||
4255 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4256 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004257 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004258 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004259 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004260 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004261
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004262 /* zeroing is for currently only for data extents, not metadata */
4263 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4264 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4265 /*
4266 * we can allocate unwritten extents or pre-zero allocated blocks,
4267 * but it makes no sense to do both at once. This would result in
4268 * zeroing the unwritten extent twice, but it still being an
4269 * unwritten extent....
4270 */
4271 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4272 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 if (unlikely(XFS_TEST_ERROR(
4275 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004276 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004277 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004278 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004279 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004283 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004286
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004287 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004288
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004289 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004291 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004293 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004294 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004295 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004296 }
4297
4298 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4299 error = xfs_iread_extents(tp, ip, whichfork);
4300 if (error)
4301 goto error0;
4302 }
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 n = 0;
4305 end = bno + len;
4306 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004307
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004308 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004309 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004310 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004311 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004312 bma.tp = tp;
4313 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004314 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004315 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004316 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004317 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004320 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004321
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004322 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004323 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004324 /*
4325 * CoW fork conversions should /never/ hit EOF or
4326 * holes. There should always be something for us
4327 * to work on.
4328 */
4329 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4330 (flags & XFS_BMAPI_COWFORK)));
4331
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004332 if (flags & XFS_BMAPI_DELALLOC) {
4333 /*
4334 * For the COW fork we can reasonably get a
4335 * request for converting an extent that races
4336 * with other threads already having converted
4337 * part of it, as there converting COW to
4338 * regular blocks is not protected using the
4339 * IOLOCK.
4340 */
4341 ASSERT(flags & XFS_BMAPI_COWFORK);
4342 if (!(flags & XFS_BMAPI_COWFORK)) {
4343 error = -EIO;
4344 goto error0;
4345 }
4346
4347 if (eof || bno >= end)
4348 break;
4349 } else {
4350 need_alloc = true;
4351 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004352 } else if (isnullstartblock(bma.got.br_startblock)) {
4353 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004354 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004355
4356 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 * First, deal with the hole before the allocated space
4358 * that we found, if any.
4359 */
Christoph Hellwigb1214592017-11-03 10:34:44 -07004360 if ((need_alloc || wasdelay) &&
4361 !(flags & XFS_BMAPI_CONVERT_ONLY)) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004362 bma.eof = eof;
4363 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4364 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004365 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004366 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004367
Dave Chinnera99ebf42011-12-01 11:24:20 +00004368 /*
4369 * There's a 32/64 bit type mismatch between the
4370 * allocation length request (which can be 64 bits in
4371 * length) and the bma length request, which is
4372 * xfs_extlen_t and therefore 32 bits. Hence we have to
4373 * check for 32-bit overflows and handle them here.
4374 */
4375 if (len > (xfs_filblks_t)MAXEXTLEN)
4376 bma.length = MAXEXTLEN;
4377 else
4378 bma.length = len;
4379
4380 ASSERT(len > 0);
4381 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004382 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 if (error)
4384 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004385 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004386 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004387
4388 /*
4389 * If this is a CoW allocation, record the data in
4390 * the refcount btree for orphan recovery.
4391 */
4392 if (whichfork == XFS_COW_FORK) {
4393 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4394 bma.blkno, bma.length);
4395 if (error)
4396 goto error0;
4397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004399
Dave Chinneraef9a892011-09-18 20:40:44 +00004400 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004401 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4402 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Dave Chinnerb447fe52011-09-18 20:40:51 +00004404 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004405 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004406 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004407 continue;
4408 if (error)
4409 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Dave Chinneraef9a892011-09-18 20:40:44 +00004411 /* update the extent map to return */
4412 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4413
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 /*
4415 * If we're done, stop now. Stop when we've allocated
4416 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4417 * the transaction may get too big.
4418 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004419 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004421
4422 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004423 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004424 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004425 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 /*
4430 * Transform from btree to extents, give it cur.
4431 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004432 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004433 int tmp_logflags = 0;
4434
Dave Chinner29c8d172011-09-18 20:41:00 +00004435 ASSERT(bma.cur);
4436 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004438 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (error)
4440 goto error0;
4441 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004444 XFS_IFORK_NEXTENTS(ip, whichfork) >
4445 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447error0:
4448 /*
4449 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004450 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004452 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004454 bma.logflags &= ~xfs_ilog_fext(whichfork);
4455 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004457 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 /*
4459 * Log whatever the flags say, even if error. Otherwise we might miss
4460 * detecting a case where the data is changed, there's an error,
4461 * and it's not logged so we don't shutdown when we should.
4462 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004463 if (bma.logflags)
4464 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004465
Dave Chinner29c8d172011-09-18 20:41:00 +00004466 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 if (!error) {
4468 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004469 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004471 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004472 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004474 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4476 }
4477 if (!error)
4478 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4479 orig_nmap, *nmap);
4480 return error;
4481}
4482
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004483static int
4484xfs_bmapi_remap(
4485 struct xfs_trans *tp,
4486 struct xfs_inode *ip,
4487 xfs_fileoff_t bno,
4488 xfs_filblks_t len,
4489 xfs_fsblock_t startblock,
4490 struct xfs_defer_ops *dfops)
4491{
4492 struct xfs_mount *mp = ip->i_mount;
4493 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4494 struct xfs_btree_cur *cur = NULL;
4495 xfs_fsblock_t firstblock = NULLFSBLOCK;
4496 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004497 struct xfs_iext_cursor icur;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004498 int logflags = 0, error;
4499
4500 ASSERT(len > 0);
4501 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4502 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4503
4504 if (unlikely(XFS_TEST_ERROR(
4505 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4506 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004507 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004508 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4509 return -EFSCORRUPTED;
4510 }
4511
4512 if (XFS_FORCED_SHUTDOWN(mp))
4513 return -EIO;
4514
4515 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4516 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4517 if (error)
4518 return error;
4519 }
4520
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004521 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004522 /* make sure we only reflink into a hole. */
4523 ASSERT(got.br_startoff > bno);
4524 ASSERT(got.br_startoff - bno >= len);
4525 }
4526
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004527 ip->i_d.di_nblocks += len;
4528 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004529
4530 if (ifp->if_flags & XFS_IFBROOT) {
4531 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4532 cur->bc_private.b.firstblock = firstblock;
4533 cur->bc_private.b.dfops = dfops;
4534 cur->bc_private.b.flags = 0;
4535 }
4536
4537 got.br_startoff = bno;
4538 got.br_startblock = startblock;
4539 got.br_blockcount = len;
4540 got.br_state = XFS_EXT_NORM;
4541
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004542 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur,
4543 &cur, &got, &firstblock, dfops, &logflags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004544 if (error)
4545 goto error0;
4546
4547 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4548 int tmp_logflags = 0;
4549
4550 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4551 &tmp_logflags, XFS_DATA_FORK);
4552 logflags |= tmp_logflags;
4553 }
4554
4555error0:
4556 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4557 logflags &= ~XFS_ILOG_DEXT;
4558 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4559 logflags &= ~XFS_ILOG_DBROOT;
4560
4561 if (logflags)
4562 xfs_trans_log_inode(tp, ip, logflags);
4563 if (cur) {
4564 xfs_btree_del_cursor(cur,
4565 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4566 }
4567 return error;
4568}
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004571 * When a delalloc extent is split (e.g., due to a hole punch), the original
4572 * indlen reservation must be shared across the two new extents that are left
4573 * behind.
4574 *
4575 * Given the original reservation and the worst case indlen for the two new
4576 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004577 * reservation fairly across the two new extents. If necessary, steal available
4578 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4579 * ores == 1). The number of stolen blocks is returned. The availability and
4580 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004581 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004582static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004583xfs_bmap_split_indlen(
4584 xfs_filblks_t ores, /* original res. */
4585 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004586 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4587 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004588{
4589 xfs_filblks_t len1 = *indlen1;
4590 xfs_filblks_t len2 = *indlen2;
4591 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004592 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004593 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004594
4595 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004596 * Steal as many blocks as we can to try and satisfy the worst case
4597 * indlen for both new extents.
4598 */
Brian Foster75d65362017-02-13 22:48:30 -08004599 if (ores < nres && avail)
4600 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4601 ores += stolen;
4602
4603 /* nothing else to do if we've satisfied the new reservation */
4604 if (ores >= nres)
4605 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004606
4607 /*
Brian Foster75d65362017-02-13 22:48:30 -08004608 * We can't meet the total required reservation for the two extents.
4609 * Calculate the percent of the overall shortage between both extents
4610 * and apply this percentage to each of the requested indlen values.
4611 * This distributes the shortage fairly and reduces the chances that one
4612 * of the two extents is left with nothing when extents are repeatedly
4613 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004614 */
Brian Foster75d65362017-02-13 22:48:30 -08004615 resfactor = (ores * 100);
4616 do_div(resfactor, nres);
4617 len1 *= resfactor;
4618 do_div(len1, 100);
4619 len2 *= resfactor;
4620 do_div(len2, 100);
4621 ASSERT(len1 + len2 <= ores);
4622 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4623
4624 /*
4625 * Hand out the remainder to each extent. If one of the two reservations
4626 * is zero, we want to make sure that one gets a block first. The loop
4627 * below starts with len1, so hand len2 a block right off the bat if it
4628 * is zero.
4629 */
4630 ores -= (len1 + len2);
4631 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4632 if (ores && !len2 && *indlen2) {
4633 len2++;
4634 ores--;
4635 }
4636 while (ores) {
4637 if (len1 < *indlen1) {
4638 len1++;
4639 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004640 }
Brian Foster75d65362017-02-13 22:48:30 -08004641 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004642 break;
Brian Foster75d65362017-02-13 22:48:30 -08004643 if (len2 < *indlen2) {
4644 len2++;
4645 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004646 }
4647 }
4648
4649 *indlen1 = len1;
4650 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004651
4652 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004653}
4654
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004655int
4656xfs_bmap_del_extent_delay(
4657 struct xfs_inode *ip,
4658 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004659 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004660 struct xfs_bmbt_irec *got,
4661 struct xfs_bmbt_irec *del)
4662{
4663 struct xfs_mount *mp = ip->i_mount;
4664 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4665 struct xfs_bmbt_irec new;
4666 int64_t da_old, da_new, da_diff = 0;
4667 xfs_fileoff_t del_endoff, got_endoff;
4668 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004669 int state = xfs_bmap_fork_to_state(whichfork);
4670 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004671 bool isrt;
4672
4673 XFS_STATS_INC(mp, xs_del_exlist);
4674
4675 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4676 del_endoff = del->br_startoff + del->br_blockcount;
4677 got_endoff = got->br_startoff + got->br_blockcount;
4678 da_old = startblockval(got->br_startblock);
4679 da_new = 0;
4680
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004681 ASSERT(del->br_blockcount > 0);
4682 ASSERT(got->br_startoff <= del->br_startoff);
4683 ASSERT(got_endoff >= del_endoff);
4684
4685 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004686 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004687
4688 do_div(rtexts, mp->m_sb.sb_rextsize);
4689 xfs_mod_frextents(mp, rtexts);
4690 }
4691
4692 /*
4693 * Update the inode delalloc counter now and wait to update the
4694 * sb counters as we might have to borrow some blocks for the
4695 * indirect block accounting.
4696 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004697 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4698 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004699 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004700 if (error)
4701 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004702 ip->i_delayed_blks -= del->br_blockcount;
4703
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004704 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004705 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004706 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004707 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004708
Christoph Hellwig0173c682017-10-17 14:16:22 -07004709 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4710 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004711 /*
4712 * Matches the whole extent. Delete the entry.
4713 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004714 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004715 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004716 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004717 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004718 /*
4719 * Deleting the first part of the extent.
4720 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004721 got->br_startoff = del_endoff;
4722 got->br_blockcount -= del->br_blockcount;
4723 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4724 got->br_blockcount), da_old);
4725 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004726 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004727 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004728 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004729 /*
4730 * Deleting the last part of the extent.
4731 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004732 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4733 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4734 got->br_blockcount), da_old);
4735 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004736 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004737 break;
4738 case 0:
4739 /*
4740 * Deleting the middle of the extent.
4741 *
4742 * Distribute the original indlen reservation across the two new
4743 * extents. Steal blocks from the deleted extent if necessary.
4744 * Stealing blocks simply fudges the fdblocks accounting below.
4745 * Warn if either of the new indlen reservations is zero as this
4746 * can lead to delalloc problems.
4747 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004748 got->br_blockcount = del->br_startoff - got->br_startoff;
4749 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4750
4751 new.br_blockcount = got_endoff - del_endoff;
4752 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4753
4754 WARN_ON_ONCE(!got_indlen || !new_indlen);
4755 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4756 del->br_blockcount);
4757
4758 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004759
4760 new.br_startoff = del_endoff;
4761 new.br_state = got->br_state;
4762 new.br_startblock = nullstartblock((int)new_indlen);
4763
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004764 xfs_iext_update_extent(ip, state, icur, got);
4765 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004766 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004767
4768 da_new = got_indlen + new_indlen - stolen;
4769 del->br_blockcount -= stolen;
4770 break;
4771 }
4772
4773 ASSERT(da_old >= da_new);
4774 da_diff = da_old - da_new;
4775 if (!isrt)
4776 da_diff += del->br_blockcount;
4777 if (da_diff)
4778 xfs_mod_fdblocks(mp, da_diff, false);
4779 return error;
4780}
4781
4782void
4783xfs_bmap_del_extent_cow(
4784 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004785 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004786 struct xfs_bmbt_irec *got,
4787 struct xfs_bmbt_irec *del)
4788{
4789 struct xfs_mount *mp = ip->i_mount;
4790 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4791 struct xfs_bmbt_irec new;
4792 xfs_fileoff_t del_endoff, got_endoff;
4793 int state = BMAP_COWFORK;
4794
4795 XFS_STATS_INC(mp, xs_del_exlist);
4796
4797 del_endoff = del->br_startoff + del->br_blockcount;
4798 got_endoff = got->br_startoff + got->br_blockcount;
4799
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004800 ASSERT(del->br_blockcount > 0);
4801 ASSERT(got->br_startoff <= del->br_startoff);
4802 ASSERT(got_endoff >= del_endoff);
4803 ASSERT(!isnullstartblock(got->br_startblock));
4804
4805 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004806 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004807 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004808 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004809
Christoph Hellwig0173c682017-10-17 14:16:22 -07004810 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4811 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004812 /*
4813 * Matches the whole extent. Delete the entry.
4814 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004815 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004816 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004817 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004818 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004819 /*
4820 * Deleting the first part of the extent.
4821 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004822 got->br_startoff = del_endoff;
4823 got->br_blockcount -= del->br_blockcount;
4824 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004825 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004826 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004827 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004828 /*
4829 * Deleting the last part of the extent.
4830 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004831 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004832 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004833 break;
4834 case 0:
4835 /*
4836 * Deleting the middle of the extent.
4837 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004838 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004839
4840 new.br_startoff = del_endoff;
4841 new.br_blockcount = got_endoff - del_endoff;
4842 new.br_state = got->br_state;
4843 new.br_startblock = del->br_startblock + del->br_blockcount;
4844
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004845 xfs_iext_update_extent(ip, state, icur, got);
4846 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004847 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004848 break;
4849 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004850 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004851}
4852
Brian Fostera9bd24a2016-03-15 11:42:46 +11004853/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004854 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004855 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004856 */
4857STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004858xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004859 xfs_inode_t *ip, /* incore inode pointer */
4860 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004861 struct xfs_iext_cursor *icur,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004862 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004863 xfs_btree_cur_t *cur, /* if null, not a btree */
4864 xfs_bmbt_irec_t *del, /* data to remove from extents */
4865 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004866 int whichfork, /* data or attr fork */
4867 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004868{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004869 xfs_fsblock_t del_endblock=0; /* first block past del */
4870 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004871 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004872 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004873 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004874 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004875 xfs_fileoff_t got_endoff; /* first offset past got */
4876 int i; /* temp state */
4877 xfs_ifork_t *ifp; /* inode fork pointer */
4878 xfs_mount_t *mp; /* mount structure */
4879 xfs_filblks_t nblks; /* quota/sb block count */
4880 xfs_bmbt_irec_t new; /* new record to be inserted */
4881 /* REFERENCED */
4882 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004883 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004884 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004885
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004886 mp = ip->i_mount;
4887 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004888
Dave Chinner9e5987a72013-02-25 12:31:26 +11004889 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004890 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004891 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004892 ASSERT(got.br_startoff <= del->br_startoff);
4893 del_endoff = del->br_startoff + del->br_blockcount;
4894 got_endoff = got.br_startoff + got.br_blockcount;
4895 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004896 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004897 qfield = 0;
4898 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004899
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004900 /*
4901 * If it's the case where the directory code is running with no block
4902 * reservation, and the deleted block is in the middle of its extent,
4903 * and the resulting insert of an extent would cause transformation to
4904 * btree format, then reject it. The calling code will then swap blocks
4905 * around instead. We have to do this now, rather than waiting for the
4906 * conversion to btree format, since the transaction will be dirty then.
4907 */
4908 if (tp->t_blk_res == 0 &&
4909 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4910 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4911 XFS_IFORK_MAXEXT(ip, whichfork) &&
4912 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4913 return -ENOSPC;
4914
4915 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004916 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4917 xfs_fsblock_t bno;
4918 xfs_filblks_t len;
4919
4920 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4921 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4922 bno = del->br_startblock;
4923 len = del->br_blockcount;
4924 do_div(bno, mp->m_sb.sb_rextsize);
4925 do_div(len, mp->m_sb.sb_rextsize);
4926 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4927 if (error)
4928 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004929 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004930 nblks = len * mp->m_sb.sb_rextsize;
4931 qfield = XFS_TRANS_DQ_RTBCOUNT;
4932 } else {
4933 do_fx = 1;
4934 nblks = del->br_blockcount;
4935 qfield = XFS_TRANS_DQ_BCOUNT;
4936 }
4937
4938 del_endblock = del->br_startblock + del->br_blockcount;
4939 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004940 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004941 if (error)
4942 goto done;
4943 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004944 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004945
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004946 if (got.br_startoff == del->br_startoff)
4947 state |= BMAP_LEFT_FILLING;
4948 if (got_endoff == del_endoff)
4949 state |= BMAP_RIGHT_FILLING;
4950
4951 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4952 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004953 /*
4954 * Matches the whole extent. Delete the entry.
4955 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004956 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004957 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004958 XFS_IFORK_NEXT_SET(ip, whichfork,
4959 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4960 flags |= XFS_ILOG_CORE;
4961 if (!cur) {
4962 flags |= xfs_ilog_fext(whichfork);
4963 break;
4964 }
4965 if ((error = xfs_btree_delete(cur, &i)))
4966 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004967 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004968 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004969 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004970 /*
4971 * Deleting the first part of the extent.
4972 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004973 got.br_startoff = del_endoff;
4974 got.br_startblock = del_endblock;
4975 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004976 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004977 if (!cur) {
4978 flags |= xfs_ilog_fext(whichfork);
4979 break;
4980 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004981 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004982 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11004983 goto done;
4984 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004985 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004986 /*
4987 * Deleting the last part of the extent.
4988 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004989 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004990 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004991 if (!cur) {
4992 flags |= xfs_ilog_fext(whichfork);
4993 break;
4994 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004995 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004996 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11004997 goto done;
4998 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004999 case 0:
5000 /*
5001 * Deleting the middle of the extent.
5002 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005003 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005004
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005005 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005006 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005007
Dave Chinner9e5987a72013-02-25 12:31:26 +11005008 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005009 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005010 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005011 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005012
Christoph Hellwige1d75532017-10-17 14:16:21 -07005013 flags |= XFS_ILOG_CORE;
5014 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005015 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005016 if (error)
5017 goto done;
5018 error = xfs_btree_increment(cur, 0, &i);
5019 if (error)
5020 goto done;
5021 cur->bc_rec.b = new;
5022 error = xfs_btree_insert(cur, &i);
5023 if (error && error != -ENOSPC)
5024 goto done;
5025 /*
5026 * If get no-space back from btree insert, it tried a
5027 * split, and we have a zero block reservation. Fix up
5028 * our state and return the error.
5029 */
5030 if (error == -ENOSPC) {
5031 /*
5032 * Reset the cursor, don't trust it after any
5033 * insert operation.
5034 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005035 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005036 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005037 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005038 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5039 /*
5040 * Update the btree record back
5041 * to the original value.
5042 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005043 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005044 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005045 goto done;
5046 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005047 * Reset the extent record back
5048 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005049 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005050 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005051 flags = 0;
5052 error = -ENOSPC;
5053 goto done;
5054 }
5055 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5056 } else
5057 flags |= xfs_ilog_fext(whichfork);
5058 XFS_IFORK_NEXT_SET(ip, whichfork,
5059 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005060 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005061 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005062 break;
5063 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005064
5065 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005066 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5067 if (error)
5068 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005069
Dave Chinner9e5987a72013-02-25 12:31:26 +11005070 /*
5071 * If we need to, add to list of extents to delete.
5072 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005073 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005074 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5075 error = xfs_refcount_decrease_extent(mp, dfops, del);
5076 if (error)
5077 goto done;
5078 } else
5079 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5080 del->br_blockcount, NULL);
5081 }
5082
Dave Chinner9e5987a72013-02-25 12:31:26 +11005083 /*
5084 * Adjust inode # blocks in the file.
5085 */
5086 if (nblks)
5087 ip->i_d.di_nblocks -= nblks;
5088 /*
5089 * Adjust quota data.
5090 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005091 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005092 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5093
Dave Chinner9e5987a72013-02-25 12:31:26 +11005094done:
5095 *logflagsp = flags;
5096 return error;
5097}
5098
5099/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 * Unmap (remove) blocks from a file.
5101 * If nexts is nonzero then the number of extents to remove is limited to
5102 * that value. If not all extents in the block range can be removed then
5103 * *done is set.
5104 */
5105int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005106__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 xfs_trans_t *tp, /* transaction pointer */
5108 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005109 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005110 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 int flags, /* misc flags */
5112 xfs_extnum_t nexts, /* number of extents max */
5113 xfs_fsblock_t *firstblock, /* first allocated block
5114 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005115 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116{
5117 xfs_btree_cur_t *cur; /* bmap btree cursor */
5118 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 int error; /* error return value */
5120 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005121 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 xfs_ifork_t *ifp; /* inode fork pointer */
5123 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 int logflags; /* transaction logging flags */
5125 xfs_extlen_t mod; /* rt extent offset */
5126 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 int tmp_logflags; /* partial logging flags */
5128 int wasdel; /* was a delayed alloc extent */
5129 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005131 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005132 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005133 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005134 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005135 struct xfs_iext_cursor icur;
5136 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005138 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005139
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005140 whichfork = xfs_bmapi_whichfork(flags);
5141 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 ifp = XFS_IFORK_PTR(ip, whichfork);
5143 if (unlikely(
5144 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5145 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5146 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5147 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005148 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 }
5150 mp = ip->i_mount;
5151 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005152 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005153
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005154 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 ASSERT(len > 0);
5156 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005157
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005158 /*
5159 * Guesstimate how many blocks we can unmap without running the risk of
5160 * blowing out the transaction with a mix of EFIs and reflink
5161 * adjustments.
5162 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005163 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005164 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5165 else
5166 max_len = len;
5167
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5169 (error = xfs_iread_extents(tp, ip, whichfork)))
5170 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005171 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005172 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 return 0;
5174 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005175 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005176 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005177 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005178
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005179 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005180 *rlen = 0;
5181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005183 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 logflags = 0;
5186 if (ifp->if_flags & XFS_IFBROOT) {
5187 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005188 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005190 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 cur->bc_private.b.flags = 0;
5192 } else
5193 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005194
5195 if (isrt) {
5196 /*
5197 * Synchronize by locking the bitmap inode.
5198 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005199 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005200 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005201 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5202 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005203 }
5204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005206 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005207 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005209 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 * Just back up to the previous extent, if so.
5211 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005212 if (got.br_startoff > end &&
5213 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5214 done = true;
5215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 }
5217 /*
5218 * Is the last block of this extent before the range
5219 * we're supposed to delete? If so, we're done.
5220 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005221 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005223 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 break;
5225 /*
5226 * Then deal with the (possibly delayed) allocated space
5227 * we found.
5228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005230 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005231
5232 /*
5233 * Make sure we don't touch multiple AGF headers out of order
5234 * in a single transaction, as that could cause AB-BA deadlocks.
5235 */
5236 if (!wasdel) {
5237 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5238 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5239 break;
5240 prev_agno = agno;
5241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 if (got.br_startoff < start) {
5243 del.br_startoff = start;
5244 del.br_blockcount -= start - got.br_startoff;
5245 if (!wasdel)
5246 del.br_startblock += start - got.br_startoff;
5247 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005248 if (del.br_startoff + del.br_blockcount > end + 1)
5249 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005250
5251 /* How much can we safely unmap? */
5252 if (max_len < del.br_blockcount) {
5253 del.br_startoff += del.br_blockcount - max_len;
5254 if (!wasdel)
5255 del.br_startblock += del.br_blockcount - max_len;
5256 del.br_blockcount = max_len;
5257 }
5258
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 sum = del.br_startblock + del.br_blockcount;
5260 if (isrt &&
5261 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5262 /*
5263 * Realtime extent not lined up at the end.
5264 * The extent could have been split into written
5265 * and unwritten pieces, or we could just be
5266 * unmapping part of it. But we can't really
5267 * get rid of part of a realtime extent.
5268 */
5269 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005270 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 /*
5272 * This piece is unwritten, or we're not
5273 * using unwritten extents. Skip over it.
5274 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005275 ASSERT(end >= mod);
5276 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005278 if (end < got.br_startoff &&
5279 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5280 done = true;
5281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 }
5283 continue;
5284 }
5285 /*
5286 * It's written, turn it unwritten.
5287 * This is better than zeroing it.
5288 */
5289 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005290 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 /*
5292 * If this spans a realtime extent boundary,
5293 * chop it back to the start of the one we end at.
5294 */
5295 if (del.br_blockcount > mod) {
5296 del.br_startoff += del.br_blockcount - mod;
5297 del.br_startblock += del.br_blockcount - mod;
5298 del.br_blockcount = mod;
5299 }
5300 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005301 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005302 whichfork, &icur, &cur, &del,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005303 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 if (error)
5305 goto error0;
5306 goto nodelete;
5307 }
5308 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5309 /*
5310 * Realtime extent is lined up at the end but not
5311 * at the front. We'll get rid of full extents if
5312 * we can.
5313 */
5314 mod = mp->m_sb.sb_rextsize - mod;
5315 if (del.br_blockcount > mod) {
5316 del.br_blockcount -= mod;
5317 del.br_startoff += mod;
5318 del.br_startblock += mod;
5319 } else if ((del.br_startoff == start &&
5320 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005321 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005322 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 /*
5324 * Can't make it unwritten. There isn't
5325 * a full extent here so just skip it.
5326 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005327 ASSERT(end >= del.br_blockcount);
5328 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005329 if (got.br_startoff > end &&
5330 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5331 done = true;
5332 break;
5333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 continue;
5335 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005336 struct xfs_bmbt_irec prev;
5337
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 /*
5339 * This one is already unwritten.
5340 * It must have a written left neighbor.
5341 * Unwrite the killed part of that one and
5342 * try again.
5343 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005344 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5345 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005347 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 ASSERT(del.br_startblock ==
5349 prev.br_startblock + prev.br_blockcount);
5350 if (prev.br_startoff < start) {
5351 mod = start - prev.br_startoff;
5352 prev.br_blockcount -= mod;
5353 prev.br_startblock += mod;
5354 prev.br_startoff = start;
5355 }
5356 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005357 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005358 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005359 &prev, firstblock, dfops,
5360 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (error)
5362 goto error0;
5363 goto nodelete;
5364 } else {
5365 ASSERT(del.br_state == XFS_EXT_NORM);
5366 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005367 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005368 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005369 &del, firstblock, dfops,
5370 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 if (error)
5372 goto error0;
5373 goto nodelete;
5374 }
5375 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005376
Brian Fosterb2706a02016-03-15 11:42:46 +11005377 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005378 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005379 &got, &del);
5380 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005381 error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005382 cur, &del, &tmp_logflags, whichfork,
5383 flags);
5384 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005385 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 if (error)
5388 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005389
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005390 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005391 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 /*
5394 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005396 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005397 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5398 (got.br_startoff > end &&
5399 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5400 done = true;
5401 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 extno++;
5404 }
5405 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005406 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005407 *rlen = 0;
5408 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005409 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 /*
5412 * Convert to a btree if necessary.
5413 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005414 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005416 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 &cur, 0, &tmp_logflags, whichfork);
5418 logflags |= tmp_logflags;
5419 if (error)
5420 goto error0;
5421 }
5422 /*
5423 * transform from btree to extents, give it cur
5424 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005425 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 ASSERT(cur != NULL);
5427 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5428 whichfork);
5429 logflags |= tmp_logflags;
5430 if (error)
5431 goto error0;
5432 }
5433 /*
5434 * transform from extents to local?
5435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 error = 0;
5437error0:
5438 /*
5439 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005440 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005442 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005444 logflags &= ~xfs_ilog_fext(whichfork);
5445 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005447 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 /*
5449 * Log inode even in the error case, if the transaction
5450 * is dirty we'll need to shut down the filesystem.
5451 */
5452 if (logflags)
5453 xfs_trans_log_inode(tp, ip, logflags);
5454 if (cur) {
5455 if (!error) {
5456 *firstblock = cur->bc_private.b.firstblock;
5457 cur->bc_private.b.allocated = 0;
5458 }
5459 xfs_btree_del_cursor(cur,
5460 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5461 }
5462 return error;
5463}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005464
Darrick J. Wong44535932016-10-03 09:11:29 -07005465/* Unmap a range of a file. */
5466int
5467xfs_bunmapi(
5468 xfs_trans_t *tp,
5469 struct xfs_inode *ip,
5470 xfs_fileoff_t bno,
5471 xfs_filblks_t len,
5472 int flags,
5473 xfs_extnum_t nexts,
5474 xfs_fsblock_t *firstblock,
5475 struct xfs_defer_ops *dfops,
5476 int *done)
5477{
5478 int error;
5479
5480 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5481 dfops);
5482 *done = (len == 0);
5483 return error;
5484}
5485
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005486/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005487 * Determine whether an extent shift can be accomplished by a merge with the
5488 * extent that precedes the target hole of the shift.
5489 */
5490STATIC bool
5491xfs_bmse_can_merge(
5492 struct xfs_bmbt_irec *left, /* preceding extent */
5493 struct xfs_bmbt_irec *got, /* current extent to shift */
5494 xfs_fileoff_t shift) /* shift fsb */
5495{
5496 xfs_fileoff_t startoff;
5497
5498 startoff = got->br_startoff - shift;
5499
5500 /*
5501 * The extent, once shifted, must be adjacent in-file and on-disk with
5502 * the preceding extent.
5503 */
5504 if ((left->br_startoff + left->br_blockcount != startoff) ||
5505 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5506 (left->br_state != got->br_state) ||
5507 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5508 return false;
5509
5510 return true;
5511}
5512
5513/*
5514 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5515 * hole in the file. If an extent shift would result in the extent being fully
5516 * adjacent to the extent that currently precedes the hole, we can merge with
5517 * the preceding extent rather than do the shift.
5518 *
5519 * This function assumes the caller has verified a shift-by-merge is possible
5520 * with the provided extents via xfs_bmse_can_merge().
5521 */
5522STATIC int
5523xfs_bmse_merge(
5524 struct xfs_inode *ip,
5525 int whichfork,
5526 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005527 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005528 struct xfs_bmbt_irec *got, /* extent to shift */
5529 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005530 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005531 int *logflags, /* output */
5532 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005533{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005534 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005535 xfs_filblks_t blockcount;
5536 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005537 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005538
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005539 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005540
5541 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5542 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005543 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005544
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005545 new = *left;
5546 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005547
5548 /*
5549 * Update the on-disk extent count, the btree if necessary and log the
5550 * inode.
5551 */
5552 XFS_IFORK_NEXT_SET(ip, whichfork,
5553 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5554 *logflags |= XFS_ILOG_CORE;
5555 if (!cur) {
5556 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005557 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005558 }
5559
5560 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005561 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005562 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005563 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005564 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005565
5566 error = xfs_btree_delete(cur, &i);
5567 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005568 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005569 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005570
5571 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005572 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005573 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005574 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005575 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005576
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005577 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005578 if (error)
5579 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005580
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005581done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005582 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005583 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5584 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5585 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005586
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005587 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005588 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5589 if (error)
5590 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005591 memcpy(&new, got, sizeof(new));
5592 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005593 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005594}
5595
Christoph Hellwigbf806282017-10-19 11:07:34 -07005596static int
5597xfs_bmap_shift_update_extent(
5598 struct xfs_inode *ip,
5599 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005600 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005601 struct xfs_bmbt_irec *got,
5602 struct xfs_btree_cur *cur,
5603 int *logflags,
5604 struct xfs_defer_ops *dfops,
5605 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005606{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005607 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005608 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005609 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005610
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005611 *logflags |= XFS_ILOG_CORE;
5612
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005613 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005614
5615 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005616 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005617 if (error)
5618 return error;
5619 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5620
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005621 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005622 if (error)
5623 return error;
5624 } else {
5625 *logflags |= XFS_ILOG_DEXT;
5626 }
5627
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005628 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5629 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005630
Darrick J. Wong9c194642016-08-03 12:16:05 +10005631 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005632 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005633 if (error)
5634 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005635 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005636}
5637
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005638int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005639xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005640 struct xfs_trans *tp,
5641 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005642 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005643 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005644 bool *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005645 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005646 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005647 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005648{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005649 int whichfork = XFS_DATA_FORK;
5650 struct xfs_mount *mp = ip->i_mount;
5651 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5652 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005653 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005654 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005655 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005656 int error = 0;
5657 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005658
5659 if (unlikely(XFS_TEST_ERROR(
5660 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5661 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005662 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005663 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005664 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005665 }
5666
5667 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005668 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005669
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005670 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005671
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005672 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005673 error = xfs_iread_extents(tp, ip, whichfork);
5674 if (error)
5675 return error;
5676 }
5677
Brian Fosterddb19e32014-09-23 15:38:09 +10005678 if (ifp->if_flags & XFS_IFBROOT) {
5679 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5680 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005681 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005682 cur->bc_private.b.flags = 0;
5683 }
5684
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005685 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005686 *done = true;
5687 goto del_cursor;
5688 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005689 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5690 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005691
Christoph Hellwigbf806282017-10-19 11:07:34 -07005692 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005693 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005694 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5695 error = -EINVAL;
5696 goto del_cursor;
5697 }
5698
Christoph Hellwigbf806282017-10-19 11:07:34 -07005699 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5700 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005701 &icur, &got, &prev, cur, &logflags,
5702 dfops);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005703 if (error)
5704 goto del_cursor;
5705 goto done;
5706 }
5707 } else {
5708 if (got.br_startoff < offset_shift_fsb) {
5709 error = -EINVAL;
5710 goto del_cursor;
5711 }
5712 }
5713
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005714 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5715 &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005716 if (error)
5717 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005718
Christoph Hellwig42630362017-11-03 10:34:41 -07005719done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005720 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5721 *done = true;
5722 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005723 }
5724
Christoph Hellwigbf806282017-10-19 11:07:34 -07005725 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005726del_cursor:
5727 if (cur)
5728 xfs_btree_del_cursor(cur,
5729 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005730 if (logflags)
5731 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005732 return error;
5733}
5734
5735int
5736xfs_bmap_insert_extents(
5737 struct xfs_trans *tp,
5738 struct xfs_inode *ip,
5739 xfs_fileoff_t *next_fsb,
5740 xfs_fileoff_t offset_shift_fsb,
5741 bool *done,
5742 xfs_fileoff_t stop_fsb,
5743 xfs_fsblock_t *firstblock,
5744 struct xfs_defer_ops *dfops)
5745{
5746 int whichfork = XFS_DATA_FORK;
5747 struct xfs_mount *mp = ip->i_mount;
5748 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5749 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005750 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005751 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005752 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005753 int error = 0;
5754 int logflags = 0;
5755
5756 if (unlikely(XFS_TEST_ERROR(
5757 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5758 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5759 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5760 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5761 return -EFSCORRUPTED;
5762 }
5763
5764 if (XFS_FORCED_SHUTDOWN(mp))
5765 return -EIO;
5766
5767 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5768
5769 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5770 error = xfs_iread_extents(tp, ip, whichfork);
5771 if (error)
5772 return error;
5773 }
5774
5775 if (ifp->if_flags & XFS_IFBROOT) {
5776 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5777 cur->bc_private.b.firstblock = *firstblock;
5778 cur->bc_private.b.dfops = dfops;
5779 cur->bc_private.b.flags = 0;
5780 }
5781
Namjae Jeona904b1c2015-03-25 15:08:56 +11005782 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005783 xfs_iext_last(ifp, &icur);
5784 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005785 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005786 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005787 goto del_cursor;
5788 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005789 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005790 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005791 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005792 goto del_cursor;
5793 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005794 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005795 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5796 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005797
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005798 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005799 error = -EIO;
5800 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005801 }
5802
Christoph Hellwigbf806282017-10-19 11:07:34 -07005803 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005804 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005805 if (new_startoff + got.br_blockcount > next.br_startoff) {
5806 error = -EINVAL;
5807 goto del_cursor;
5808 }
5809
5810 /*
5811 * Unlike a left shift (which involves a hole punch), a right
5812 * shift does not modify extent neighbors in any way. We should
5813 * never find mergeable extents in this scenario. Check anyways
5814 * and warn if we encounter two extents that could be one.
5815 */
5816 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5817 WARN_ON_ONCE(1);
5818 }
5819
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005820 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5821 &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005822 if (error)
5823 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005824
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005825 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005826 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005827 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005828 goto del_cursor;
5829 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005830
5831 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005832del_cursor:
5833 if (cur)
5834 xfs_btree_del_cursor(cur,
5835 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005836 if (logflags)
5837 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005838 return error;
5839}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005840
5841/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005842 * Splits an extent into two extents at split_fsb block such that it is the
5843 * first block of the current_ext. @ext is a target extent to be split.
5844 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5845 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005846 */
5847STATIC int
5848xfs_bmap_split_extent_at(
5849 struct xfs_trans *tp,
5850 struct xfs_inode *ip,
5851 xfs_fileoff_t split_fsb,
5852 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005853 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005854{
5855 int whichfork = XFS_DATA_FORK;
5856 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005857 struct xfs_bmbt_irec got;
5858 struct xfs_bmbt_irec new; /* split extent */
5859 struct xfs_mount *mp = ip->i_mount;
5860 struct xfs_ifork *ifp;
5861 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005862 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005863 int error = 0;
5864 int logflags = 0;
5865 int i = 0;
5866
5867 if (unlikely(XFS_TEST_ERROR(
5868 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5869 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005870 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005871 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5872 XFS_ERRLEVEL_LOW, mp);
5873 return -EFSCORRUPTED;
5874 }
5875
5876 if (XFS_FORCED_SHUTDOWN(mp))
5877 return -EIO;
5878
5879 ifp = XFS_IFORK_PTR(ip, whichfork);
5880 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5881 /* Read in all the extents */
5882 error = xfs_iread_extents(tp, ip, whichfork);
5883 if (error)
5884 return error;
5885 }
5886
5887 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005888 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005889 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005890 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005891 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005892 return 0;
5893
5894 gotblkcnt = split_fsb - got.br_startoff;
5895 new.br_startoff = split_fsb;
5896 new.br_startblock = got.br_startblock + gotblkcnt;
5897 new.br_blockcount = got.br_blockcount - gotblkcnt;
5898 new.br_state = got.br_state;
5899
5900 if (ifp->if_flags & XFS_IFBROOT) {
5901 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5902 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005903 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005904 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005905 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005906 if (error)
5907 goto del_cursor;
5908 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5909 }
5910
Namjae Jeona904b1c2015-03-25 15:08:56 +11005911 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005912 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5913 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005914
5915 logflags = XFS_ILOG_CORE;
5916 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005917 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005918 if (error)
5919 goto del_cursor;
5920 } else
5921 logflags |= XFS_ILOG_DEXT;
5922
5923 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005924 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005925 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005926 XFS_IFORK_NEXT_SET(ip, whichfork,
5927 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5928
5929 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005930 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005931 if (error)
5932 goto del_cursor;
5933 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 error = xfs_btree_insert(cur, &i);
5935 if (error)
5936 goto del_cursor;
5937 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5938 }
5939
5940 /*
5941 * Convert to a btree if necessary.
5942 */
5943 if (xfs_bmap_needs_btree(ip, whichfork)) {
5944 int tmp_logflags; /* partial log flag return val */
5945
5946 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005947 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005948 &cur, 0, &tmp_logflags, whichfork);
5949 logflags |= tmp_logflags;
5950 }
5951
5952del_cursor:
5953 if (cur) {
5954 cur->bc_private.b.allocated = 0;
5955 xfs_btree_del_cursor(cur,
5956 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5957 }
5958
5959 if (logflags)
5960 xfs_trans_log_inode(tp, ip, logflags);
5961 return error;
5962}
5963
5964int
5965xfs_bmap_split_extent(
5966 struct xfs_inode *ip,
5967 xfs_fileoff_t split_fsb)
5968{
5969 struct xfs_mount *mp = ip->i_mount;
5970 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005971 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005972 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973 int error;
5974
Christoph Hellwig253f4912016-04-06 09:19:55 +10005975 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5976 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5977 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005978 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005979
5980 xfs_ilock(ip, XFS_ILOCK_EXCL);
5981 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5982
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005983 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005984
5985 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005986 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987 if (error)
5988 goto out;
5989
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07005990 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005991 if (error)
5992 goto out;
5993
Christoph Hellwig70393312015-06-04 13:48:08 +10005994 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995
5996out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005997 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005998 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005999 return error;
6000}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006001
6002/* Deferred mapping is only for real extents in the data fork. */
6003static bool
6004xfs_bmap_is_update_needed(
6005 struct xfs_bmbt_irec *bmap)
6006{
6007 return bmap->br_startblock != HOLESTARTBLOCK &&
6008 bmap->br_startblock != DELAYSTARTBLOCK;
6009}
6010
6011/* Record a bmap intent. */
6012static int
6013__xfs_bmap_add(
6014 struct xfs_mount *mp,
6015 struct xfs_defer_ops *dfops,
6016 enum xfs_bmap_intent_type type,
6017 struct xfs_inode *ip,
6018 int whichfork,
6019 struct xfs_bmbt_irec *bmap)
6020{
6021 int error;
6022 struct xfs_bmap_intent *bi;
6023
6024 trace_xfs_bmap_defer(mp,
6025 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6026 type,
6027 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6028 ip->i_ino, whichfork,
6029 bmap->br_startoff,
6030 bmap->br_blockcount,
6031 bmap->br_state);
6032
6033 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6034 INIT_LIST_HEAD(&bi->bi_list);
6035 bi->bi_type = type;
6036 bi->bi_owner = ip;
6037 bi->bi_whichfork = whichfork;
6038 bi->bi_bmap = *bmap;
6039
Christoph Hellwig882d8782017-08-28 10:21:03 -07006040 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006041 if (error) {
6042 kmem_free(bi);
6043 return error;
6044 }
6045
6046 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6047 return 0;
6048}
6049
6050/* Map an extent into a file. */
6051int
6052xfs_bmap_map_extent(
6053 struct xfs_mount *mp,
6054 struct xfs_defer_ops *dfops,
6055 struct xfs_inode *ip,
6056 struct xfs_bmbt_irec *PREV)
6057{
6058 if (!xfs_bmap_is_update_needed(PREV))
6059 return 0;
6060
6061 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6062 XFS_DATA_FORK, PREV);
6063}
6064
6065/* Unmap an extent out of a file. */
6066int
6067xfs_bmap_unmap_extent(
6068 struct xfs_mount *mp,
6069 struct xfs_defer_ops *dfops,
6070 struct xfs_inode *ip,
6071 struct xfs_bmbt_irec *PREV)
6072{
6073 if (!xfs_bmap_is_update_needed(PREV))
6074 return 0;
6075
6076 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6077 XFS_DATA_FORK, PREV);
6078}
6079
6080/*
6081 * Process one of the deferred bmap operations. We pass back the
6082 * btree cursor to maintain our lock on the bmapbt between calls.
6083 */
6084int
6085xfs_bmap_finish_one(
6086 struct xfs_trans *tp,
6087 struct xfs_defer_ops *dfops,
6088 struct xfs_inode *ip,
6089 enum xfs_bmap_intent_type type,
6090 int whichfork,
6091 xfs_fileoff_t startoff,
6092 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006093 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006094 xfs_exntst_t state)
6095{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006096 xfs_fsblock_t firstfsb;
6097 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006098
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006099 /*
6100 * firstfsb is tied to the transaction lifetime and is used to
6101 * ensure correct AG locking order and schedule work item
6102 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6103 * to only making one bmap call per transaction, so it should
6104 * be safe to have it as a local variable here.
6105 */
6106 firstfsb = NULLFSBLOCK;
6107
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006108 trace_xfs_bmap_deferred(tp->t_mountp,
6109 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6110 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006111 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006112
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006113 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006114 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006115
6116 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006117 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006118 return -EIO;
6119
6120 switch (type) {
6121 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006122 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006123 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006124 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006125 break;
6126 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006127 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6128 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006129 break;
6130 default:
6131 ASSERT(0);
6132 error = -EFSCORRUPTED;
6133 }
6134
6135 return error;
6136}