blob: 6a7c2f03ea11a123bcd04fbc70be96cbc897382f [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);
Darrick J. Wong90a58f92018-03-23 10:06:52 -07001247 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1248 __func__, block, sizeof(*block),
1249 __this_address);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001250 error = -EFSCORRUPTED;
1251 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001252 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001253 /*
1254 * Read-ahead the next leaf block, if any.
1255 */
1256 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1257 if (nextbno != NULLFSBLOCK)
1258 xfs_btree_reada_bufl(mp, nextbno, 1,
1259 &xfs_bmbt_buf_ops);
1260 /*
1261 * Copy records into the extent records.
1262 */
1263 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Christoph Hellwig6bdcf262017-11-03 10:34:46 -07001264 for (j = 0; j < num_recs; j++, frp++, i++) {
Darrick J. Wong30b09842018-03-23 10:06:52 -07001265 xfs_failaddr_t fa;
1266
Christoph Hellwigdac9c9b2017-11-03 10:34:47 -07001267 xfs_bmbt_disk_get_all(frp, &new);
Darrick J. Wong30b09842018-03-23 10:06:52 -07001268 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1269 if (fa) {
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001270 error = -EFSCORRUPTED;
Darrick J. Wong30b09842018-03-23 10:06:52 -07001271 xfs_inode_verifier_error(ip, error,
1272 "xfs_iread_extents(2)",
1273 frp, sizeof(*frp), fa);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001274 goto out_brelse;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001275 }
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001276 xfs_iext_insert(ip, &icur, &new, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001277 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1278 xfs_iext_next(ifp, &icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001279 }
1280 xfs_trans_brelse(tp, bp);
1281 bno = nextbno;
1282 /*
1283 * If we've reached the end, stop.
1284 */
1285 if (bno == NULLFSBLOCK)
1286 break;
1287 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1288 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1289 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001290 goto out;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001291 block = XFS_BUF_TO_BLOCK(bp);
1292 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001293
1294 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1295 error = -EFSCORRUPTED;
1296 goto out;
1297 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001298 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001299
1300 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001301 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001302
1303out_brelse:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001304 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001305out:
1306 xfs_iext_destroy(ifp);
1307 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001308}
1309
Dave Chinner9e5987a72013-02-25 12:31:26 +11001310/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001311 * Returns the relative block number of the first unused block(s) in the given
1312 * fork with at least "len" logically contiguous blocks free. This is the
1313 * lowest-address hole if the fork has holes, else the first block past the end
1314 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a72013-02-25 12:31:26 +11001315 */
1316int /* error */
1317xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001318 struct xfs_trans *tp, /* transaction pointer */
1319 struct xfs_inode *ip, /* incore inode */
1320 xfs_extlen_t len, /* size of hole to find */
1321 xfs_fileoff_t *first_unused, /* unused block */
1322 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001323{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001324 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1325 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001326 struct xfs_iext_cursor icur;
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001327 xfs_fileoff_t lastaddr = 0;
1328 xfs_fileoff_t lowest, max;
1329 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001330
1331 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1332 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1333 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001334
Dave Chinner9e5987a72013-02-25 12:31:26 +11001335 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1336 *first_unused = 0;
1337 return 0;
1338 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001339
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001340 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1341 error = xfs_iread_extents(tp, ip, whichfork);
1342 if (error)
1343 return error;
1344 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001345
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001346 lowest = max = *first_unused;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001347 for_each_xfs_iext(ifp, &icur, &got) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001348 /*
1349 * See if the hole before this extent will work.
1350 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001351 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001352 got.br_startoff - max >= len)
1353 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001354 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001355 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1356 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001357
Dave Chinner9e5987a72013-02-25 12:31:26 +11001358 *first_unused = max;
1359 return 0;
1360}
1361
1362/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001363 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001364 * last_block (input value) in the file.
1365 * This is not based on i_size, it is based on the extent records.
1366 * Returns 0 for local files, as they do not have extent records.
1367 */
1368int /* error */
1369xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001370 struct xfs_trans *tp, /* transaction pointer */
1371 struct xfs_inode *ip, /* incore inode */
1372 xfs_fileoff_t *last_block, /* last block */
1373 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001374{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001375 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1376 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001377 struct xfs_iext_cursor icur;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001378 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001379
Christoph Hellwig86685f72016-11-24 11:39:38 +11001380 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1381 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001382 *last_block = 0;
1383 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001384 case XFS_DINODE_FMT_BTREE:
1385 case XFS_DINODE_FMT_EXTENTS:
1386 break;
1387 default:
1388 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001389 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001390
1391 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1392 error = xfs_iread_extents(tp, ip, whichfork);
1393 if (error)
1394 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001395 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001396
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001397 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
Christoph Hellwigdc560152017-10-23 16:32:39 -07001398 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001399 return 0;
1400}
1401
Dave Chinner68988112013-08-12 20:49:42 +10001402int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001403xfs_bmap_last_extent(
1404 struct xfs_trans *tp,
1405 struct xfs_inode *ip,
1406 int whichfork,
1407 struct xfs_bmbt_irec *rec,
1408 int *is_empty)
1409{
1410 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001411 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001412 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001413
1414 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1415 error = xfs_iread_extents(tp, ip, whichfork);
1416 if (error)
1417 return error;
1418 }
1419
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001420 xfs_iext_last(ifp, &icur);
1421 if (!xfs_iext_get_extent(ifp, &icur, rec))
Dave Chinner9e5987a72013-02-25 12:31:26 +11001422 *is_empty = 1;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001423 else
1424 *is_empty = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001425 return 0;
1426}
1427
1428/*
1429 * Check the last inode extent to determine whether this allocation will result
1430 * in blocks being allocated at the end of the file. When we allocate new data
1431 * blocks at the end of the file which do not start at the previous data block,
1432 * we will try to align the new blocks at stripe unit boundaries.
1433 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001434 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001435 * at, or past the EOF.
1436 */
1437STATIC int
1438xfs_bmap_isaeof(
1439 struct xfs_bmalloca *bma,
1440 int whichfork)
1441{
1442 struct xfs_bmbt_irec rec;
1443 int is_empty;
1444 int error;
1445
Thomas Meyer749f24f2017-10-09 11:38:54 -07001446 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001447 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1448 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001449 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001450 return error;
1451
Dave Chinner6e708bc2013-11-22 10:41:16 +11001452 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001453 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001454 return 0;
1455 }
1456
Dave Chinner9e5987a72013-02-25 12:31:26 +11001457 /*
1458 * Check if we are allocation or past the last extent, or at least into
1459 * the last delayed allocated extent.
1460 */
1461 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1462 (bma->offset >= rec.br_startoff &&
1463 isnullstartblock(rec.br_startblock));
1464 return 0;
1465}
1466
1467/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001468 * Returns the file-relative block number of the first block past eof in
1469 * the file. This is not based on i_size, it is based on the extent records.
1470 * Returns 0 for local files, as they do not have extent records.
1471 */
1472int
1473xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001474 struct xfs_inode *ip,
1475 xfs_fileoff_t *last_block,
1476 int whichfork)
1477{
1478 struct xfs_bmbt_irec rec;
1479 int is_empty;
1480 int error;
1481
1482 *last_block = 0;
1483
1484 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1485 return 0;
1486
1487 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1488 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001489 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001490
1491 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1492 if (error || is_empty)
1493 return error;
1494
1495 *last_block = rec.br_startoff + rec.br_blockcount;
1496 return 0;
1497}
1498
1499/*
1500 * Returns whether the selected fork of the inode has exactly one
1501 * block or not. For the data fork we check this matches di_size,
1502 * implying the file's range is 0..bsize-1.
1503 */
1504int /* 1=>1 block, 0=>otherwise */
1505xfs_bmap_one_block(
1506 xfs_inode_t *ip, /* incore inode */
1507 int whichfork) /* data or attr fork */
1508{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001509 xfs_ifork_t *ifp; /* inode fork pointer */
1510 int rval; /* return value */
1511 xfs_bmbt_irec_t s; /* internal version of extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001512 struct xfs_iext_cursor icur;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001513
1514#ifndef DEBUG
1515 if (whichfork == XFS_DATA_FORK)
1516 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1517#endif /* !DEBUG */
1518 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1519 return 0;
1520 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1521 return 0;
1522 ifp = XFS_IFORK_PTR(ip, whichfork);
1523 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001524 xfs_iext_first(ifp, &icur);
1525 xfs_iext_get_extent(ifp, &icur, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001526 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1527 if (rval && whichfork == XFS_DATA_FORK)
1528 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1529 return rval;
1530}
1531
1532/*
1533 * Extent tree manipulation functions used during allocation.
1534 */
1535
1536/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001537 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
1539STATIC int /* error */
1540xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001541 struct xfs_bmalloca *bma,
1542 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001544 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001547 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 xfs_fileoff_t new_endoff; /* end offset of new entry */
1549 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1550 /* left is 0, right is 1, prev is 2 */
1551 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001552 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001553 xfs_filblks_t da_new; /* new count del alloc blocks used */
1554 xfs_filblks_t da_old; /* old count del alloc blocks used */
1555 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001557 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001558 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001559 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001561 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001562 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001563 ASSERT(whichfork != XFS_ATTR_FORK);
1564 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1565 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001566
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001567 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001568 ASSERT(!bma->cur ||
1569 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001570
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001571 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573#define LEFT r[0]
1574#define RIGHT r[1]
1575#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /*
1578 * Set up a bunch of variables to make the tests simpler.
1579 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001580 xfs_iext_get_extent(ifp, &bma->icur, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001582 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ASSERT(PREV.br_startoff <= new->br_startoff);
1584 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001585
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001586 da_old = startblockval(PREV.br_startblock);
1587 da_new = 0;
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * Set flags determining what part of the previous delayed allocation
1591 * extent is being replaced by a real allocation.
1592 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001593 if (PREV.br_startoff == new->br_startoff)
1594 state |= BMAP_LEFT_FILLING;
1595 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1596 state |= BMAP_RIGHT_FILLING;
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /*
1599 * Check and set flags if this segment has a left neighbor.
1600 * Don't set contiguous if the combined extent would be too large.
1601 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001602 if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001603 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001604 if (isnullstartblock(LEFT.br_startblock))
1605 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001607
1608 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1609 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1610 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1611 LEFT.br_state == new->br_state &&
1612 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1613 state |= BMAP_LEFT_CONTIG;
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
1616 * Check and set flags if this segment has a right neighbor.
1617 * Don't set contiguous if the combined extent would be too large.
1618 * Also check for all-three-contiguous being too large.
1619 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001620 if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001621 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001622 if (isnullstartblock(RIGHT.br_startblock))
1623 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001625
1626 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1627 new_endoff == RIGHT.br_startoff &&
1628 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1629 new->br_state == RIGHT.br_state &&
1630 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1631 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1632 BMAP_RIGHT_FILLING)) !=
1633 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1634 BMAP_RIGHT_FILLING) ||
1635 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1636 <= MAXEXTLEN))
1637 state |= BMAP_RIGHT_CONTIG;
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 error = 0;
1640 /*
1641 * Switch out based on the FILLING and CONTIG state bits.
1642 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001643 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1644 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1645 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1646 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Filling in all of a previously delayed allocation extent.
1649 * The left and right neighbors are both contiguous with new.
1650 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001651 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001652
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001653 xfs_iext_remove(bma->ip, &bma->icur, state);
1654 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001655 xfs_iext_prev(ifp, &bma->icur);
1656 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001657 (*nextents)--;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001658
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1661 else {
1662 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001663 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001664 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001666 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001667 error = xfs_btree_delete(bma->cur, &i);
1668 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001670 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001671 error = xfs_btree_decrement(bma->cur, 0, &i);
1672 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001674 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001675 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001676 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto done;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 break;
1680
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001681 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /*
1683 * Filling in all of a previously delayed allocation extent.
1684 * The left neighbor is contiguous, the right is not.
1685 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001686 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001687 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001688
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001689 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001690 xfs_iext_prev(ifp, &bma->icur);
1691 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001692
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001693 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 rval = XFS_ILOG_DEXT;
1695 else {
1696 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001697 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001700 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001701 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001702 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto done;
1704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001707 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /*
1709 * Filling in all of a previously delayed allocation extent.
1710 * The right neighbor is contiguous, the left is not.
1711 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001712 PREV.br_startblock = new->br_startblock;
1713 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwig0d045542017-11-03 10:34:39 -07001714
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001715 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07001716 xfs_iext_remove(bma->ip, &bma->icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001717 xfs_iext_prev(ifp, &bma->icur);
1718 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001719
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001720 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 rval = XFS_ILOG_DEXT;
1722 else {
1723 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001724 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001727 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001728 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001729 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 goto done;
1731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 break;
1733
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001734 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
1736 * Filling in all of a previously delayed allocation extent.
1737 * Neither the left nor right neighbors are contiguous with
1738 * the new one.
1739 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001740 PREV.br_startblock = new->br_startblock;
1741 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001742 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001743
Darrick J. Wong60b49842016-10-03 09:11:34 -07001744 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1747 else {
1748 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001749 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001752 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 error = xfs_btree_insert(bma->cur, &i);
1754 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001756 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 break;
1759
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001760 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /*
1762 * Filling in the first part of a previous delayed allocation.
1763 * The left neighbor is contiguous.
1764 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765 old = LEFT;
1766 temp = PREV.br_blockcount - new->br_blockcount;
1767 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1768 startblockval(PREV.br_startblock));
1769
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001770 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001771
Christoph Hellwigbf999712017-11-03 10:34:38 -07001772 PREV.br_blockcount = temp;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001773 PREV.br_startoff += new->br_blockcount;
1774 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwig0d045542017-11-03 10:34:39 -07001775
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001776 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1777 xfs_iext_prev(ifp, &bma->icur);
1778 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001779
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001780 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 rval = XFS_ILOG_DEXT;
1782 else {
1783 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001784 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001785 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001787 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001788 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001789 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto done;
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001794 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /*
1796 * Filling in the first part of a previous delayed allocation.
1797 * The left neighbor is not contiguous.
1798 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001799 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001800 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1803 else {
1804 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001805 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001808 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 error = xfs_btree_insert(bma->cur, &i);
1810 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001814
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001815 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001817 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001818 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 rval |= tmp_rval;
1820 if (error)
1821 goto done;
1822 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823
1824 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001825 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001826 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001828
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001829 PREV.br_startoff = new_endoff;
1830 PREV.br_blockcount = temp;
1831 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001832 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001833 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001834 xfs_iext_prev(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 break;
1836
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001837 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /*
1839 * Filling in the last part of a previous delayed allocation.
1840 * The right neighbor is contiguous with the new allocation.
1841 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001842 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001843 RIGHT.br_startoff = new->br_startoff;
1844 RIGHT.br_startblock = new->br_startblock;
1845 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001846
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 rval = XFS_ILOG_DEXT;
1849 else {
1850 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001851 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001852 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001854 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001855 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001856 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto done;
1858 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001859
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001860 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001862 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001863
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001864 PREV.br_blockcount = temp;
1865 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001866
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001867 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1868 xfs_iext_next(ifp, &bma->icur);
1869 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 break;
1871
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001872 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /*
1874 * Filling in the last part of a previous delayed allocation.
1875 * The right neighbor is not contiguous.
1876 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001877 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001878 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1881 else {
1882 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001883 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001884 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001886 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 error = xfs_btree_insert(bma->cur, &i);
1888 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001890 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001892
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001893 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001895 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001896 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 rval |= tmp_rval;
1898 if (error)
1899 goto done;
1900 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001901
1902 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001904 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001905 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001906
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001907 PREV.br_startblock = nullstartblock(da_new);
1908 PREV.br_blockcount = temp;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001909 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001910 xfs_iext_next(ifp, &bma->icur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 break;
1912
1913 case 0:
1914 /*
1915 * Filling in the middle part of a previous delayed allocation.
1916 * Contiguity is impossible here.
1917 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001918 *
1919 * We start with a delayed allocation:
1920 *
1921 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1922 * PREV @ idx
1923 *
1924 * and we are allocating:
1925 * +rrrrrrrrrrrrrrrrr+
1926 * new
1927 *
1928 * and we set it up for insertion as:
1929 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1930 * new
1931 * PREV @ idx LEFT RIGHT
1932 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001934 old = PREV;
1935
1936 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001937 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001938
1939 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001940 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001941 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001942 RIGHT.br_blockcount =
1943 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1944 RIGHT.br_startblock =
1945 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1946 RIGHT.br_blockcount));
1947
1948 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001949 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1950 PREV.br_startblock =
1951 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1952 PREV.br_blockcount));
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001953 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001954
Christoph Hellwigb2b17122017-11-03 10:34:43 -07001955 xfs_iext_next(ifp, &bma->icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07001956 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1957 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001958 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001959
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1962 else {
1963 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001964 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001967 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 error = xfs_btree_insert(bma->cur, &i);
1969 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001971 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001973
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001974 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001976 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001977 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 rval |= tmp_rval;
1979 if (error)
1980 goto done;
1981 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001982
1983 da_new = startblockval(PREV.br_startblock) +
1984 startblockval(RIGHT.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 break;
1986
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001987 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1988 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1989 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1990 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1991 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1992 case BMAP_LEFT_CONTIG:
1993 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /*
1995 * These cases are all impossible.
1996 */
1997 ASSERT(0);
1998 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001999
Darrick J. Wong9c194642016-08-03 12:16:05 +10002000 /* add reverse mapping */
2001 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2002 if (error)
2003 goto done;
2004
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002005 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002006 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002007 int tmp_logflags; /* partial log flag return val */
2008
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 ASSERT(bma->cur == NULL);
2010 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002011 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002012 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002013 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002014 if (error)
2015 goto done;
2016 }
2017
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002018 if (bma->cur) {
2019 da_new += bma->cur->bc_private.b.allocated;
2020 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002021 }
2022
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002023 /* adjust for changes in reserved delayed indirect blocks */
2024 if (da_new != da_old) {
2025 ASSERT(state == 0 || da_new < da_old);
2026 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2027 false);
2028 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002030 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002032 if (whichfork != XFS_COW_FORK)
2033 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return error;
2035#undef LEFT
2036#undef RIGHT
2037#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002041 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
2043STATIC int /* error */
2044xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002045 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002047 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002048 struct xfs_iext_cursor *icur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002050 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002051 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002052 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002053 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002058 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2061 /* left is 0, right is 1, prev is 2 */
2062 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002063 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002064 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002065 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002067 *logflagsp = 0;
2068
2069 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002070 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002071
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002072 ASSERT(!isnullstartblock(new->br_startblock));
2073
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002074 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076#define LEFT r[0]
2077#define RIGHT r[1]
2078#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /*
2081 * Set up a bunch of variables to make the tests simpler.
2082 */
2083 error = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002084 xfs_iext_get_extent(ifp, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002085 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 new_endoff = new->br_startoff + new->br_blockcount;
2087 ASSERT(PREV.br_startoff <= new->br_startoff);
2088 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /*
2091 * Set flags determining what part of the previous oldext allocation
2092 * extent is being replaced by a newext allocation.
2093 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002094 if (PREV.br_startoff == new->br_startoff)
2095 state |= BMAP_LEFT_FILLING;
2096 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2097 state |= BMAP_RIGHT_FILLING;
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /*
2100 * Check and set flags if this segment has a left neighbor.
2101 * Don't set contiguous if the combined extent would be too large.
2102 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002103 if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002104 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002105 if (isnullstartblock(LEFT.br_startblock))
2106 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002108
2109 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2110 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2111 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002112 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002113 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2114 state |= BMAP_LEFT_CONTIG;
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /*
2117 * Check and set flags if this segment has a right neighbor.
2118 * Don't set contiguous if the combined extent would be too large.
2119 * Also check for all-three-contiguous being too large.
2120 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002121 if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002122 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002123 if (isnullstartblock(RIGHT.br_startblock))
2124 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002126
2127 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2128 new_endoff == RIGHT.br_startoff &&
2129 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002130 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002131 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2132 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2133 BMAP_RIGHT_FILLING)) !=
2134 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2135 BMAP_RIGHT_FILLING) ||
2136 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2137 <= MAXEXTLEN))
2138 state |= BMAP_RIGHT_CONTIG;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /*
2141 * Switch out based on the FILLING and CONTIG state bits.
2142 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002143 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2144 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2145 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2146 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /*
2148 * Setting all of a previous oldext extent to newext.
2149 * The left and right neighbors are both contiguous with new.
2150 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002151 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002152
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002153 xfs_iext_remove(ip, icur, state);
2154 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002155 xfs_iext_prev(ifp, icur);
2156 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002157 XFS_IFORK_NEXT_SET(ip, whichfork,
2158 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (cur == NULL)
2160 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2161 else {
2162 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002163 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2164 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002166 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002167 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002169 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002170 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002172 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002173 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002175 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002176 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002178 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002179 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002180 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 goto done;
2182 }
2183 break;
2184
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002185 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /*
2187 * Setting all of a previous oldext extent to newext.
2188 * The left neighbor is contiguous, the right is not.
2189 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002190 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002191
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002192 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002193 xfs_iext_prev(ifp, icur);
2194 xfs_iext_update_extent(ip, state, icur, &LEFT);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002195 XFS_IFORK_NEXT_SET(ip, whichfork,
2196 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (cur == NULL)
2198 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2199 else {
2200 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002201 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2202 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002204 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002205 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002207 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002208 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002210 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002211 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002212 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 goto done;
2214 }
2215 break;
2216
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002217 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /*
2219 * Setting all of a previous oldext extent to newext.
2220 * The right neighbor is contiguous, the left is not.
2221 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002222 PREV.br_blockcount += RIGHT.br_blockcount;
2223 PREV.br_state = new->br_state;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002224
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002225 xfs_iext_next(ifp, icur);
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002226 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002227 xfs_iext_prev(ifp, icur);
2228 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002229
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002230 XFS_IFORK_NEXT_SET(ip, whichfork,
2231 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (cur == NULL)
2233 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2234 else {
2235 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002236 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2237 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002239 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002240 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002242 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002243 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002245 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002246 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002247 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 goto done;
2249 }
2250 break;
2251
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002252 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 /*
2254 * Setting all of a previous oldext extent to newext.
2255 * Neither the left nor right neighbors are contiguous with
2256 * the new one.
2257 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002258 PREV.br_state = new->br_state;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002259 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (cur == NULL)
2262 rval = XFS_ILOG_DEXT;
2263 else {
2264 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002265 error = xfs_bmbt_lookup_eq(cur, new, &i);
2266 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002268 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002269 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002270 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 goto done;
2272 }
2273 break;
2274
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002275 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 /*
2277 * Setting the first part of a previous oldext extent to newext.
2278 * The left neighbor is contiguous.
2279 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002280 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002281
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002282 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002283 PREV.br_startoff += new->br_blockcount;
2284 PREV.br_startblock += new->br_blockcount;
2285 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002286
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002287 xfs_iext_update_extent(ip, state, icur, &PREV);
2288 xfs_iext_prev(ifp, icur);
2289 xfs_iext_update_extent(ip, state, icur, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (cur == NULL)
2292 rval = XFS_ILOG_DEXT;
2293 else {
2294 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002295 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002298 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002299 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002300 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002302 error = xfs_btree_decrement(cur, 0, &i);
2303 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002305 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002306 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 goto done;
2308 }
2309 break;
2310
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002311 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Setting the first part of a previous oldext extent to newext.
2314 * The left neighbor is not contiguous.
2315 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002316 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002317 PREV.br_startoff += new->br_blockcount;
2318 PREV.br_startblock += new->br_blockcount;
2319 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002320
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002321 xfs_iext_update_extent(ip, state, icur, &PREV);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002322 xfs_iext_insert(ip, icur, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002323 XFS_IFORK_NEXT_SET(ip, whichfork,
2324 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (cur == NULL)
2326 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2327 else {
2328 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002329 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002330 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002332 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002333 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002334 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 goto done;
2336 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002337 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002339 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341 break;
2342
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002343 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 /*
2345 * Setting the last part of a previous oldext extent to newext.
2346 * The right neighbor is contiguous with the new allocation.
2347 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002348 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002349 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002350
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002351 RIGHT.br_startoff = new->br_startoff;
2352 RIGHT.br_startblock = new->br_startblock;
2353 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwiga6818472017-11-03 10:34:40 -07002354
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002355 xfs_iext_update_extent(ip, state, icur, &PREV);
2356 xfs_iext_next(ifp, icur);
2357 xfs_iext_update_extent(ip, state, icur, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (cur == NULL)
2360 rval = XFS_ILOG_DEXT;
2361 else {
2362 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002363 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002366 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002367 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002370 error = xfs_btree_increment(cur, 0, &i);
2371 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002373 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002374 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 goto done;
2376 }
2377 break;
2378
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002379 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /*
2381 * Setting the last part of a previous oldext extent to newext.
2382 * The right neighbor is not contiguous.
2383 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002384 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002385 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002386
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002387 xfs_iext_update_extent(ip, state, icur, &PREV);
2388 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002389 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002390
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002391 XFS_IFORK_NEXT_SET(ip, whichfork,
2392 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (cur == NULL)
2394 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2395 else {
2396 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002397 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002398 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002400 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002401 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002402 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002404 error = xfs_bmbt_lookup_eq(cur, new, &i);
2405 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002407 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002408 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002410 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412 break;
2413
2414 case 0:
2415 /*
2416 * Setting the middle part of a previous oldext extent to
2417 * newext. Contiguity is impossible here.
2418 * One extent becomes three extents.
2419 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002420 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002421 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 r[0] = *new;
2424 r[1].br_startoff = new_endoff;
2425 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002426 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002428 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002429
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002430 xfs_iext_update_extent(ip, state, icur, &PREV);
2431 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002432 xfs_iext_insert(ip, icur, &r[1], state);
2433 xfs_iext_insert(ip, icur, &r[0], state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002434
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002435 XFS_IFORK_NEXT_SET(ip, whichfork,
2436 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (cur == NULL)
2438 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2439 else {
2440 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002441 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002444 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002446 error = xfs_bmbt_update(cur, &r[1]);
2447 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto done;
2449 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002451 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002453 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002454 /*
2455 * Reset the cursor to the position of the new extent
2456 * we are about to insert as we can't trust it after
2457 * the previous insert.
2458 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002459 error = xfs_bmbt_lookup_eq(cur, new, &i);
2460 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002462 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002464 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002466 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468 break;
2469
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002470 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2471 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2472 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2473 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2474 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2475 case BMAP_LEFT_CONTIG:
2476 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /*
2478 * These cases are all impossible.
2479 */
2480 ASSERT(0);
2481 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002482
Darrick J. Wong9c194642016-08-03 12:16:05 +10002483 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002484 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002485 if (error)
2486 goto done;
2487
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002488 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002489 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002490 int tmp_logflags; /* partial log flag return val */
2491
2492 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002493 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002494 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002495 *logflagsp |= tmp_logflags;
2496 if (error)
2497 goto done;
2498 }
2499
2500 /* clear out the allocated field, done with it now in any case. */
2501 if (cur) {
2502 cur->bc_private.b.allocated = 0;
2503 *curp = cur;
2504 }
2505
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002506 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002508 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return error;
2510#undef LEFT
2511#undef RIGHT
2512#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002516 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002518STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519xfs_bmap_add_extent_hole_delay(
2520 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002521 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002522 struct xfs_iext_cursor *icur,
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002523 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002525 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2527 xfs_filblks_t newlen=0; /* new indirect size */
2528 xfs_filblks_t oldlen=0; /* old indirect size */
2529 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002530 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002531 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002533 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002534 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /*
2537 * Check and set flags if this segment has a left neighbor
2538 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002539 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002540 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002541 if (isnullstartblock(left.br_startblock))
2542 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /*
2546 * Check and set flags if the current (right) segment exists.
2547 * If it doesn't exist, we're converting the hole at end-of-file.
2548 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002549 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002550 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002551 if (isnullstartblock(right.br_startblock))
2552 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /*
2556 * Set contiguity flags on the left and right neighbors.
2557 * Don't let extents get too large, even if the pieces are contiguous.
2558 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002559 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2560 left.br_startoff + left.br_blockcount == new->br_startoff &&
2561 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2562 state |= BMAP_LEFT_CONTIG;
2563
2564 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2565 new->br_startoff + new->br_blockcount == right.br_startoff &&
2566 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2567 (!(state & BMAP_LEFT_CONTIG) ||
2568 (left.br_blockcount + new->br_blockcount +
2569 right.br_blockcount <= MAXEXTLEN)))
2570 state |= BMAP_RIGHT_CONTIG;
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * Switch out based on the contiguity flags.
2574 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002575 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2576 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /*
2578 * New allocation is contiguous with delayed allocations
2579 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002580 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 */
2582 temp = left.br_blockcount + new->br_blockcount +
2583 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002584
Eric Sandeen9d87c312009-01-14 23:22:07 -06002585 oldlen = startblockval(left.br_startblock) +
2586 startblockval(new->br_startblock) +
2587 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002588 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2589 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002590 left.br_startblock = nullstartblock(newlen);
2591 left.br_blockcount = temp;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002592
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002593 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002594 xfs_iext_prev(ifp, icur);
2595 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 break;
2597
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002598 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /*
2600 * New allocation is contiguous with a delayed allocation
2601 * on the left.
2602 * Merge the new allocation with the left neighbor.
2603 */
2604 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002605
Eric Sandeen9d87c312009-01-14 23:22:07 -06002606 oldlen = startblockval(left.br_startblock) +
2607 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002608 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2609 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002610 left.br_blockcount = temp;
2611 left.br_startblock = nullstartblock(newlen);
Christoph Hellwig41d196f2017-11-03 10:34:39 -07002612
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002613 xfs_iext_prev(ifp, icur);
2614 xfs_iext_update_extent(ip, state, icur, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 break;
2616
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002617 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 /*
2619 * New allocation is contiguous with a delayed allocation
2620 * on the right.
2621 * Merge the new allocation with the right neighbor.
2622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002624 oldlen = startblockval(new->br_startblock) +
2625 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002626 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2627 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002628 right.br_startoff = new->br_startoff;
2629 right.br_startblock = nullstartblock(newlen);
2630 right.br_blockcount = temp;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002631 xfs_iext_update_extent(ip, state, icur, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 break;
2633
2634 case 0:
2635 /*
2636 * New allocation is not contiguous with another
2637 * delayed allocation.
2638 * Insert a new entry.
2639 */
2640 oldlen = newlen = 0;
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002641 xfs_iext_insert(ip, icur, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 break;
2643 }
2644 if (oldlen != newlen) {
2645 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002646 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2647 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /*
2649 * Nothing to do for disk quota accounting here.
2650 */
2651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
2654/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002655 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 */
2657STATIC int /* error */
2658xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002659 struct xfs_trans *tp,
2660 struct xfs_inode *ip,
2661 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002662 struct xfs_iext_cursor *icur,
Christoph Hellwig6d045582017-04-11 16:45:54 -07002663 struct xfs_btree_cur **curp,
2664 struct xfs_bmbt_irec *new,
2665 xfs_fsblock_t *first,
2666 struct xfs_defer_ops *dfops,
2667 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002669 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2670 struct xfs_mount *mp = ip->i_mount;
2671 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2675 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002676 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002677 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002678 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002680 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002681 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002682
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002683 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 /*
2686 * Check and set flags if this segment has a left neighbor.
2687 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002688 if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002689 state |= BMAP_LEFT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002690 if (isnullstartblock(left.br_startblock))
2691 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /*
2695 * Check and set flags if this segment has a current value.
2696 * Not true if we're inserting into the "hole" at eof.
2697 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002698 if (xfs_iext_get_extent(ifp, icur, &right)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002699 state |= BMAP_RIGHT_VALID;
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002700 if (isnullstartblock(right.br_startblock))
2701 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /*
2705 * We're inserting a real allocation between "left" and "right".
2706 * Set the contiguity flags. Don't let extents get too large.
2707 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002708 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2709 left.br_startoff + left.br_blockcount == new->br_startoff &&
2710 left.br_startblock + left.br_blockcount == new->br_startblock &&
2711 left.br_state == new->br_state &&
2712 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2713 state |= BMAP_LEFT_CONTIG;
2714
2715 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2716 new->br_startoff + new->br_blockcount == right.br_startoff &&
2717 new->br_startblock + new->br_blockcount == right.br_startblock &&
2718 new->br_state == right.br_state &&
2719 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2720 (!(state & BMAP_LEFT_CONTIG) ||
2721 left.br_blockcount + new->br_blockcount +
2722 right.br_blockcount <= MAXEXTLEN))
2723 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002725 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /*
2727 * Select which case we're in here, and implement it.
2728 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002729 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2730 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 /*
2732 * New allocation is contiguous with real allocations on the
2733 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002734 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002736 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002737
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07002738 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002739 xfs_iext_prev(ifp, icur);
2740 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002741
Christoph Hellwig6d045582017-04-11 16:45:54 -07002742 XFS_IFORK_NEXT_SET(ip, whichfork,
2743 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2744 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002745 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 } else {
2747 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002748 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002749 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002750 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002752 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002753 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002754 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002755 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002756 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002757 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002758 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002759 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002760 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002761 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002762 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002766 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /*
2768 * New allocation is contiguous with a real allocation
2769 * on the left.
2770 * Merge the new allocation with the left neighbor.
2771 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002772 old = left;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002773 left.br_blockcount += new->br_blockcount;
Christoph Hellwig1d2e0082017-11-03 10:34:40 -07002774
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002775 xfs_iext_prev(ifp, icur);
2776 xfs_iext_update_extent(ip, state, icur, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002777
Christoph Hellwig6d045582017-04-11 16:45:54 -07002778 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002779 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002780 } else {
2781 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002782 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002783 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002784 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002785 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002786 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002787 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002788 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002792 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /*
2794 * New allocation is contiguous with a real allocation
2795 * on the right.
2796 * Merge the new allocation with the right neighbor.
2797 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002798 old = right;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07002799
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002800 right.br_startoff = new->br_startoff;
2801 right.br_startblock = new->br_startblock;
2802 right.br_blockcount += new->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07002803 xfs_iext_update_extent(ip, state, icur, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002804
Christoph Hellwig6d045582017-04-11 16:45:54 -07002805 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002806 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002807 } else {
2808 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002809 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002810 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002811 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002813 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002814 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002815 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 case 0:
2820 /*
2821 * New allocation is not contiguous with another
2822 * real allocation.
2823 * Insert a new entry.
2824 */
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07002825 xfs_iext_insert(ip, icur, new, state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002826 XFS_IFORK_NEXT_SET(ip, whichfork,
2827 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2828 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002829 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002830 } else {
2831 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002832 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002833 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002834 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002835 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002836 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002837 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002838 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002839 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002843
Darrick J. Wong9c194642016-08-03 12:16:05 +10002844 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002845 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002846 if (error)
2847 goto done;
2848
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002849 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002851 int tmp_logflags; /* partial log flag return val */
2852
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 ASSERT(cur == NULL);
2854 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002855 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002856 *logflagsp |= tmp_logflags;
2857 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002858 if (error)
2859 goto done;
2860 }
2861
2862 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002863 if (cur)
2864 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002865
Christoph Hellwig6d045582017-04-11 16:45:54 -07002866 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002867done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002868 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002869 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
Nathan Scottdd9f4382006-01-11 15:28:28 +11002872/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002873 * Functions used in the extent read, allocate and remove paths
2874 */
2875
2876/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002877 * Adjust the size of the new extent based on di_extsize and rt extsize.
2878 */
Dave Chinner68988112013-08-12 20:49:42 +10002879int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002880xfs_bmap_extsize_align(
2881 xfs_mount_t *mp,
2882 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2883 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2884 xfs_extlen_t extsz, /* align to this extent size */
2885 int rt, /* is this a realtime inode? */
2886 int eof, /* is extent at end-of-file? */
2887 int delay, /* creating delalloc extent? */
2888 int convert, /* overwriting unwritten extent? */
2889 xfs_fileoff_t *offp, /* in/out: aligned offset */
2890 xfs_extlen_t *lenp) /* in/out: aligned length */
2891{
2892 xfs_fileoff_t orig_off; /* original offset */
2893 xfs_extlen_t orig_alen; /* original length */
2894 xfs_fileoff_t orig_end; /* original off+len */
2895 xfs_fileoff_t nexto; /* next file offset */
2896 xfs_fileoff_t prevo; /* previous file offset */
2897 xfs_fileoff_t align_off; /* temp for offset */
2898 xfs_extlen_t align_alen; /* temp for length */
2899 xfs_extlen_t temp; /* temp for calculations */
2900
2901 if (convert)
2902 return 0;
2903
2904 orig_off = align_off = *offp;
2905 orig_alen = align_alen = *lenp;
2906 orig_end = orig_off + orig_alen;
2907
2908 /*
2909 * If this request overlaps an existing extent, then don't
2910 * attempt to perform any additional alignment.
2911 */
2912 if (!delay && !eof &&
2913 (orig_off >= gotp->br_startoff) &&
2914 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2915 return 0;
2916 }
2917
2918 /*
2919 * If the file offset is unaligned vs. the extent size
2920 * we need to align it. This will be possible unless
2921 * the file was previously written with a kernel that didn't
2922 * perform this alignment, or if a truncate shot us in the
2923 * foot.
2924 */
2925 temp = do_mod(orig_off, extsz);
2926 if (temp) {
2927 align_alen += temp;
2928 align_off -= temp;
2929 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002930
2931 /* Same adjustment for the end of the requested area. */
2932 temp = (align_alen % extsz);
2933 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002934 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002935
2936 /*
2937 * For large extent hint sizes, the aligned extent might be larger than
2938 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2939 * the length back under MAXEXTLEN. The outer allocation loops handle
2940 * short allocation just fine, so it is safe to do this. We only want to
2941 * do it when we are forced to, though, because it means more allocation
2942 * operations are required.
2943 */
2944 while (align_alen > MAXEXTLEN)
2945 align_alen -= extsz;
2946 ASSERT(align_alen <= MAXEXTLEN);
2947
Nathan Scottdd9f4382006-01-11 15:28:28 +11002948 /*
2949 * If the previous block overlaps with this proposed allocation
2950 * then move the start forward without adjusting the length.
2951 */
2952 if (prevp->br_startoff != NULLFILEOFF) {
2953 if (prevp->br_startblock == HOLESTARTBLOCK)
2954 prevo = prevp->br_startoff;
2955 else
2956 prevo = prevp->br_startoff + prevp->br_blockcount;
2957 } else
2958 prevo = 0;
2959 if (align_off != orig_off && align_off < prevo)
2960 align_off = prevo;
2961 /*
2962 * If the next block overlaps with this proposed allocation
2963 * then move the start back without adjusting the length,
2964 * but not before offset 0.
2965 * This may of course make the start overlap previous block,
2966 * and if we hit the offset 0 limit then the next block
2967 * can still overlap too.
2968 */
2969 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2970 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2971 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2972 nexto = gotp->br_startoff + gotp->br_blockcount;
2973 else
2974 nexto = gotp->br_startoff;
2975 } else
2976 nexto = NULLFILEOFF;
2977 if (!eof &&
2978 align_off + align_alen != orig_end &&
2979 align_off + align_alen > nexto)
2980 align_off = nexto > align_alen ? nexto - align_alen : 0;
2981 /*
2982 * If we're now overlapping the next or previous extent that
2983 * means we can't fit an extsz piece in this hole. Just move
2984 * the start forward to the first valid spot and set
2985 * the length so we hit the end.
2986 */
2987 if (align_off != orig_off && align_off < prevo)
2988 align_off = prevo;
2989 if (align_off + align_alen != orig_end &&
2990 align_off + align_alen > nexto &&
2991 nexto != NULLFILEOFF) {
2992 ASSERT(nexto > prevo);
2993 align_alen = nexto - align_off;
2994 }
2995
2996 /*
2997 * If realtime, and the result isn't a multiple of the realtime
2998 * extent size we need to remove blocks until it is.
2999 */
3000 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3001 /*
3002 * We're not covering the original request, or
3003 * we won't be able to once we fix the length.
3004 */
3005 if (orig_off < align_off ||
3006 orig_end > align_off + align_alen ||
3007 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003008 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003009 /*
3010 * Try to fix it by moving the start up.
3011 */
3012 if (align_off + temp <= orig_off) {
3013 align_alen -= temp;
3014 align_off += temp;
3015 }
3016 /*
3017 * Try to fix it by moving the end in.
3018 */
3019 else if (align_off + align_alen - temp >= orig_end)
3020 align_alen -= temp;
3021 /*
3022 * Set the start to the minimum then trim the length.
3023 */
3024 else {
3025 align_alen -= orig_off - align_off;
3026 align_off = orig_off;
3027 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3028 }
3029 /*
3030 * Result doesn't cover the request, fail it.
3031 */
3032 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003033 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003034 } else {
3035 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003036 /* see MAXEXTLEN handling above */
3037 ASSERT(orig_end <= align_off + align_alen ||
3038 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003039 }
3040
3041#ifdef DEBUG
3042 if (!eof && gotp->br_startoff != NULLFILEOFF)
3043 ASSERT(align_off + align_alen <= gotp->br_startoff);
3044 if (prevp->br_startoff != NULLFILEOFF)
3045 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3046#endif
3047
3048 *lenp = align_alen;
3049 *offp = align_off;
3050 return 0;
3051}
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053#define XFS_ALLOC_GAP_UNITS 4
3054
Dave Chinner68988112013-08-12 20:49:42 +10003055void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003056xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003057 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
3059 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3061 xfs_mount_t *mp; /* mount point structure */
3062 int nullfb; /* true if ap->firstblock isn't set */
3063 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065#define ISVALID(x,y) \
3066 (rt ? \
3067 (x) < mp->m_sb.sb_rblocks : \
3068 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3069 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3070 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003073 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003074 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3075 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003076 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 /*
3078 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003079 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003081 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3082 !isnullstartblock(ap->prev.br_startblock) &&
3083 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3084 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003085 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 /*
3087 * Adjust for the gap between prevp and us.
3088 */
Dave Chinner3a756672011-09-18 20:40:58 +00003089 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003090 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003092 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3093 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095 /*
3096 * If not at eof, then compare the two neighbor blocks.
3097 * Figure out whether either one gives us a good starting point,
3098 * and pick the better one.
3099 */
3100 else if (!ap->eof) {
3101 xfs_fsblock_t gotbno; /* right side block number */
3102 xfs_fsblock_t gotdiff=0; /* right side difference */
3103 xfs_fsblock_t prevbno; /* left side block number */
3104 xfs_fsblock_t prevdiff=0; /* left side difference */
3105
3106 /*
3107 * If there's a previous (left) block, select a requested
3108 * start block based on it.
3109 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003110 if (ap->prev.br_startoff != NULLFILEOFF &&
3111 !isnullstartblock(ap->prev.br_startblock) &&
3112 (prevbno = ap->prev.br_startblock +
3113 ap->prev.br_blockcount) &&
3114 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 /*
3116 * Calculate gap to end of previous block.
3117 */
Dave Chinner3a756672011-09-18 20:40:58 +00003118 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003119 (ap->prev.br_startoff +
3120 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 /*
3122 * Figure the startblock based on the previous block's
3123 * end and the gap size.
3124 * Heuristic!
3125 * If the gap is large relative to the piece we're
3126 * allocating, or using it gives us an invalid block
3127 * number, then just use the end of the previous block.
3128 */
Dave Chinner3a756672011-09-18 20:40:58 +00003129 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003131 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 prevbno += adjust;
3133 else
3134 prevdiff += adjust;
3135 /*
3136 * If the firstblock forbids it, can't use it,
3137 * must use default.
3138 */
3139 if (!rt && !nullfb &&
3140 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3141 prevbno = NULLFSBLOCK;
3142 }
3143 /*
3144 * No previous block or can't follow it, just default.
3145 */
3146 else
3147 prevbno = NULLFSBLOCK;
3148 /*
3149 * If there's a following (right) block, select a requested
3150 * start block based on it.
3151 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003152 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /*
3154 * Calculate gap to start of next block.
3155 */
Dave Chinner3a756672011-09-18 20:40:58 +00003156 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /*
3158 * Figure the startblock based on the next block's
3159 * start and the gap size.
3160 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003161 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 /*
3163 * Heuristic!
3164 * If the gap is large relative to the piece we're
3165 * allocating, or using it gives us an invalid block
3166 * number, then just use the start of the next block
3167 * offset by our length.
3168 */
Dave Chinner3a756672011-09-18 20:40:58 +00003169 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 ISVALID(gotbno - gotdiff, gotbno))
3171 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003172 else if (ISVALID(gotbno - ap->length, gotbno)) {
3173 gotbno -= ap->length;
3174 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 } else
3176 gotdiff += adjust;
3177 /*
3178 * If the firstblock forbids it, can't use it,
3179 * must use default.
3180 */
3181 if (!rt && !nullfb &&
3182 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3183 gotbno = NULLFSBLOCK;
3184 }
3185 /*
3186 * No next block, just default.
3187 */
3188 else
3189 gotbno = NULLFSBLOCK;
3190 /*
3191 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003192 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 */
3194 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003195 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003197 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003199 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003201#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003202}
3203
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003204static int
3205xfs_bmap_longest_free_extent(
3206 struct xfs_trans *tp,
3207 xfs_agnumber_t ag,
3208 xfs_extlen_t *blen,
3209 int *notinit)
3210{
3211 struct xfs_mount *mp = tp->t_mountp;
3212 struct xfs_perag *pag;
3213 xfs_extlen_t longest;
3214 int error = 0;
3215
3216 pag = xfs_perag_get(mp, ag);
3217 if (!pag->pagf_init) {
3218 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3219 if (error)
3220 goto out;
3221
3222 if (!pag->pagf_init) {
3223 *notinit = 1;
3224 goto out;
3225 }
3226 }
3227
Eric Sandeena1f69412018-04-06 10:09:42 -07003228 longest = xfs_alloc_longest_free_extent(pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003229 xfs_alloc_min_freelist(mp, pag),
3230 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003231 if (*blen < longest)
3232 *blen = longest;
3233
3234out:
3235 xfs_perag_put(pag);
3236 return error;
3237}
3238
3239static void
3240xfs_bmap_select_minlen(
3241 struct xfs_bmalloca *ap,
3242 struct xfs_alloc_arg *args,
3243 xfs_extlen_t *blen,
3244 int notinit)
3245{
3246 if (notinit || *blen < ap->minlen) {
3247 /*
3248 * Since we did a BUF_TRYLOCK above, it is possible that
3249 * there is space for this request.
3250 */
3251 args->minlen = ap->minlen;
3252 } else if (*blen < args->maxlen) {
3253 /*
3254 * If the best seen length is less than the request length,
3255 * use the best as the minimum.
3256 */
3257 args->minlen = *blen;
3258 } else {
3259 /*
3260 * Otherwise we've seen an extent as big as maxlen, use that
3261 * as the minimum.
3262 */
3263 args->minlen = args->maxlen;
3264 }
3265}
3266
Nathan Scotta365bdd2006-03-14 13:34:16 +11003267STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003268xfs_bmap_btalloc_nullfb(
3269 struct xfs_bmalloca *ap,
3270 struct xfs_alloc_arg *args,
3271 xfs_extlen_t *blen)
3272{
3273 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003274 xfs_agnumber_t ag, startag;
3275 int notinit = 0;
3276 int error;
3277
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003278 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003279 args->total = ap->total;
3280
Christoph Hellwigc467c042010-02-15 23:34:42 +00003281 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3282 if (startag == NULLAGNUMBER)
3283 startag = ag = 0;
3284
Dave Chinner14b064c2011-01-27 12:16:28 +11003285 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003286 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3287 &notinit);
3288 if (error)
3289 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003290
Christoph Hellwigc467c042010-02-15 23:34:42 +00003291 if (++ag == mp->m_sb.sb_agcount)
3292 ag = 0;
3293 if (ag == startag)
3294 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003295 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003296
3297 xfs_bmap_select_minlen(ap, args, blen, notinit);
3298 return 0;
3299}
3300
3301STATIC int
3302xfs_bmap_btalloc_filestreams(
3303 struct xfs_bmalloca *ap,
3304 struct xfs_alloc_arg *args,
3305 xfs_extlen_t *blen)
3306{
3307 struct xfs_mount *mp = ap->ip->i_mount;
3308 xfs_agnumber_t ag;
3309 int notinit = 0;
3310 int error;
3311
3312 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3313 args->total = ap->total;
3314
3315 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3316 if (ag == NULLAGNUMBER)
3317 ag = 0;
3318
3319 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3320 if (error)
3321 return error;
3322
3323 if (*blen < args->maxlen) {
3324 error = xfs_filestream_new_ag(ap, &ag);
3325 if (error)
3326 return error;
3327
3328 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3329 &notinit);
3330 if (error)
3331 return error;
3332
3333 }
3334
3335 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003336
3337 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003338 * Set the failure fallback case to look in the selected AG as stream
3339 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003340 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003341 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003342 return 0;
3343}
3344
Darrick J. Wong751f3762018-01-25 13:58:13 -08003345/* Update all inode and quota accounting for the allocation we just did. */
3346static void
3347xfs_bmap_btalloc_accounting(
3348 struct xfs_bmalloca *ap,
3349 struct xfs_alloc_arg *args)
3350{
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08003351 if (ap->flags & XFS_BMAPI_COWFORK) {
3352 /*
3353 * COW fork blocks are in-core only and thus are treated as
3354 * in-core quota reservation (like delalloc blocks) even when
3355 * converted to real blocks. The quota reservation is not
3356 * accounted to disk until blocks are remapped to the data
3357 * fork. So if these blocks were previously delalloc, we
3358 * already have quota reservation and there's nothing to do
3359 * yet.
3360 */
3361 if (ap->wasdel)
3362 return;
3363
3364 /*
3365 * Otherwise, we've allocated blocks in a hole. The transaction
3366 * has acquired in-core quota reservation for this extent.
3367 * Rather than account these as real blocks, however, we reduce
3368 * the transaction quota reservation based on the allocation.
3369 * This essentially transfers the transaction quota reservation
3370 * to that of a delalloc extent.
3371 */
3372 ap->ip->i_delayed_blks += args->len;
3373 xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3374 -(long)args->len);
3375 return;
3376 }
3377
3378 /* data/attr fork only */
3379 ap->ip->i_d.di_nblocks += args->len;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003380 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3381 if (ap->wasdel)
3382 ap->ip->i_delayed_blks -= args->len;
3383 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3384 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3385 args->len);
3386}
3387
Christoph Hellwigc467c042010-02-15 23:34:42 +00003388STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003389xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003390 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003391{
3392 xfs_mount_t *mp; /* mount point structure */
3393 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003394 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003395 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003396 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003397 xfs_alloc_arg_t args;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003398 xfs_fileoff_t orig_offset;
3399 xfs_extlen_t orig_length;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003400 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003401 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003402 int nullfb; /* true if ap->firstblock isn't set */
3403 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003404 int tryagain;
3405 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003406 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003407
Dave Chinnera99ebf42011-12-01 11:24:20 +00003408 ASSERT(ap->length);
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003409 orig_offset = ap->offset;
3410 orig_length = ap->length;
Dave Chinnera99ebf42011-12-01 11:24:20 +00003411
Nathan Scotta365bdd2006-03-14 13:34:16 +11003412 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003413
3414 /* stripe alignment for allocation is determined by mount parameters */
3415 stripe_align = 0;
3416 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3417 stripe_align = mp->m_swidth;
3418 else if (mp->m_dalign)
3419 stripe_align = mp->m_dalign;
3420
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003421 if (ap->flags & XFS_BMAPI_COWFORK)
3422 align = xfs_get_cowextsz_hint(ap->ip);
3423 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003424 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003425 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003426 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003427 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003428 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003429 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003430 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003431 }
Dave Chinner33177f052013-12-12 16:34:36 +11003432
3433
Dave Chinner0937e0f2011-09-18 20:40:57 +00003434 nullfb = *ap->firstblock == NULLFSBLOCK;
3435 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003436 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003437 if (xfs_alloc_is_userdata(ap->datatype) &&
3438 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003439 ag = xfs_filestream_lookup_ag(ap->ip);
3440 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003441 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003442 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003443 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003444 }
3445 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003446 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003447
3448 xfs_bmap_adjacent(ap);
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003451 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 * it's in the right allocation group.
3453 */
Dave Chinner3a756672011-09-18 20:40:58 +00003454 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 ;
3456 else
Dave Chinner3a756672011-09-18 20:40:58 +00003457 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 * Normal allocation, done through xfs_alloc_vextent.
3460 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003461 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003462 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003463 args.tp = ap->tp;
3464 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003465 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003466 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003467
3468 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003469 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003470 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003471 blen = 0;
3472 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003473 /*
3474 * Search for an allocation group with a single extent large
3475 * enough for the request. If one isn't found, then adjust
3476 * the minimum allocation size to the largest space found.
3477 */
Dave Chinner292378e2016-09-26 08:21:28 +10003478 if (xfs_alloc_is_userdata(ap->datatype) &&
3479 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003480 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3481 else
3482 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003483 if (error)
3484 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003485 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003486 if (xfs_inode_is_filestream(ap->ip))
3487 args.type = XFS_ALLOCTYPE_FIRST_AG;
3488 else
3489 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003490 args.total = args.minlen = ap->minlen;
3491 } else {
3492 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3493 args.total = ap->total;
3494 args.minlen = ap->minlen;
3495 }
David Chinner957d0eb2007-06-18 16:50:37 +10003496 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003497 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003498 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003499 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003500 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003501 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003502 args.prod = 1;
3503 args.mod = 0;
3504 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003505 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003506 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003507 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3508 }
3509 /*
3510 * If we are not low on available data blocks, and the
3511 * underlying logical volume manager is a stripe, and
3512 * the file offset is zero then try to allocate data
3513 * blocks on stripe unit boundary.
3514 * NOTE: ap->aeof is only set if the allocation length
3515 * is >= the stripe unit and the allocation offset is
3516 * at the end of file.
3517 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003518 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003519 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003520 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003521 atype = args.type;
3522 isaligned = 1;
3523 /*
3524 * Adjust for alignment
3525 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003526 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003527 args.minlen = blen - args.alignment;
3528 args.minalignslop = 0;
3529 } else {
3530 /*
3531 * First try an exact bno allocation.
3532 * If it fails then do a near or start bno
3533 * allocation with alignment turned on.
3534 */
3535 atype = args.type;
3536 tryagain = 1;
3537 args.type = XFS_ALLOCTYPE_THIS_BNO;
3538 args.alignment = 1;
3539 /*
3540 * Compute the minlen+alignment for the
3541 * next case. Set slop so that the value
3542 * of minlen+alignment+slop doesn't go up
3543 * between the calls.
3544 */
Dave Chinner33177f052013-12-12 16:34:36 +11003545 if (blen > stripe_align && blen <= args.maxlen)
3546 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003547 else
3548 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003549 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003550 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003551 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003552 args.minlen - 1;
3553 else
3554 args.minalignslop = 0;
3555 }
3556 } else {
3557 args.alignment = 1;
3558 args.minalignslop = 0;
3559 }
3560 args.minleft = ap->minleft;
3561 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003562 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003563 args.datatype = ap->datatype;
3564 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003565 args.ip = ap->ip;
3566
3567 error = xfs_alloc_vextent(&args);
3568 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003569 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003570
Nathan Scotta365bdd2006-03-14 13:34:16 +11003571 if (tryagain && args.fsbno == NULLFSBLOCK) {
3572 /*
3573 * Exact allocation failed. Now try with alignment
3574 * turned on.
3575 */
3576 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003577 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003578 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003579 args.minlen = nextminlen;
3580 args.minalignslop = 0;
3581 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 if ((error = xfs_alloc_vextent(&args)))
3583 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003584 }
3585 if (isaligned && args.fsbno == NULLFSBLOCK) {
3586 /*
3587 * allocation failed, so turn off alignment and
3588 * try again.
3589 */
3590 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003591 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 args.alignment = 0;
3593 if ((error = xfs_alloc_vextent(&args)))
3594 return error;
3595 }
3596 if (args.fsbno == NULLFSBLOCK && nullfb &&
3597 args.minlen > ap->minlen) {
3598 args.minlen = ap->minlen;
3599 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003600 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003601 if ((error = xfs_alloc_vextent(&args)))
3602 return error;
3603 }
3604 if (args.fsbno == NULLFSBLOCK && nullfb) {
3605 args.fsbno = 0;
3606 args.type = XFS_ALLOCTYPE_FIRST_AG;
3607 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608 if ((error = xfs_alloc_vextent(&args)))
3609 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003610 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 }
3612 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003613 /*
3614 * check the allocation happened at the same or higher AG than
3615 * the first block that was allocated.
3616 */
3617 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003618 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3619 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003620
Dave Chinner3a756672011-09-18 20:40:58 +00003621 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003622 if (*ap->firstblock == NULLFSBLOCK)
3623 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003624 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003625 ap->length = args.len;
Darrick J. Wong6d8a45c2018-01-19 17:47:36 -08003626 /*
3627 * If the extent size hint is active, we tried to round the
3628 * caller's allocation request offset down to extsz and the
3629 * length up to another extsz boundary. If we found a free
3630 * extent we mapped it in starting at this new offset. If the
3631 * newly mapped space isn't long enough to cover any of the
3632 * range of offsets that was originally requested, move the
3633 * mapping up so that we can fill as much of the caller's
3634 * original request as possible. Free space is apparently
3635 * very fragmented so we're unlikely to be able to satisfy the
3636 * hints anyway.
3637 */
3638 if (ap->length <= orig_length)
3639 ap->offset = orig_offset;
3640 else if (ap->offset + ap->length < orig_offset + orig_length)
3641 ap->offset = orig_offset + orig_length - ap->length;
Darrick J. Wong751f3762018-01-25 13:58:13 -08003642 xfs_bmap_btalloc_accounting(ap, &args);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003643 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003644 ap->blkno = NULLFSBLOCK;
3645 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
3647 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003648}
3649
3650/*
3651 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3652 * It figures out where to ask the underlying allocator to put the new extent.
3653 */
3654STATIC int
3655xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003656 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657{
Dave Chinner292378e2016-09-26 08:21:28 +10003658 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3659 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003660 return xfs_bmap_rtalloc(ap);
3661 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003664/* Trim extent to fit a logical block range. */
3665void
3666xfs_trim_extent(
3667 struct xfs_bmbt_irec *irec,
3668 xfs_fileoff_t bno,
3669 xfs_filblks_t len)
3670{
3671 xfs_fileoff_t distance;
3672 xfs_fileoff_t end = bno + len;
3673
3674 if (irec->br_startoff + irec->br_blockcount <= bno ||
3675 irec->br_startoff >= end) {
3676 irec->br_blockcount = 0;
3677 return;
3678 }
3679
3680 if (irec->br_startoff < bno) {
3681 distance = bno - irec->br_startoff;
3682 if (isnullstartblock(irec->br_startblock))
3683 irec->br_startblock = DELAYSTARTBLOCK;
3684 if (irec->br_startblock != DELAYSTARTBLOCK &&
3685 irec->br_startblock != HOLESTARTBLOCK)
3686 irec->br_startblock += distance;
3687 irec->br_startoff += distance;
3688 irec->br_blockcount -= distance;
3689 }
3690
3691 if (end < irec->br_startoff + irec->br_blockcount) {
3692 distance = irec->br_startoff + irec->br_blockcount - end;
3693 irec->br_blockcount -= distance;
3694 }
3695}
3696
Brian Foster40214d12017-10-13 09:47:46 -07003697/* trim extent to within eof */
3698void
3699xfs_trim_extent_eof(
3700 struct xfs_bmbt_irec *irec,
3701 struct xfs_inode *ip)
3702
3703{
3704 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3705 i_size_read(VFS_I(ip))));
3706}
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003709 * Trim the returned map to the required bounds
3710 */
3711STATIC void
3712xfs_bmapi_trim_map(
3713 struct xfs_bmbt_irec *mval,
3714 struct xfs_bmbt_irec *got,
3715 xfs_fileoff_t *bno,
3716 xfs_filblks_t len,
3717 xfs_fileoff_t obno,
3718 xfs_fileoff_t end,
3719 int n,
3720 int flags)
3721{
3722 if ((flags & XFS_BMAPI_ENTIRE) ||
3723 got->br_startoff + got->br_blockcount <= obno) {
3724 *mval = *got;
3725 if (isnullstartblock(got->br_startblock))
3726 mval->br_startblock = DELAYSTARTBLOCK;
3727 return;
3728 }
3729
3730 if (obno > *bno)
3731 *bno = obno;
3732 ASSERT((*bno >= obno) || (n == 0));
3733 ASSERT(*bno < end);
3734 mval->br_startoff = *bno;
3735 if (isnullstartblock(got->br_startblock))
3736 mval->br_startblock = DELAYSTARTBLOCK;
3737 else
3738 mval->br_startblock = got->br_startblock +
3739 (*bno - got->br_startoff);
3740 /*
3741 * Return the minimum of what we got and what we asked for for
3742 * the length. We can use the len variable here because it is
3743 * modified below and we could have been there before coming
3744 * here if the first part of the allocation didn't overlap what
3745 * was asked for.
3746 */
3747 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3748 got->br_blockcount - (*bno - got->br_startoff));
3749 mval->br_state = got->br_state;
3750 ASSERT(mval->br_blockcount <= len);
3751 return;
3752}
3753
3754/*
3755 * Update and validate the extent map to return
3756 */
3757STATIC void
3758xfs_bmapi_update_map(
3759 struct xfs_bmbt_irec **map,
3760 xfs_fileoff_t *bno,
3761 xfs_filblks_t *len,
3762 xfs_fileoff_t obno,
3763 xfs_fileoff_t end,
3764 int *n,
3765 int flags)
3766{
3767 xfs_bmbt_irec_t *mval = *map;
3768
3769 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3770 ((mval->br_startoff + mval->br_blockcount) <= end));
3771 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3772 (mval->br_startoff < obno));
3773
3774 *bno = mval->br_startoff + mval->br_blockcount;
3775 *len = end - *bno;
3776 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3777 /* update previous map with new information */
3778 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3779 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3780 ASSERT(mval->br_state == mval[-1].br_state);
3781 mval[-1].br_blockcount = mval->br_blockcount;
3782 mval[-1].br_state = mval->br_state;
3783 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3784 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3785 mval[-1].br_startblock != HOLESTARTBLOCK &&
3786 mval->br_startblock == mval[-1].br_startblock +
3787 mval[-1].br_blockcount &&
3788 ((flags & XFS_BMAPI_IGSTATE) ||
3789 mval[-1].br_state == mval->br_state)) {
3790 ASSERT(mval->br_startoff ==
3791 mval[-1].br_startoff + mval[-1].br_blockcount);
3792 mval[-1].br_blockcount += mval->br_blockcount;
3793 } else if (*n > 0 &&
3794 mval->br_startblock == DELAYSTARTBLOCK &&
3795 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3796 mval->br_startoff ==
3797 mval[-1].br_startoff + mval[-1].br_blockcount) {
3798 mval[-1].br_blockcount += mval->br_blockcount;
3799 mval[-1].br_state = mval->br_state;
3800 } else if (!((*n == 0) &&
3801 ((mval->br_startoff + mval->br_blockcount) <=
3802 obno))) {
3803 mval++;
3804 (*n)++;
3805 }
3806 *map = mval;
3807}
3808
3809/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003810 * Map file blocks to filesystem blocks without allocation.
3811 */
3812int
3813xfs_bmapi_read(
3814 struct xfs_inode *ip,
3815 xfs_fileoff_t bno,
3816 xfs_filblks_t len,
3817 struct xfs_bmbt_irec *mval,
3818 int *nmap,
3819 int flags)
3820{
3821 struct xfs_mount *mp = ip->i_mount;
3822 struct xfs_ifork *ifp;
3823 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003824 xfs_fileoff_t obno;
3825 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003826 struct xfs_iext_cursor icur;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003827 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003828 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003829 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003830 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003831
3832 ASSERT(*nmap >= 1);
3833 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003834 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003835 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003836
3837 if (unlikely(XFS_TEST_ERROR(
3838 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3839 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003840 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003841 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003842 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003843 }
3844
3845 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003846 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003847
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003848 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003849
3850 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003851
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003852 /* No CoW fork? Return a hole. */
3853 if (whichfork == XFS_COW_FORK && !ifp) {
3854 mval->br_startoff = bno;
3855 mval->br_startblock = HOLESTARTBLOCK;
3856 mval->br_blockcount = len;
3857 mval->br_state = XFS_EXT_NORM;
3858 *nmap = 1;
3859 return 0;
3860 }
3861
Dave Chinner5c8ed202011-09-18 20:40:45 +00003862 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3863 error = xfs_iread_extents(NULL, ip, whichfork);
3864 if (error)
3865 return error;
3866 }
3867
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003868 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003869 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003870 end = bno + len;
3871 obno = bno;
3872
3873 while (bno < end && n < *nmap) {
3874 /* Reading past eof, act as though there's a hole up to end. */
3875 if (eof)
3876 got.br_startoff = end;
3877 if (got.br_startoff > bno) {
3878 /* Reading in a hole. */
3879 mval->br_startoff = bno;
3880 mval->br_startblock = HOLESTARTBLOCK;
3881 mval->br_blockcount =
3882 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3883 mval->br_state = XFS_EXT_NORM;
3884 bno += mval->br_blockcount;
3885 len -= mval->br_blockcount;
3886 mval++;
3887 n++;
3888 continue;
3889 }
3890
3891 /* set up the extent map to return. */
3892 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3893 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3894
3895 /* If we're done, stop now. */
3896 if (bno >= end || n >= *nmap)
3897 break;
3898
3899 /* Else go on to the next record. */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003900 if (!xfs_iext_next_extent(ifp, &icur, &got))
Christoph Hellwig334f3422016-11-24 11:39:43 +11003901 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003902 }
3903 *nmap = n;
3904 return 0;
3905}
3906
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003907/*
3908 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3909 * global pool and the extent inserted into the inode in-core extent tree.
3910 *
3911 * On entry, got refers to the first extent beyond the offset of the extent to
3912 * allocate or eof is specified if no such extent exists. On return, got refers
3913 * to the extent record that was inserted to the inode fork.
3914 *
3915 * Note that the allocated extent may have been merged with contiguous extents
3916 * during insertion into the inode fork. Thus, got does not reflect the current
3917 * state of the inode fork on return. If necessary, the caller can use lastx to
3918 * look up the updated record in the inode fork.
3919 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003920int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003921xfs_bmapi_reserve_delalloc(
3922 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003923 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003924 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003925 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003926 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003927 struct xfs_bmbt_irec *got,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003928 struct xfs_iext_cursor *icur,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003929 int eof)
3930{
3931 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003932 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003933 xfs_extlen_t alen;
3934 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003935 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003936 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003937
Brian Foster974ae922016-11-28 14:57:42 +11003938 /*
3939 * Cap the alloc length. Keep track of prealloc so we know whether to
3940 * tag the inode before we return.
3941 */
3942 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003943 if (!eof)
3944 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003945 if (prealloc && alen >= len)
3946 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003947
3948 /* Figure out the extent size, adjust alen */
Shan Hai6ca30722018-01-23 13:56:11 -08003949 if (whichfork == XFS_COW_FORK) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003950 struct xfs_bmbt_irec prev;
Shan Hai6ca30722018-01-23 13:56:11 -08003951 xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003952
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003953 if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003954 prev.br_startoff = NULLFILEOFF;
3955
Shan Hai6ca30722018-01-23 13:56:11 -08003956 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003957 1, 0, &aoff, &alen);
3958 ASSERT(!error);
3959 }
3960
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003961 /*
3962 * Make a transaction-less quota reservation for delayed allocation
3963 * blocks. This number gets adjusted later. We return if we haven't
3964 * allocated blocks already inside this loop.
3965 */
3966 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
Shan Hai6ca30722018-01-23 13:56:11 -08003967 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003968 if (error)
3969 return error;
3970
3971 /*
3972 * Split changing sb for alen and indlen since they could be coming
3973 * from different places.
3974 */
3975 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3976 ASSERT(indlen > 0);
3977
Shan Hai6ca30722018-01-23 13:56:11 -08003978 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003979 if (error)
3980 goto out_unreserve_quota;
3981
Dave Chinner0d485ad2015-02-23 21:22:03 +11003982 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003983 if (error)
3984 goto out_unreserve_blocks;
3985
3986
3987 ip->i_delayed_blks += alen;
3988
3989 got->br_startoff = aoff;
3990 got->br_startblock = nullstartblock(indlen);
3991 got->br_blockcount = alen;
3992 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003993
Christoph Hellwigb2b17122017-11-03 10:34:43 -07003994 xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003995
Brian Foster974ae922016-11-28 14:57:42 +11003996 /*
3997 * Tag the inode if blocks were preallocated. Note that COW fork
3998 * preallocation can occur at the start or end of the extent, even when
3999 * prealloc == 0, so we must also check the aligned offset and length.
4000 */
4001 if (whichfork == XFS_DATA_FORK && prealloc)
4002 xfs_inode_set_eofblocks_tag(ip);
4003 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4004 xfs_inode_set_cowblocks_tag(ip);
4005
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004006 return 0;
4007
4008out_unreserve_blocks:
Shan Hai6ca30722018-01-23 13:56:11 -08004009 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004010out_unreserve_quota:
4011 if (XFS_IS_QUOTA_ON(mp))
Shan Hai6ca30722018-01-23 13:56:11 -08004012 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0,
4013 XFS_QMOPT_RES_REGBLKS);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004014 return error;
4015}
4016
Dave Chinnercf11da92014-07-15 07:08:24 +10004017static int
4018xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004019 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004020{
4021 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004022 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004023 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004024 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004025 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004026
Dave Chinnera99ebf42011-12-01 11:24:20 +00004027 ASSERT(bma->length > 0);
4028
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004029 /*
4030 * For the wasdelay case, we could also just allocate the stuff asked
4031 * for in this bmap call but that wouldn't be as good.
4032 */
4033 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004034 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4035 bma->offset = bma->got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004036 xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004037 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004038 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004039 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004040 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004041 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004042 }
4043
4044 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004045 * Set the data type being allocated. For the data fork, the first data
4046 * in the file is treated differently to all other allocations. For the
4047 * attribute fork, we only need to ensure the allocated range is not on
4048 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004049 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004050 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004051 bma->datatype = XFS_ALLOC_NOBUSY;
4052 if (whichfork == XFS_DATA_FORK) {
4053 if (bma->offset == 0)
4054 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4055 else
4056 bma->datatype |= XFS_ALLOC_USERDATA;
4057 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004058 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004059 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004060 }
4061
Dave Chinnere04426b2012-10-05 11:06:59 +10004062 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004063
4064 /*
4065 * Only want to do the alignment at the eof if it is userdata and
4066 * allocation length is larger than a stripe unit.
4067 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004068 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004069 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004070 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004071 if (error)
4072 return error;
4073 }
4074
4075 error = xfs_bmap_alloc(bma);
4076 if (error)
4077 return error;
4078
Dave Chinner29c8d172011-09-18 20:41:00 +00004079 if (bma->cur)
4080 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004081 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004082 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004083 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4084 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4085 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004086 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004087 }
4088 /*
4089 * Bump the number of extents we've allocated
4090 * in this call.
4091 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004092 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004093
Dave Chinner29c8d172011-09-18 20:41:00 +00004094 if (bma->cur)
4095 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004096 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4097
Dave Chinner963c30c2011-09-18 20:40:59 +00004098 bma->got.br_startoff = bma->offset;
4099 bma->got.br_startblock = bma->blkno;
4100 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004101 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004102
4103 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004104 * In the data fork, a wasdelay extent has been initialized, so
4105 * shouldn't be flagged as unwritten.
4106 *
4107 * For the cow fork, however, we convert delalloc reservations
4108 * (extents allocated for speculative preallocation) to
4109 * allocated unwritten extents, and only convert the unwritten
4110 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004111 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004112 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4113 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004114 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004115 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004116
Christoph Hellwigc6534242011-09-18 20:41:05 +00004117 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004118 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004119 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004120 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004121 whichfork, &bma->icur, &bma->cur, &bma->got,
Christoph Hellwig6d045582017-04-11 16:45:54 -07004122 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004123
Christoph Hellwigc315c902011-09-18 20:41:02 +00004124 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004125 if (error)
4126 return error;
4127
4128 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004129 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4130 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4131 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004132 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004133 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004134
Dave Chinner963c30c2011-09-18 20:40:59 +00004135 ASSERT(bma->got.br_startoff <= bma->offset);
4136 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4137 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004138 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4139 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004140 return 0;
4141}
4142
Dave Chinnerb447fe52011-09-18 20:40:51 +00004143STATIC int
4144xfs_bmapi_convert_unwritten(
4145 struct xfs_bmalloca *bma,
4146 struct xfs_bmbt_irec *mval,
4147 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004148 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004149{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004150 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004151 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004152 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004153 int error;
4154
Dave Chinnerb447fe52011-09-18 20:40:51 +00004155 /* check if we need to do unwritten->real conversion */
4156 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4157 (flags & XFS_BMAPI_PREALLOC))
4158 return 0;
4159
4160 /* check if we need to do real->unwritten conversion */
4161 if (mval->br_state == XFS_EXT_NORM &&
4162 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4163 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4164 return 0;
4165
4166 /*
4167 * Modify (by adding) the state flag, if writing.
4168 */
4169 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004170 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4171 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004172 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004173 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004174 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004175 }
4176 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4177 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4178
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004179 /*
4180 * Before insertion into the bmbt, zero the range being converted
4181 * if required.
4182 */
4183 if (flags & XFS_BMAPI_ZERO) {
4184 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4185 mval->br_blockcount);
4186 if (error)
4187 return error;
4188 }
4189
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004190 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004191 &bma->icur, &bma->cur, mval, bma->firstblock,
4192 bma->dfops, &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004193 /*
4194 * Log the inode core unconditionally in the unwritten extent conversion
4195 * path because the conversion might not have done so (e.g., if the
4196 * extent count hasn't changed). We need to make sure the inode is dirty
4197 * in the transaction for the sake of fsync(), even if nothing has
4198 * changed, because fsync() will not force the log for this transaction
4199 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004200 *
4201 * Note: If we're only converting cow fork extents, there aren't
4202 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004203 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004204 if (whichfork != XFS_COW_FORK)
4205 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004206 if (error)
4207 return error;
4208
4209 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004210 * Update our extent pointer, given that
4211 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4212 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004213 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004214 xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004215
4216 /*
4217 * We may have combined previously unwritten space with written space,
4218 * so generate another request.
4219 */
4220 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004221 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004222 return 0;
4223}
4224
Christoph Hellwig44032802011-09-18 20:40:48 +00004225/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004226 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4227 * extent state if necessary. Details behaviour is controlled by the flags
4228 * parameter. Only allocates blocks from a single allocation group, to avoid
4229 * locking problems.
4230 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 * The returned value in "firstblock" from the first call in a transaction
4232 * must be remembered and presented to subsequent calls in "firstblock".
4233 * An upper bound for the number of blocks to be allocated is supplied to
4234 * the first call in "total"; if no allocation group has that many free
4235 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4236 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004237int
4238xfs_bmapi_write(
4239 struct xfs_trans *tp, /* transaction pointer */
4240 struct xfs_inode *ip, /* incore inode */
4241 xfs_fileoff_t bno, /* starting file offs. mapped */
4242 xfs_filblks_t len, /* length to map in file */
4243 int flags, /* XFS_BMAPI_... */
4244 xfs_fsblock_t *firstblock, /* first allocated block
4245 controls a.g. for allocs */
4246 xfs_extlen_t total, /* total blocks needed */
4247 struct xfs_bmbt_irec *mval, /* output: map values */
4248 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004249 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004251 struct xfs_mount *mp = ip->i_mount;
4252 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004253 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004254 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004255 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004256 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004257 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004258 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004259 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004262 xfs_fileoff_t orig_bno; /* original block number value */
4263 int orig_flags; /* original flags arg value */
4264 xfs_filblks_t orig_len; /* original value of len arg */
4265 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4266 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 orig_bno = bno;
4269 orig_len = len;
4270 orig_flags = flags;
4271 orig_mval = mval;
4272 orig_nmap = *nmap;
4273#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004274 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004277 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4278 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004279 ASSERT(tp != NULL ||
4280 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4281 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004282 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004283 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004284 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004285 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004286
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004287 /* zeroing is for currently only for data extents, not metadata */
4288 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4289 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4290 /*
4291 * we can allocate unwritten extents or pre-zero allocated blocks,
4292 * but it makes no sense to do both at once. This would result in
4293 * zeroing the unwritten extent twice, but it still being an
4294 * unwritten extent....
4295 */
4296 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4297 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 if (unlikely(XFS_TEST_ERROR(
4300 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004301 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004302 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004303 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004304 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004308 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004311
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004312 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004313
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004314 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004316 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004318 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004319 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004320 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004321 }
4322
4323 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4324 error = xfs_iread_extents(tp, ip, whichfork);
4325 if (error)
4326 goto error0;
4327 }
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 n = 0;
4330 end = bno + len;
4331 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004332
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004333 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004334 eof = true;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004335 if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004336 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004337 bma.tp = tp;
4338 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004339 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004340 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004341 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004342 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004345 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004346
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004347 /* in hole or beyond EOF? */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004348 if (eof || bma.got.br_startoff > bno) {
Darrick J. Wongbe78ff02018-01-16 19:03:59 -08004349 /*
4350 * CoW fork conversions should /never/ hit EOF or
4351 * holes. There should always be something for us
4352 * to work on.
4353 */
4354 ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4355 (flags & XFS_BMAPI_COWFORK)));
4356
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004357 if (flags & XFS_BMAPI_DELALLOC) {
4358 /*
4359 * For the COW fork we can reasonably get a
4360 * request for converting an extent that races
4361 * with other threads already having converted
4362 * part of it, as there converting COW to
4363 * regular blocks is not protected using the
4364 * IOLOCK.
4365 */
4366 ASSERT(flags & XFS_BMAPI_COWFORK);
4367 if (!(flags & XFS_BMAPI_COWFORK)) {
4368 error = -EIO;
4369 goto error0;
4370 }
4371
4372 if (eof || bno >= end)
4373 break;
4374 } else {
4375 need_alloc = true;
4376 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004377 } else if (isnullstartblock(bma.got.br_startblock)) {
4378 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004379 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004380
4381 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 * First, deal with the hole before the allocated space
4383 * that we found, if any.
4384 */
Christoph Hellwigb1214592017-11-03 10:34:44 -07004385 if ((need_alloc || wasdelay) &&
4386 !(flags & XFS_BMAPI_CONVERT_ONLY)) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004387 bma.eof = eof;
4388 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4389 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004390 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004391 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004392
Dave Chinnera99ebf42011-12-01 11:24:20 +00004393 /*
4394 * There's a 32/64 bit type mismatch between the
4395 * allocation length request (which can be 64 bits in
4396 * length) and the bma length request, which is
4397 * xfs_extlen_t and therefore 32 bits. Hence we have to
4398 * check for 32-bit overflows and handle them here.
4399 */
4400 if (len > (xfs_filblks_t)MAXEXTLEN)
4401 bma.length = MAXEXTLEN;
4402 else
4403 bma.length = len;
4404
4405 ASSERT(len > 0);
4406 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004407 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 if (error)
4409 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004410 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004411 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004412
4413 /*
4414 * If this is a CoW allocation, record the data in
4415 * the refcount btree for orphan recovery.
4416 */
4417 if (whichfork == XFS_COW_FORK) {
4418 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4419 bma.blkno, bma.length);
4420 if (error)
4421 goto error0;
4422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004424
Dave Chinneraef9a892011-09-18 20:40:44 +00004425 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004426 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4427 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428
Dave Chinnerb447fe52011-09-18 20:40:51 +00004429 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004430 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004431 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004432 continue;
4433 if (error)
4434 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Dave Chinneraef9a892011-09-18 20:40:44 +00004436 /* update the extent map to return */
4437 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 /*
4440 * If we're done, stop now. Stop when we've allocated
4441 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4442 * the transaction may get too big.
4443 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004444 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004446
4447 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004448 bma.prev = bma.got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004449 if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004450 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 /*
4455 * Transform from btree to extents, give it cur.
4456 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004457 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004458 int tmp_logflags = 0;
4459
Dave Chinner29c8d172011-09-18 20:41:00 +00004460 ASSERT(bma.cur);
4461 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004463 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 if (error)
4465 goto error0;
4466 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004469 XFS_IFORK_NEXTENTS(ip, whichfork) >
4470 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472error0:
4473 /*
4474 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004475 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004477 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004479 bma.logflags &= ~xfs_ilog_fext(whichfork);
4480 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004482 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 /*
4484 * Log whatever the flags say, even if error. Otherwise we might miss
4485 * detecting a case where the data is changed, there's an error,
4486 * and it's not logged so we don't shutdown when we should.
4487 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004488 if (bma.logflags)
4489 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490
Dave Chinner29c8d172011-09-18 20:41:00 +00004491 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 if (!error) {
4493 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004494 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004496 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004497 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004499 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4501 }
4502 if (!error)
4503 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4504 orig_nmap, *nmap);
4505 return error;
4506}
4507
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004508static int
4509xfs_bmapi_remap(
4510 struct xfs_trans *tp,
4511 struct xfs_inode *ip,
4512 xfs_fileoff_t bno,
4513 xfs_filblks_t len,
4514 xfs_fsblock_t startblock,
4515 struct xfs_defer_ops *dfops)
4516{
4517 struct xfs_mount *mp = ip->i_mount;
4518 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4519 struct xfs_btree_cur *cur = NULL;
4520 xfs_fsblock_t firstblock = NULLFSBLOCK;
4521 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004522 struct xfs_iext_cursor icur;
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004523 int logflags = 0, error;
4524
4525 ASSERT(len > 0);
4526 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4527 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4528
4529 if (unlikely(XFS_TEST_ERROR(
4530 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4531 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004532 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004533 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4534 return -EFSCORRUPTED;
4535 }
4536
4537 if (XFS_FORCED_SHUTDOWN(mp))
4538 return -EIO;
4539
4540 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4541 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4542 if (error)
4543 return error;
4544 }
4545
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004546 if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004547 /* make sure we only reflink into a hole. */
4548 ASSERT(got.br_startoff > bno);
4549 ASSERT(got.br_startoff - bno >= len);
4550 }
4551
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004552 ip->i_d.di_nblocks += len;
4553 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004554
4555 if (ifp->if_flags & XFS_IFBROOT) {
4556 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4557 cur->bc_private.b.firstblock = firstblock;
4558 cur->bc_private.b.dfops = dfops;
4559 cur->bc_private.b.flags = 0;
4560 }
4561
4562 got.br_startoff = bno;
4563 got.br_startblock = startblock;
4564 got.br_blockcount = len;
4565 got.br_state = XFS_EXT_NORM;
4566
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004567 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &icur,
4568 &cur, &got, &firstblock, dfops, &logflags);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004569 if (error)
4570 goto error0;
4571
4572 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4573 int tmp_logflags = 0;
4574
4575 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4576 &tmp_logflags, XFS_DATA_FORK);
4577 logflags |= tmp_logflags;
4578 }
4579
4580error0:
4581 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4582 logflags &= ~XFS_ILOG_DEXT;
4583 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4584 logflags &= ~XFS_ILOG_DBROOT;
4585
4586 if (logflags)
4587 xfs_trans_log_inode(tp, ip, logflags);
4588 if (cur) {
4589 xfs_btree_del_cursor(cur,
4590 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4591 }
4592 return error;
4593}
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004596 * When a delalloc extent is split (e.g., due to a hole punch), the original
4597 * indlen reservation must be shared across the two new extents that are left
4598 * behind.
4599 *
4600 * Given the original reservation and the worst case indlen for the two new
4601 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004602 * reservation fairly across the two new extents. If necessary, steal available
4603 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4604 * ores == 1). The number of stolen blocks is returned. The availability and
4605 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004606 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004607static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004608xfs_bmap_split_indlen(
4609 xfs_filblks_t ores, /* original res. */
4610 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004611 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4612 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004613{
4614 xfs_filblks_t len1 = *indlen1;
4615 xfs_filblks_t len2 = *indlen2;
4616 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004617 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004618 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004619
4620 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004621 * Steal as many blocks as we can to try and satisfy the worst case
4622 * indlen for both new extents.
4623 */
Brian Foster75d65362017-02-13 22:48:30 -08004624 if (ores < nres && avail)
4625 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4626 ores += stolen;
4627
4628 /* nothing else to do if we've satisfied the new reservation */
4629 if (ores >= nres)
4630 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004631
4632 /*
Brian Foster75d65362017-02-13 22:48:30 -08004633 * We can't meet the total required reservation for the two extents.
4634 * Calculate the percent of the overall shortage between both extents
4635 * and apply this percentage to each of the requested indlen values.
4636 * This distributes the shortage fairly and reduces the chances that one
4637 * of the two extents is left with nothing when extents are repeatedly
4638 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004639 */
Brian Foster75d65362017-02-13 22:48:30 -08004640 resfactor = (ores * 100);
4641 do_div(resfactor, nres);
4642 len1 *= resfactor;
4643 do_div(len1, 100);
4644 len2 *= resfactor;
4645 do_div(len2, 100);
4646 ASSERT(len1 + len2 <= ores);
4647 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4648
4649 /*
4650 * Hand out the remainder to each extent. If one of the two reservations
4651 * is zero, we want to make sure that one gets a block first. The loop
4652 * below starts with len1, so hand len2 a block right off the bat if it
4653 * is zero.
4654 */
4655 ores -= (len1 + len2);
4656 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4657 if (ores && !len2 && *indlen2) {
4658 len2++;
4659 ores--;
4660 }
4661 while (ores) {
4662 if (len1 < *indlen1) {
4663 len1++;
4664 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004665 }
Brian Foster75d65362017-02-13 22:48:30 -08004666 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004667 break;
Brian Foster75d65362017-02-13 22:48:30 -08004668 if (len2 < *indlen2) {
4669 len2++;
4670 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004671 }
4672 }
4673
4674 *indlen1 = len1;
4675 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004676
4677 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004678}
4679
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004680int
4681xfs_bmap_del_extent_delay(
4682 struct xfs_inode *ip,
4683 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004684 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004685 struct xfs_bmbt_irec *got,
4686 struct xfs_bmbt_irec *del)
4687{
4688 struct xfs_mount *mp = ip->i_mount;
4689 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4690 struct xfs_bmbt_irec new;
4691 int64_t da_old, da_new, da_diff = 0;
4692 xfs_fileoff_t del_endoff, got_endoff;
4693 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004694 int state = xfs_bmap_fork_to_state(whichfork);
4695 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004696 bool isrt;
4697
4698 XFS_STATS_INC(mp, xs_del_exlist);
4699
4700 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4701 del_endoff = del->br_startoff + del->br_blockcount;
4702 got_endoff = got->br_startoff + got->br_blockcount;
4703 da_old = startblockval(got->br_startblock);
4704 da_new = 0;
4705
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004706 ASSERT(del->br_blockcount > 0);
4707 ASSERT(got->br_startoff <= del->br_startoff);
4708 ASSERT(got_endoff >= del_endoff);
4709
4710 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004711 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004712
4713 do_div(rtexts, mp->m_sb.sb_rextsize);
4714 xfs_mod_frextents(mp, rtexts);
4715 }
4716
4717 /*
4718 * Update the inode delalloc counter now and wait to update the
4719 * sb counters as we might have to borrow some blocks for the
4720 * indirect block accounting.
4721 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004722 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4723 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004724 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004725 if (error)
4726 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004727 ip->i_delayed_blks -= del->br_blockcount;
4728
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004729 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004730 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004731 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004732 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004733
Christoph Hellwig0173c682017-10-17 14:16:22 -07004734 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4735 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004736 /*
4737 * Matches the whole extent. Delete the entry.
4738 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004739 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004740 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004741 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004742 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004743 /*
4744 * Deleting the first part of the extent.
4745 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004746 got->br_startoff = del_endoff;
4747 got->br_blockcount -= del->br_blockcount;
4748 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4749 got->br_blockcount), da_old);
4750 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004751 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004752 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004753 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004754 /*
4755 * Deleting the last part of the extent.
4756 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004757 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4758 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4759 got->br_blockcount), da_old);
4760 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004761 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004762 break;
4763 case 0:
4764 /*
4765 * Deleting the middle of the extent.
4766 *
4767 * Distribute the original indlen reservation across the two new
4768 * extents. Steal blocks from the deleted extent if necessary.
4769 * Stealing blocks simply fudges the fdblocks accounting below.
4770 * Warn if either of the new indlen reservations is zero as this
4771 * can lead to delalloc problems.
4772 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004773 got->br_blockcount = del->br_startoff - got->br_startoff;
4774 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4775
4776 new.br_blockcount = got_endoff - del_endoff;
4777 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4778
4779 WARN_ON_ONCE(!got_indlen || !new_indlen);
4780 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4781 del->br_blockcount);
4782
4783 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004784
4785 new.br_startoff = del_endoff;
4786 new.br_state = got->br_state;
4787 new.br_startblock = nullstartblock((int)new_indlen);
4788
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004789 xfs_iext_update_extent(ip, state, icur, got);
4790 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004791 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004792
4793 da_new = got_indlen + new_indlen - stolen;
4794 del->br_blockcount -= stolen;
4795 break;
4796 }
4797
4798 ASSERT(da_old >= da_new);
4799 da_diff = da_old - da_new;
4800 if (!isrt)
4801 da_diff += del->br_blockcount;
4802 if (da_diff)
4803 xfs_mod_fdblocks(mp, da_diff, false);
4804 return error;
4805}
4806
4807void
4808xfs_bmap_del_extent_cow(
4809 struct xfs_inode *ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004810 struct xfs_iext_cursor *icur,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004811 struct xfs_bmbt_irec *got,
4812 struct xfs_bmbt_irec *del)
4813{
4814 struct xfs_mount *mp = ip->i_mount;
4815 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4816 struct xfs_bmbt_irec new;
4817 xfs_fileoff_t del_endoff, got_endoff;
4818 int state = BMAP_COWFORK;
4819
4820 XFS_STATS_INC(mp, xs_del_exlist);
4821
4822 del_endoff = del->br_startoff + del->br_blockcount;
4823 got_endoff = got->br_startoff + got->br_blockcount;
4824
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004825 ASSERT(del->br_blockcount > 0);
4826 ASSERT(got->br_startoff <= del->br_startoff);
4827 ASSERT(got_endoff >= del_endoff);
4828 ASSERT(!isnullstartblock(got->br_startblock));
4829
4830 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004831 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004832 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004833 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004834
Christoph Hellwig0173c682017-10-17 14:16:22 -07004835 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4836 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004837 /*
4838 * Matches the whole extent. Delete the entry.
4839 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004840 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004841 xfs_iext_prev(ifp, icur);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004842 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004843 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004844 /*
4845 * Deleting the first part of the extent.
4846 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004847 got->br_startoff = del_endoff;
4848 got->br_blockcount -= del->br_blockcount;
4849 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004850 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004851 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004852 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004853 /*
4854 * Deleting the last part of the extent.
4855 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004856 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004857 xfs_iext_update_extent(ip, state, icur, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004858 break;
4859 case 0:
4860 /*
4861 * Deleting the middle of the extent.
4862 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004863 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004864
4865 new.br_startoff = del_endoff;
4866 new.br_blockcount = got_endoff - del_endoff;
4867 new.br_state = got->br_state;
4868 new.br_startblock = del->br_startblock + del->br_blockcount;
4869
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004870 xfs_iext_update_extent(ip, state, icur, got);
4871 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07004872 xfs_iext_insert(ip, icur, &new, state);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004873 break;
4874 }
Darrick J. Wong4b4c1322018-01-19 09:05:48 -08004875 ip->i_delayed_blks -= del->br_blockcount;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004876}
4877
Brian Fostera9bd24a2016-03-15 11:42:46 +11004878/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004879 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004880 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004881 */
4882STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004883xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004884 xfs_inode_t *ip, /* incore inode pointer */
4885 xfs_trans_t *tp, /* current transaction pointer */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004886 struct xfs_iext_cursor *icur,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004887 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004888 xfs_btree_cur_t *cur, /* if null, not a btree */
4889 xfs_bmbt_irec_t *del, /* data to remove from extents */
4890 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004891 int whichfork, /* data or attr fork */
4892 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004893{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004894 xfs_fsblock_t del_endblock=0; /* first block past del */
4895 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004896 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004897 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004898 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004899 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004900 xfs_fileoff_t got_endoff; /* first offset past got */
4901 int i; /* temp state */
4902 xfs_ifork_t *ifp; /* inode fork pointer */
4903 xfs_mount_t *mp; /* mount structure */
4904 xfs_filblks_t nblks; /* quota/sb block count */
4905 xfs_bmbt_irec_t new; /* new record to be inserted */
4906 /* REFERENCED */
4907 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004908 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004909 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004910
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004911 mp = ip->i_mount;
4912 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004913
Dave Chinner9e5987a72013-02-25 12:31:26 +11004914 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004915 ASSERT(del->br_blockcount > 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004916 xfs_iext_get_extent(ifp, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004917 ASSERT(got.br_startoff <= del->br_startoff);
4918 del_endoff = del->br_startoff + del->br_blockcount;
4919 got_endoff = got.br_startoff + got.br_blockcount;
4920 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004921 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004922 qfield = 0;
4923 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004924
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004925 /*
4926 * If it's the case where the directory code is running with no block
4927 * reservation, and the deleted block is in the middle of its extent,
4928 * and the resulting insert of an extent would cause transformation to
4929 * btree format, then reject it. The calling code will then swap blocks
4930 * around instead. We have to do this now, rather than waiting for the
4931 * conversion to btree format, since the transaction will be dirty then.
4932 */
4933 if (tp->t_blk_res == 0 &&
4934 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4935 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4936 XFS_IFORK_MAXEXT(ip, whichfork) &&
4937 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4938 return -ENOSPC;
4939
4940 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004941 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4942 xfs_fsblock_t bno;
4943 xfs_filblks_t len;
4944
4945 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4946 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4947 bno = del->br_startblock;
4948 len = del->br_blockcount;
4949 do_div(bno, mp->m_sb.sb_rextsize);
4950 do_div(len, mp->m_sb.sb_rextsize);
4951 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4952 if (error)
4953 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004954 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004955 nblks = len * mp->m_sb.sb_rextsize;
4956 qfield = XFS_TRANS_DQ_RTBCOUNT;
4957 } else {
4958 do_fx = 1;
4959 nblks = del->br_blockcount;
4960 qfield = XFS_TRANS_DQ_BCOUNT;
4961 }
4962
4963 del_endblock = del->br_startblock + del->br_blockcount;
4964 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004965 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004966 if (error)
4967 goto done;
4968 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004969 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004970
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004971 if (got.br_startoff == del->br_startoff)
4972 state |= BMAP_LEFT_FILLING;
4973 if (got_endoff == del_endoff)
4974 state |= BMAP_RIGHT_FILLING;
4975
4976 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4977 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004978 /*
4979 * Matches the whole extent. Delete the entry.
4980 */
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07004981 xfs_iext_remove(ip, icur, state);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07004982 xfs_iext_prev(ifp, icur);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004983 XFS_IFORK_NEXT_SET(ip, whichfork,
4984 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4985 flags |= XFS_ILOG_CORE;
4986 if (!cur) {
4987 flags |= xfs_ilog_fext(whichfork);
4988 break;
4989 }
4990 if ((error = xfs_btree_delete(cur, &i)))
4991 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004992 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004993 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004994 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11004995 /*
4996 * Deleting the first part of the extent.
4997 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004998 got.br_startoff = del_endoff;
4999 got.br_startblock = del_endblock;
5000 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005001 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005002 if (!cur) {
5003 flags |= xfs_ilog_fext(whichfork);
5004 break;
5005 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005006 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005007 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005008 goto done;
5009 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005010 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005011 /*
5012 * Deleting the last part of the extent.
5013 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005014 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005015 xfs_iext_update_extent(ip, state, icur, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005016 if (!cur) {
5017 flags |= xfs_ilog_fext(whichfork);
5018 break;
5019 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005020 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005021 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005022 goto done;
5023 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005024 case 0:
5025 /*
5026 * Deleting the middle of the extent.
5027 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005028 old = got;
Christoph Hellwigca5d8e5b2017-10-19 11:04:44 -07005029
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005030 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005031 xfs_iext_update_extent(ip, state, icur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005032
Dave Chinner9e5987a72013-02-25 12:31:26 +11005033 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005034 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005035 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005036 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005037
Christoph Hellwige1d75532017-10-17 14:16:21 -07005038 flags |= XFS_ILOG_CORE;
5039 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005040 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005041 if (error)
5042 goto done;
5043 error = xfs_btree_increment(cur, 0, &i);
5044 if (error)
5045 goto done;
5046 cur->bc_rec.b = new;
5047 error = xfs_btree_insert(cur, &i);
5048 if (error && error != -ENOSPC)
5049 goto done;
5050 /*
5051 * If get no-space back from btree insert, it tried a
5052 * split, and we have a zero block reservation. Fix up
5053 * our state and return the error.
5054 */
5055 if (error == -ENOSPC) {
5056 /*
5057 * Reset the cursor, don't trust it after any
5058 * insert operation.
5059 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005060 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005061 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005062 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005063 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5064 /*
5065 * Update the btree record back
5066 * to the original value.
5067 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005068 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005069 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005070 goto done;
5071 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005072 * Reset the extent record back
5073 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005074 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005075 xfs_iext_update_extent(ip, state, icur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005076 flags = 0;
5077 error = -ENOSPC;
5078 goto done;
5079 }
5080 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5081 } else
5082 flags |= xfs_ilog_fext(whichfork);
5083 XFS_IFORK_NEXT_SET(ip, whichfork,
5084 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005085 xfs_iext_next(ifp, icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005086 xfs_iext_insert(ip, icur, &new, state);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005087 break;
5088 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005089
5090 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005091 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5092 if (error)
5093 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005094
Dave Chinner9e5987a72013-02-25 12:31:26 +11005095 /*
5096 * If we need to, add to list of extents to delete.
5097 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005098 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005099 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5100 error = xfs_refcount_decrease_extent(mp, dfops, del);
5101 if (error)
5102 goto done;
5103 } else
5104 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5105 del->br_blockcount, NULL);
5106 }
5107
Dave Chinner9e5987a72013-02-25 12:31:26 +11005108 /*
5109 * Adjust inode # blocks in the file.
5110 */
5111 if (nblks)
5112 ip->i_d.di_nblocks -= nblks;
5113 /*
5114 * Adjust quota data.
5115 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005116 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005117 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5118
Dave Chinner9e5987a72013-02-25 12:31:26 +11005119done:
5120 *logflagsp = flags;
5121 return error;
5122}
5123
5124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 * Unmap (remove) blocks from a file.
5126 * If nexts is nonzero then the number of extents to remove is limited to
5127 * that value. If not all extents in the block range can be removed then
5128 * *done is set.
5129 */
5130int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005131__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 xfs_trans_t *tp, /* transaction pointer */
5133 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005134 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005135 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 int flags, /* misc flags */
5137 xfs_extnum_t nexts, /* number of extents max */
5138 xfs_fsblock_t *firstblock, /* first allocated block
5139 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005140 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
5142 xfs_btree_cur_t *cur; /* bmap btree cursor */
5143 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 int error; /* error return value */
5145 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005146 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 xfs_ifork_t *ifp; /* inode fork pointer */
5148 int isrt; /* freeing in rt area */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 int logflags; /* transaction logging flags */
5150 xfs_extlen_t mod; /* rt extent offset */
5151 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 int tmp_logflags; /* partial logging flags */
5153 int wasdel; /* was a delayed alloc extent */
5154 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005156 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005157 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005158 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005159 xfs_fileoff_t end;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005160 struct xfs_iext_cursor icur;
5161 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005163 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005164
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005165 whichfork = xfs_bmapi_whichfork(flags);
5166 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 ifp = XFS_IFORK_PTR(ip, whichfork);
5168 if (unlikely(
5169 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5170 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5171 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5172 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005173 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 }
5175 mp = ip->i_mount;
5176 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005177 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005178
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005179 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 ASSERT(len > 0);
5181 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005182
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005183 /*
5184 * Guesstimate how many blocks we can unmap without running the risk of
5185 * blowing out the transaction with a mix of EFIs and reflink
5186 * adjustments.
5187 */
Darrick J. Wong8c57b882017-12-10 18:03:53 -08005188 if (tp && xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005189 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5190 else
5191 max_len = len;
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5194 (error = xfs_iread_extents(tp, ip, whichfork)))
5195 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005196 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005197 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return 0;
5199 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005200 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005201 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005202 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005203
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005204 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
Christoph Hellwigdc560152017-10-23 16:32:39 -07005205 *rlen = 0;
5206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005208 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 logflags = 0;
5211 if (ifp->if_flags & XFS_IFBROOT) {
5212 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005213 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005215 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 cur->bc_private.b.flags = 0;
5217 } else
5218 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005219
5220 if (isrt) {
5221 /*
5222 * Synchronize by locking the bitmap inode.
5223 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005224 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005225 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005226 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5227 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005228 }
5229
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 extno = 0;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005231 while (end != (xfs_fileoff_t)-1 && end >= start &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005232 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005234 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 * Just back up to the previous extent, if so.
5236 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005237 if (got.br_startoff > end &&
5238 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5239 done = true;
5240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 }
5242 /*
5243 * Is the last block of this extent before the range
5244 * we're supposed to delete? If so, we're done.
5245 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005246 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005248 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 break;
5250 /*
5251 * Then deal with the (possibly delayed) allocated space
5252 * we found.
5253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005255 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005256
5257 /*
5258 * Make sure we don't touch multiple AGF headers out of order
5259 * in a single transaction, as that could cause AB-BA deadlocks.
5260 */
5261 if (!wasdel) {
5262 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5263 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5264 break;
5265 prev_agno = agno;
5266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 if (got.br_startoff < start) {
5268 del.br_startoff = start;
5269 del.br_blockcount -= start - got.br_startoff;
5270 if (!wasdel)
5271 del.br_startblock += start - got.br_startoff;
5272 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005273 if (del.br_startoff + del.br_blockcount > end + 1)
5274 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005275
5276 /* How much can we safely unmap? */
5277 if (max_len < del.br_blockcount) {
5278 del.br_startoff += del.br_blockcount - max_len;
5279 if (!wasdel)
5280 del.br_startblock += del.br_blockcount - max_len;
5281 del.br_blockcount = max_len;
5282 }
5283
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 sum = del.br_startblock + del.br_blockcount;
5285 if (isrt &&
5286 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5287 /*
5288 * Realtime extent not lined up at the end.
5289 * The extent could have been split into written
5290 * and unwritten pieces, or we could just be
5291 * unmapping part of it. But we can't really
5292 * get rid of part of a realtime extent.
5293 */
5294 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005295 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 /*
5297 * This piece is unwritten, or we're not
5298 * using unwritten extents. Skip over it.
5299 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005300 ASSERT(end >= mod);
5301 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 del.br_blockcount : mod;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005303 if (end < got.br_startoff &&
5304 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5305 done = true;
5306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 }
5308 continue;
5309 }
5310 /*
5311 * It's written, turn it unwritten.
5312 * This is better than zeroing it.
5313 */
5314 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005315 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 /*
5317 * If this spans a realtime extent boundary,
5318 * chop it back to the start of the one we end at.
5319 */
5320 if (del.br_blockcount > mod) {
5321 del.br_startoff += del.br_blockcount - mod;
5322 del.br_startblock += del.br_blockcount - mod;
5323 del.br_blockcount = mod;
5324 }
5325 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005326 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005327 whichfork, &icur, &cur, &del,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005328 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 if (error)
5330 goto error0;
5331 goto nodelete;
5332 }
5333 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5334 /*
5335 * Realtime extent is lined up at the end but not
5336 * at the front. We'll get rid of full extents if
5337 * we can.
5338 */
5339 mod = mp->m_sb.sb_rextsize - mod;
5340 if (del.br_blockcount > mod) {
5341 del.br_blockcount -= mod;
5342 del.br_startoff += mod;
5343 del.br_startblock += mod;
5344 } else if ((del.br_startoff == start &&
5345 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005346 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005347 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 /*
5349 * Can't make it unwritten. There isn't
5350 * a full extent here so just skip it.
5351 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005352 ASSERT(end >= del.br_blockcount);
5353 end -= del.br_blockcount;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005354 if (got.br_startoff > end &&
5355 !xfs_iext_prev_extent(ifp, &icur, &got)) {
5356 done = true;
5357 break;
5358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 continue;
5360 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005361 struct xfs_bmbt_irec prev;
5362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 /*
5364 * This one is already unwritten.
5365 * It must have a written left neighbor.
5366 * Unwrite the killed part of that one and
5367 * try again.
5368 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005369 if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5370 ASSERT(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005372 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 ASSERT(del.br_startblock ==
5374 prev.br_startblock + prev.br_blockcount);
5375 if (prev.br_startoff < start) {
5376 mod = start - prev.br_startoff;
5377 prev.br_blockcount -= mod;
5378 prev.br_startblock += mod;
5379 prev.br_startoff = start;
5380 }
5381 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005382 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005383 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005384 &prev, firstblock, dfops,
5385 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (error)
5387 goto error0;
5388 goto nodelete;
5389 } else {
5390 ASSERT(del.br_state == XFS_EXT_NORM);
5391 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005392 error = xfs_bmap_add_extent_unwritten_real(tp,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005393 ip, whichfork, &icur, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005394 &del, firstblock, dfops,
5395 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 if (error)
5397 goto error0;
5398 goto nodelete;
5399 }
5400 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005401
Brian Fosterb2706a02016-03-15 11:42:46 +11005402 if (wasdel) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005403 error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005404 &got, &del);
5405 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005406 error = xfs_bmap_del_extent_real(ip, tp, &icur, dfops,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005407 cur, &del, &tmp_logflags, whichfork,
5408 flags);
5409 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005410 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 if (error)
5413 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005414
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005415 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005416 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 /*
5419 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005421 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005422 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5423 (got.br_startoff > end &&
5424 !xfs_iext_prev_extent(ifp, &icur, &got))) {
5425 done = true;
5426 break;
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 extno++;
5429 }
5430 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005431 if (done || end == (xfs_fileoff_t)-1 || end < start)
Darrick J. Wong44535932016-10-03 09:11:29 -07005432 *rlen = 0;
5433 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005434 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 /*
5437 * Convert to a btree if necessary.
5438 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005439 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005441 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 &cur, 0, &tmp_logflags, whichfork);
5443 logflags |= tmp_logflags;
5444 if (error)
5445 goto error0;
5446 }
5447 /*
5448 * transform from btree to extents, give it cur
5449 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005450 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 ASSERT(cur != NULL);
5452 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5453 whichfork);
5454 logflags |= tmp_logflags;
5455 if (error)
5456 goto error0;
5457 }
5458 /*
5459 * transform from extents to local?
5460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 error = 0;
5462error0:
5463 /*
5464 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005465 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005467 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005469 logflags &= ~xfs_ilog_fext(whichfork);
5470 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005472 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 /*
5474 * Log inode even in the error case, if the transaction
5475 * is dirty we'll need to shut down the filesystem.
5476 */
5477 if (logflags)
5478 xfs_trans_log_inode(tp, ip, logflags);
5479 if (cur) {
5480 if (!error) {
5481 *firstblock = cur->bc_private.b.firstblock;
5482 cur->bc_private.b.allocated = 0;
5483 }
5484 xfs_btree_del_cursor(cur,
5485 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5486 }
5487 return error;
5488}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005489
Darrick J. Wong44535932016-10-03 09:11:29 -07005490/* Unmap a range of a file. */
5491int
5492xfs_bunmapi(
5493 xfs_trans_t *tp,
5494 struct xfs_inode *ip,
5495 xfs_fileoff_t bno,
5496 xfs_filblks_t len,
5497 int flags,
5498 xfs_extnum_t nexts,
5499 xfs_fsblock_t *firstblock,
5500 struct xfs_defer_ops *dfops,
5501 int *done)
5502{
5503 int error;
5504
5505 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5506 dfops);
5507 *done = (len == 0);
5508 return error;
5509}
5510
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005511/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005512 * Determine whether an extent shift can be accomplished by a merge with the
5513 * extent that precedes the target hole of the shift.
5514 */
5515STATIC bool
5516xfs_bmse_can_merge(
5517 struct xfs_bmbt_irec *left, /* preceding extent */
5518 struct xfs_bmbt_irec *got, /* current extent to shift */
5519 xfs_fileoff_t shift) /* shift fsb */
5520{
5521 xfs_fileoff_t startoff;
5522
5523 startoff = got->br_startoff - shift;
5524
5525 /*
5526 * The extent, once shifted, must be adjacent in-file and on-disk with
5527 * the preceding extent.
5528 */
5529 if ((left->br_startoff + left->br_blockcount != startoff) ||
5530 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5531 (left->br_state != got->br_state) ||
5532 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5533 return false;
5534
5535 return true;
5536}
5537
5538/*
5539 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5540 * hole in the file. If an extent shift would result in the extent being fully
5541 * adjacent to the extent that currently precedes the hole, we can merge with
5542 * the preceding extent rather than do the shift.
5543 *
5544 * This function assumes the caller has verified a shift-by-merge is possible
5545 * with the provided extents via xfs_bmse_can_merge().
5546 */
5547STATIC int
5548xfs_bmse_merge(
5549 struct xfs_inode *ip,
5550 int whichfork,
5551 xfs_fileoff_t shift, /* shift fsb */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005552 struct xfs_iext_cursor *icur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005553 struct xfs_bmbt_irec *got, /* extent to shift */
5554 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005555 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005556 int *logflags, /* output */
5557 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005558{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005559 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005560 xfs_filblks_t blockcount;
5561 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005562 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005563
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005564 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005565
5566 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5567 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005568 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005569
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005570 new = *left;
5571 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005572
5573 /*
5574 * Update the on-disk extent count, the btree if necessary and log the
5575 * inode.
5576 */
5577 XFS_IFORK_NEXT_SET(ip, whichfork,
5578 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5579 *logflags |= XFS_ILOG_CORE;
5580 if (!cur) {
5581 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005582 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005583 }
5584
5585 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005586 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005587 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005588 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005589 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005590
5591 error = xfs_btree_delete(cur, &i);
5592 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005593 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005594 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005595
5596 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005597 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005598 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005599 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005600 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005601
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005602 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005603 if (error)
5604 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005605
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005606done:
Christoph Hellwigc38ccf52017-11-03 10:34:47 -07005607 xfs_iext_remove(ip, icur, 0);
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005608 xfs_iext_prev(XFS_IFORK_PTR(ip, whichfork), icur);
5609 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5610 &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005611
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005612 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005613 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5614 if (error)
5615 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005616 memcpy(&new, got, sizeof(new));
5617 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005618 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005619}
5620
Christoph Hellwigbf806282017-10-19 11:07:34 -07005621static int
5622xfs_bmap_shift_update_extent(
5623 struct xfs_inode *ip,
5624 int whichfork,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005625 struct xfs_iext_cursor *icur,
Christoph Hellwigbf806282017-10-19 11:07:34 -07005626 struct xfs_bmbt_irec *got,
5627 struct xfs_btree_cur *cur,
5628 int *logflags,
5629 struct xfs_defer_ops *dfops,
5630 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005631{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005632 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005633 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005634 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005635
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005636 *logflags |= XFS_ILOG_CORE;
5637
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005638 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005639
5640 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005641 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005642 if (error)
5643 return error;
5644 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5645
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005646 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005647 if (error)
5648 return error;
5649 } else {
5650 *logflags |= XFS_ILOG_DEXT;
5651 }
5652
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005653 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5654 got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005655
Darrick J. Wong9c194642016-08-03 12:16:05 +10005656 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005657 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005658 if (error)
5659 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005660 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005661}
5662
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005663int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005664xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005665 struct xfs_trans *tp,
5666 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005667 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005668 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005669 bool *done,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005670 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005671 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005672{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005673 int whichfork = XFS_DATA_FORK;
5674 struct xfs_mount *mp = ip->i_mount;
5675 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5676 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005677 struct xfs_bmbt_irec got, prev;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005678 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005679 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005680 int error = 0;
5681 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005682
5683 if (unlikely(XFS_TEST_ERROR(
5684 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5685 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005686 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005687 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005688 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005689 }
5690
5691 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005692 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005693
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005694 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005695
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005696 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005697 error = xfs_iread_extents(tp, ip, whichfork);
5698 if (error)
5699 return error;
5700 }
5701
Brian Fosterddb19e32014-09-23 15:38:09 +10005702 if (ifp->if_flags & XFS_IFBROOT) {
5703 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5704 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005705 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005706 cur->bc_private.b.flags = 0;
5707 }
5708
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005709 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005710 *done = true;
5711 goto del_cursor;
5712 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005713 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5714 del_cursor);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005715
Christoph Hellwigbf806282017-10-19 11:07:34 -07005716 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005717 if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005718 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5719 error = -EINVAL;
5720 goto del_cursor;
5721 }
5722
Christoph Hellwigbf806282017-10-19 11:07:34 -07005723 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5724 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005725 &icur, &got, &prev, cur, &logflags,
5726 dfops);
Christoph Hellwigbf806282017-10-19 11:07:34 -07005727 if (error)
5728 goto del_cursor;
5729 goto done;
5730 }
5731 } else {
5732 if (got.br_startoff < offset_shift_fsb) {
5733 error = -EINVAL;
5734 goto del_cursor;
5735 }
5736 }
5737
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005738 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5739 &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005740 if (error)
5741 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005742
Christoph Hellwig42630362017-11-03 10:34:41 -07005743done:
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005744 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5745 *done = true;
5746 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005747 }
5748
Christoph Hellwigbf806282017-10-19 11:07:34 -07005749 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005750del_cursor:
5751 if (cur)
5752 xfs_btree_del_cursor(cur,
5753 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005754 if (logflags)
5755 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005756 return error;
5757}
5758
5759int
5760xfs_bmap_insert_extents(
5761 struct xfs_trans *tp,
5762 struct xfs_inode *ip,
5763 xfs_fileoff_t *next_fsb,
5764 xfs_fileoff_t offset_shift_fsb,
5765 bool *done,
5766 xfs_fileoff_t stop_fsb,
5767 xfs_fsblock_t *firstblock,
5768 struct xfs_defer_ops *dfops)
5769{
5770 int whichfork = XFS_DATA_FORK;
5771 struct xfs_mount *mp = ip->i_mount;
5772 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5773 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005774 struct xfs_bmbt_irec got, next;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005775 struct xfs_iext_cursor icur;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005776 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005777 int error = 0;
5778 int logflags = 0;
5779
5780 if (unlikely(XFS_TEST_ERROR(
5781 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5782 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5783 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5784 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5785 return -EFSCORRUPTED;
5786 }
5787
5788 if (XFS_FORCED_SHUTDOWN(mp))
5789 return -EIO;
5790
5791 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5792
5793 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5794 error = xfs_iread_extents(tp, ip, whichfork);
5795 if (error)
5796 return error;
5797 }
5798
5799 if (ifp->if_flags & XFS_IFBROOT) {
5800 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5801 cur->bc_private.b.firstblock = *firstblock;
5802 cur->bc_private.b.dfops = dfops;
5803 cur->bc_private.b.flags = 0;
5804 }
5805
Namjae Jeona904b1c2015-03-25 15:08:56 +11005806 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005807 xfs_iext_last(ifp, &icur);
5808 if (!xfs_iext_get_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005809 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005810 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005811 goto del_cursor;
5812 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005813 } else {
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005814 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005815 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005816 goto del_cursor;
5817 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005818 }
Eric Sandeend41c6172017-11-27 18:23:32 -08005819 XFS_WANT_CORRUPTED_GOTO(mp, !isnullstartblock(got.br_startblock),
5820 del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005821
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005822 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005823 error = -EIO;
5824 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005825 }
5826
Christoph Hellwigbf806282017-10-19 11:07:34 -07005827 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005828 if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005829 if (new_startoff + got.br_blockcount > next.br_startoff) {
5830 error = -EINVAL;
5831 goto del_cursor;
5832 }
5833
5834 /*
5835 * Unlike a left shift (which involves a hole punch), a right
5836 * shift does not modify extent neighbors in any way. We should
5837 * never find mergeable extents in this scenario. Check anyways
5838 * and warn if we encounter two extents that could be one.
5839 */
5840 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5841 WARN_ON_ONCE(1);
5842 }
5843
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005844 error = xfs_bmap_shift_update_extent(ip, whichfork, &icur, &got, cur,
5845 &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005846 if (error)
5847 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005848
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005849 if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005850 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005851 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005852 goto del_cursor;
5853 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005854
5855 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005856del_cursor:
5857 if (cur)
5858 xfs_btree_del_cursor(cur,
5859 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005860 if (logflags)
5861 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005862 return error;
5863}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005864
5865/*
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005866 * Splits an extent into two extents at split_fsb block such that it is the
5867 * first block of the current_ext. @ext is a target extent to be split.
5868 * @split_fsb is a block where the extents is split. If split_fsb lies in a
5869 * hole or the first block of extents, just return 0.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005870 */
5871STATIC int
5872xfs_bmap_split_extent_at(
5873 struct xfs_trans *tp,
5874 struct xfs_inode *ip,
5875 xfs_fileoff_t split_fsb,
5876 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005877 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005878{
5879 int whichfork = XFS_DATA_FORK;
5880 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005881 struct xfs_bmbt_irec got;
5882 struct xfs_bmbt_irec new; /* split extent */
5883 struct xfs_mount *mp = ip->i_mount;
5884 struct xfs_ifork *ifp;
5885 xfs_fsblock_t gotblkcnt; /* new block count for got */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005886 struct xfs_iext_cursor icur;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005887 int error = 0;
5888 int logflags = 0;
5889 int i = 0;
5890
5891 if (unlikely(XFS_TEST_ERROR(
5892 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5893 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005894 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005895 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5896 XFS_ERRLEVEL_LOW, mp);
5897 return -EFSCORRUPTED;
5898 }
5899
5900 if (XFS_FORCED_SHUTDOWN(mp))
5901 return -EIO;
5902
5903 ifp = XFS_IFORK_PTR(ip, whichfork);
5904 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5905 /* Read in all the extents */
5906 error = xfs_iread_extents(tp, ip, whichfork);
5907 if (error)
5908 return error;
5909 }
5910
5911 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005912 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005913 */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005914 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005915 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005916 return 0;
5917
5918 gotblkcnt = split_fsb - got.br_startoff;
5919 new.br_startoff = split_fsb;
5920 new.br_startblock = got.br_startblock + gotblkcnt;
5921 new.br_blockcount = got.br_blockcount - gotblkcnt;
5922 new.br_state = got.br_state;
5923
5924 if (ifp->if_flags & XFS_IFBROOT) {
5925 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5926 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005927 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005928 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005929 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005930 if (error)
5931 goto del_cursor;
5932 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5933 }
5934
Namjae Jeona904b1c2015-03-25 15:08:56 +11005935 got.br_blockcount = gotblkcnt;
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005936 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
5937 &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005938
5939 logflags = XFS_ILOG_CORE;
5940 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005941 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005942 if (error)
5943 goto del_cursor;
5944 } else
5945 logflags |= XFS_ILOG_DEXT;
5946
5947 /* Add new extent */
Christoph Hellwigb2b17122017-11-03 10:34:43 -07005948 xfs_iext_next(ifp, &icur);
Christoph Hellwig0254c2f2017-11-03 10:34:46 -07005949 xfs_iext_insert(ip, &icur, &new, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005950 XFS_IFORK_NEXT_SET(ip, whichfork,
5951 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5952
5953 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005954 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005955 if (error)
5956 goto del_cursor;
5957 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005958 error = xfs_btree_insert(cur, &i);
5959 if (error)
5960 goto del_cursor;
5961 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5962 }
5963
5964 /*
5965 * Convert to a btree if necessary.
5966 */
5967 if (xfs_bmap_needs_btree(ip, whichfork)) {
5968 int tmp_logflags; /* partial log flag return val */
5969
5970 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005971 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005972 &cur, 0, &tmp_logflags, whichfork);
5973 logflags |= tmp_logflags;
5974 }
5975
5976del_cursor:
5977 if (cur) {
5978 cur->bc_private.b.allocated = 0;
5979 xfs_btree_del_cursor(cur,
5980 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5981 }
5982
5983 if (logflags)
5984 xfs_trans_log_inode(tp, ip, logflags);
5985 return error;
5986}
5987
5988int
5989xfs_bmap_split_extent(
5990 struct xfs_inode *ip,
5991 xfs_fileoff_t split_fsb)
5992{
5993 struct xfs_mount *mp = ip->i_mount;
5994 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005995 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005996 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005997 int error;
5998
Christoph Hellwig253f4912016-04-06 09:19:55 +10005999 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6000 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6001 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006002 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003
6004 xfs_ilock(ip, XFS_ILOCK_EXCL);
6005 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6006
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006007 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006008
6009 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006010 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 if (error)
6012 goto out;
6013
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006014 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006015 if (error)
6016 goto out;
6017
Christoph Hellwig70393312015-06-04 13:48:08 +10006018 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006019
6020out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006021 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006022 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006023 return error;
6024}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006025
6026/* Deferred mapping is only for real extents in the data fork. */
6027static bool
6028xfs_bmap_is_update_needed(
6029 struct xfs_bmbt_irec *bmap)
6030{
6031 return bmap->br_startblock != HOLESTARTBLOCK &&
6032 bmap->br_startblock != DELAYSTARTBLOCK;
6033}
6034
6035/* Record a bmap intent. */
6036static int
6037__xfs_bmap_add(
6038 struct xfs_mount *mp,
6039 struct xfs_defer_ops *dfops,
6040 enum xfs_bmap_intent_type type,
6041 struct xfs_inode *ip,
6042 int whichfork,
6043 struct xfs_bmbt_irec *bmap)
6044{
6045 int error;
6046 struct xfs_bmap_intent *bi;
6047
6048 trace_xfs_bmap_defer(mp,
6049 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6050 type,
6051 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6052 ip->i_ino, whichfork,
6053 bmap->br_startoff,
6054 bmap->br_blockcount,
6055 bmap->br_state);
6056
6057 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6058 INIT_LIST_HEAD(&bi->bi_list);
6059 bi->bi_type = type;
6060 bi->bi_owner = ip;
6061 bi->bi_whichfork = whichfork;
6062 bi->bi_bmap = *bmap;
6063
Christoph Hellwig882d8782017-08-28 10:21:03 -07006064 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006065 if (error) {
6066 kmem_free(bi);
6067 return error;
6068 }
6069
6070 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6071 return 0;
6072}
6073
6074/* Map an extent into a file. */
6075int
6076xfs_bmap_map_extent(
6077 struct xfs_mount *mp,
6078 struct xfs_defer_ops *dfops,
6079 struct xfs_inode *ip,
6080 struct xfs_bmbt_irec *PREV)
6081{
6082 if (!xfs_bmap_is_update_needed(PREV))
6083 return 0;
6084
6085 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6086 XFS_DATA_FORK, PREV);
6087}
6088
6089/* Unmap an extent out of a file. */
6090int
6091xfs_bmap_unmap_extent(
6092 struct xfs_mount *mp,
6093 struct xfs_defer_ops *dfops,
6094 struct xfs_inode *ip,
6095 struct xfs_bmbt_irec *PREV)
6096{
6097 if (!xfs_bmap_is_update_needed(PREV))
6098 return 0;
6099
6100 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6101 XFS_DATA_FORK, PREV);
6102}
6103
6104/*
6105 * Process one of the deferred bmap operations. We pass back the
6106 * btree cursor to maintain our lock on the bmapbt between calls.
6107 */
6108int
6109xfs_bmap_finish_one(
6110 struct xfs_trans *tp,
6111 struct xfs_defer_ops *dfops,
6112 struct xfs_inode *ip,
6113 enum xfs_bmap_intent_type type,
6114 int whichfork,
6115 xfs_fileoff_t startoff,
6116 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006117 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006118 xfs_exntst_t state)
6119{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006120 xfs_fsblock_t firstfsb;
6121 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006122
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006123 /*
6124 * firstfsb is tied to the transaction lifetime and is used to
6125 * ensure correct AG locking order and schedule work item
6126 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6127 * to only making one bmap call per transaction, so it should
6128 * be safe to have it as a local variable here.
6129 */
6130 firstfsb = NULLFSBLOCK;
6131
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006132 trace_xfs_bmap_deferred(tp->t_mountp,
6133 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6134 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006135 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006136
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006137 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006138 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006139
6140 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006141 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006142 return -EIO;
6143
6144 switch (type) {
6145 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006146 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006147 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006148 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006149 break;
6150 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006151 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6152 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006153 break;
6154 default:
6155 ASSERT(0);
6156 error = -EFSCORRUPTED;
6157 }
6158
6159 return error;
6160}
Darrick J. Wong30b09842018-03-23 10:06:52 -07006161
6162/* Check that an inode's extent does not have invalid flags or bad ranges. */
6163xfs_failaddr_t
6164xfs_bmap_validate_extent(
6165 struct xfs_inode *ip,
6166 int whichfork,
6167 struct xfs_bmbt_irec *irec)
6168{
6169 struct xfs_mount *mp = ip->i_mount;
6170 xfs_fsblock_t endfsb;
6171 bool isrt;
6172
6173 isrt = XFS_IS_REALTIME_INODE(ip);
6174 endfsb = irec->br_startblock + irec->br_blockcount - 1;
6175 if (isrt) {
6176 if (!xfs_verify_rtbno(mp, irec->br_startblock))
6177 return __this_address;
6178 if (!xfs_verify_rtbno(mp, endfsb))
6179 return __this_address;
6180 } else {
6181 if (!xfs_verify_fsbno(mp, irec->br_startblock))
6182 return __this_address;
6183 if (!xfs_verify_fsbno(mp, endfsb))
6184 return __this_address;
6185 if (XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
6186 XFS_FSB_TO_AGNO(mp, endfsb))
6187 return __this_address;
6188 }
6189 if (irec->br_state != XFS_EXT_NORM) {
6190 if (whichfork != XFS_DATA_FORK)
6191 return __this_address;
6192 if (!xfs_sb_version_hasextflgbit(&mp->m_sb))
6193 return __this_address;
6194 }
6195 return NULL;
6196}