blob: 070b078c3494074fbae2235cf6fcf027ad66f4a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100038#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
41#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110046#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100049#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100050#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070051#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110052#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055kmem_zone_t *xfs_bmap_free_item_zone;
56
57/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110058 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110062 * Compute and fill in the value of the maximum depth of a bmap btree
63 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110065void
66xfs_bmap_compute_maxlevels(
67 xfs_mount_t *mp, /* file system mount structure */
68 int whichfork) /* data or attr fork */
69{
70 int level; /* btree level */
71 uint maxblocks; /* max blocks at this level */
72 uint maxleafents; /* max leaf entries possible */
73 int maxrootrecs; /* max records in root block */
74 int minleafrecs; /* min records in leaf block */
75 int minnoderecs; /* min records in node block */
76 int sz; /* root block size */
77
78 /*
79 * The maximum number of extents in a file, hence the maximum
80 * number of leaf entries, is controlled by the type of di_nextents
81 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
82 * (a signed 16-bit number, xfs_aextnum_t).
83 *
84 * Note that we can no longer assume that if we are in ATTR1 that
85 * the fork offset of all the inodes will be
86 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
87 * with ATTR2 and then mounted back with ATTR1, keeping the
88 * di_forkoff's fixed but probably at various positions. Therefore,
89 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
90 * of a minimum size available.
91 */
92 if (whichfork == XFS_DATA_FORK) {
93 maxleafents = MAXEXTNUM;
94 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
95 } else {
96 maxleafents = MAXAEXTNUM;
97 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
98 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100099 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100100 minleafrecs = mp->m_bmap_dmnr[0];
101 minnoderecs = mp->m_bmap_dmnr[1];
102 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
103 for (level = 1; maxblocks > 1; level++) {
104 if (maxblocks <= maxrootrecs)
105 maxblocks = 1;
106 else
107 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
108 }
109 mp->m_bm_maxlevels[whichfork] = level;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100112STATIC int /* error */
113xfs_bmbt_lookup_eq(
114 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700115 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100116 int *stat) /* success/failure */
117{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700118 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100119 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
120}
121
122STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700123xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100124 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100125 int *stat) /* success/failure */
126{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700127 cur->bc_rec.b.br_startoff = 0;
128 cur->bc_rec.b.br_startblock = 0;
129 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100130 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
131}
132
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100133/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000134 * Check if the inode needs to be converted to btree format.
135 */
136static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
137{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700138 return whichfork != XFS_COW_FORK &&
139 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140 XFS_IFORK_NEXTENTS(ip, whichfork) >
141 XFS_IFORK_MAXEXT(ip, whichfork);
142}
143
144/*
145 * Check if the inode should be converted to extent format.
146 */
147static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
148{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700149 return whichfork != XFS_COW_FORK &&
150 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000151 XFS_IFORK_NEXTENTS(ip, whichfork) <=
152 XFS_IFORK_MAXEXT(ip, whichfork);
153}
154
155/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700156 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100157 * This either works (return 0) or gets an EFSCORRUPTED error.
158 */
159STATIC int
160xfs_bmbt_update(
161 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700162 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100163{
164 union xfs_btree_rec rec;
165
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700166 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100167 return xfs_btree_update(cur, &rec);
168}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
173 */
174STATIC xfs_filblks_t
175xfs_bmap_worst_indlen(
176 xfs_inode_t *ip, /* incore inode pointer */
177 xfs_filblks_t len) /* delayed extent length */
178{
179 int level; /* btree level number */
180 int maxrecs; /* maximum record count at this level */
181 xfs_mount_t *mp; /* mount structure */
182 xfs_filblks_t rval; /* return value */
183
184 mp = ip->i_mount;
185 maxrecs = mp->m_bmap_dmxr[0];
186 for (level = 0, rval = 0;
187 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
188 level++) {
189 len += maxrecs - 1;
190 do_div(len, maxrecs);
191 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700192 if (len == 1)
193 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100194 level - 1;
195 if (level == 0)
196 maxrecs = mp->m_bmap_dmxr[1];
197 }
198 return rval;
199}
200
201/*
202 * Calculate the default attribute fork offset for newly created inodes.
203 */
204uint
205xfs_default_attroffset(
206 struct xfs_inode *ip)
207{
208 struct xfs_mount *mp = ip->i_mount;
209 uint offset;
210
211 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100212 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100213 XFS_BMDR_SPACE_CALC(MINABTPTRS);
214 } else {
215 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
216 }
217
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100218 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100219 return offset;
220}
221
222/*
223 * Helper routine to reset inode di_forkoff field when switching
224 * attribute fork from local to extent format - we reset it where
225 * possible to make space available for inline data fork extents.
226 */
227STATIC void
228xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100229 xfs_inode_t *ip,
230 int whichfork)
231{
232 if (whichfork == XFS_ATTR_FORK &&
233 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
234 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
235 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 Chinner9e5987a2013-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 Chinner9e5987a2013-02-25 12:31:26 +1100328 */
Dave Chinner9e5987a2013-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 Chinner9e5987a2013-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 Chinner9e5987a2013-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 Chinner9e5987a2013-02-25 12:31:26 +1100391 if (level == 0)
392 break;
393
394 /*
395 * Check this block for basic sanity (increasing keys and
396 * no duplicate blocks).
397 */
398
399 xfs_check_block(block, mp, 0, 0);
400 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
401 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100402 XFS_WANT_CORRUPTED_GOTO(mp,
403 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-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 Chinner9e5987a2013-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/*
491 * Add bmap trace insert entries for all the contents of the extent records.
492 */
493void
494xfs_bmap_trace_exlist(
495 xfs_inode_t *ip, /* incore inode pointer */
496 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100497 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100498 unsigned long caller_ip)
499{
500 xfs_extnum_t idx; /* extent record index */
501 xfs_ifork_t *ifp; /* inode fork pointer */
Christoph Hellwig060ea652017-10-19 11:02:29 -0700502 int state = xfs_bmap_fork_to_state(whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100503
504 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100505 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100506 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100507 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100508}
509
510/*
511 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000512 * given the caller's original parameters. Specifically check the
513 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100514 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
515 */
516STATIC void
517xfs_bmap_validate_ret(
518 xfs_fileoff_t bno,
519 xfs_filblks_t len,
520 int flags,
521 xfs_bmbt_irec_t *mval,
522 int nmap,
523 int ret_nmap)
524{
525 int i; /* index to map values */
526
527 ASSERT(ret_nmap <= nmap);
528
529 for (i = 0; i < ret_nmap; i++) {
530 ASSERT(mval[i].br_blockcount > 0);
531 if (!(flags & XFS_BMAPI_ENTIRE)) {
532 ASSERT(mval[i].br_startoff >= bno);
533 ASSERT(mval[i].br_blockcount <= len);
534 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
535 bno + len);
536 } else {
537 ASSERT(mval[i].br_startoff < bno + len);
538 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
539 bno);
540 }
541 ASSERT(i == 0 ||
542 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
543 mval[i].br_startoff);
544 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
545 mval[i].br_startblock != HOLESTARTBLOCK);
546 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
547 mval[i].br_state == XFS_EXT_UNWRITTEN);
548 }
549}
550
551#else
552#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700553#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100554#endif /* DEBUG */
555
556/*
557 * bmap free list manipulation functions
558 */
559
560/*
561 * Add the extent to the list of extents to be free at transaction end.
562 * The list is maintained sorted (by block number).
563 */
564void
565xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000566 struct xfs_mount *mp,
567 struct xfs_defer_ops *dfops,
568 xfs_fsblock_t bno,
569 xfs_filblks_t len,
570 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100571{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000572 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100573#ifdef DEBUG
574 xfs_agnumber_t agno;
575 xfs_agblock_t agbno;
576
577 ASSERT(bno != NULLFSBLOCK);
578 ASSERT(len > 0);
579 ASSERT(len <= MAXEXTLEN);
580 ASSERT(!isnullstartblock(bno));
581 agno = XFS_FSB_TO_AGNO(mp, bno);
582 agbno = XFS_FSB_TO_AGBNO(mp, bno);
583 ASSERT(agno < mp->m_sb.sb_agcount);
584 ASSERT(agbno < mp->m_sb.sb_agblocks);
585 ASSERT(len < mp->m_sb.sb_agblocks);
586 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
587#endif
588 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000589
Dave Chinner9e5987a2013-02-25 12:31:26 +1100590 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000591 new->xefi_startblock = bno;
592 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000593 if (oinfo)
594 new->xefi_oinfo = *oinfo;
595 else
596 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000597 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
598 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000599 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600}
601
602/*
603 * Inode fork format manipulation functions
604 */
605
606/*
607 * Transform a btree format file with only one leaf node, where the
608 * extents list will fit in the inode, into an extents format file.
609 * Since the file extents are already in-core, all we have to do is
610 * give up the space for the btree root and pitch the leaf block.
611 */
612STATIC int /* error */
613xfs_bmap_btree_to_extents(
614 xfs_trans_t *tp, /* transaction pointer */
615 xfs_inode_t *ip, /* incore inode pointer */
616 xfs_btree_cur_t *cur, /* btree cursor */
617 int *logflagsp, /* inode logging flags */
618 int whichfork) /* data or attr fork */
619{
620 /* REFERENCED */
621 struct xfs_btree_block *cblock;/* child btree block */
622 xfs_fsblock_t cbno; /* child block number */
623 xfs_buf_t *cbp; /* child block's buffer */
624 int error; /* error return value */
625 xfs_ifork_t *ifp; /* inode fork data */
626 xfs_mount_t *mp; /* mount point structure */
627 __be64 *pp; /* ptr to block address */
628 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000629 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100630
631 mp = ip->i_mount;
632 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700633 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100634 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
635 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
636 rblock = ifp->if_broot;
637 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
638 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
639 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
640 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
641 cbno = be64_to_cpu(*pp);
642 *logflagsp = 0;
643#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700644 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
645 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100646#endif
647 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
648 &xfs_bmbt_buf_ops);
649 if (error)
650 return error;
651 cblock = XFS_BUF_TO_BLOCK(cbp);
652 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
653 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000654 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
655 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100656 ip->i_d.di_nblocks--;
657 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
658 xfs_trans_binval(tp, cbp);
659 if (cur->bc_bufs[0] == cbp)
660 cur->bc_bufs[0] = NULL;
661 xfs_iroot_realloc(ip, -1, whichfork);
662 ASSERT(ifp->if_broot == NULL);
663 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
664 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
665 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
666 return 0;
667}
668
669/*
670 * Convert an extents-format file into a btree-format file.
671 * The new file will have a root block (in the inode) and a single child block.
672 */
673STATIC int /* error */
674xfs_bmap_extents_to_btree(
675 xfs_trans_t *tp, /* transaction pointer */
676 xfs_inode_t *ip, /* incore inode pointer */
677 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000678 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100679 xfs_btree_cur_t **curp, /* cursor returned to caller */
680 int wasdel, /* converting a delayed alloc */
681 int *logflagsp, /* inode logging flags */
682 int whichfork) /* data or attr fork */
683{
684 struct xfs_btree_block *ablock; /* allocated (child) bt block */
685 xfs_buf_t *abp; /* buffer for ablock */
686 xfs_alloc_arg_t args; /* allocation arguments */
687 xfs_bmbt_rec_t *arp; /* child record pointer */
688 struct xfs_btree_block *block; /* btree root block */
689 xfs_btree_cur_t *cur; /* bmap btree cursor */
690 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
691 int error; /* error return value */
692 xfs_extnum_t i, cnt; /* extent record index */
693 xfs_ifork_t *ifp; /* inode fork pointer */
694 xfs_bmbt_key_t *kp; /* root block key pointer */
695 xfs_mount_t *mp; /* mount structure */
696 xfs_extnum_t nextents; /* number of file extents */
697 xfs_bmbt_ptr_t *pp; /* root block address pointer */
698
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500699 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700700 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100701 ifp = XFS_IFORK_PTR(ip, whichfork);
702 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
703
704 /*
705 * Make space in the inode incore.
706 */
707 xfs_iroot_realloc(ip, 1, whichfork);
708 ifp->if_flags |= XFS_IFBROOT;
709
710 /*
711 * Fill in the root.
712 */
713 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800714 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
715 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800716 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100717 /*
718 * Need a cursor. Can't allocate until bb_level is filled in.
719 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100720 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
721 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000722 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100723 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
724 /*
725 * Convert to a btree with two levels, one record in root.
726 */
727 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
728 memset(&args, 0, sizeof(args));
729 args.tp = tp;
730 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000731 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100732 args.firstblock = *firstblock;
733 if (*firstblock == NULLFSBLOCK) {
734 args.type = XFS_ALLOCTYPE_START_BNO;
735 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000736 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100737 args.type = XFS_ALLOCTYPE_START_BNO;
738 args.fsbno = *firstblock;
739 } else {
740 args.type = XFS_ALLOCTYPE_NEAR_BNO;
741 args.fsbno = *firstblock;
742 }
743 args.minlen = args.maxlen = args.prod = 1;
744 args.wasdel = wasdel;
745 *logflagsp = 0;
746 if ((error = xfs_alloc_vextent(&args))) {
747 xfs_iroot_realloc(ip, -1, whichfork);
748 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
749 return error;
750 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700751
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800752 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
753 xfs_iroot_realloc(ip, -1, whichfork);
754 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
755 return -ENOSPC;
756 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100757 /*
758 * Allocation can't fail, the space was reserved.
759 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100760 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800761 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100762 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
763 cur->bc_private.b.allocated++;
764 ip->i_d.di_nblocks++;
765 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
766 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
767 /*
768 * Fill in the child block.
769 */
770 abp->b_ops = &xfs_bmbt_buf_ops;
771 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800772 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
773 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500774 XFS_BTREE_LONG_PTRS);
775
Dave Chinner9e5987a2013-02-25 12:31:26 +1100776 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100777 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100778 for (cnt = i = 0; i < nextents; i++) {
779 ep = xfs_iext_get_ext(ifp, i);
780 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
781 arp->l0 = cpu_to_be64(ep->l0);
782 arp->l1 = cpu_to_be64(ep->l1);
783 arp++; cnt++;
784 }
785 }
786 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
787 xfs_btree_set_numrecs(ablock, cnt);
788
789 /*
790 * Fill in the root key and pointer.
791 */
792 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
793 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
794 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
795 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
796 be16_to_cpu(block->bb_level)));
797 *pp = cpu_to_be64(args.fsbno);
798
799 /*
800 * Do all this logging at the end so that
801 * the root is at the right level.
802 */
803 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
804 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
805 ASSERT(*curp == NULL);
806 *curp = cur;
807 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
808 return 0;
809}
810
811/*
812 * Convert a local file to an extents file.
813 * This code is out of bounds for data forks of regular files,
814 * since the file data needs to get logged so things will stay consistent.
815 * (The bmap-level manipulations are ok, though).
816 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000817void
818xfs_bmap_local_to_extents_empty(
819 struct xfs_inode *ip,
820 int whichfork)
821{
822 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
823
Darrick J. Wong60b49842016-10-03 09:11:34 -0700824 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000825 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
826 ASSERT(ifp->if_bytes == 0);
827 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
828
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000829 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000830 ifp->if_flags &= ~XFS_IFINLINE;
831 ifp->if_flags |= XFS_IFEXTENTS;
832 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
833}
834
835
Dave Chinner9e5987a2013-02-25 12:31:26 +1100836STATIC int /* error */
837xfs_bmap_local_to_extents(
838 xfs_trans_t *tp, /* transaction pointer */
839 xfs_inode_t *ip, /* incore inode pointer */
840 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
841 xfs_extlen_t total, /* total blocks needed by transaction */
842 int *logflagsp, /* inode logging flags */
843 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500844 void (*init_fn)(struct xfs_trans *tp,
845 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100846 struct xfs_inode *ip,
847 struct xfs_ifork *ifp))
848{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000849 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100850 int flags; /* logging flags returned */
851 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000852 xfs_alloc_arg_t args; /* allocation arguments */
853 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700854 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100855
856 /*
857 * We don't want to deal with the case of keeping inode data inline yet.
858 * So sending the data fork of a regular inode is invalid.
859 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100860 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100861 ifp = XFS_IFORK_PTR(ip, whichfork);
862 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000863
864 if (!ifp->if_bytes) {
865 xfs_bmap_local_to_extents_empty(ip, whichfork);
866 flags = XFS_ILOG_CORE;
867 goto done;
868 }
869
Dave Chinner9e5987a2013-02-25 12:31:26 +1100870 flags = 0;
871 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000872 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
873 XFS_IFINLINE);
874 memset(&args, 0, sizeof(args));
875 args.tp = tp;
876 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000877 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000878 args.firstblock = *firstblock;
879 /*
880 * Allocate a block. We know we need only one, since the
881 * file currently fits in an inode.
882 */
883 if (*firstblock == NULLFSBLOCK) {
884 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
885 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100886 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000887 args.fsbno = *firstblock;
888 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100889 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000890 args.total = total;
891 args.minlen = args.maxlen = args.prod = 1;
892 error = xfs_alloc_vextent(&args);
893 if (error)
894 goto done;
895
896 /* Can't fail, the space was reserved. */
897 ASSERT(args.fsbno != NULLFSBLOCK);
898 ASSERT(args.len == 1);
899 *firstblock = args.fsbno;
900 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
901
Dave Chinnerfe22d552015-01-22 09:30:06 +1100902 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100903 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100904 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100905 * The callout is responsible for logging because the remote format
906 * might differ from the local format and thus we don't know how much to
907 * log here. Note that init_fn must also set the buffer log item type
908 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100909 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000910 init_fn(tp, bp, ip, ifp);
911
Brian Fosterb7cdc662015-10-12 15:40:24 +1100912 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000913 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
914 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100915 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000916
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700917 rec.br_startoff = 0;
918 rec.br_startblock = args.fsbno;
919 rec.br_blockcount = 1;
920 rec.br_state = XFS_EXT_NORM;
921 xfs_iext_insert(ip, 0, 1, &rec, 0);
922
Christoph Hellwig060ea652017-10-19 11:02:29 -0700923 trace_xfs_bmap_post_update(ip, 0, xfs_bmap_fork_to_state(whichfork),
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000924 _THIS_IP_);
925 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
926 ip->i_d.di_nblocks = 1;
927 xfs_trans_mod_dquot_byino(tp, ip,
928 XFS_TRANS_DQ_BCOUNT, 1L);
929 flags |= xfs_ilog_fext(whichfork);
930
Dave Chinner9e5987a2013-02-25 12:31:26 +1100931done:
932 *logflagsp = flags;
933 return error;
934}
935
936/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * Called from xfs_bmap_add_attrfork to handle btree format files.
938 */
939STATIC int /* error */
940xfs_bmap_add_attrfork_btree(
941 xfs_trans_t *tp, /* transaction pointer */
942 xfs_inode_t *ip, /* incore inode pointer */
943 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000944 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 int *flags) /* inode logging flags */
946{
947 xfs_btree_cur_t *cur; /* btree cursor */
948 int error; /* error return value */
949 xfs_mount_t *mp; /* file system mount struct */
950 int stat; /* newroot status */
951
952 mp = ip->i_mount;
953 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
954 *flags |= XFS_ILOG_DBROOT;
955 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100956 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000957 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700959 error = xfs_bmbt_lookup_first(cur, &stat);
960 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000962 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100963 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100964 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 goto error0;
966 if (stat == 0) {
967 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000968 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 *firstblock = cur->bc_private.b.firstblock;
971 cur->bc_private.b.allocated = 0;
972 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
973 }
974 return 0;
975error0:
976 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
977 return error;
978}
979
980/*
981 * Called from xfs_bmap_add_attrfork to handle extents format files.
982 */
983STATIC int /* error */
984xfs_bmap_add_attrfork_extents(
985 xfs_trans_t *tp, /* transaction pointer */
986 xfs_inode_t *ip, /* incore inode pointer */
987 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000988 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int *flags) /* inode logging flags */
990{
991 xfs_btree_cur_t *cur; /* bmap btree cursor */
992 int error; /* error return value */
993
994 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
995 return 0;
996 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000997 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 flags, XFS_DATA_FORK);
999 if (cur) {
1000 cur->bc_private.b.allocated = 0;
1001 xfs_btree_del_cursor(cur,
1002 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1003 }
1004 return error;
1005}
1006
1007/*
Dave Chinner1e823792013-02-11 15:58:13 +11001008 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1009 * different data fork content type needs a different callout to do the
1010 * conversion. Some are basic and only require special block initialisation
1011 * callouts for the data formating, others (directories) are so specialised they
1012 * handle everything themselves.
1013 *
1014 * XXX (dgc): investigate whether directory conversion can use the generic
1015 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001016 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 */
1018STATIC int /* error */
1019xfs_bmap_add_attrfork_local(
1020 xfs_trans_t *tp, /* transaction pointer */
1021 xfs_inode_t *ip, /* incore inode pointer */
1022 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001023 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int *flags) /* inode logging flags */
1025{
1026 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1029 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001030
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001031 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001033 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 dargs.dp = ip;
1035 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001036 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001037 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 dargs.whichfork = XFS_DATA_FORK;
1039 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001040 return xfs_dir2_sf_to_block(&dargs);
1041 }
1042
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001043 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001044 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1045 flags, XFS_DATA_FORK,
1046 xfs_symlink_local_to_remote);
1047
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001048 /* should only be called for types that support local format data */
1049 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001050 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001054 * Convert inode from non-attributed to attributed.
1055 * Must not be in a transaction, ip must not be locked.
1056 */
1057int /* error code */
1058xfs_bmap_add_attrfork(
1059 xfs_inode_t *ip, /* incore inode pointer */
1060 int size, /* space new attribute needs */
1061 int rsvd) /* xact may use reserved blks */
1062{
1063 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001064 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001065 xfs_mount_t *mp; /* mount structure */
1066 xfs_trans_t *tp; /* transaction pointer */
1067 int blks; /* space reservation */
1068 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001069 int logflags; /* logging flags */
1070 int error; /* error return value */
1071
1072 ASSERT(XFS_IFORK_Q(ip) == 0);
1073
1074 mp = ip->i_mount;
1075 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001076
Dave Chinner9e5987a2013-02-25 12:31:26 +11001077 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001078
1079 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1080 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1081 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001082 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001083
Dave Chinner9e5987a2013-02-25 12:31:26 +11001084 xfs_ilock(ip, XFS_ILOCK_EXCL);
1085 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1086 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1087 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001088 if (error)
1089 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001090 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001091 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001092 if (ip->i_d.di_anextents != 0) {
1093 error = -EFSCORRUPTED;
1094 goto trans_cancel;
1095 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001096 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1097 /*
1098 * For inodes coming from pre-6.2 filesystems.
1099 */
1100 ASSERT(ip->i_d.di_aformat == 0);
1101 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1102 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001103
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001104 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001105 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1106
1107 switch (ip->i_d.di_format) {
1108 case XFS_DINODE_FMT_DEV:
1109 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1110 break;
1111 case XFS_DINODE_FMT_UUID:
1112 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1113 break;
1114 case XFS_DINODE_FMT_LOCAL:
1115 case XFS_DINODE_FMT_EXTENTS:
1116 case XFS_DINODE_FMT_BTREE:
1117 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1118 if (!ip->i_d.di_forkoff)
1119 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1120 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1121 version = 2;
1122 break;
1123 default:
1124 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001125 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001126 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001127 }
1128
1129 ASSERT(ip->i_afp == NULL);
1130 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1131 ip->i_afp->if_flags = XFS_IFEXTENTS;
1132 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001133 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001134 switch (ip->i_d.di_format) {
1135 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001136 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001137 &logflags);
1138 break;
1139 case XFS_DINODE_FMT_EXTENTS:
1140 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001141 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001142 break;
1143 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001144 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001145 &logflags);
1146 break;
1147 default:
1148 error = 0;
1149 break;
1150 }
1151 if (logflags)
1152 xfs_trans_log_inode(tp, ip, logflags);
1153 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001154 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001155 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1156 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001157 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001158
1159 spin_lock(&mp->m_sb_lock);
1160 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1161 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001162 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001163 }
1164 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1165 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001166 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001167 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001168 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001169 if (log_sb)
1170 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001171 }
1172
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001173 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001174 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001175 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001176 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001177 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001178 return error;
1179
1180bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001181 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001182trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001183 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001184 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001185 return error;
1186}
1187
1188/*
1189 * Internal and external extent tree search functions.
1190 */
1191
1192/*
1193 * Read in the extents to if_extents.
1194 * All inode fields are set up by caller, we just traverse the btree
1195 * and copy the records in. If the file system cannot contain unwritten
1196 * extents, the records are checked for no "state" flags.
1197 */
1198int /* error */
1199xfs_bmap_read_extents(
1200 xfs_trans_t *tp, /* transaction pointer */
1201 xfs_inode_t *ip, /* incore inode */
1202 int whichfork) /* data or attr fork */
1203{
1204 struct xfs_btree_block *block; /* current btree block */
1205 xfs_fsblock_t bno; /* block # of "block" */
1206 xfs_buf_t *bp; /* buffer for "block" */
1207 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001208 xfs_extnum_t i, j; /* index into the extents list */
1209 xfs_ifork_t *ifp; /* fork structure */
1210 int level; /* btree level, for checking */
1211 xfs_mount_t *mp; /* file system mount structure */
1212 __be64 *pp; /* pointer to block address */
1213 /* REFERENCED */
1214 xfs_extnum_t room; /* number of entries there's room for */
1215
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 mp = ip->i_mount;
1217 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001218 block = ifp->if_broot;
1219 /*
1220 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1221 */
1222 level = be16_to_cpu(block->bb_level);
1223 ASSERT(level > 0);
1224 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1225 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001226
Dave Chinner9e5987a2013-02-25 12:31:26 +11001227 /*
1228 * Go down the tree until leaf level is reached, following the first
1229 * pointer (leftmost) at each level.
1230 */
1231 while (level-- > 0) {
1232 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1233 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1234 if (error)
1235 return error;
1236 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001237 if (level == 0)
1238 break;
1239 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1240 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001241 XFS_WANT_CORRUPTED_GOTO(mp,
1242 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001243 xfs_trans_brelse(tp, bp);
1244 }
1245 /*
1246 * Here with bp and block set to the leftmost leaf node in the tree.
1247 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001248 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001249 i = 0;
1250 /*
1251 * Loop over all leaf nodes. Copy information to the extent records.
1252 */
1253 for (;;) {
1254 xfs_bmbt_rec_t *frp;
1255 xfs_fsblock_t nextbno;
1256 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001257
1258 num_recs = xfs_btree_get_numrecs(block);
1259 if (unlikely(i + num_recs > room)) {
1260 ASSERT(i + num_recs <= room);
1261 xfs_warn(ip->i_mount,
1262 "corrupt dinode %Lu, (btree extents).",
1263 (unsigned long long) ip->i_ino);
1264 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1265 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1266 goto error0;
1267 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001268 /*
1269 * Read-ahead the next leaf block, if any.
1270 */
1271 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1272 if (nextbno != NULLFSBLOCK)
1273 xfs_btree_reada_bufl(mp, nextbno, 1,
1274 &xfs_bmbt_buf_ops);
1275 /*
1276 * Copy records into the extent records.
1277 */
1278 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001279 for (j = 0; j < num_recs; j++, i++, frp++) {
1280 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1281 trp->l0 = be64_to_cpu(frp->l0);
1282 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001283 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001284 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001285 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001286 goto error0;
1287 }
1288 }
1289 xfs_trans_brelse(tp, bp);
1290 bno = nextbno;
1291 /*
1292 * If we've reached the end, stop.
1293 */
1294 if (bno == NULLFSBLOCK)
1295 break;
1296 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1297 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1298 if (error)
1299 return error;
1300 block = XFS_BUF_TO_BLOCK(bp);
1301 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001302 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1303 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001304 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001305 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1306 return 0;
1307error0:
1308 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001309 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001310}
1311
Dave Chinner9e5987a2013-02-25 12:31:26 +11001312/*
1313 * Returns the file-relative block number of the first unused block(s)
1314 * in the file with at least "len" logically contiguous blocks free.
1315 * This is the lowest-address hole if the file has holes, else the first block
1316 * past the end of file.
1317 * Return 0 if the file is currently local (in-inode).
1318 */
1319int /* error */
1320xfs_bmap_first_unused(
1321 xfs_trans_t *tp, /* transaction pointer */
1322 xfs_inode_t *ip, /* incore inode */
1323 xfs_extlen_t len, /* size of hole to find */
1324 xfs_fileoff_t *first_unused, /* unused block */
1325 int whichfork) /* data or attr fork */
1326{
1327 int error; /* error return value */
1328 int idx; /* extent record index */
1329 xfs_ifork_t *ifp; /* inode fork pointer */
1330 xfs_fileoff_t lastaddr; /* last block number seen */
1331 xfs_fileoff_t lowest; /* lowest useful block */
1332 xfs_fileoff_t max; /* starting useful block */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001333 xfs_extnum_t nextents; /* number of extent entries */
1334
1335 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1336 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1337 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1338 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1339 *first_unused = 0;
1340 return 0;
1341 }
1342 ifp = XFS_IFORK_PTR(ip, whichfork);
1343 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1344 (error = xfs_iread_extents(tp, ip, whichfork)))
1345 return error;
1346 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001347 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001348 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001349 struct xfs_bmbt_irec got;
1350
1351 xfs_iext_get_extent(ifp, idx, &got);
1352
Dave Chinner9e5987a2013-02-25 12:31:26 +11001353 /*
1354 * See if the hole before this extent will work.
1355 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001356 if (got.br_startoff >= lowest + len &&
1357 got.br_startoff - max >= len) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001358 *first_unused = max;
1359 return 0;
1360 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001361 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001362 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1363 }
1364 *first_unused = max;
1365 return 0;
1366}
1367
1368/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001369 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001370 * last_block (input value) in the file.
1371 * This is not based on i_size, it is based on the extent records.
1372 * Returns 0 for local files, as they do not have extent records.
1373 */
1374int /* error */
1375xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001376 struct xfs_trans *tp, /* transaction pointer */
1377 struct xfs_inode *ip, /* incore inode */
1378 xfs_fileoff_t *last_block, /* last block */
1379 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001380{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001381 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1382 struct xfs_bmbt_irec got;
1383 xfs_extnum_t idx;
1384 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001385
Christoph Hellwig86685f72016-11-24 11:39:38 +11001386 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1387 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001388 *last_block = 0;
1389 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001390 case XFS_DINODE_FMT_BTREE:
1391 case XFS_DINODE_FMT_EXTENTS:
1392 break;
1393 default:
1394 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001395 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001396
1397 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1398 error = xfs_iread_extents(tp, ip, whichfork);
1399 if (error)
1400 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001401 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001402
1403 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1404 if (got.br_startoff <= *last_block - 1)
1405 return 0;
1406 }
1407
1408 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1409 *last_block = got.br_startoff + got.br_blockcount;
1410 return 0;
1411 }
1412
1413 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001414 return 0;
1415}
1416
Dave Chinner68988112013-08-12 20:49:42 +10001417int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001418xfs_bmap_last_extent(
1419 struct xfs_trans *tp,
1420 struct xfs_inode *ip,
1421 int whichfork,
1422 struct xfs_bmbt_irec *rec,
1423 int *is_empty)
1424{
1425 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1426 int error;
1427 int nextents;
1428
1429 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1430 error = xfs_iread_extents(tp, ip, whichfork);
1431 if (error)
1432 return error;
1433 }
1434
Eric Sandeen5d829302016-11-08 12:59:42 +11001435 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001436 if (nextents == 0) {
1437 *is_empty = 1;
1438 return 0;
1439 }
1440
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001441 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001442 *is_empty = 0;
1443 return 0;
1444}
1445
1446/*
1447 * Check the last inode extent to determine whether this allocation will result
1448 * in blocks being allocated at the end of the file. When we allocate new data
1449 * blocks at the end of the file which do not start at the previous data block,
1450 * we will try to align the new blocks at stripe unit boundaries.
1451 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001452 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001453 * at, or past the EOF.
1454 */
1455STATIC int
1456xfs_bmap_isaeof(
1457 struct xfs_bmalloca *bma,
1458 int whichfork)
1459{
1460 struct xfs_bmbt_irec rec;
1461 int is_empty;
1462 int error;
1463
Thomas Meyer749f24f2017-10-09 11:38:54 -07001464 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001465 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1466 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001467 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001468 return error;
1469
Dave Chinner6e708bc2013-11-22 10:41:16 +11001470 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001471 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001472 return 0;
1473 }
1474
Dave Chinner9e5987a2013-02-25 12:31:26 +11001475 /*
1476 * Check if we are allocation or past the last extent, or at least into
1477 * the last delayed allocated extent.
1478 */
1479 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1480 (bma->offset >= rec.br_startoff &&
1481 isnullstartblock(rec.br_startblock));
1482 return 0;
1483}
1484
1485/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001486 * Returns the file-relative block number of the first block past eof in
1487 * the file. This is not based on i_size, it is based on the extent records.
1488 * Returns 0 for local files, as they do not have extent records.
1489 */
1490int
1491xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001492 struct xfs_inode *ip,
1493 xfs_fileoff_t *last_block,
1494 int whichfork)
1495{
1496 struct xfs_bmbt_irec rec;
1497 int is_empty;
1498 int error;
1499
1500 *last_block = 0;
1501
1502 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1503 return 0;
1504
1505 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1506 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001507 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001508
1509 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1510 if (error || is_empty)
1511 return error;
1512
1513 *last_block = rec.br_startoff + rec.br_blockcount;
1514 return 0;
1515}
1516
1517/*
1518 * Returns whether the selected fork of the inode has exactly one
1519 * block or not. For the data fork we check this matches di_size,
1520 * implying the file's range is 0..bsize-1.
1521 */
1522int /* 1=>1 block, 0=>otherwise */
1523xfs_bmap_one_block(
1524 xfs_inode_t *ip, /* incore inode */
1525 int whichfork) /* data or attr fork */
1526{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001527 xfs_ifork_t *ifp; /* inode fork pointer */
1528 int rval; /* return value */
1529 xfs_bmbt_irec_t s; /* internal version of extent */
1530
1531#ifndef DEBUG
1532 if (whichfork == XFS_DATA_FORK)
1533 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1534#endif /* !DEBUG */
1535 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1536 return 0;
1537 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1538 return 0;
1539 ifp = XFS_IFORK_PTR(ip, whichfork);
1540 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001541 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001542 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1543 if (rval && whichfork == XFS_DATA_FORK)
1544 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1545 return rval;
1546}
1547
1548/*
1549 * Extent tree manipulation functions used during allocation.
1550 */
1551
1552/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001553 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
1555STATIC int /* error */
1556xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001557 struct xfs_bmalloca *bma,
1558 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001560 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001563 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 xfs_fileoff_t new_endoff; /* end offset of new entry */
1565 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1566 /* left is 0, right is 1, prev is 2 */
1567 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001568 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001569 xfs_filblks_t da_new; /* new count del alloc blocks used */
1570 xfs_filblks_t da_old; /* old count del alloc blocks used */
1571 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001573 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001574 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001575 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001577 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001578 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001579 ASSERT(whichfork != XFS_ATTR_FORK);
1580 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1581 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001582
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001583 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001584 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001585 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001586 ASSERT(!bma->cur ||
1587 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001588
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001589 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591#define LEFT r[0]
1592#define RIGHT r[1]
1593#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /*
1596 * Set up a bunch of variables to make the tests simpler.
1597 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001598 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001600 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 ASSERT(PREV.br_startoff <= new->br_startoff);
1602 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001603
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001604 da_old = startblockval(PREV.br_startblock);
1605 da_new = 0;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /*
1608 * Set flags determining what part of the previous delayed allocation
1609 * extent is being replaced by a real allocation.
1610 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001611 if (PREV.br_startoff == new->br_startoff)
1612 state |= BMAP_LEFT_FILLING;
1613 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1614 state |= BMAP_RIGHT_FILLING;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /*
1617 * Check and set flags if this segment has a left neighbor.
1618 * Don't set contiguous if the combined extent would be too large.
1619 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001620 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001621 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001622 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001623
1624 if (isnullstartblock(LEFT.br_startblock))
1625 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001627
1628 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1629 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1630 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1631 LEFT.br_state == new->br_state &&
1632 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1633 state |= BMAP_LEFT_CONTIG;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * Check and set flags if this segment has a right neighbor.
1637 * Don't set contiguous if the combined extent would be too large.
1638 * Also check for all-three-contiguous being too large.
1639 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001640 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001641 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001642 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001643
1644 if (isnullstartblock(RIGHT.br_startblock))
1645 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001647
1648 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1649 new_endoff == RIGHT.br_startoff &&
1650 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1651 new->br_state == RIGHT.br_state &&
1652 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1653 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1654 BMAP_RIGHT_FILLING)) !=
1655 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1656 BMAP_RIGHT_FILLING) ||
1657 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1658 <= MAXEXTLEN))
1659 state |= BMAP_RIGHT_CONTIG;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 error = 0;
1662 /*
1663 * Switch out based on the FILLING and CONTIG state bits.
1664 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001665 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1666 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1667 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1668 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /*
1670 * Filling in all of a previously delayed allocation extent.
1671 * The left and right neighbors are both contiguous with new.
1672 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001673 bma->idx--;
1674 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001675 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1676 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001677 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001678
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001679 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001680 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001681 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1683 else {
1684 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001685 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001686 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001688 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001689 error = xfs_btree_delete(bma->cur, &i);
1690 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001692 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001693 error = xfs_btree_decrement(bma->cur, 0, &i);
1694 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001696 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001697 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 goto done;
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 break;
1702
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001703 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /*
1705 * Filling in all of a previously delayed allocation extent.
1706 * The left neighbor is contiguous, the right is not.
1707 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001708 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001709
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001710 old = LEFT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001711 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001712 LEFT.br_blockcount += PREV.br_blockcount;
1713 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001714 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001715
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001716 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1717 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 rval = XFS_ILOG_DEXT;
1719 else {
1720 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001721 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001722 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001724 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001725 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001726 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto done;
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
1730
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001731 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /*
1733 * Filling in all of a previously delayed allocation extent.
1734 * The right neighbor is contiguous, the left is not.
1735 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001736 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001737 PREV.br_startblock = new->br_startblock;
1738 PREV.br_blockcount += RIGHT.br_blockcount;
1739 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001741
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001742 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1743 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 rval = XFS_ILOG_DEXT;
1745 else {
1746 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001747 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001750 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001751 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001752 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 goto done;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 break;
1756
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001757 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
1759 * Filling in all of a previously delayed allocation extent.
1760 * Neither the left nor right neighbors are contiguous with
1761 * the new one.
1762 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001763 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001764 PREV.br_startblock = new->br_startblock;
1765 PREV.br_state = new->br_state;
1766 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001767 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001768
Darrick J. Wong60b49842016-10-03 09:11:34 -07001769 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001770 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1772 else {
1773 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001774 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001777 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 error = xfs_btree_insert(bma->cur, &i);
1779 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001781 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 break;
1784
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001785 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /*
1787 * Filling in the first part of a previous delayed allocation.
1788 * The left neighbor is contiguous.
1789 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001790 old = LEFT;
1791 temp = PREV.br_blockcount - new->br_blockcount;
1792 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1793 startblockval(PREV.br_startblock));
1794
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001795 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001796 LEFT.br_blockcount += new->br_blockcount;
1797 xfs_iext_update_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001798 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001799
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001801 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1802 PREV.br_startoff += new->br_blockcount;
1803 PREV.br_startblock = nullstartblock(da_new);
1804 xfs_iext_update_extent(ifp, bma->idx, &PREV);
1805 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1806
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001807 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 rval = XFS_ILOG_DEXT;
1809 else {
1810 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001811 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001814 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001815 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto done;
1818 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001819
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001820 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 break;
1822
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001823 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * Filling in the first part of a previous delayed allocation.
1826 * The left neighbor is not contiguous.
1827 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001828 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001829 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1832 else {
1833 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001834 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001835 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001837 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001838 error = xfs_btree_insert(bma->cur, &i);
1839 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001841 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001843
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001844 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001846 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001847 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 rval |= tmp_rval;
1849 if (error)
1850 goto done;
1851 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001852
1853 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001855 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001856 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001857
1858 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1859 PREV.br_startoff = new_endoff;
1860 PREV.br_blockcount = temp;
1861 PREV.br_startblock = nullstartblock(da_new);
1862 xfs_iext_update_extent(ifp, bma->idx + 1, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 break;
1866
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001867 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * Filling in the last part of a previous delayed allocation.
1870 * The right neighbor is contiguous with the new allocation.
1871 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001872 old = RIGHT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001874 RIGHT.br_startoff = new->br_startoff;
1875 RIGHT.br_startblock = new->br_startblock;
1876 RIGHT.br_blockcount += new->br_blockcount;
1877 xfs_iext_update_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001879
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 rval = XFS_ILOG_DEXT;
1882 else {
1883 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001884 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001887 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001888 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001889 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 goto done;
1891 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001892
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001895 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001896
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001898 PREV.br_blockcount = temp;
1899 PREV.br_startblock = nullstartblock(da_new);
1900 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001902
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 break;
1905
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001906 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
1908 * Filling in the last part of a previous delayed allocation.
1909 * The right neighbor is not contiguous.
1910 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001912 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1915 else {
1916 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001917 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001920 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 error = xfs_btree_insert(bma->cur, &i);
1922 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001924 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001926
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001927 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001929 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001930 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 rval |= tmp_rval;
1932 if (error)
1933 goto done;
1934 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001935
1936 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001938 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001939 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001940
1941 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1942 PREV.br_startblock = nullstartblock(da_new);
1943 PREV.br_blockcount = temp;
1944 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001946
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949
1950 case 0:
1951 /*
1952 * Filling in the middle part of a previous delayed allocation.
1953 * Contiguity is impossible here.
1954 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001955 *
1956 * We start with a delayed allocation:
1957 *
1958 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1959 * PREV @ idx
1960 *
1961 * and we are allocating:
1962 * +rrrrrrrrrrrrrrrrr+
1963 * new
1964 *
1965 * and we set it up for insertion as:
1966 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1967 * new
1968 * PREV @ idx LEFT RIGHT
1969 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001971 old = PREV;
1972
1973 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001974 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001975
1976 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001977 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001978 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001979 RIGHT.br_blockcount =
1980 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1981 RIGHT.br_startblock =
1982 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1983 RIGHT.br_blockcount));
1984
1985 /* truncate PREV */
1986 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
1987 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1988 PREV.br_startblock =
1989 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1990 PREV.br_blockcount));
1991 xfs_iext_update_extent(ifp, bma->idx, &PREV);
1992 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1993
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001994 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001995 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001996 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001997
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001998 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2000 else {
2001 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002002 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002003 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002005 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002006 error = xfs_btree_insert(bma->cur, &i);
2007 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002009 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002011
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002012 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002013 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002014 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002015 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 rval |= tmp_rval;
2017 if (error)
2018 goto done;
2019 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002020
2021 da_new = startblockval(PREV.br_startblock) +
2022 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002023 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002026 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2027 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2028 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2029 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2030 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2031 case BMAP_LEFT_CONTIG:
2032 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /*
2034 * These cases are all impossible.
2035 */
2036 ASSERT(0);
2037 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002038
Darrick J. Wong9c194642016-08-03 12:16:05 +10002039 /* add reverse mapping */
2040 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2041 if (error)
2042 goto done;
2043
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002044 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002045 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002046 int tmp_logflags; /* partial log flag return val */
2047
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 ASSERT(bma->cur == NULL);
2049 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002050 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002051 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002052 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002053 if (error)
2054 goto done;
2055 }
2056
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002057 if (bma->cur) {
2058 da_new += bma->cur->bc_private.b.allocated;
2059 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002060 }
2061
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002062 /* adjust for changes in reserved delayed indirect blocks */
2063 if (da_new != da_old) {
2064 ASSERT(state == 0 || da_new < da_old);
2065 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2066 false);
2067 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002068
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002069 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002071 if (whichfork != XFS_COW_FORK)
2072 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return error;
2074#undef LEFT
2075#undef RIGHT
2076#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
2079/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002080 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 */
2082STATIC int /* error */
2083xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002084 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002086 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002087 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002089 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002090 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002091 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002092 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002097 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2100 /* left is 0, right is 1, prev is 2 */
2101 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002102 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002103 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002104 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002106 *logflagsp = 0;
2107
2108 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002109 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002110
2111 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002112 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002113 ASSERT(!isnullstartblock(new->br_startblock));
2114
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002115 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117#define LEFT r[0]
2118#define RIGHT r[1]
2119#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /*
2122 * Set up a bunch of variables to make the tests simpler.
2123 */
2124 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002125 xfs_iext_get_extent(ifp, *idx, &PREV);
2126 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 new_endoff = new->br_startoff + new->br_blockcount;
2128 ASSERT(PREV.br_startoff <= new->br_startoff);
2129 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
2132 * Set flags determining what part of the previous oldext allocation
2133 * extent is being replaced by a newext allocation.
2134 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002135 if (PREV.br_startoff == new->br_startoff)
2136 state |= BMAP_LEFT_FILLING;
2137 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2138 state |= BMAP_RIGHT_FILLING;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /*
2141 * Check and set flags if this segment has a left neighbor.
2142 * Don't set contiguous if the combined extent would be too large.
2143 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002144 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002145 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002146 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002147
2148 if (isnullstartblock(LEFT.br_startblock))
2149 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002151
2152 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2153 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2154 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002155 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002156 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2157 state |= BMAP_LEFT_CONTIG;
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /*
2160 * Check and set flags if this segment has a right neighbor.
2161 * Don't set contiguous if the combined extent would be too large.
2162 * Also check for all-three-contiguous being too large.
2163 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002164 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002165 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002166 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002167 if (isnullstartblock(RIGHT.br_startblock))
2168 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002170
2171 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2172 new_endoff == RIGHT.br_startoff &&
2173 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002174 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002175 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2176 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2177 BMAP_RIGHT_FILLING)) !=
2178 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2179 BMAP_RIGHT_FILLING) ||
2180 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2181 <= MAXEXTLEN))
2182 state |= BMAP_RIGHT_CONTIG;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /*
2185 * Switch out based on the FILLING and CONTIG state bits.
2186 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002187 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2188 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2189 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2190 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /*
2192 * Setting all of a previous oldext extent to newext.
2193 * The left and right neighbors are both contiguous with new.
2194 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002195 --*idx;
2196
2197 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002198 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2199 xfs_iext_update_extent(ifp, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002200 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002201
Christoph Hellwigec90c552011-05-23 08:52:53 +00002202 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002203 XFS_IFORK_NEXT_SET(ip, whichfork,
2204 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (cur == NULL)
2206 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2207 else {
2208 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002209 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2210 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002212 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002213 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002215 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002216 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002218 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002219 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002221 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002222 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002224 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002225 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002226 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 goto done;
2228 }
2229 break;
2230
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002231 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /*
2233 * Setting all of a previous oldext extent to newext.
2234 * The left neighbor is contiguous, the right is not.
2235 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002236 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002237
Christoph Hellwigec90c552011-05-23 08:52:53 +00002238 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002239 LEFT.br_blockcount += PREV.br_blockcount;
2240 xfs_iext_update_extent(ifp, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002241 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2242
2243 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002244 XFS_IFORK_NEXT_SET(ip, whichfork,
2245 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (cur == NULL)
2247 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2248 else {
2249 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002250 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2251 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002253 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002254 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002256 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002257 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002259 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002260 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002261 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto done;
2263 }
2264 break;
2265
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002266 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /*
2268 * Setting all of a previous oldext extent to newext.
2269 * The right neighbor is contiguous, the left is not.
2270 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002271 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002272 PREV.br_blockcount += RIGHT.br_blockcount;
2273 PREV.br_state = new->br_state;
2274 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002275 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002276
Christoph Hellwigec90c552011-05-23 08:52:53 +00002277 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002278 XFS_IFORK_NEXT_SET(ip, whichfork,
2279 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (cur == NULL)
2281 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2282 else {
2283 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002284 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2285 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002287 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002288 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002290 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002291 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002294 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002295 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto done;
2297 }
2298 break;
2299
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002300 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 /*
2302 * Setting all of a previous oldext extent to newext.
2303 * Neither the left nor right neighbors are contiguous with
2304 * the new one.
2305 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002306 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002307 PREV.br_state = new->br_state;
2308 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002309 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 if (cur == NULL)
2312 rval = XFS_ILOG_DEXT;
2313 else {
2314 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002315 error = xfs_bmbt_lookup_eq(cur, new, &i);
2316 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002318 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002319 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002320 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 goto done;
2322 }
2323 break;
2324
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002325 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /*
2327 * Setting the first part of a previous oldext extent to newext.
2328 * The left neighbor is contiguous.
2329 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002330 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002331 LEFT.br_blockcount += new->br_blockcount;
2332 xfs_iext_update_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002333 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002334
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002335 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002336 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002337 PREV.br_startoff += new->br_blockcount;
2338 PREV.br_startblock += new->br_blockcount;
2339 PREV.br_blockcount -= new->br_blockcount;
2340 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002341 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002342
Christoph Hellwigec90c552011-05-23 08:52:53 +00002343 --*idx;
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (cur == NULL)
2346 rval = XFS_ILOG_DEXT;
2347 else {
2348 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002349 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002350 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002352 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002353 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002356 error = xfs_btree_decrement(cur, 0, &i);
2357 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002359 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002360 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto done;
2362 }
2363 break;
2364
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002365 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 /*
2367 * Setting the first part of a previous oldext extent to newext.
2368 * The left neighbor is not contiguous.
2369 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002370 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002371 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002372 PREV.br_startoff += new->br_blockcount;
2373 PREV.br_startblock += new->br_blockcount;
2374 PREV.br_blockcount -= new->br_blockcount;
2375 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002376 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002377
Christoph Hellwigec90c552011-05-23 08:52:53 +00002378 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002379 XFS_IFORK_NEXT_SET(ip, whichfork,
2380 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (cur == NULL)
2382 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2383 else {
2384 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002385 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002386 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002388 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002389 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002390 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 goto done;
2392 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002393 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002395 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397 break;
2398
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002399 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /*
2401 * Setting the last part of a previous oldext extent to newext.
2402 * The right neighbor is contiguous with the new allocation.
2403 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002404 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002405 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002406 PREV.br_blockcount -= new->br_blockcount;
2407 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002408 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2409
2410 ++*idx;
2411
2412 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002413 RIGHT.br_startoff = new->br_startoff;
2414 RIGHT.br_startblock = new->br_startblock;
2415 RIGHT.br_blockcount += new->br_blockcount;
2416 xfs_iext_update_extent(ifp, *idx, &RIGHT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002417 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (cur == NULL)
2420 rval = XFS_ILOG_DEXT;
2421 else {
2422 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002423 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002424 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002426 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002427 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002428 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002430 error = xfs_btree_increment(cur, 0, &i);
2431 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002433 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002434 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 goto done;
2436 }
2437 break;
2438
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002439 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /*
2441 * Setting the last part of a previous oldext extent to newext.
2442 * The right neighbor is not contiguous.
2443 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002444 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002445 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002446 PREV.br_blockcount -= new->br_blockcount;
2447 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002448 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002449
Christoph Hellwigec90c552011-05-23 08:52:53 +00002450 ++*idx;
2451 xfs_iext_insert(ip, *idx, 1, new, state);
2452
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002453 XFS_IFORK_NEXT_SET(ip, whichfork,
2454 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (cur == NULL)
2456 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2457 else {
2458 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002459 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002460 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 == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002463 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002464 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002466 error = xfs_bmbt_lookup_eq(cur, new, &i);
2467 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002469 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002470 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002472 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
2474 break;
2475
2476 case 0:
2477 /*
2478 * Setting the middle part of a previous oldext extent to
2479 * newext. Contiguity is impossible here.
2480 * One extent becomes three extents.
2481 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002482 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002483 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002484 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2485 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002486 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 r[0] = *new;
2489 r[1].br_startoff = new_endoff;
2490 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002491 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002493 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002494
2495 ++*idx;
2496 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2497
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002498 XFS_IFORK_NEXT_SET(ip, whichfork,
2499 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (cur == NULL)
2501 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2502 else {
2503 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002504 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002505 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002507 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002509 error = xfs_bmbt_update(cur, &r[1]);
2510 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 goto done;
2512 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002514 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002516 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002517 /*
2518 * Reset the cursor to the position of the new extent
2519 * we are about to insert as we can't trust it after
2520 * the previous insert.
2521 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002522 error = xfs_bmbt_lookup_eq(cur, new, &i);
2523 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002525 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002527 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002529 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 break;
2532
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002533 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2534 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2535 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2536 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2537 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2538 case BMAP_LEFT_CONTIG:
2539 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * These cases are all impossible.
2542 */
2543 ASSERT(0);
2544 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002545
Darrick J. Wong9c194642016-08-03 12:16:05 +10002546 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002547 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002548 if (error)
2549 goto done;
2550
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002551 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002552 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002553 int tmp_logflags; /* partial log flag return val */
2554
2555 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002556 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002557 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002558 *logflagsp |= tmp_logflags;
2559 if (error)
2560 goto done;
2561 }
2562
2563 /* clear out the allocated field, done with it now in any case. */
2564 if (cur) {
2565 cur->bc_private.b.allocated = 0;
2566 *curp = cur;
2567 }
2568
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002569 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002571 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 return error;
2573#undef LEFT
2574#undef RIGHT
2575#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
2578/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002579 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002581STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582xfs_bmap_add_extent_hole_delay(
2583 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002584 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002586 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002588 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2590 xfs_filblks_t newlen=0; /* new indirect size */
2591 xfs_filblks_t oldlen=0; /* old indirect size */
2592 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002593 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002594 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002596 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002597 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /*
2600 * Check and set flags if this segment has a left neighbor
2601 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002602 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002603 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002604 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002605
2606 if (isnullstartblock(left.br_startblock))
2607 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /*
2611 * Check and set flags if the current (right) segment exists.
2612 * If it doesn't exist, we're converting the hole at end-of-file.
2613 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002614 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002615 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002616 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002617
2618 if (isnullstartblock(right.br_startblock))
2619 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /*
2623 * Set contiguity flags on the left and right neighbors.
2624 * Don't let extents get too large, even if the pieces are contiguous.
2625 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002626 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2627 left.br_startoff + left.br_blockcount == new->br_startoff &&
2628 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2629 state |= BMAP_LEFT_CONTIG;
2630
2631 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2632 new->br_startoff + new->br_blockcount == right.br_startoff &&
2633 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2634 (!(state & BMAP_LEFT_CONTIG) ||
2635 (left.br_blockcount + new->br_blockcount +
2636 right.br_blockcount <= MAXEXTLEN)))
2637 state |= BMAP_RIGHT_CONTIG;
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /*
2640 * Switch out based on the contiguity flags.
2641 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002642 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2643 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 /*
2645 * New allocation is contiguous with delayed allocations
2646 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002647 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002649 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 temp = left.br_blockcount + new->br_blockcount +
2651 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002652
Christoph Hellwigec90c552011-05-23 08:52:53 +00002653 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002654 oldlen = startblockval(left.br_startblock) +
2655 startblockval(new->br_startblock) +
2656 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002657 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2658 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002659 left.br_startblock = nullstartblock(newlen);
2660 left.br_blockcount = temp;
2661 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002662 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002663
Christoph Hellwigec90c552011-05-23 08:52:53 +00002664 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 break;
2666
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002667 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 /*
2669 * New allocation is contiguous with a delayed allocation
2670 * on the left.
2671 * Merge the new allocation with the left neighbor.
2672 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002673 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002675
2676 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002677 oldlen = startblockval(left.br_startblock) +
2678 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002679 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2680 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002681 left.br_blockcount = temp;
2682 left.br_startblock = nullstartblock(newlen);
2683 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002684 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 break;
2686
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002687 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 /*
2689 * New allocation is contiguous with a delayed allocation
2690 * on the right.
2691 * Merge the new allocation with the right neighbor.
2692 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002693 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002695 oldlen = startblockval(new->br_startblock) +
2696 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002697 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2698 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002699 right.br_startoff = new->br_startoff;
2700 right.br_startblock = nullstartblock(newlen);
2701 right.br_blockcount = temp;
2702 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002703 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 break;
2705
2706 case 0:
2707 /*
2708 * New allocation is not contiguous with another
2709 * delayed allocation.
2710 * Insert a new entry.
2711 */
2712 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002713 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 break;
2715 }
2716 if (oldlen != newlen) {
2717 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002718 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2719 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 /*
2721 * Nothing to do for disk quota accounting here.
2722 */
2723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
2726/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002727 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 */
2729STATIC int /* error */
2730xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002731 struct xfs_trans *tp,
2732 struct xfs_inode *ip,
2733 int whichfork,
2734 xfs_extnum_t *idx,
2735 struct xfs_btree_cur **curp,
2736 struct xfs_bmbt_irec *new,
2737 xfs_fsblock_t *first,
2738 struct xfs_defer_ops *dfops,
2739 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002741 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2742 struct xfs_mount *mp = ip->i_mount;
2743 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2747 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002748 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002749 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002750 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Christoph Hellwig6d045582017-04-11 16:45:54 -07002752 ASSERT(*idx >= 0);
2753 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002754 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002755 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002756
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002757 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /*
2760 * Check and set flags if this segment has a left neighbor.
2761 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002762 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002763 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002764 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002765 if (isnullstartblock(left.br_startblock))
2766 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /*
2770 * Check and set flags if this segment has a current value.
2771 * Not true if we're inserting into the "hole" at eof.
2772 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002773 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002774 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002775 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002776 if (isnullstartblock(right.br_startblock))
2777 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 /*
2781 * We're inserting a real allocation between "left" and "right".
2782 * Set the contiguity flags. Don't let extents get too large.
2783 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002784 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2785 left.br_startoff + left.br_blockcount == new->br_startoff &&
2786 left.br_startblock + left.br_blockcount == new->br_startblock &&
2787 left.br_state == new->br_state &&
2788 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2789 state |= BMAP_LEFT_CONTIG;
2790
2791 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2792 new->br_startoff + new->br_blockcount == right.br_startoff &&
2793 new->br_startblock + new->br_blockcount == right.br_startblock &&
2794 new->br_state == right.br_state &&
2795 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2796 (!(state & BMAP_LEFT_CONTIG) ||
2797 left.br_blockcount + new->br_blockcount +
2798 right.br_blockcount <= MAXEXTLEN))
2799 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002801 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /*
2803 * Select which case we're in here, and implement it.
2804 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002805 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2806 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 /*
2808 * New allocation is contiguous with real allocations on the
2809 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002810 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002812 --*idx;
2813 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002814 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2815 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002816 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002817
Christoph Hellwig6d045582017-04-11 16:45:54 -07002818 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002819
Christoph Hellwig6d045582017-04-11 16:45:54 -07002820 XFS_IFORK_NEXT_SET(ip, whichfork,
2821 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2822 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002823 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002824 } else {
2825 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002826 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002827 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002828 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002829 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002830 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002831 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002832 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002833 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002834 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002835 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002836 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002837 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002838 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002839 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002840 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002842 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002844 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 /*
2846 * New allocation is contiguous with a real allocation
2847 * on the left.
2848 * Merge the new allocation with the left neighbor.
2849 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002851 old = left;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002852 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002853 left.br_blockcount += new->br_blockcount;
2854 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002855 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002856
Christoph Hellwig6d045582017-04-11 16:45:54 -07002857 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002858 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002859 } else {
2860 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002861 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002862 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002863 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002864 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002865 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002866 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002867 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002869 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002871 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /*
2873 * New allocation is contiguous with a real allocation
2874 * on the right.
2875 * Merge the new allocation with the right neighbor.
2876 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002877 old = right;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002878 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002879 right.br_startoff = new->br_startoff;
2880 right.br_startblock = new->br_startblock;
2881 right.br_blockcount += new->br_blockcount;
2882 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002883 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002884
Christoph Hellwig6d045582017-04-11 16:45:54 -07002885 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002886 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002887 } else {
2888 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002889 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002890 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002891 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002892 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002893 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002894 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002895 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002897 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
2899 case 0:
2900 /*
2901 * New allocation is not contiguous with another
2902 * real allocation.
2903 * Insert a new entry.
2904 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002905 xfs_iext_insert(ip, *idx, 1, new, state);
2906 XFS_IFORK_NEXT_SET(ip, whichfork,
2907 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2908 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002909 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002910 } else {
2911 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002912 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002913 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002914 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002915 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002916 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002917 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002918 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002919 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002923
Darrick J. Wong9c194642016-08-03 12:16:05 +10002924 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002926 if (error)
2927 goto done;
2928
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002929 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002930 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002931 int tmp_logflags; /* partial log flag return val */
2932
Christoph Hellwig6d045582017-04-11 16:45:54 -07002933 ASSERT(cur == NULL);
2934 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002935 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002936 *logflagsp |= tmp_logflags;
2937 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002938 if (error)
2939 goto done;
2940 }
2941
2942 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002943 if (cur)
2944 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002945
Christoph Hellwig6d045582017-04-11 16:45:54 -07002946 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002947done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002948 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002949 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
Nathan Scottdd9f4382006-01-11 15:28:28 +11002952/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002953 * Functions used in the extent read, allocate and remove paths
2954 */
2955
2956/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002957 * Adjust the size of the new extent based on di_extsize and rt extsize.
2958 */
Dave Chinner68988112013-08-12 20:49:42 +10002959int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002960xfs_bmap_extsize_align(
2961 xfs_mount_t *mp,
2962 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2963 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2964 xfs_extlen_t extsz, /* align to this extent size */
2965 int rt, /* is this a realtime inode? */
2966 int eof, /* is extent at end-of-file? */
2967 int delay, /* creating delalloc extent? */
2968 int convert, /* overwriting unwritten extent? */
2969 xfs_fileoff_t *offp, /* in/out: aligned offset */
2970 xfs_extlen_t *lenp) /* in/out: aligned length */
2971{
2972 xfs_fileoff_t orig_off; /* original offset */
2973 xfs_extlen_t orig_alen; /* original length */
2974 xfs_fileoff_t orig_end; /* original off+len */
2975 xfs_fileoff_t nexto; /* next file offset */
2976 xfs_fileoff_t prevo; /* previous file offset */
2977 xfs_fileoff_t align_off; /* temp for offset */
2978 xfs_extlen_t align_alen; /* temp for length */
2979 xfs_extlen_t temp; /* temp for calculations */
2980
2981 if (convert)
2982 return 0;
2983
2984 orig_off = align_off = *offp;
2985 orig_alen = align_alen = *lenp;
2986 orig_end = orig_off + orig_alen;
2987
2988 /*
2989 * If this request overlaps an existing extent, then don't
2990 * attempt to perform any additional alignment.
2991 */
2992 if (!delay && !eof &&
2993 (orig_off >= gotp->br_startoff) &&
2994 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2995 return 0;
2996 }
2997
2998 /*
2999 * If the file offset is unaligned vs. the extent size
3000 * we need to align it. This will be possible unless
3001 * the file was previously written with a kernel that didn't
3002 * perform this alignment, or if a truncate shot us in the
3003 * foot.
3004 */
3005 temp = do_mod(orig_off, extsz);
3006 if (temp) {
3007 align_alen += temp;
3008 align_off -= temp;
3009 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003010
3011 /* Same adjustment for the end of the requested area. */
3012 temp = (align_alen % extsz);
3013 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003014 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003015
3016 /*
3017 * For large extent hint sizes, the aligned extent might be larger than
3018 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3019 * the length back under MAXEXTLEN. The outer allocation loops handle
3020 * short allocation just fine, so it is safe to do this. We only want to
3021 * do it when we are forced to, though, because it means more allocation
3022 * operations are required.
3023 */
3024 while (align_alen > MAXEXTLEN)
3025 align_alen -= extsz;
3026 ASSERT(align_alen <= MAXEXTLEN);
3027
Nathan Scottdd9f4382006-01-11 15:28:28 +11003028 /*
3029 * If the previous block overlaps with this proposed allocation
3030 * then move the start forward without adjusting the length.
3031 */
3032 if (prevp->br_startoff != NULLFILEOFF) {
3033 if (prevp->br_startblock == HOLESTARTBLOCK)
3034 prevo = prevp->br_startoff;
3035 else
3036 prevo = prevp->br_startoff + prevp->br_blockcount;
3037 } else
3038 prevo = 0;
3039 if (align_off != orig_off && align_off < prevo)
3040 align_off = prevo;
3041 /*
3042 * If the next block overlaps with this proposed allocation
3043 * then move the start back without adjusting the length,
3044 * but not before offset 0.
3045 * This may of course make the start overlap previous block,
3046 * and if we hit the offset 0 limit then the next block
3047 * can still overlap too.
3048 */
3049 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3050 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3051 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3052 nexto = gotp->br_startoff + gotp->br_blockcount;
3053 else
3054 nexto = gotp->br_startoff;
3055 } else
3056 nexto = NULLFILEOFF;
3057 if (!eof &&
3058 align_off + align_alen != orig_end &&
3059 align_off + align_alen > nexto)
3060 align_off = nexto > align_alen ? nexto - align_alen : 0;
3061 /*
3062 * If we're now overlapping the next or previous extent that
3063 * means we can't fit an extsz piece in this hole. Just move
3064 * the start forward to the first valid spot and set
3065 * the length so we hit the end.
3066 */
3067 if (align_off != orig_off && align_off < prevo)
3068 align_off = prevo;
3069 if (align_off + align_alen != orig_end &&
3070 align_off + align_alen > nexto &&
3071 nexto != NULLFILEOFF) {
3072 ASSERT(nexto > prevo);
3073 align_alen = nexto - align_off;
3074 }
3075
3076 /*
3077 * If realtime, and the result isn't a multiple of the realtime
3078 * extent size we need to remove blocks until it is.
3079 */
3080 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3081 /*
3082 * We're not covering the original request, or
3083 * we won't be able to once we fix the length.
3084 */
3085 if (orig_off < align_off ||
3086 orig_end > align_off + align_alen ||
3087 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003088 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003089 /*
3090 * Try to fix it by moving the start up.
3091 */
3092 if (align_off + temp <= orig_off) {
3093 align_alen -= temp;
3094 align_off += temp;
3095 }
3096 /*
3097 * Try to fix it by moving the end in.
3098 */
3099 else if (align_off + align_alen - temp >= orig_end)
3100 align_alen -= temp;
3101 /*
3102 * Set the start to the minimum then trim the length.
3103 */
3104 else {
3105 align_alen -= orig_off - align_off;
3106 align_off = orig_off;
3107 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3108 }
3109 /*
3110 * Result doesn't cover the request, fail it.
3111 */
3112 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003113 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003114 } else {
3115 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003116 /* see MAXEXTLEN handling above */
3117 ASSERT(orig_end <= align_off + align_alen ||
3118 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003119 }
3120
3121#ifdef DEBUG
3122 if (!eof && gotp->br_startoff != NULLFILEOFF)
3123 ASSERT(align_off + align_alen <= gotp->br_startoff);
3124 if (prevp->br_startoff != NULLFILEOFF)
3125 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3126#endif
3127
3128 *lenp = align_alen;
3129 *offp = align_off;
3130 return 0;
3131}
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133#define XFS_ALLOC_GAP_UNITS 4
3134
Dave Chinner68988112013-08-12 20:49:42 +10003135void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003136xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003137 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
3139 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3141 xfs_mount_t *mp; /* mount point structure */
3142 int nullfb; /* true if ap->firstblock isn't set */
3143 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145#define ISVALID(x,y) \
3146 (rt ? \
3147 (x) < mp->m_sb.sb_rblocks : \
3148 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3149 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3150 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003153 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003154 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3155 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003156 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /*
3158 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003159 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003161 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3162 !isnullstartblock(ap->prev.br_startblock) &&
3163 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3164 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003165 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 /*
3167 * Adjust for the gap between prevp and us.
3168 */
Dave Chinner3a756672011-09-18 20:40:58 +00003169 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003170 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003172 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3173 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175 /*
3176 * If not at eof, then compare the two neighbor blocks.
3177 * Figure out whether either one gives us a good starting point,
3178 * and pick the better one.
3179 */
3180 else if (!ap->eof) {
3181 xfs_fsblock_t gotbno; /* right side block number */
3182 xfs_fsblock_t gotdiff=0; /* right side difference */
3183 xfs_fsblock_t prevbno; /* left side block number */
3184 xfs_fsblock_t prevdiff=0; /* left side difference */
3185
3186 /*
3187 * If there's a previous (left) block, select a requested
3188 * start block based on it.
3189 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003190 if (ap->prev.br_startoff != NULLFILEOFF &&
3191 !isnullstartblock(ap->prev.br_startblock) &&
3192 (prevbno = ap->prev.br_startblock +
3193 ap->prev.br_blockcount) &&
3194 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 /*
3196 * Calculate gap to end of previous block.
3197 */
Dave Chinner3a756672011-09-18 20:40:58 +00003198 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003199 (ap->prev.br_startoff +
3200 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 /*
3202 * Figure the startblock based on the previous block's
3203 * end and the gap size.
3204 * Heuristic!
3205 * If the gap is large relative to the piece we're
3206 * allocating, or using it gives us an invalid block
3207 * number, then just use the end of the previous block.
3208 */
Dave Chinner3a756672011-09-18 20:40:58 +00003209 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003211 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 prevbno += adjust;
3213 else
3214 prevdiff += adjust;
3215 /*
3216 * If the firstblock forbids it, can't use it,
3217 * must use default.
3218 */
3219 if (!rt && !nullfb &&
3220 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3221 prevbno = NULLFSBLOCK;
3222 }
3223 /*
3224 * No previous block or can't follow it, just default.
3225 */
3226 else
3227 prevbno = NULLFSBLOCK;
3228 /*
3229 * If there's a following (right) block, select a requested
3230 * start block based on it.
3231 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003232 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 /*
3234 * Calculate gap to start of next block.
3235 */
Dave Chinner3a756672011-09-18 20:40:58 +00003236 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 /*
3238 * Figure the startblock based on the next block's
3239 * start and the gap size.
3240 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003241 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 /*
3243 * Heuristic!
3244 * If the gap is large relative to the piece we're
3245 * allocating, or using it gives us an invalid block
3246 * number, then just use the start of the next block
3247 * offset by our length.
3248 */
Dave Chinner3a756672011-09-18 20:40:58 +00003249 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 ISVALID(gotbno - gotdiff, gotbno))
3251 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003252 else if (ISVALID(gotbno - ap->length, gotbno)) {
3253 gotbno -= ap->length;
3254 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 } else
3256 gotdiff += adjust;
3257 /*
3258 * If the firstblock forbids it, can't use it,
3259 * must use default.
3260 */
3261 if (!rt && !nullfb &&
3262 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3263 gotbno = NULLFSBLOCK;
3264 }
3265 /*
3266 * No next block, just default.
3267 */
3268 else
3269 gotbno = NULLFSBLOCK;
3270 /*
3271 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003272 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 */
3274 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003275 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003277 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003279 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003281#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003282}
3283
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003284static int
3285xfs_bmap_longest_free_extent(
3286 struct xfs_trans *tp,
3287 xfs_agnumber_t ag,
3288 xfs_extlen_t *blen,
3289 int *notinit)
3290{
3291 struct xfs_mount *mp = tp->t_mountp;
3292 struct xfs_perag *pag;
3293 xfs_extlen_t longest;
3294 int error = 0;
3295
3296 pag = xfs_perag_get(mp, ag);
3297 if (!pag->pagf_init) {
3298 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3299 if (error)
3300 goto out;
3301
3302 if (!pag->pagf_init) {
3303 *notinit = 1;
3304 goto out;
3305 }
3306 }
3307
Dave Chinner50adbcb2015-06-22 10:04:31 +10003308 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003309 xfs_alloc_min_freelist(mp, pag),
3310 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003311 if (*blen < longest)
3312 *blen = longest;
3313
3314out:
3315 xfs_perag_put(pag);
3316 return error;
3317}
3318
3319static void
3320xfs_bmap_select_minlen(
3321 struct xfs_bmalloca *ap,
3322 struct xfs_alloc_arg *args,
3323 xfs_extlen_t *blen,
3324 int notinit)
3325{
3326 if (notinit || *blen < ap->minlen) {
3327 /*
3328 * Since we did a BUF_TRYLOCK above, it is possible that
3329 * there is space for this request.
3330 */
3331 args->minlen = ap->minlen;
3332 } else if (*blen < args->maxlen) {
3333 /*
3334 * If the best seen length is less than the request length,
3335 * use the best as the minimum.
3336 */
3337 args->minlen = *blen;
3338 } else {
3339 /*
3340 * Otherwise we've seen an extent as big as maxlen, use that
3341 * as the minimum.
3342 */
3343 args->minlen = args->maxlen;
3344 }
3345}
3346
Nathan Scotta365bdd2006-03-14 13:34:16 +11003347STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003348xfs_bmap_btalloc_nullfb(
3349 struct xfs_bmalloca *ap,
3350 struct xfs_alloc_arg *args,
3351 xfs_extlen_t *blen)
3352{
3353 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003354 xfs_agnumber_t ag, startag;
3355 int notinit = 0;
3356 int error;
3357
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003358 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003359 args->total = ap->total;
3360
Christoph Hellwigc467c042010-02-15 23:34:42 +00003361 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3362 if (startag == NULLAGNUMBER)
3363 startag = ag = 0;
3364
Dave Chinner14b064c2011-01-27 12:16:28 +11003365 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003366 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3367 &notinit);
3368 if (error)
3369 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003370
Christoph Hellwigc467c042010-02-15 23:34:42 +00003371 if (++ag == mp->m_sb.sb_agcount)
3372 ag = 0;
3373 if (ag == startag)
3374 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003375 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003376
3377 xfs_bmap_select_minlen(ap, args, blen, notinit);
3378 return 0;
3379}
3380
3381STATIC int
3382xfs_bmap_btalloc_filestreams(
3383 struct xfs_bmalloca *ap,
3384 struct xfs_alloc_arg *args,
3385 xfs_extlen_t *blen)
3386{
3387 struct xfs_mount *mp = ap->ip->i_mount;
3388 xfs_agnumber_t ag;
3389 int notinit = 0;
3390 int error;
3391
3392 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3393 args->total = ap->total;
3394
3395 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3396 if (ag == NULLAGNUMBER)
3397 ag = 0;
3398
3399 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3400 if (error)
3401 return error;
3402
3403 if (*blen < args->maxlen) {
3404 error = xfs_filestream_new_ag(ap, &ag);
3405 if (error)
3406 return error;
3407
3408 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3409 &notinit);
3410 if (error)
3411 return error;
3412
3413 }
3414
3415 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003416
3417 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003418 * Set the failure fallback case to look in the selected AG as stream
3419 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003420 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003421 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003422 return 0;
3423}
3424
3425STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003426xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003427 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003428{
3429 xfs_mount_t *mp; /* mount point structure */
3430 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003431 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003432 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003433 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003434 xfs_alloc_arg_t args;
3435 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003436 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003437 int nullfb; /* true if ap->firstblock isn't set */
3438 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003439 int tryagain;
3440 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003441 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003442
Dave Chinnera99ebf42011-12-01 11:24:20 +00003443 ASSERT(ap->length);
3444
Nathan Scotta365bdd2006-03-14 13:34:16 +11003445 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003446
3447 /* stripe alignment for allocation is determined by mount parameters */
3448 stripe_align = 0;
3449 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3450 stripe_align = mp->m_swidth;
3451 else if (mp->m_dalign)
3452 stripe_align = mp->m_dalign;
3453
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003454 if (ap->flags & XFS_BMAPI_COWFORK)
3455 align = xfs_get_cowextsz_hint(ap->ip);
3456 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003457 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003458 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003459 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003460 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003461 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003462 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003463 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003464 }
Dave Chinner33177f052013-12-12 16:34:36 +11003465
3466
Dave Chinner0937e0f2011-09-18 20:40:57 +00003467 nullfb = *ap->firstblock == NULLFSBLOCK;
3468 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003469 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003470 if (xfs_alloc_is_userdata(ap->datatype) &&
3471 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003472 ag = xfs_filestream_lookup_ag(ap->ip);
3473 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003474 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003475 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003476 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003477 }
3478 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003479 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003480
3481 xfs_bmap_adjacent(ap);
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003484 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 * it's in the right allocation group.
3486 */
Dave Chinner3a756672011-09-18 20:40:58 +00003487 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 ;
3489 else
Dave Chinner3a756672011-09-18 20:40:58 +00003490 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 * Normal allocation, done through xfs_alloc_vextent.
3493 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003494 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003495 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003496 args.tp = ap->tp;
3497 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003498 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003499 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003500
3501 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003502 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003503 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003504 blen = 0;
3505 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003506 /*
3507 * Search for an allocation group with a single extent large
3508 * enough for the request. If one isn't found, then adjust
3509 * the minimum allocation size to the largest space found.
3510 */
Dave Chinner292378e2016-09-26 08:21:28 +10003511 if (xfs_alloc_is_userdata(ap->datatype) &&
3512 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003513 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3514 else
3515 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003516 if (error)
3517 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003518 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003519 if (xfs_inode_is_filestream(ap->ip))
3520 args.type = XFS_ALLOCTYPE_FIRST_AG;
3521 else
3522 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003523 args.total = args.minlen = ap->minlen;
3524 } else {
3525 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3526 args.total = ap->total;
3527 args.minlen = ap->minlen;
3528 }
David Chinner957d0eb2007-06-18 16:50:37 +10003529 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003530 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003531 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003532 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003533 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003534 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003535 args.prod = 1;
3536 args.mod = 0;
3537 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003538 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003539 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003540 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3541 }
3542 /*
3543 * If we are not low on available data blocks, and the
3544 * underlying logical volume manager is a stripe, and
3545 * the file offset is zero then try to allocate data
3546 * blocks on stripe unit boundary.
3547 * NOTE: ap->aeof is only set if the allocation length
3548 * is >= the stripe unit and the allocation offset is
3549 * at the end of file.
3550 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003551 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003552 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003553 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003554 atype = args.type;
3555 isaligned = 1;
3556 /*
3557 * Adjust for alignment
3558 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003559 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003560 args.minlen = blen - args.alignment;
3561 args.minalignslop = 0;
3562 } else {
3563 /*
3564 * First try an exact bno allocation.
3565 * If it fails then do a near or start bno
3566 * allocation with alignment turned on.
3567 */
3568 atype = args.type;
3569 tryagain = 1;
3570 args.type = XFS_ALLOCTYPE_THIS_BNO;
3571 args.alignment = 1;
3572 /*
3573 * Compute the minlen+alignment for the
3574 * next case. Set slop so that the value
3575 * of minlen+alignment+slop doesn't go up
3576 * between the calls.
3577 */
Dave Chinner33177f052013-12-12 16:34:36 +11003578 if (blen > stripe_align && blen <= args.maxlen)
3579 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003580 else
3581 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003582 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003583 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003584 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003585 args.minlen - 1;
3586 else
3587 args.minalignslop = 0;
3588 }
3589 } else {
3590 args.alignment = 1;
3591 args.minalignslop = 0;
3592 }
3593 args.minleft = ap->minleft;
3594 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003595 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003596 args.datatype = ap->datatype;
3597 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003598 args.ip = ap->ip;
3599
3600 error = xfs_alloc_vextent(&args);
3601 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003603
Nathan Scotta365bdd2006-03-14 13:34:16 +11003604 if (tryagain && args.fsbno == NULLFSBLOCK) {
3605 /*
3606 * Exact allocation failed. Now try with alignment
3607 * turned on.
3608 */
3609 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003610 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003611 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003612 args.minlen = nextminlen;
3613 args.minalignslop = 0;
3614 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 if ((error = xfs_alloc_vextent(&args)))
3616 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003617 }
3618 if (isaligned && args.fsbno == NULLFSBLOCK) {
3619 /*
3620 * allocation failed, so turn off alignment and
3621 * try again.
3622 */
3623 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003624 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003625 args.alignment = 0;
3626 if ((error = xfs_alloc_vextent(&args)))
3627 return error;
3628 }
3629 if (args.fsbno == NULLFSBLOCK && nullfb &&
3630 args.minlen > ap->minlen) {
3631 args.minlen = ap->minlen;
3632 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003633 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003634 if ((error = xfs_alloc_vextent(&args)))
3635 return error;
3636 }
3637 if (args.fsbno == NULLFSBLOCK && nullfb) {
3638 args.fsbno = 0;
3639 args.type = XFS_ALLOCTYPE_FIRST_AG;
3640 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003641 if ((error = xfs_alloc_vextent(&args)))
3642 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003643 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003644 }
3645 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003646 /*
3647 * check the allocation happened at the same or higher AG than
3648 * the first block that was allocated.
3649 */
3650 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003651 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3652 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003653
Dave Chinner3a756672011-09-18 20:40:58 +00003654 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003655 if (*ap->firstblock == NULLFSBLOCK)
3656 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003657 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003658 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003659 if (!(ap->flags & XFS_BMAPI_COWFORK))
3660 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3662 if (ap->wasdel)
3663 ap->ip->i_delayed_blks -= args.len;
3664 /*
3665 * Adjust the disk quota also. This was reserved
3666 * earlier.
3667 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003668 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003669 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3670 XFS_TRANS_DQ_BCOUNT,
3671 (long) args.len);
3672 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003673 ap->blkno = NULLFSBLOCK;
3674 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
3676 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003677}
3678
3679/*
3680 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3681 * It figures out where to ask the underlying allocator to put the new extent.
3682 */
3683STATIC int
3684xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003685 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003686{
Dave Chinner292378e2016-09-26 08:21:28 +10003687 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3688 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003689 return xfs_bmap_rtalloc(ap);
3690 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003693/* Trim extent to fit a logical block range. */
3694void
3695xfs_trim_extent(
3696 struct xfs_bmbt_irec *irec,
3697 xfs_fileoff_t bno,
3698 xfs_filblks_t len)
3699{
3700 xfs_fileoff_t distance;
3701 xfs_fileoff_t end = bno + len;
3702
3703 if (irec->br_startoff + irec->br_blockcount <= bno ||
3704 irec->br_startoff >= end) {
3705 irec->br_blockcount = 0;
3706 return;
3707 }
3708
3709 if (irec->br_startoff < bno) {
3710 distance = bno - irec->br_startoff;
3711 if (isnullstartblock(irec->br_startblock))
3712 irec->br_startblock = DELAYSTARTBLOCK;
3713 if (irec->br_startblock != DELAYSTARTBLOCK &&
3714 irec->br_startblock != HOLESTARTBLOCK)
3715 irec->br_startblock += distance;
3716 irec->br_startoff += distance;
3717 irec->br_blockcount -= distance;
3718 }
3719
3720 if (end < irec->br_startoff + irec->br_blockcount) {
3721 distance = irec->br_startoff + irec->br_blockcount - end;
3722 irec->br_blockcount -= distance;
3723 }
3724}
3725
Brian Foster40214d12017-10-13 09:47:46 -07003726/* trim extent to within eof */
3727void
3728xfs_trim_extent_eof(
3729 struct xfs_bmbt_irec *irec,
3730 struct xfs_inode *ip)
3731
3732{
3733 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3734 i_size_read(VFS_I(ip))));
3735}
3736
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003738 * Trim the returned map to the required bounds
3739 */
3740STATIC void
3741xfs_bmapi_trim_map(
3742 struct xfs_bmbt_irec *mval,
3743 struct xfs_bmbt_irec *got,
3744 xfs_fileoff_t *bno,
3745 xfs_filblks_t len,
3746 xfs_fileoff_t obno,
3747 xfs_fileoff_t end,
3748 int n,
3749 int flags)
3750{
3751 if ((flags & XFS_BMAPI_ENTIRE) ||
3752 got->br_startoff + got->br_blockcount <= obno) {
3753 *mval = *got;
3754 if (isnullstartblock(got->br_startblock))
3755 mval->br_startblock = DELAYSTARTBLOCK;
3756 return;
3757 }
3758
3759 if (obno > *bno)
3760 *bno = obno;
3761 ASSERT((*bno >= obno) || (n == 0));
3762 ASSERT(*bno < end);
3763 mval->br_startoff = *bno;
3764 if (isnullstartblock(got->br_startblock))
3765 mval->br_startblock = DELAYSTARTBLOCK;
3766 else
3767 mval->br_startblock = got->br_startblock +
3768 (*bno - got->br_startoff);
3769 /*
3770 * Return the minimum of what we got and what we asked for for
3771 * the length. We can use the len variable here because it is
3772 * modified below and we could have been there before coming
3773 * here if the first part of the allocation didn't overlap what
3774 * was asked for.
3775 */
3776 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3777 got->br_blockcount - (*bno - got->br_startoff));
3778 mval->br_state = got->br_state;
3779 ASSERT(mval->br_blockcount <= len);
3780 return;
3781}
3782
3783/*
3784 * Update and validate the extent map to return
3785 */
3786STATIC void
3787xfs_bmapi_update_map(
3788 struct xfs_bmbt_irec **map,
3789 xfs_fileoff_t *bno,
3790 xfs_filblks_t *len,
3791 xfs_fileoff_t obno,
3792 xfs_fileoff_t end,
3793 int *n,
3794 int flags)
3795{
3796 xfs_bmbt_irec_t *mval = *map;
3797
3798 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3799 ((mval->br_startoff + mval->br_blockcount) <= end));
3800 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3801 (mval->br_startoff < obno));
3802
3803 *bno = mval->br_startoff + mval->br_blockcount;
3804 *len = end - *bno;
3805 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3806 /* update previous map with new information */
3807 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3808 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3809 ASSERT(mval->br_state == mval[-1].br_state);
3810 mval[-1].br_blockcount = mval->br_blockcount;
3811 mval[-1].br_state = mval->br_state;
3812 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3813 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3814 mval[-1].br_startblock != HOLESTARTBLOCK &&
3815 mval->br_startblock == mval[-1].br_startblock +
3816 mval[-1].br_blockcount &&
3817 ((flags & XFS_BMAPI_IGSTATE) ||
3818 mval[-1].br_state == mval->br_state)) {
3819 ASSERT(mval->br_startoff ==
3820 mval[-1].br_startoff + mval[-1].br_blockcount);
3821 mval[-1].br_blockcount += mval->br_blockcount;
3822 } else if (*n > 0 &&
3823 mval->br_startblock == DELAYSTARTBLOCK &&
3824 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3825 mval->br_startoff ==
3826 mval[-1].br_startoff + mval[-1].br_blockcount) {
3827 mval[-1].br_blockcount += mval->br_blockcount;
3828 mval[-1].br_state = mval->br_state;
3829 } else if (!((*n == 0) &&
3830 ((mval->br_startoff + mval->br_blockcount) <=
3831 obno))) {
3832 mval++;
3833 (*n)++;
3834 }
3835 *map = mval;
3836}
3837
3838/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003839 * Map file blocks to filesystem blocks without allocation.
3840 */
3841int
3842xfs_bmapi_read(
3843 struct xfs_inode *ip,
3844 xfs_fileoff_t bno,
3845 xfs_filblks_t len,
3846 struct xfs_bmbt_irec *mval,
3847 int *nmap,
3848 int flags)
3849{
3850 struct xfs_mount *mp = ip->i_mount;
3851 struct xfs_ifork *ifp;
3852 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003853 xfs_fileoff_t obno;
3854 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003855 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003856 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003857 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003858 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003859 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003860
3861 ASSERT(*nmap >= 1);
3862 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003863 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003864 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003865
3866 if (unlikely(XFS_TEST_ERROR(
3867 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3868 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003869 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003870 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003871 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003872 }
3873
3874 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003875 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003876
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003877 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003878
3879 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003880
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003881 /* No CoW fork? Return a hole. */
3882 if (whichfork == XFS_COW_FORK && !ifp) {
3883 mval->br_startoff = bno;
3884 mval->br_startblock = HOLESTARTBLOCK;
3885 mval->br_blockcount = len;
3886 mval->br_state = XFS_EXT_NORM;
3887 *nmap = 1;
3888 return 0;
3889 }
3890
Dave Chinner5c8ed202011-09-18 20:40:45 +00003891 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3892 error = xfs_iread_extents(NULL, ip, whichfork);
3893 if (error)
3894 return error;
3895 }
3896
Christoph Hellwig334f3422016-11-24 11:39:43 +11003897 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3898 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003899 end = bno + len;
3900 obno = bno;
3901
3902 while (bno < end && n < *nmap) {
3903 /* Reading past eof, act as though there's a hole up to end. */
3904 if (eof)
3905 got.br_startoff = end;
3906 if (got.br_startoff > bno) {
3907 /* Reading in a hole. */
3908 mval->br_startoff = bno;
3909 mval->br_startblock = HOLESTARTBLOCK;
3910 mval->br_blockcount =
3911 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3912 mval->br_state = XFS_EXT_NORM;
3913 bno += mval->br_blockcount;
3914 len -= mval->br_blockcount;
3915 mval++;
3916 n++;
3917 continue;
3918 }
3919
3920 /* set up the extent map to return. */
3921 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3922 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3923
3924 /* If we're done, stop now. */
3925 if (bno >= end || n >= *nmap)
3926 break;
3927
3928 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003929 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3930 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003931 }
3932 *nmap = n;
3933 return 0;
3934}
3935
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003936/*
3937 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3938 * global pool and the extent inserted into the inode in-core extent tree.
3939 *
3940 * On entry, got refers to the first extent beyond the offset of the extent to
3941 * allocate or eof is specified if no such extent exists. On return, got refers
3942 * to the extent record that was inserted to the inode fork.
3943 *
3944 * Note that the allocated extent may have been merged with contiguous extents
3945 * during insertion into the inode fork. Thus, got does not reflect the current
3946 * state of the inode fork on return. If necessary, the caller can use lastx to
3947 * look up the updated record in the inode fork.
3948 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003949int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003950xfs_bmapi_reserve_delalloc(
3951 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003952 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003953 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003954 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003955 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003956 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003957 xfs_extnum_t *lastx,
3958 int eof)
3959{
3960 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003961 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003962 xfs_extlen_t alen;
3963 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003964 char rt = XFS_IS_REALTIME_INODE(ip);
3965 xfs_extlen_t extsz;
3966 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003967 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003968
Brian Foster974ae922016-11-28 14:57:42 +11003969 /*
3970 * Cap the alloc length. Keep track of prealloc so we know whether to
3971 * tag the inode before we return.
3972 */
3973 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003974 if (!eof)
3975 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003976 if (prealloc && alen >= len)
3977 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003978
3979 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003980 if (whichfork == XFS_COW_FORK)
3981 extsz = xfs_get_cowextsz_hint(ip);
3982 else
3983 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003984 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003985 struct xfs_bmbt_irec prev;
3986
3987 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
3988 prev.br_startoff = NULLFILEOFF;
3989
3990 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003991 1, 0, &aoff, &alen);
3992 ASSERT(!error);
3993 }
3994
3995 if (rt)
3996 extsz = alen / mp->m_sb.sb_rextsize;
3997
3998 /*
3999 * Make a transaction-less quota reservation for delayed allocation
4000 * blocks. This number gets adjusted later. We return if we haven't
4001 * allocated blocks already inside this loop.
4002 */
4003 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4004 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4005 if (error)
4006 return error;
4007
4008 /*
4009 * Split changing sb for alen and indlen since they could be coming
4010 * from different places.
4011 */
4012 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4013 ASSERT(indlen > 0);
4014
4015 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004016 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004017 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004018 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004019 }
4020
4021 if (error)
4022 goto out_unreserve_quota;
4023
Dave Chinner0d485ad2015-02-23 21:22:03 +11004024 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004025 if (error)
4026 goto out_unreserve_blocks;
4027
4028
4029 ip->i_delayed_blks += alen;
4030
4031 got->br_startoff = aoff;
4032 got->br_startblock = nullstartblock(indlen);
4033 got->br_blockcount = alen;
4034 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004035
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004036 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004037
Brian Foster974ae922016-11-28 14:57:42 +11004038 /*
4039 * Tag the inode if blocks were preallocated. Note that COW fork
4040 * preallocation can occur at the start or end of the extent, even when
4041 * prealloc == 0, so we must also check the aligned offset and length.
4042 */
4043 if (whichfork == XFS_DATA_FORK && prealloc)
4044 xfs_inode_set_eofblocks_tag(ip);
4045 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4046 xfs_inode_set_cowblocks_tag(ip);
4047
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004048 return 0;
4049
4050out_unreserve_blocks:
4051 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004052 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004053 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004054 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004055out_unreserve_quota:
4056 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004057 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004058 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4059 return error;
4060}
4061
Dave Chinnercf11da92014-07-15 07:08:24 +10004062static int
4063xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004064 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004065{
4066 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004067 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004068 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004069 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004070 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004071
Dave Chinnera99ebf42011-12-01 11:24:20 +00004072 ASSERT(bma->length > 0);
4073
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004074 /*
4075 * For the wasdelay case, we could also just allocate the stuff asked
4076 * for in this bmap call but that wouldn't be as good.
4077 */
4078 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004079 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4080 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004081 if (bma->idx)
4082 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004083 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004084 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004085 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004086 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004087 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004088 }
4089
4090 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004091 * Set the data type being allocated. For the data fork, the first data
4092 * in the file is treated differently to all other allocations. For the
4093 * attribute fork, we only need to ensure the allocated range is not on
4094 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004095 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004096 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004097 bma->datatype = XFS_ALLOC_NOBUSY;
4098 if (whichfork == XFS_DATA_FORK) {
4099 if (bma->offset == 0)
4100 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4101 else
4102 bma->datatype |= XFS_ALLOC_USERDATA;
4103 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004104 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004105 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004106 }
4107
Dave Chinnere04426b2012-10-05 11:06:59 +10004108 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004109
4110 /*
4111 * Only want to do the alignment at the eof if it is userdata and
4112 * allocation length is larger than a stripe unit.
4113 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004114 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004115 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004116 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004117 if (error)
4118 return error;
4119 }
4120
4121 error = xfs_bmap_alloc(bma);
4122 if (error)
4123 return error;
4124
Dave Chinner29c8d172011-09-18 20:41:00 +00004125 if (bma->cur)
4126 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004127 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004128 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004129 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4130 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4131 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004132 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004133 }
4134 /*
4135 * Bump the number of extents we've allocated
4136 * in this call.
4137 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004138 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004139
Dave Chinner29c8d172011-09-18 20:41:00 +00004140 if (bma->cur)
4141 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004142 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4143
Dave Chinner963c30c2011-09-18 20:40:59 +00004144 bma->got.br_startoff = bma->offset;
4145 bma->got.br_startblock = bma->blkno;
4146 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004147 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004148
4149 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004150 * In the data fork, a wasdelay extent has been initialized, so
4151 * shouldn't be flagged as unwritten.
4152 *
4153 * For the cow fork, however, we convert delalloc reservations
4154 * (extents allocated for speculative preallocation) to
4155 * allocated unwritten extents, and only convert the unwritten
4156 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004157 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004158 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4159 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004160 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004161 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004162
Christoph Hellwigc6534242011-09-18 20:41:05 +00004163 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004164 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004165 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004166 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4167 whichfork, &bma->idx, &bma->cur, &bma->got,
4168 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004169
Christoph Hellwigc315c902011-09-18 20:41:02 +00004170 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004171 if (error)
4172 return error;
4173
4174 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004175 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4176 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4177 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004178 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004179 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004180
Dave Chinner963c30c2011-09-18 20:40:59 +00004181 ASSERT(bma->got.br_startoff <= bma->offset);
4182 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4183 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004184 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4185 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004186 return 0;
4187}
4188
Dave Chinnerb447fe52011-09-18 20:40:51 +00004189STATIC int
4190xfs_bmapi_convert_unwritten(
4191 struct xfs_bmalloca *bma,
4192 struct xfs_bmbt_irec *mval,
4193 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004194 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004195{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004196 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004197 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004198 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004199 int error;
4200
Dave Chinnerb447fe52011-09-18 20:40:51 +00004201 /* check if we need to do unwritten->real conversion */
4202 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4203 (flags & XFS_BMAPI_PREALLOC))
4204 return 0;
4205
4206 /* check if we need to do real->unwritten conversion */
4207 if (mval->br_state == XFS_EXT_NORM &&
4208 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4209 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4210 return 0;
4211
4212 /*
4213 * Modify (by adding) the state flag, if writing.
4214 */
4215 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004216 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4217 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004218 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004219 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004220 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004221 }
4222 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4223 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4224
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004225 /*
4226 * Before insertion into the bmbt, zero the range being converted
4227 * if required.
4228 */
4229 if (flags & XFS_BMAPI_ZERO) {
4230 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4231 mval->br_blockcount);
4232 if (error)
4233 return error;
4234 }
4235
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004236 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4237 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004238 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004239 /*
4240 * Log the inode core unconditionally in the unwritten extent conversion
4241 * path because the conversion might not have done so (e.g., if the
4242 * extent count hasn't changed). We need to make sure the inode is dirty
4243 * in the transaction for the sake of fsync(), even if nothing has
4244 * changed, because fsync() will not force the log for this transaction
4245 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004246 *
4247 * Note: If we're only converting cow fork extents, there aren't
4248 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004249 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004250 if (whichfork != XFS_COW_FORK)
4251 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004252 if (error)
4253 return error;
4254
4255 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004256 * Update our extent pointer, given that
4257 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4258 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004259 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004260 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004261
4262 /*
4263 * We may have combined previously unwritten space with written space,
4264 * so generate another request.
4265 */
4266 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004267 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004268 return 0;
4269}
4270
Christoph Hellwig44032802011-09-18 20:40:48 +00004271/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004272 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4273 * extent state if necessary. Details behaviour is controlled by the flags
4274 * parameter. Only allocates blocks from a single allocation group, to avoid
4275 * locking problems.
4276 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 * The returned value in "firstblock" from the first call in a transaction
4278 * must be remembered and presented to subsequent calls in "firstblock".
4279 * An upper bound for the number of blocks to be allocated is supplied to
4280 * the first call in "total"; if no allocation group has that many free
4281 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4282 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004283int
4284xfs_bmapi_write(
4285 struct xfs_trans *tp, /* transaction pointer */
4286 struct xfs_inode *ip, /* incore inode */
4287 xfs_fileoff_t bno, /* starting file offs. mapped */
4288 xfs_filblks_t len, /* length to map in file */
4289 int flags, /* XFS_BMAPI_... */
4290 xfs_fsblock_t *firstblock, /* first allocated block
4291 controls a.g. for allocs */
4292 xfs_extlen_t total, /* total blocks needed */
4293 struct xfs_bmbt_irec *mval, /* output: map values */
4294 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004295 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004297 struct xfs_mount *mp = ip->i_mount;
4298 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004299 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004300 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004301 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004302 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004303 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004304 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004305 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004306
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004308 xfs_fileoff_t orig_bno; /* original block number value */
4309 int orig_flags; /* original flags arg value */
4310 xfs_filblks_t orig_len; /* original value of len arg */
4311 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4312 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
4314 orig_bno = bno;
4315 orig_len = len;
4316 orig_flags = flags;
4317 orig_mval = mval;
4318 orig_nmap = *nmap;
4319#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004320 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004323 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4324 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004325 ASSERT(tp != NULL ||
4326 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4327 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004328 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004329 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004330 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004331 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004332
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004333 /* zeroing is for currently only for data extents, not metadata */
4334 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4335 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4336 /*
4337 * we can allocate unwritten extents or pre-zero allocated blocks,
4338 * but it makes no sense to do both at once. This would result in
4339 * zeroing the unwritten extent twice, but it still being an
4340 * unwritten extent....
4341 */
4342 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4343 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 if (unlikely(XFS_TEST_ERROR(
4346 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004347 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004348 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004349 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004350 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004354 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004357
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004358 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004359
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004360 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004362 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004364 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004365 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004366 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004367 }
4368
4369 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4370 error = xfs_iread_extents(tp, ip, whichfork);
4371 if (error)
4372 goto error0;
4373 }
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 n = 0;
4376 end = bno + len;
4377 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004378
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004379 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4380 eof = true;
4381 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4382 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004383 bma.tp = tp;
4384 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004385 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004386 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004387 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004388 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004391 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004392
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004393 /* in hole or beyoned EOF? */
4394 if (eof || bma.got.br_startoff > bno) {
4395 if (flags & XFS_BMAPI_DELALLOC) {
4396 /*
4397 * For the COW fork we can reasonably get a
4398 * request for converting an extent that races
4399 * with other threads already having converted
4400 * part of it, as there converting COW to
4401 * regular blocks is not protected using the
4402 * IOLOCK.
4403 */
4404 ASSERT(flags & XFS_BMAPI_COWFORK);
4405 if (!(flags & XFS_BMAPI_COWFORK)) {
4406 error = -EIO;
4407 goto error0;
4408 }
4409
4410 if (eof || bno >= end)
4411 break;
4412 } else {
4413 need_alloc = true;
4414 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004415 } else if (isnullstartblock(bma.got.br_startblock)) {
4416 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004417 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004418
4419 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 * First, deal with the hole before the allocated space
4421 * that we found, if any.
4422 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004423 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004424 bma.eof = eof;
4425 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4426 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004427 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004428 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004429
Dave Chinnera99ebf42011-12-01 11:24:20 +00004430 /*
4431 * There's a 32/64 bit type mismatch between the
4432 * allocation length request (which can be 64 bits in
4433 * length) and the bma length request, which is
4434 * xfs_extlen_t and therefore 32 bits. Hence we have to
4435 * check for 32-bit overflows and handle them here.
4436 */
4437 if (len > (xfs_filblks_t)MAXEXTLEN)
4438 bma.length = MAXEXTLEN;
4439 else
4440 bma.length = len;
4441
4442 ASSERT(len > 0);
4443 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004444 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (error)
4446 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004447 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004448 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004449
4450 /*
4451 * If this is a CoW allocation, record the data in
4452 * the refcount btree for orphan recovery.
4453 */
4454 if (whichfork == XFS_COW_FORK) {
4455 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4456 bma.blkno, bma.length);
4457 if (error)
4458 goto error0;
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004461
Dave Chinneraef9a892011-09-18 20:40:44 +00004462 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004463 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4464 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
Dave Chinnerb447fe52011-09-18 20:40:51 +00004466 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004467 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004468 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004469 continue;
4470 if (error)
4471 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Dave Chinneraef9a892011-09-18 20:40:44 +00004473 /* update the extent map to return */
4474 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 /*
4477 * If we're done, stop now. Stop when we've allocated
4478 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4479 * the transaction may get too big.
4480 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004481 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004483
4484 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004485 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004486 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4487 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 /*
4492 * Transform from btree to extents, give it cur.
4493 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004494 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004495 int tmp_logflags = 0;
4496
Dave Chinner29c8d172011-09-18 20:41:00 +00004497 ASSERT(bma.cur);
4498 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004500 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 if (error)
4502 goto error0;
4503 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004506 XFS_IFORK_NEXTENTS(ip, whichfork) >
4507 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509error0:
4510 /*
4511 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004512 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004514 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004516 bma.logflags &= ~xfs_ilog_fext(whichfork);
4517 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004519 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 /*
4521 * Log whatever the flags say, even if error. Otherwise we might miss
4522 * detecting a case where the data is changed, there's an error,
4523 * and it's not logged so we don't shutdown when we should.
4524 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004525 if (bma.logflags)
4526 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004527
Dave Chinner29c8d172011-09-18 20:41:00 +00004528 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 if (!error) {
4530 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004531 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004533 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004534 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004536 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4538 }
4539 if (!error)
4540 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4541 orig_nmap, *nmap);
4542 return error;
4543}
4544
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004545static int
4546xfs_bmapi_remap(
4547 struct xfs_trans *tp,
4548 struct xfs_inode *ip,
4549 xfs_fileoff_t bno,
4550 xfs_filblks_t len,
4551 xfs_fsblock_t startblock,
4552 struct xfs_defer_ops *dfops)
4553{
4554 struct xfs_mount *mp = ip->i_mount;
4555 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4556 struct xfs_btree_cur *cur = NULL;
4557 xfs_fsblock_t firstblock = NULLFSBLOCK;
4558 struct xfs_bmbt_irec got;
4559 xfs_extnum_t idx;
4560 int logflags = 0, error;
4561
4562 ASSERT(len > 0);
4563 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4564 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4565
4566 if (unlikely(XFS_TEST_ERROR(
4567 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4568 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004569 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004570 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4571 return -EFSCORRUPTED;
4572 }
4573
4574 if (XFS_FORCED_SHUTDOWN(mp))
4575 return -EIO;
4576
4577 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4578 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4579 if (error)
4580 return error;
4581 }
4582
4583 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4584 /* make sure we only reflink into a hole. */
4585 ASSERT(got.br_startoff > bno);
4586 ASSERT(got.br_startoff - bno >= len);
4587 }
4588
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004589 ip->i_d.di_nblocks += len;
4590 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004591
4592 if (ifp->if_flags & XFS_IFBROOT) {
4593 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4594 cur->bc_private.b.firstblock = firstblock;
4595 cur->bc_private.b.dfops = dfops;
4596 cur->bc_private.b.flags = 0;
4597 }
4598
4599 got.br_startoff = bno;
4600 got.br_startblock = startblock;
4601 got.br_blockcount = len;
4602 got.br_state = XFS_EXT_NORM;
4603
4604 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4605 &got, &firstblock, dfops, &logflags);
4606 if (error)
4607 goto error0;
4608
4609 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4610 int tmp_logflags = 0;
4611
4612 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4613 &tmp_logflags, XFS_DATA_FORK);
4614 logflags |= tmp_logflags;
4615 }
4616
4617error0:
4618 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4619 logflags &= ~XFS_ILOG_DEXT;
4620 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4621 logflags &= ~XFS_ILOG_DBROOT;
4622
4623 if (logflags)
4624 xfs_trans_log_inode(tp, ip, logflags);
4625 if (cur) {
4626 xfs_btree_del_cursor(cur,
4627 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4628 }
4629 return error;
4630}
4631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004633 * When a delalloc extent is split (e.g., due to a hole punch), the original
4634 * indlen reservation must be shared across the two new extents that are left
4635 * behind.
4636 *
4637 * Given the original reservation and the worst case indlen for the two new
4638 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004639 * reservation fairly across the two new extents. If necessary, steal available
4640 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4641 * ores == 1). The number of stolen blocks is returned. The availability and
4642 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004643 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004644static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004645xfs_bmap_split_indlen(
4646 xfs_filblks_t ores, /* original res. */
4647 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004648 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4649 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004650{
4651 xfs_filblks_t len1 = *indlen1;
4652 xfs_filblks_t len2 = *indlen2;
4653 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004654 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004655 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004656
4657 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004658 * Steal as many blocks as we can to try and satisfy the worst case
4659 * indlen for both new extents.
4660 */
Brian Foster75d65362017-02-13 22:48:30 -08004661 if (ores < nres && avail)
4662 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4663 ores += stolen;
4664
4665 /* nothing else to do if we've satisfied the new reservation */
4666 if (ores >= nres)
4667 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004668
4669 /*
Brian Foster75d65362017-02-13 22:48:30 -08004670 * We can't meet the total required reservation for the two extents.
4671 * Calculate the percent of the overall shortage between both extents
4672 * and apply this percentage to each of the requested indlen values.
4673 * This distributes the shortage fairly and reduces the chances that one
4674 * of the two extents is left with nothing when extents are repeatedly
4675 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004676 */
Brian Foster75d65362017-02-13 22:48:30 -08004677 resfactor = (ores * 100);
4678 do_div(resfactor, nres);
4679 len1 *= resfactor;
4680 do_div(len1, 100);
4681 len2 *= resfactor;
4682 do_div(len2, 100);
4683 ASSERT(len1 + len2 <= ores);
4684 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4685
4686 /*
4687 * Hand out the remainder to each extent. If one of the two reservations
4688 * is zero, we want to make sure that one gets a block first. The loop
4689 * below starts with len1, so hand len2 a block right off the bat if it
4690 * is zero.
4691 */
4692 ores -= (len1 + len2);
4693 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4694 if (ores && !len2 && *indlen2) {
4695 len2++;
4696 ores--;
4697 }
4698 while (ores) {
4699 if (len1 < *indlen1) {
4700 len1++;
4701 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004702 }
Brian Foster75d65362017-02-13 22:48:30 -08004703 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004704 break;
Brian Foster75d65362017-02-13 22:48:30 -08004705 if (len2 < *indlen2) {
4706 len2++;
4707 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004708 }
4709 }
4710
4711 *indlen1 = len1;
4712 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004713
4714 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004715}
4716
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004717int
4718xfs_bmap_del_extent_delay(
4719 struct xfs_inode *ip,
4720 int whichfork,
4721 xfs_extnum_t *idx,
4722 struct xfs_bmbt_irec *got,
4723 struct xfs_bmbt_irec *del)
4724{
4725 struct xfs_mount *mp = ip->i_mount;
4726 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4727 struct xfs_bmbt_irec new;
4728 int64_t da_old, da_new, da_diff = 0;
4729 xfs_fileoff_t del_endoff, got_endoff;
4730 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004731 int state = xfs_bmap_fork_to_state(whichfork);
4732 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004733 bool isrt;
4734
4735 XFS_STATS_INC(mp, xs_del_exlist);
4736
4737 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4738 del_endoff = del->br_startoff + del->br_blockcount;
4739 got_endoff = got->br_startoff + got->br_blockcount;
4740 da_old = startblockval(got->br_startblock);
4741 da_new = 0;
4742
4743 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004744 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004745 ASSERT(del->br_blockcount > 0);
4746 ASSERT(got->br_startoff <= del->br_startoff);
4747 ASSERT(got_endoff >= del_endoff);
4748
4749 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004750 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004751
4752 do_div(rtexts, mp->m_sb.sb_rextsize);
4753 xfs_mod_frextents(mp, rtexts);
4754 }
4755
4756 /*
4757 * Update the inode delalloc counter now and wait to update the
4758 * sb counters as we might have to borrow some blocks for the
4759 * indirect block accounting.
4760 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004761 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4762 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004763 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004764 if (error)
4765 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004766 ip->i_delayed_blks -= del->br_blockcount;
4767
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004768 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004769 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004770 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004771 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004772
Christoph Hellwig0173c682017-10-17 14:16:22 -07004773 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4774 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004775 /*
4776 * Matches the whole extent. Delete the entry.
4777 */
4778 xfs_iext_remove(ip, *idx, 1, state);
4779 --*idx;
4780 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004781 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004782 /*
4783 * Deleting the first part of the extent.
4784 */
4785 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4786 got->br_startoff = del_endoff;
4787 got->br_blockcount -= del->br_blockcount;
4788 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4789 got->br_blockcount), da_old);
4790 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004791 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004792 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4793 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004794 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004795 /*
4796 * Deleting the last part of the extent.
4797 */
4798 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4799 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4800 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4801 got->br_blockcount), da_old);
4802 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004803 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4805 break;
4806 case 0:
4807 /*
4808 * Deleting the middle of the extent.
4809 *
4810 * Distribute the original indlen reservation across the two new
4811 * extents. Steal blocks from the deleted extent if necessary.
4812 * Stealing blocks simply fudges the fdblocks accounting below.
4813 * Warn if either of the new indlen reservations is zero as this
4814 * can lead to delalloc problems.
4815 */
4816 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4817
4818 got->br_blockcount = del->br_startoff - got->br_startoff;
4819 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4820
4821 new.br_blockcount = got_endoff - del_endoff;
4822 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4823
4824 WARN_ON_ONCE(!got_indlen || !new_indlen);
4825 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4826 del->br_blockcount);
4827
4828 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004829 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004830 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4831
4832 new.br_startoff = del_endoff;
4833 new.br_state = got->br_state;
4834 new.br_startblock = nullstartblock((int)new_indlen);
4835
4836 ++*idx;
4837 xfs_iext_insert(ip, *idx, 1, &new, state);
4838
4839 da_new = got_indlen + new_indlen - stolen;
4840 del->br_blockcount -= stolen;
4841 break;
4842 }
4843
4844 ASSERT(da_old >= da_new);
4845 da_diff = da_old - da_new;
4846 if (!isrt)
4847 da_diff += del->br_blockcount;
4848 if (da_diff)
4849 xfs_mod_fdblocks(mp, da_diff, false);
4850 return error;
4851}
4852
4853void
4854xfs_bmap_del_extent_cow(
4855 struct xfs_inode *ip,
4856 xfs_extnum_t *idx,
4857 struct xfs_bmbt_irec *got,
4858 struct xfs_bmbt_irec *del)
4859{
4860 struct xfs_mount *mp = ip->i_mount;
4861 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4862 struct xfs_bmbt_irec new;
4863 xfs_fileoff_t del_endoff, got_endoff;
4864 int state = BMAP_COWFORK;
4865
4866 XFS_STATS_INC(mp, xs_del_exlist);
4867
4868 del_endoff = del->br_startoff + del->br_blockcount;
4869 got_endoff = got->br_startoff + got->br_blockcount;
4870
4871 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004872 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004873 ASSERT(del->br_blockcount > 0);
4874 ASSERT(got->br_startoff <= del->br_startoff);
4875 ASSERT(got_endoff >= del_endoff);
4876 ASSERT(!isnullstartblock(got->br_startblock));
4877
4878 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004879 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004880 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004881 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004882
Christoph Hellwig0173c682017-10-17 14:16:22 -07004883 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4884 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004885 /*
4886 * Matches the whole extent. Delete the entry.
4887 */
4888 xfs_iext_remove(ip, *idx, 1, state);
4889 --*idx;
4890 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004891 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004892 /*
4893 * Deleting the first part of the extent.
4894 */
4895 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4896 got->br_startoff = del_endoff;
4897 got->br_blockcount -= del->br_blockcount;
4898 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004899 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004900 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4901 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004902 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004903 /*
4904 * Deleting the last part of the extent.
4905 */
4906 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4907 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004908 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004909 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4910 break;
4911 case 0:
4912 /*
4913 * Deleting the middle of the extent.
4914 */
4915 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4916 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004917 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004918 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4919
4920 new.br_startoff = del_endoff;
4921 new.br_blockcount = got_endoff - del_endoff;
4922 new.br_state = got->br_state;
4923 new.br_startblock = del->br_startblock + del->br_blockcount;
4924
4925 ++*idx;
4926 xfs_iext_insert(ip, *idx, 1, &new, state);
4927 break;
4928 }
4929}
4930
Brian Fostera9bd24a2016-03-15 11:42:46 +11004931/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004932 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004933 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004934 */
4935STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004936xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004937 xfs_inode_t *ip, /* incore inode pointer */
4938 xfs_trans_t *tp, /* current transaction pointer */
4939 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004940 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004941 xfs_btree_cur_t *cur, /* if null, not a btree */
4942 xfs_bmbt_irec_t *del, /* data to remove from extents */
4943 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004944 int whichfork, /* data or attr fork */
4945 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004946{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004947 xfs_fsblock_t del_endblock=0; /* first block past del */
4948 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004949 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004950 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004951 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004952 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004953 xfs_fileoff_t got_endoff; /* first offset past got */
4954 int i; /* temp state */
4955 xfs_ifork_t *ifp; /* inode fork pointer */
4956 xfs_mount_t *mp; /* mount structure */
4957 xfs_filblks_t nblks; /* quota/sb block count */
4958 xfs_bmbt_irec_t new; /* new record to be inserted */
4959 /* REFERENCED */
4960 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004961 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004962 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004963
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004964 mp = ip->i_mount;
4965 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004966
Dave Chinner9e5987a2013-02-25 12:31:26 +11004967 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004968 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004969 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004970 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004971 ASSERT(got.br_startoff <= del->br_startoff);
4972 del_endoff = del->br_startoff + del->br_blockcount;
4973 got_endoff = got.br_startoff + got.br_blockcount;
4974 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004975 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004976 qfield = 0;
4977 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004978
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004979 /*
4980 * If it's the case where the directory code is running with no block
4981 * reservation, and the deleted block is in the middle of its extent,
4982 * and the resulting insert of an extent would cause transformation to
4983 * btree format, then reject it. The calling code will then swap blocks
4984 * around instead. We have to do this now, rather than waiting for the
4985 * conversion to btree format, since the transaction will be dirty then.
4986 */
4987 if (tp->t_blk_res == 0 &&
4988 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4989 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4990 XFS_IFORK_MAXEXT(ip, whichfork) &&
4991 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4992 return -ENOSPC;
4993
4994 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004995 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4996 xfs_fsblock_t bno;
4997 xfs_filblks_t len;
4998
4999 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
5000 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
5001 bno = del->br_startblock;
5002 len = del->br_blockcount;
5003 do_div(bno, mp->m_sb.sb_rextsize);
5004 do_div(len, mp->m_sb.sb_rextsize);
5005 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5006 if (error)
5007 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005008 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005009 nblks = len * mp->m_sb.sb_rextsize;
5010 qfield = XFS_TRANS_DQ_RTBCOUNT;
5011 } else {
5012 do_fx = 1;
5013 nblks = del->br_blockcount;
5014 qfield = XFS_TRANS_DQ_BCOUNT;
5015 }
5016
5017 del_endblock = del->br_startblock + del->br_blockcount;
5018 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005019 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005020 if (error)
5021 goto done;
5022 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005023 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005024
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005025 if (got.br_startoff == del->br_startoff)
5026 state |= BMAP_LEFT_FILLING;
5027 if (got_endoff == del_endoff)
5028 state |= BMAP_RIGHT_FILLING;
5029
5030 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5031 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005032 /*
5033 * Matches the whole extent. Delete the entry.
5034 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005035 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005036 xfs_iext_remove(ip, *idx, 1,
5037 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5038 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005039
5040 XFS_IFORK_NEXT_SET(ip, whichfork,
5041 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5042 flags |= XFS_ILOG_CORE;
5043 if (!cur) {
5044 flags |= xfs_ilog_fext(whichfork);
5045 break;
5046 }
5047 if ((error = xfs_btree_delete(cur, &i)))
5048 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005049 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005050 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005051 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005052 /*
5053 * Deleting the first part of the extent.
5054 */
5055 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005056 got.br_startoff = del_endoff;
5057 got.br_startblock = del_endblock;
5058 got.br_blockcount -= del->br_blockcount;
5059 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005060 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5061 if (!cur) {
5062 flags |= xfs_ilog_fext(whichfork);
5063 break;
5064 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005065 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005066 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005067 goto done;
5068 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005069 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005070 /*
5071 * Deleting the last part of the extent.
5072 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005073 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005074 got.br_blockcount -= del->br_blockcount;
5075 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005076 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5077 if (!cur) {
5078 flags |= xfs_ilog_fext(whichfork);
5079 break;
5080 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005081 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005082 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005083 goto done;
5084 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005085 case 0:
5086 /*
5087 * Deleting the middle of the extent.
5088 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005089 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005090
5091 old = got;
5092 got.br_blockcount = del->br_startoff - got.br_startoff;
5093 xfs_iext_update_extent(ifp, *idx, &got);
5094
Dave Chinner9e5987a2013-02-25 12:31:26 +11005095 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005096 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005097 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005098 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005099
Christoph Hellwige1d75532017-10-17 14:16:21 -07005100 flags |= XFS_ILOG_CORE;
5101 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005102 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005103 if (error)
5104 goto done;
5105 error = xfs_btree_increment(cur, 0, &i);
5106 if (error)
5107 goto done;
5108 cur->bc_rec.b = new;
5109 error = xfs_btree_insert(cur, &i);
5110 if (error && error != -ENOSPC)
5111 goto done;
5112 /*
5113 * If get no-space back from btree insert, it tried a
5114 * split, and we have a zero block reservation. Fix up
5115 * our state and return the error.
5116 */
5117 if (error == -ENOSPC) {
5118 /*
5119 * Reset the cursor, don't trust it after any
5120 * insert operation.
5121 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005122 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005123 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005124 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005125 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5126 /*
5127 * Update the btree record back
5128 * to the original value.
5129 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005130 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005131 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005132 goto done;
5133 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005134 * Reset the extent record back
5135 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005136 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005137 xfs_iext_update_extent(ifp, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005138 flags = 0;
5139 error = -ENOSPC;
5140 goto done;
5141 }
5142 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5143 } else
5144 flags |= xfs_ilog_fext(whichfork);
5145 XFS_IFORK_NEXT_SET(ip, whichfork,
5146 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005147 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5148 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5149 ++*idx;
5150 break;
5151 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005152
5153 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005154 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5155 if (error)
5156 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005157
Dave Chinner9e5987a2013-02-25 12:31:26 +11005158 /*
5159 * If we need to, add to list of extents to delete.
5160 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005161 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005162 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5163 error = xfs_refcount_decrease_extent(mp, dfops, del);
5164 if (error)
5165 goto done;
5166 } else
5167 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5168 del->br_blockcount, NULL);
5169 }
5170
Dave Chinner9e5987a2013-02-25 12:31:26 +11005171 /*
5172 * Adjust inode # blocks in the file.
5173 */
5174 if (nblks)
5175 ip->i_d.di_nblocks -= nblks;
5176 /*
5177 * Adjust quota data.
5178 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005179 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005180 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5181
Dave Chinner9e5987a2013-02-25 12:31:26 +11005182done:
5183 *logflagsp = flags;
5184 return error;
5185}
5186
5187/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 * Unmap (remove) blocks from a file.
5189 * If nexts is nonzero then the number of extents to remove is limited to
5190 * that value. If not all extents in the block range can be removed then
5191 * *done is set.
5192 */
5193int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005194__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 xfs_trans_t *tp, /* transaction pointer */
5196 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005197 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005198 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 int flags, /* misc flags */
5200 xfs_extnum_t nexts, /* number of extents max */
5201 xfs_fsblock_t *firstblock, /* first allocated block
5202 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005203 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204{
5205 xfs_btree_cur_t *cur; /* bmap btree cursor */
5206 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 int error; /* error return value */
5208 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005209 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 xfs_ifork_t *ifp; /* inode fork pointer */
5211 int isrt; /* freeing in rt area */
5212 xfs_extnum_t lastx; /* last extent index used */
5213 int logflags; /* transaction logging flags */
5214 xfs_extlen_t mod; /* rt extent offset */
5215 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 int tmp_logflags; /* partial logging flags */
5217 int wasdel; /* was a delayed alloc extent */
5218 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005220 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005221 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005222 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005223 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005225 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005226
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005227 whichfork = xfs_bmapi_whichfork(flags);
5228 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 ifp = XFS_IFORK_PTR(ip, whichfork);
5230 if (unlikely(
5231 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5232 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5233 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5234 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005235 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 }
5237 mp = ip->i_mount;
5238 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005239 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005240
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005241 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 ASSERT(len > 0);
5243 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005244
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005245 /*
5246 * Guesstimate how many blocks we can unmap without running the risk of
5247 * blowing out the transaction with a mix of EFIs and reflink
5248 * adjustments.
5249 */
5250 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5251 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5252 else
5253 max_len = len;
5254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5256 (error = xfs_iread_extents(tp, ip, whichfork)))
5257 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005258 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005259 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 return 0;
5261 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005262 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005263 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005264 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 /*
5267 * Check to see if the given block number is past the end of the
5268 * file, back up to the last block if so...
5269 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005270 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005271 ASSERT(lastx > 0);
5272 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005273 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005275
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 logflags = 0;
5277 if (ifp->if_flags & XFS_IFBROOT) {
5278 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005279 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005281 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 cur->bc_private.b.flags = 0;
5283 } else
5284 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005285
5286 if (isrt) {
5287 /*
5288 * Synchronize by locking the bitmap inode.
5289 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005290 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005291 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005292 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5293 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005294 }
5295
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005297 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005298 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005300 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 * Just back up to the previous extent, if so.
5302 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005303 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 if (--lastx < 0)
5305 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005306 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 }
5308 /*
5309 * Is the last block of this extent before the range
5310 * we're supposed to delete? If so, we're done.
5311 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005312 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005314 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 break;
5316 /*
5317 * Then deal with the (possibly delayed) allocated space
5318 * we found.
5319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005321 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005322
5323 /*
5324 * Make sure we don't touch multiple AGF headers out of order
5325 * in a single transaction, as that could cause AB-BA deadlocks.
5326 */
5327 if (!wasdel) {
5328 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5329 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5330 break;
5331 prev_agno = agno;
5332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 if (got.br_startoff < start) {
5334 del.br_startoff = start;
5335 del.br_blockcount -= start - got.br_startoff;
5336 if (!wasdel)
5337 del.br_startblock += start - got.br_startoff;
5338 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005339 if (del.br_startoff + del.br_blockcount > end + 1)
5340 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005341
5342 /* How much can we safely unmap? */
5343 if (max_len < del.br_blockcount) {
5344 del.br_startoff += del.br_blockcount - max_len;
5345 if (!wasdel)
5346 del.br_startblock += del.br_blockcount - max_len;
5347 del.br_blockcount = max_len;
5348 }
5349
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 sum = del.br_startblock + del.br_blockcount;
5351 if (isrt &&
5352 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5353 /*
5354 * Realtime extent not lined up at the end.
5355 * The extent could have been split into written
5356 * and unwritten pieces, or we could just be
5357 * unmapping part of it. But we can't really
5358 * get rid of part of a realtime extent.
5359 */
5360 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005361 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 /*
5363 * This piece is unwritten, or we're not
5364 * using unwritten extents. Skip over it.
5365 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005366 ASSERT(end >= mod);
5367 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005369 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005371 xfs_iext_get_extent(ifp, lastx,
5372 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 }
5374 continue;
5375 }
5376 /*
5377 * It's written, turn it unwritten.
5378 * This is better than zeroing it.
5379 */
5380 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005381 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 /*
5383 * If this spans a realtime extent boundary,
5384 * chop it back to the start of the one we end at.
5385 */
5386 if (del.br_blockcount > mod) {
5387 del.br_startoff += del.br_blockcount - mod;
5388 del.br_startblock += del.br_blockcount - mod;
5389 del.br_blockcount = mod;
5390 }
5391 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005392 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005393 whichfork, &lastx, &cur, &del,
5394 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 if (error)
5396 goto error0;
5397 goto nodelete;
5398 }
5399 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5400 /*
5401 * Realtime extent is lined up at the end but not
5402 * at the front. We'll get rid of full extents if
5403 * we can.
5404 */
5405 mod = mp->m_sb.sb_rextsize - mod;
5406 if (del.br_blockcount > mod) {
5407 del.br_blockcount -= mod;
5408 del.br_startoff += mod;
5409 del.br_startblock += mod;
5410 } else if ((del.br_startoff == start &&
5411 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005412 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005413 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 /*
5415 * Can't make it unwritten. There isn't
5416 * a full extent here so just skip it.
5417 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005418 ASSERT(end >= del.br_blockcount);
5419 end -= del.br_blockcount;
5420 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005421 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 continue;
5423 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005424 struct xfs_bmbt_irec prev;
5425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 /*
5427 * This one is already unwritten.
5428 * It must have a written left neighbor.
5429 * Unwrite the killed part of that one and
5430 * try again.
5431 */
5432 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005433 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005435 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 ASSERT(del.br_startblock ==
5437 prev.br_startblock + prev.br_blockcount);
5438 if (prev.br_startoff < start) {
5439 mod = start - prev.br_startoff;
5440 prev.br_blockcount -= mod;
5441 prev.br_startblock += mod;
5442 prev.br_startoff = start;
5443 }
5444 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005445 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005446 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005447 ip, whichfork, &lastx, &cur,
5448 &prev, firstblock, dfops,
5449 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 if (error)
5451 goto error0;
5452 goto nodelete;
5453 } else {
5454 ASSERT(del.br_state == XFS_EXT_NORM);
5455 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005456 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005457 ip, whichfork, &lastx, &cur,
5458 &del, firstblock, dfops,
5459 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 if (error)
5461 goto error0;
5462 goto nodelete;
5463 }
5464 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005465
Brian Fosterb2706a02016-03-15 11:42:46 +11005466 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005467 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5468 &got, &del);
5469 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005470 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5471 cur, &del, &tmp_logflags, whichfork,
5472 flags);
5473 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005474 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005475
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 if (error)
5477 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005478
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005479 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005480 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 /*
5483 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005485 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005486 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005487 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005488 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005489 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 extno++;
5492 }
5493 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005494 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005495 *rlen = 0;
5496 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005497 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005498
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 /*
5500 * Convert to a btree if necessary.
5501 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005502 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005504 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 &cur, 0, &tmp_logflags, whichfork);
5506 logflags |= tmp_logflags;
5507 if (error)
5508 goto error0;
5509 }
5510 /*
5511 * transform from btree to extents, give it cur
5512 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005513 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 ASSERT(cur != NULL);
5515 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5516 whichfork);
5517 logflags |= tmp_logflags;
5518 if (error)
5519 goto error0;
5520 }
5521 /*
5522 * transform from extents to local?
5523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 error = 0;
5525error0:
5526 /*
5527 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005528 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005530 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005532 logflags &= ~xfs_ilog_fext(whichfork);
5533 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005535 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 /*
5537 * Log inode even in the error case, if the transaction
5538 * is dirty we'll need to shut down the filesystem.
5539 */
5540 if (logflags)
5541 xfs_trans_log_inode(tp, ip, logflags);
5542 if (cur) {
5543 if (!error) {
5544 *firstblock = cur->bc_private.b.firstblock;
5545 cur->bc_private.b.allocated = 0;
5546 }
5547 xfs_btree_del_cursor(cur,
5548 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5549 }
5550 return error;
5551}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005552
Darrick J. Wong44535932016-10-03 09:11:29 -07005553/* Unmap a range of a file. */
5554int
5555xfs_bunmapi(
5556 xfs_trans_t *tp,
5557 struct xfs_inode *ip,
5558 xfs_fileoff_t bno,
5559 xfs_filblks_t len,
5560 int flags,
5561 xfs_extnum_t nexts,
5562 xfs_fsblock_t *firstblock,
5563 struct xfs_defer_ops *dfops,
5564 int *done)
5565{
5566 int error;
5567
5568 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5569 dfops);
5570 *done = (len == 0);
5571 return error;
5572}
5573
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005574/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005575 * Determine whether an extent shift can be accomplished by a merge with the
5576 * extent that precedes the target hole of the shift.
5577 */
5578STATIC bool
5579xfs_bmse_can_merge(
5580 struct xfs_bmbt_irec *left, /* preceding extent */
5581 struct xfs_bmbt_irec *got, /* current extent to shift */
5582 xfs_fileoff_t shift) /* shift fsb */
5583{
5584 xfs_fileoff_t startoff;
5585
5586 startoff = got->br_startoff - shift;
5587
5588 /*
5589 * The extent, once shifted, must be adjacent in-file and on-disk with
5590 * the preceding extent.
5591 */
5592 if ((left->br_startoff + left->br_blockcount != startoff) ||
5593 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5594 (left->br_state != got->br_state) ||
5595 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5596 return false;
5597
5598 return true;
5599}
5600
5601/*
5602 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5603 * hole in the file. If an extent shift would result in the extent being fully
5604 * adjacent to the extent that currently precedes the hole, we can merge with
5605 * the preceding extent rather than do the shift.
5606 *
5607 * This function assumes the caller has verified a shift-by-merge is possible
5608 * with the provided extents via xfs_bmse_can_merge().
5609 */
5610STATIC int
5611xfs_bmse_merge(
5612 struct xfs_inode *ip,
5613 int whichfork,
5614 xfs_fileoff_t shift, /* shift fsb */
5615 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005616 struct xfs_bmbt_irec *got, /* extent to shift */
5617 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005618 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005619 int *logflags, /* output */
5620 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005621{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005622 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5623 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005624 xfs_filblks_t blockcount;
5625 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005626 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005627
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005628 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005629
5630 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5631 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005632 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005633
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005634 new = *left;
5635 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005636
5637 /*
5638 * Update the on-disk extent count, the btree if necessary and log the
5639 * inode.
5640 */
5641 XFS_IFORK_NEXT_SET(ip, whichfork,
5642 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5643 *logflags |= XFS_ILOG_CORE;
5644 if (!cur) {
5645 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005646 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005647 }
5648
5649 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005650 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005651 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005652 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005653 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005654
5655 error = xfs_btree_delete(cur, &i);
5656 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005657 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005658 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005659
5660 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005661 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005662 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005663 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005664 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005665
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005666 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005667 if (error)
5668 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005669
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005670done:
5671 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5672 xfs_iext_remove(ip, current_ext, 1, 0);
5673
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005674 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005675 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5676 if (error)
5677 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005678 memcpy(&new, got, sizeof(new));
5679 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005680 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005681}
5682
5683/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005684 * Shift a single extent.
5685 */
5686STATIC int
5687xfs_bmse_shift_one(
5688 struct xfs_inode *ip,
5689 int whichfork,
5690 xfs_fileoff_t offset_shift_fsb,
5691 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005692 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005693 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005694 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005695 enum shift_direction direction,
5696 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005697{
5698 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005699 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005700 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005701 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005702 int error;
5703 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005704 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005705
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005706 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005707 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005708 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005709
Brian Fosterf71721d2014-09-23 15:39:05 +10005710 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005711 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005712
Namjae Jeona904b1c2015-03-25 15:08:56 +11005713 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005714 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005715
Namjae Jeona904b1c2015-03-25 15:08:56 +11005716 /*
5717 * Check for merge if we've got an extent to the left,
5718 * otherwise make sure there's enough room at the start
5719 * of the file for the shift.
5720 */
5721 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005722 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005723 return -EINVAL;
5724 goto update_current_ext;
5725 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005726
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005727 /*
5728 * grab the left extent and check for a large enough hole.
5729 */
5730 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
5731 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005732 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005733
Dave Chinnerb11bd672014-12-04 09:42:24 +11005734 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005735 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
5736 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5737 *current_ext, got, &adj_irec,
5738 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005739 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005740 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005741 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005742 /* nothing to move if this is the last extent */
5743 if (*current_ext >= (total_extents - 1))
5744 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005745
Namjae Jeona904b1c2015-03-25 15:08:56 +11005746 /*
5747 * If this is not the last extent in the file, make sure there
5748 * is enough room between current extent and next extent for
5749 * accommodating the shift.
5750 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005751 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
5752 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005753 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005754
Namjae Jeona904b1c2015-03-25 15:08:56 +11005755 /*
5756 * Unlike a left shift (which involves a hole punch),
5757 * a right shift does not modify extent neighbors
5758 * in any way. We should never find mergeable extents
5759 * in this scenario. Check anyways and warn if we
5760 * encounter two extents that could be one.
5761 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005762 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11005763 WARN_ON_ONCE(1);
5764 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005765
Brian Fostera979bdf2014-09-23 15:39:04 +10005766 /*
5767 * Increment the extent index for the next iteration, update the start
5768 * offset of the in-core extent and update the btree if applicable.
5769 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005770update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005771 *logflags |= XFS_ILOG_CORE;
5772
5773 new = *got;
5774 new.br_startoff = startoff;
5775
5776 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005777 error = xfs_bmbt_lookup_eq(cur, got, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005778 if (error)
5779 return error;
5780 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5781
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005782 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005783 if (error)
5784 return error;
5785 } else {
5786 *logflags |= XFS_ILOG_DEXT;
5787 }
5788
5789 xfs_iext_update_extent(ifp, *current_ext, &new);
5790
Namjae Jeona904b1c2015-03-25 15:08:56 +11005791 if (direction == SHIFT_LEFT)
5792 (*current_ext)++;
5793 else
5794 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005795
Darrick J. Wong9c194642016-08-03 12:16:05 +10005796 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005797 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005798 if (error)
5799 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005800 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10005801}
5802
5803/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005804 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005805 *
Brian Foster2c845f52014-09-23 15:37:09 +10005806 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005807 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005808 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5809 * is the length by which each extent is shifted. If there is no hole to shift
5810 * the extents into, this will be considered invalid operation and we abort
5811 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005812 */
5813int
5814xfs_bmap_shift_extents(
5815 struct xfs_trans *tp,
5816 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005817 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005818 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005819 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005820 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005821 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005822 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005823 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005824 int num_exts)
5825{
Brian Fosterca446d82014-09-02 12:12:53 +10005826 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005827 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005828 struct xfs_mount *mp = ip->i_mount;
5829 struct xfs_ifork *ifp;
5830 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005831 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005832 xfs_extnum_t total_extents;
5833 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005834 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005835 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005836 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005837
5838 if (unlikely(XFS_TEST_ERROR(
5839 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5840 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005841 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005842 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5843 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005844 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005845 }
5846
5847 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005848 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005849
Brian Foster2c845f52014-09-23 15:37:09 +10005850 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5851 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005852 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005853
5854 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005855 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5856 /* Read in all the extents */
5857 error = xfs_iread_extents(tp, ip, whichfork);
5858 if (error)
5859 return error;
5860 }
5861
Brian Fosterddb19e32014-09-23 15:38:09 +10005862 if (ifp->if_flags & XFS_IFBROOT) {
5863 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5864 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005865 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005866 cur->bc_private.b.flags = 0;
5867 }
5868
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005869 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005870 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005871 * are collapsing out, so we cannot use the count of real extents here.
5872 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005873 */
Eric Sandeen5d829302016-11-08 12:59:42 +11005874 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005875 if (total_extents == 0) {
5876 *done = 1;
5877 goto del_cursor;
5878 }
5879
5880 /*
5881 * In case of first right shift, we need to initialize next_fsb
5882 */
5883 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005884 ASSERT(direction == SHIFT_RIGHT);
5885
5886 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005887 xfs_iext_get_extent(ifp, current_ext, &got);
5888 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005889 *done = 1;
5890 goto del_cursor;
5891 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005892 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005893 } else {
5894 /*
5895 * Look up the extent index for the fsb where we start shifting. We can
5896 * henceforth iterate with current_ext as extent list changes are locked
5897 * out via ilock.
5898 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005899 * If next_fsb lies in a hole beyond which there are no extents we are
5900 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005901 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005902 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5903 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005904 *done = 1;
5905 goto del_cursor;
5906 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005907 }
5908
5909 /* Lookup the extent index at which we have to stop */
5910 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005911 struct xfs_bmbt_irec s;
5912
5913 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005914 /* Make stop_extent exclusive of shift range */
5915 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005916 if (current_ext <= stop_extent) {
5917 error = -EIO;
5918 goto del_cursor;
5919 }
5920 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005921 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005922 if (current_ext >= stop_extent) {
5923 error = -EIO;
5924 goto del_cursor;
5925 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005926 }
5927
5928 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005929 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005930 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005931 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10005932 if (error)
5933 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 /*
5935 * If there was an extent merge during the shift, the extent
5936 * count can change. Update the total and grade the next record.
5937 */
5938 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11005939 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005940 stop_extent = total_extents;
5941 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005942
Namjae Jeona904b1c2015-03-25 15:08:56 +11005943 if (current_ext == stop_extent) {
5944 *done = 1;
5945 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005946 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005947 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005948 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005949 }
5950
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005951 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10005952 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005953
5954del_cursor:
5955 if (cur)
5956 xfs_btree_del_cursor(cur,
5957 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5958
Brian Fosterca446d82014-09-02 12:12:53 +10005959 if (logflags)
5960 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005961
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005962 return error;
5963}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005964
5965/*
5966 * Splits an extent into two extents at split_fsb block such that it is
5967 * the first block of the current_ext. @current_ext is a target extent
5968 * to be split. @split_fsb is a block where the extents is split.
5969 * If split_fsb lies in a hole or the first block of extents, just return 0.
5970 */
5971STATIC int
5972xfs_bmap_split_extent_at(
5973 struct xfs_trans *tp,
5974 struct xfs_inode *ip,
5975 xfs_fileoff_t split_fsb,
5976 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005977 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005978{
5979 int whichfork = XFS_DATA_FORK;
5980 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005981 struct xfs_bmbt_irec got;
5982 struct xfs_bmbt_irec new; /* split extent */
5983 struct xfs_mount *mp = ip->i_mount;
5984 struct xfs_ifork *ifp;
5985 xfs_fsblock_t gotblkcnt; /* new block count for got */
5986 xfs_extnum_t current_ext;
5987 int error = 0;
5988 int logflags = 0;
5989 int i = 0;
5990
5991 if (unlikely(XFS_TEST_ERROR(
5992 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5993 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005994 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5996 XFS_ERRLEVEL_LOW, mp);
5997 return -EFSCORRUPTED;
5998 }
5999
6000 if (XFS_FORCED_SHUTDOWN(mp))
6001 return -EIO;
6002
6003 ifp = XFS_IFORK_PTR(ip, whichfork);
6004 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6005 /* Read in all the extents */
6006 error = xfs_iread_extents(tp, ip, whichfork);
6007 if (error)
6008 return error;
6009 }
6010
6011 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006012 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006013 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006014 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6015 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006016 return 0;
6017
6018 gotblkcnt = split_fsb - got.br_startoff;
6019 new.br_startoff = split_fsb;
6020 new.br_startblock = got.br_startblock + gotblkcnt;
6021 new.br_blockcount = got.br_blockcount - gotblkcnt;
6022 new.br_state = got.br_state;
6023
6024 if (ifp->if_flags & XFS_IFBROOT) {
6025 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6026 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006027 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006028 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006029 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006030 if (error)
6031 goto del_cursor;
6032 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6033 }
6034
Namjae Jeona904b1c2015-03-25 15:08:56 +11006035 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006036 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006037
6038 logflags = XFS_ILOG_CORE;
6039 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006040 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006041 if (error)
6042 goto del_cursor;
6043 } else
6044 logflags |= XFS_ILOG_DEXT;
6045
6046 /* Add new extent */
6047 current_ext++;
6048 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6049 XFS_IFORK_NEXT_SET(ip, whichfork,
6050 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6051
6052 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006053 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006054 if (error)
6055 goto del_cursor;
6056 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006057 error = xfs_btree_insert(cur, &i);
6058 if (error)
6059 goto del_cursor;
6060 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6061 }
6062
6063 /*
6064 * Convert to a btree if necessary.
6065 */
6066 if (xfs_bmap_needs_btree(ip, whichfork)) {
6067 int tmp_logflags; /* partial log flag return val */
6068
6069 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006070 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006071 &cur, 0, &tmp_logflags, whichfork);
6072 logflags |= tmp_logflags;
6073 }
6074
6075del_cursor:
6076 if (cur) {
6077 cur->bc_private.b.allocated = 0;
6078 xfs_btree_del_cursor(cur,
6079 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6080 }
6081
6082 if (logflags)
6083 xfs_trans_log_inode(tp, ip, logflags);
6084 return error;
6085}
6086
6087int
6088xfs_bmap_split_extent(
6089 struct xfs_inode *ip,
6090 xfs_fileoff_t split_fsb)
6091{
6092 struct xfs_mount *mp = ip->i_mount;
6093 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006094 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006095 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006096 int error;
6097
Christoph Hellwig253f4912016-04-06 09:19:55 +10006098 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6099 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6100 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006101 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006102
6103 xfs_ilock(ip, XFS_ILOCK_EXCL);
6104 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6105
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006106 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006107
6108 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006109 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006110 if (error)
6111 goto out;
6112
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006113 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006114 if (error)
6115 goto out;
6116
Christoph Hellwig70393312015-06-04 13:48:08 +10006117 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006118
6119out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006120 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006121 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006122 return error;
6123}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006124
6125/* Deferred mapping is only for real extents in the data fork. */
6126static bool
6127xfs_bmap_is_update_needed(
6128 struct xfs_bmbt_irec *bmap)
6129{
6130 return bmap->br_startblock != HOLESTARTBLOCK &&
6131 bmap->br_startblock != DELAYSTARTBLOCK;
6132}
6133
6134/* Record a bmap intent. */
6135static int
6136__xfs_bmap_add(
6137 struct xfs_mount *mp,
6138 struct xfs_defer_ops *dfops,
6139 enum xfs_bmap_intent_type type,
6140 struct xfs_inode *ip,
6141 int whichfork,
6142 struct xfs_bmbt_irec *bmap)
6143{
6144 int error;
6145 struct xfs_bmap_intent *bi;
6146
6147 trace_xfs_bmap_defer(mp,
6148 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6149 type,
6150 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6151 ip->i_ino, whichfork,
6152 bmap->br_startoff,
6153 bmap->br_blockcount,
6154 bmap->br_state);
6155
6156 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6157 INIT_LIST_HEAD(&bi->bi_list);
6158 bi->bi_type = type;
6159 bi->bi_owner = ip;
6160 bi->bi_whichfork = whichfork;
6161 bi->bi_bmap = *bmap;
6162
Christoph Hellwig882d8782017-08-28 10:21:03 -07006163 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006164 if (error) {
6165 kmem_free(bi);
6166 return error;
6167 }
6168
6169 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6170 return 0;
6171}
6172
6173/* Map an extent into a file. */
6174int
6175xfs_bmap_map_extent(
6176 struct xfs_mount *mp,
6177 struct xfs_defer_ops *dfops,
6178 struct xfs_inode *ip,
6179 struct xfs_bmbt_irec *PREV)
6180{
6181 if (!xfs_bmap_is_update_needed(PREV))
6182 return 0;
6183
6184 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6185 XFS_DATA_FORK, PREV);
6186}
6187
6188/* Unmap an extent out of a file. */
6189int
6190xfs_bmap_unmap_extent(
6191 struct xfs_mount *mp,
6192 struct xfs_defer_ops *dfops,
6193 struct xfs_inode *ip,
6194 struct xfs_bmbt_irec *PREV)
6195{
6196 if (!xfs_bmap_is_update_needed(PREV))
6197 return 0;
6198
6199 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6200 XFS_DATA_FORK, PREV);
6201}
6202
6203/*
6204 * Process one of the deferred bmap operations. We pass back the
6205 * btree cursor to maintain our lock on the bmapbt between calls.
6206 */
6207int
6208xfs_bmap_finish_one(
6209 struct xfs_trans *tp,
6210 struct xfs_defer_ops *dfops,
6211 struct xfs_inode *ip,
6212 enum xfs_bmap_intent_type type,
6213 int whichfork,
6214 xfs_fileoff_t startoff,
6215 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006216 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006217 xfs_exntst_t state)
6218{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006219 xfs_fsblock_t firstfsb;
6220 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006221
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006222 /*
6223 * firstfsb is tied to the transaction lifetime and is used to
6224 * ensure correct AG locking order and schedule work item
6225 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6226 * to only making one bmap call per transaction, so it should
6227 * be safe to have it as a local variable here.
6228 */
6229 firstfsb = NULLFSBLOCK;
6230
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006231 trace_xfs_bmap_deferred(tp->t_mountp,
6232 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6233 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006234 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006235
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006236 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006237 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006238
6239 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006240 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006241 return -EIO;
6242
6243 switch (type) {
6244 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006245 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006246 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006247 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006248 break;
6249 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006250 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6251 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006252 break;
6253 default:
6254 ASSERT(0);
6255 error = -EFSCORRUPTED;
6256 }
6257
6258 return error;
6259}