blob: 7eac21a310bf134f6e19b3b3faa8d4a35b4ab209 [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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100243#ifdef DEBUG
244STATIC struct xfs_buf *
245xfs_bmap_get_bp(
246 struct xfs_btree_cur *cur,
247 xfs_fsblock_t bno)
248{
249 struct xfs_log_item_desc *lidp;
250 int i;
251
252 if (!cur)
253 return NULL;
254
255 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
256 if (!cur->bc_bufs[i])
257 break;
258 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
259 return cur->bc_bufs[i];
260 }
261
262 /* Chase down all the log items to see if the bp is there */
263 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
264 struct xfs_buf_log_item *bip;
265 bip = (struct xfs_buf_log_item *)lidp->lid_item;
266 if (bip->bli_item.li_type == XFS_LI_BUF &&
267 XFS_BUF_ADDR(bip->bli_buf) == bno)
268 return bip->bli_buf;
269 }
270
271 return NULL;
272}
273
274STATIC void
275xfs_check_block(
276 struct xfs_btree_block *block,
277 xfs_mount_t *mp,
278 int root,
279 short sz)
280{
281 int i, j, dmxr;
282 __be64 *pp, *thispa; /* pointer to block address */
283 xfs_bmbt_key_t *prevp, *keyp;
284
285 ASSERT(be16_to_cpu(block->bb_level) > 0);
286
287 prevp = NULL;
288 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
289 dmxr = mp->m_bmap_dmxr[0];
290 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
291
292 if (prevp) {
293 ASSERT(be64_to_cpu(prevp->br_startoff) <
294 be64_to_cpu(keyp->br_startoff));
295 }
296 prevp = keyp;
297
298 /*
299 * Compare the block numbers to see if there are dups.
300 */
301 if (root)
302 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
303 else
304 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
305
306 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
307 if (root)
308 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
309 else
310 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
311 if (*thispa == *pp) {
312 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
313 __func__, j, i,
314 (unsigned long long)be64_to_cpu(*thispa));
315 panic("%s: ptrs are equal in node\n",
316 __func__);
317 }
318 }
319 }
320}
321
322/*
323 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100324 * btree leaves. THis becomes prohibitively expensive for large extent count
325 * files, so don't bother with inodes that have more than 10,000 extents in
326 * them. The btree record ordering checks will still be done, so for such large
327 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100328 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100329STATIC void
330xfs_bmap_check_leaf_extents(
331 xfs_btree_cur_t *cur, /* btree cursor or null */
332 xfs_inode_t *ip, /* incore inode pointer */
333 int whichfork) /* data or attr fork */
334{
335 struct xfs_btree_block *block; /* current btree block */
336 xfs_fsblock_t bno; /* block # of "block" */
337 xfs_buf_t *bp; /* buffer for "block" */
338 int error; /* error return value */
339 xfs_extnum_t i=0, j; /* index into the extents list */
340 xfs_ifork_t *ifp; /* fork structure */
341 int level; /* btree level, for checking */
342 xfs_mount_t *mp; /* file system mount structure */
343 __be64 *pp; /* pointer to block address */
344 xfs_bmbt_rec_t *ep; /* pointer to current extent */
345 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
346 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
347 int bp_release = 0;
348
349 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
350 return;
351 }
352
Dave Chinnere3543812016-01-08 11:28:49 +1100353 /* skip large extent count inodes */
354 if (ip->i_d.di_nextents > 10000)
355 return;
356
Dave Chinner9e5987a72013-02-25 12:31:26 +1100357 bno = NULLFSBLOCK;
358 mp = ip->i_mount;
359 ifp = XFS_IFORK_PTR(ip, whichfork);
360 block = ifp->if_broot;
361 /*
362 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
363 */
364 level = be16_to_cpu(block->bb_level);
365 ASSERT(level > 0);
366 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
367 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
368 bno = be64_to_cpu(*pp);
369
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000370 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100371 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
372 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
373
374 /*
375 * Go down the tree until leaf level is reached, following the first
376 * pointer (leftmost) at each level.
377 */
378 while (level-- > 0) {
379 /* See if buf is in cur first */
380 bp_release = 0;
381 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
382 if (!bp) {
383 bp_release = 1;
384 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
385 XFS_BMAP_BTREE_REF,
386 &xfs_bmbt_buf_ops);
387 if (error)
388 goto error_norelse;
389 }
390 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100391 if (level == 0)
392 break;
393
394 /*
395 * Check this block for basic sanity (increasing keys and
396 * no duplicate blocks).
397 */
398
399 xfs_check_block(block, mp, 0, 0);
400 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
401 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100402 XFS_WANT_CORRUPTED_GOTO(mp,
403 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100404 if (bp_release) {
405 bp_release = 0;
406 xfs_trans_brelse(NULL, bp);
407 }
408 }
409
410 /*
411 * Here with bp and block set to the leftmost leaf node in the tree.
412 */
413 i = 0;
414
415 /*
416 * Loop over all leaf nodes checking that all extents are in the right order.
417 */
418 for (;;) {
419 xfs_fsblock_t nextbno;
420 xfs_extnum_t num_recs;
421
422
423 num_recs = xfs_btree_get_numrecs(block);
424
425 /*
426 * Read-ahead the next leaf block, if any.
427 */
428
429 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
430
431 /*
432 * Check all the extents to make sure they are OK.
433 * If we had a previous block, the last entry should
434 * conform with the first entry in this one.
435 */
436
437 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
438 if (i) {
439 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
440 xfs_bmbt_disk_get_blockcount(&last) <=
441 xfs_bmbt_disk_get_startoff(ep));
442 }
443 for (j = 1; j < num_recs; j++) {
444 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
445 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
446 xfs_bmbt_disk_get_blockcount(ep) <=
447 xfs_bmbt_disk_get_startoff(nextp));
448 ep = nextp;
449 }
450
451 last = *ep;
452 i += num_recs;
453 if (bp_release) {
454 bp_release = 0;
455 xfs_trans_brelse(NULL, bp);
456 }
457 bno = nextbno;
458 /*
459 * If we've reached the end, stop.
460 */
461 if (bno == NULLFSBLOCK)
462 break;
463
464 bp_release = 0;
465 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
466 if (!bp) {
467 bp_release = 1;
468 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
469 XFS_BMAP_BTREE_REF,
470 &xfs_bmbt_buf_ops);
471 if (error)
472 goto error_norelse;
473 }
474 block = XFS_BUF_TO_BLOCK(bp);
475 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100476
Dave Chinner9e5987a72013-02-25 12:31:26 +1100477 return;
478
479error0:
480 xfs_warn(mp, "%s: at error0", __func__);
481 if (bp_release)
482 xfs_trans_brelse(NULL, bp);
483error_norelse:
484 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
485 __func__, i);
486 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
487 return;
488}
489
490/*
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 Chinner9e5987a72013-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 */
502 int state = 0;
503
504 if (whichfork == XFS_ATTR_FORK)
505 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100506 else if (whichfork == XFS_COW_FORK)
507 state |= BMAP_COWFORK;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100508
509 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100510 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100511 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100512 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100513}
514
515/*
516 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000517 * given the caller's original parameters. Specifically check the
518 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100519 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
520 */
521STATIC void
522xfs_bmap_validate_ret(
523 xfs_fileoff_t bno,
524 xfs_filblks_t len,
525 int flags,
526 xfs_bmbt_irec_t *mval,
527 int nmap,
528 int ret_nmap)
529{
530 int i; /* index to map values */
531
532 ASSERT(ret_nmap <= nmap);
533
534 for (i = 0; i < ret_nmap; i++) {
535 ASSERT(mval[i].br_blockcount > 0);
536 if (!(flags & XFS_BMAPI_ENTIRE)) {
537 ASSERT(mval[i].br_startoff >= bno);
538 ASSERT(mval[i].br_blockcount <= len);
539 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
540 bno + len);
541 } else {
542 ASSERT(mval[i].br_startoff < bno + len);
543 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
544 bno);
545 }
546 ASSERT(i == 0 ||
547 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
548 mval[i].br_startoff);
549 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
550 mval[i].br_startblock != HOLESTARTBLOCK);
551 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
552 mval[i].br_state == XFS_EXT_UNWRITTEN);
553 }
554}
555
556#else
557#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700558#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100559#endif /* DEBUG */
560
561/*
562 * bmap free list manipulation functions
563 */
564
565/*
566 * Add the extent to the list of extents to be free at transaction end.
567 * The list is maintained sorted (by block number).
568 */
569void
570xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000571 struct xfs_mount *mp,
572 struct xfs_defer_ops *dfops,
573 xfs_fsblock_t bno,
574 xfs_filblks_t len,
575 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100576{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000577 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100578#ifdef DEBUG
579 xfs_agnumber_t agno;
580 xfs_agblock_t agbno;
581
582 ASSERT(bno != NULLFSBLOCK);
583 ASSERT(len > 0);
584 ASSERT(len <= MAXEXTLEN);
585 ASSERT(!isnullstartblock(bno));
586 agno = XFS_FSB_TO_AGNO(mp, bno);
587 agbno = XFS_FSB_TO_AGBNO(mp, bno);
588 ASSERT(agno < mp->m_sb.sb_agcount);
589 ASSERT(agbno < mp->m_sb.sb_agblocks);
590 ASSERT(len < mp->m_sb.sb_agblocks);
591 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
592#endif
593 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000594
Dave Chinner9e5987a72013-02-25 12:31:26 +1100595 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000596 new->xefi_startblock = bno;
597 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000598 if (oinfo)
599 new->xefi_oinfo = *oinfo;
600 else
601 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000602 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
603 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000604 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100605}
606
607/*
608 * Inode fork format manipulation functions
609 */
610
611/*
612 * Transform a btree format file with only one leaf node, where the
613 * extents list will fit in the inode, into an extents format file.
614 * Since the file extents are already in-core, all we have to do is
615 * give up the space for the btree root and pitch the leaf block.
616 */
617STATIC int /* error */
618xfs_bmap_btree_to_extents(
619 xfs_trans_t *tp, /* transaction pointer */
620 xfs_inode_t *ip, /* incore inode pointer */
621 xfs_btree_cur_t *cur, /* btree cursor */
622 int *logflagsp, /* inode logging flags */
623 int whichfork) /* data or attr fork */
624{
625 /* REFERENCED */
626 struct xfs_btree_block *cblock;/* child btree block */
627 xfs_fsblock_t cbno; /* child block number */
628 xfs_buf_t *cbp; /* child block's buffer */
629 int error; /* error return value */
630 xfs_ifork_t *ifp; /* inode fork data */
631 xfs_mount_t *mp; /* mount point structure */
632 __be64 *pp; /* ptr to block address */
633 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000634 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100635
636 mp = ip->i_mount;
637 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700638 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100639 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
640 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
641 rblock = ifp->if_broot;
642 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
643 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
644 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
645 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
646 cbno = be64_to_cpu(*pp);
647 *logflagsp = 0;
648#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700649 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
650 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100651#endif
652 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
653 &xfs_bmbt_buf_ops);
654 if (error)
655 return error;
656 cblock = XFS_BUF_TO_BLOCK(cbp);
657 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
658 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000659 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
660 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100661 ip->i_d.di_nblocks--;
662 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
663 xfs_trans_binval(tp, cbp);
664 if (cur->bc_bufs[0] == cbp)
665 cur->bc_bufs[0] = NULL;
666 xfs_iroot_realloc(ip, -1, whichfork);
667 ASSERT(ifp->if_broot == NULL);
668 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
669 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
670 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
671 return 0;
672}
673
674/*
675 * Convert an extents-format file into a btree-format file.
676 * The new file will have a root block (in the inode) and a single child block.
677 */
678STATIC int /* error */
679xfs_bmap_extents_to_btree(
680 xfs_trans_t *tp, /* transaction pointer */
681 xfs_inode_t *ip, /* incore inode pointer */
682 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000683 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100684 xfs_btree_cur_t **curp, /* cursor returned to caller */
685 int wasdel, /* converting a delayed alloc */
686 int *logflagsp, /* inode logging flags */
687 int whichfork) /* data or attr fork */
688{
689 struct xfs_btree_block *ablock; /* allocated (child) bt block */
690 xfs_buf_t *abp; /* buffer for ablock */
691 xfs_alloc_arg_t args; /* allocation arguments */
692 xfs_bmbt_rec_t *arp; /* child record pointer */
693 struct xfs_btree_block *block; /* btree root block */
694 xfs_btree_cur_t *cur; /* bmap btree cursor */
695 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
696 int error; /* error return value */
697 xfs_extnum_t i, cnt; /* extent record index */
698 xfs_ifork_t *ifp; /* inode fork pointer */
699 xfs_bmbt_key_t *kp; /* root block key pointer */
700 xfs_mount_t *mp; /* mount structure */
701 xfs_extnum_t nextents; /* number of file extents */
702 xfs_bmbt_ptr_t *pp; /* root block address pointer */
703
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500704 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700705 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100706 ifp = XFS_IFORK_PTR(ip, whichfork);
707 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
708
709 /*
710 * Make space in the inode incore.
711 */
712 xfs_iroot_realloc(ip, 1, whichfork);
713 ifp->if_flags |= XFS_IFBROOT;
714
715 /*
716 * Fill in the root.
717 */
718 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800719 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
720 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800721 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100722 /*
723 * Need a cursor. Can't allocate until bb_level is filled in.
724 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100725 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
726 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000727 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100728 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
729 /*
730 * Convert to a btree with two levels, one record in root.
731 */
732 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
733 memset(&args, 0, sizeof(args));
734 args.tp = tp;
735 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000736 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100737 args.firstblock = *firstblock;
738 if (*firstblock == NULLFSBLOCK) {
739 args.type = XFS_ALLOCTYPE_START_BNO;
740 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000741 } else if (dfops->dop_low) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100742 args.type = XFS_ALLOCTYPE_START_BNO;
743 args.fsbno = *firstblock;
744 } else {
745 args.type = XFS_ALLOCTYPE_NEAR_BNO;
746 args.fsbno = *firstblock;
747 }
748 args.minlen = args.maxlen = args.prod = 1;
749 args.wasdel = wasdel;
750 *logflagsp = 0;
751 if ((error = xfs_alloc_vextent(&args))) {
752 xfs_iroot_realloc(ip, -1, whichfork);
753 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
754 return error;
755 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700756
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800757 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
758 xfs_iroot_realloc(ip, -1, whichfork);
759 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
760 return -ENOSPC;
761 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100762 /*
763 * Allocation can't fail, the space was reserved.
764 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100765 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800766 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100767 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
768 cur->bc_private.b.allocated++;
769 ip->i_d.di_nblocks++;
770 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
771 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
772 /*
773 * Fill in the child block.
774 */
775 abp->b_ops = &xfs_bmbt_buf_ops;
776 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800777 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
778 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500779 XFS_BTREE_LONG_PTRS);
780
Dave Chinner9e5987a72013-02-25 12:31:26 +1100781 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100782 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100783 for (cnt = i = 0; i < nextents; i++) {
784 ep = xfs_iext_get_ext(ifp, i);
785 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
786 arp->l0 = cpu_to_be64(ep->l0);
787 arp->l1 = cpu_to_be64(ep->l1);
788 arp++; cnt++;
789 }
790 }
791 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
792 xfs_btree_set_numrecs(ablock, cnt);
793
794 /*
795 * Fill in the root key and pointer.
796 */
797 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
798 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
799 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
800 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
801 be16_to_cpu(block->bb_level)));
802 *pp = cpu_to_be64(args.fsbno);
803
804 /*
805 * Do all this logging at the end so that
806 * the root is at the right level.
807 */
808 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
809 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
810 ASSERT(*curp == NULL);
811 *curp = cur;
812 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
813 return 0;
814}
815
816/*
817 * Convert a local file to an extents file.
818 * This code is out of bounds for data forks of regular files,
819 * since the file data needs to get logged so things will stay consistent.
820 * (The bmap-level manipulations are ok, though).
821 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000822void
823xfs_bmap_local_to_extents_empty(
824 struct xfs_inode *ip,
825 int whichfork)
826{
827 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
828
Darrick J. Wong60b49842016-10-03 09:11:34 -0700829 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000830 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
831 ASSERT(ifp->if_bytes == 0);
832 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
833
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000834 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000835 ifp->if_flags &= ~XFS_IFINLINE;
836 ifp->if_flags |= XFS_IFEXTENTS;
837 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
838}
839
840
Dave Chinner9e5987a72013-02-25 12:31:26 +1100841STATIC int /* error */
842xfs_bmap_local_to_extents(
843 xfs_trans_t *tp, /* transaction pointer */
844 xfs_inode_t *ip, /* incore inode pointer */
845 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
846 xfs_extlen_t total, /* total blocks needed by transaction */
847 int *logflagsp, /* inode logging flags */
848 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500849 void (*init_fn)(struct xfs_trans *tp,
850 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100851 struct xfs_inode *ip,
852 struct xfs_ifork *ifp))
853{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000854 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100855 int flags; /* logging flags returned */
856 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000857 xfs_alloc_arg_t args; /* allocation arguments */
858 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700859 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100860
861 /*
862 * We don't want to deal with the case of keeping inode data inline yet.
863 * So sending the data fork of a regular inode is invalid.
864 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100865 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100866 ifp = XFS_IFORK_PTR(ip, whichfork);
867 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000868
869 if (!ifp->if_bytes) {
870 xfs_bmap_local_to_extents_empty(ip, whichfork);
871 flags = XFS_ILOG_CORE;
872 goto done;
873 }
874
Dave Chinner9e5987a72013-02-25 12:31:26 +1100875 flags = 0;
876 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000877 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
878 XFS_IFINLINE);
879 memset(&args, 0, sizeof(args));
880 args.tp = tp;
881 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000882 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000883 args.firstblock = *firstblock;
884 /*
885 * Allocate a block. We know we need only one, since the
886 * file currently fits in an inode.
887 */
888 if (*firstblock == NULLFSBLOCK) {
889 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
890 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100891 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892 args.fsbno = *firstblock;
893 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100894 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000895 args.total = total;
896 args.minlen = args.maxlen = args.prod = 1;
897 error = xfs_alloc_vextent(&args);
898 if (error)
899 goto done;
900
901 /* Can't fail, the space was reserved. */
902 ASSERT(args.fsbno != NULLFSBLOCK);
903 ASSERT(args.len == 1);
904 *firstblock = args.fsbno;
905 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
906
Dave Chinnerfe22d552015-01-22 09:30:06 +1100907 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100908 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100909 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100910 * The callout is responsible for logging because the remote format
911 * might differ from the local format and thus we don't know how much to
912 * log here. Note that init_fn must also set the buffer log item type
913 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100914 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000915 init_fn(tp, bp, ip, ifp);
916
Brian Fosterb7cdc662015-10-12 15:40:24 +1100917 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000918 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
919 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100920 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000921
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700922 rec.br_startoff = 0;
923 rec.br_startblock = args.fsbno;
924 rec.br_blockcount = 1;
925 rec.br_state = XFS_EXT_NORM;
926 xfs_iext_insert(ip, 0, 1, &rec, 0);
927
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000928 trace_xfs_bmap_post_update(ip, 0,
929 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
930 _THIS_IP_);
931 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
932 ip->i_d.di_nblocks = 1;
933 xfs_trans_mod_dquot_byino(tp, ip,
934 XFS_TRANS_DQ_BCOUNT, 1L);
935 flags |= xfs_ilog_fext(whichfork);
936
Dave Chinner9e5987a72013-02-25 12:31:26 +1100937done:
938 *logflagsp = flags;
939 return error;
940}
941
942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * Called from xfs_bmap_add_attrfork to handle btree format files.
944 */
945STATIC int /* error */
946xfs_bmap_add_attrfork_btree(
947 xfs_trans_t *tp, /* transaction pointer */
948 xfs_inode_t *ip, /* incore inode pointer */
949 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000950 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int *flags) /* inode logging flags */
952{
953 xfs_btree_cur_t *cur; /* btree cursor */
954 int error; /* error return value */
955 xfs_mount_t *mp; /* file system mount struct */
956 int stat; /* newroot status */
957
958 mp = ip->i_mount;
959 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
960 *flags |= XFS_ILOG_DBROOT;
961 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100962 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000963 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700965 error = xfs_bmbt_lookup_first(cur, &stat);
966 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000968 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100969 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100970 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto error0;
972 if (stat == 0) {
973 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000974 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976 *firstblock = cur->bc_private.b.firstblock;
977 cur->bc_private.b.allocated = 0;
978 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
979 }
980 return 0;
981error0:
982 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
983 return error;
984}
985
986/*
987 * Called from xfs_bmap_add_attrfork to handle extents format files.
988 */
989STATIC int /* error */
990xfs_bmap_add_attrfork_extents(
991 xfs_trans_t *tp, /* transaction pointer */
992 xfs_inode_t *ip, /* incore inode pointer */
993 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000994 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int *flags) /* inode logging flags */
996{
997 xfs_btree_cur_t *cur; /* bmap btree cursor */
998 int error; /* error return value */
999
1000 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1001 return 0;
1002 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001003 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 flags, XFS_DATA_FORK);
1005 if (cur) {
1006 cur->bc_private.b.allocated = 0;
1007 xfs_btree_del_cursor(cur,
1008 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1009 }
1010 return error;
1011}
1012
1013/*
Dave Chinner1e823792013-02-11 15:58:13 +11001014 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1015 * different data fork content type needs a different callout to do the
1016 * conversion. Some are basic and only require special block initialisation
1017 * callouts for the data formating, others (directories) are so specialised they
1018 * handle everything themselves.
1019 *
1020 * XXX (dgc): investigate whether directory conversion can use the generic
1021 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001022 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
1024STATIC int /* error */
1025xfs_bmap_add_attrfork_local(
1026 xfs_trans_t *tp, /* transaction pointer */
1027 xfs_inode_t *ip, /* incore inode pointer */
1028 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001029 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 int *flags) /* inode logging flags */
1031{
1032 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1035 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001036
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001037 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001039 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 dargs.dp = ip;
1041 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001042 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001043 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dargs.whichfork = XFS_DATA_FORK;
1045 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001046 return xfs_dir2_sf_to_block(&dargs);
1047 }
1048
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001049 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001050 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1051 flags, XFS_DATA_FORK,
1052 xfs_symlink_local_to_remote);
1053
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001054 /* should only be called for types that support local format data */
1055 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001056 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001060 * Convert inode from non-attributed to attributed.
1061 * Must not be in a transaction, ip must not be locked.
1062 */
1063int /* error code */
1064xfs_bmap_add_attrfork(
1065 xfs_inode_t *ip, /* incore inode pointer */
1066 int size, /* space new attribute needs */
1067 int rsvd) /* xact may use reserved blks */
1068{
1069 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001070 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001071 xfs_mount_t *mp; /* mount structure */
1072 xfs_trans_t *tp; /* transaction pointer */
1073 int blks; /* space reservation */
1074 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001075 int logflags; /* logging flags */
1076 int error; /* error return value */
1077
1078 ASSERT(XFS_IFORK_Q(ip) == 0);
1079
1080 mp = ip->i_mount;
1081 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001082
Dave Chinner9e5987a72013-02-25 12:31:26 +11001083 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001084
1085 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1086 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1087 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001088 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001089
Dave Chinner9e5987a72013-02-25 12:31:26 +11001090 xfs_ilock(ip, XFS_ILOCK_EXCL);
1091 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1092 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1093 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001094 if (error)
1095 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001096 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001097 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001098 if (ip->i_d.di_anextents != 0) {
1099 error = -EFSCORRUPTED;
1100 goto trans_cancel;
1101 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001102 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1103 /*
1104 * For inodes coming from pre-6.2 filesystems.
1105 */
1106 ASSERT(ip->i_d.di_aformat == 0);
1107 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1108 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001109
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001110 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001111 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1112
1113 switch (ip->i_d.di_format) {
1114 case XFS_DINODE_FMT_DEV:
1115 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1116 break;
1117 case XFS_DINODE_FMT_UUID:
1118 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1119 break;
1120 case XFS_DINODE_FMT_LOCAL:
1121 case XFS_DINODE_FMT_EXTENTS:
1122 case XFS_DINODE_FMT_BTREE:
1123 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1124 if (!ip->i_d.di_forkoff)
1125 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1126 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1127 version = 2;
1128 break;
1129 default:
1130 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001131 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001132 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001133 }
1134
1135 ASSERT(ip->i_afp == NULL);
1136 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1137 ip->i_afp->if_flags = XFS_IFEXTENTS;
1138 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001139 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001140 switch (ip->i_d.di_format) {
1141 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001142 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001143 &logflags);
1144 break;
1145 case XFS_DINODE_FMT_EXTENTS:
1146 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001147 &dfops, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001148 break;
1149 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001150 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001151 &logflags);
1152 break;
1153 default:
1154 error = 0;
1155 break;
1156 }
1157 if (logflags)
1158 xfs_trans_log_inode(tp, ip, logflags);
1159 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001160 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001161 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1162 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001163 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001164
1165 spin_lock(&mp->m_sb_lock);
1166 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1167 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001168 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001169 }
1170 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1171 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001172 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001173 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001174 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001175 if (log_sb)
1176 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001177 }
1178
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001179 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001180 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001181 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001182 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001183 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001184 return error;
1185
1186bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001187 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001188trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001189 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001190 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001191 return error;
1192}
1193
1194/*
1195 * Internal and external extent tree search functions.
1196 */
1197
1198/*
1199 * Read in the extents to if_extents.
1200 * All inode fields are set up by caller, we just traverse the btree
1201 * and copy the records in. If the file system cannot contain unwritten
1202 * extents, the records are checked for no "state" flags.
1203 */
1204int /* error */
1205xfs_bmap_read_extents(
1206 xfs_trans_t *tp, /* transaction pointer */
1207 xfs_inode_t *ip, /* incore inode */
1208 int whichfork) /* data or attr fork */
1209{
1210 struct xfs_btree_block *block; /* current btree block */
1211 xfs_fsblock_t bno; /* block # of "block" */
1212 xfs_buf_t *bp; /* buffer for "block" */
1213 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001214 xfs_extnum_t i, j; /* index into the extents list */
1215 xfs_ifork_t *ifp; /* fork structure */
1216 int level; /* btree level, for checking */
1217 xfs_mount_t *mp; /* file system mount structure */
1218 __be64 *pp; /* pointer to block address */
1219 /* REFERENCED */
1220 xfs_extnum_t room; /* number of entries there's room for */
1221
Dave Chinner9e5987a72013-02-25 12:31:26 +11001222 mp = ip->i_mount;
1223 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001224 block = ifp->if_broot;
1225 /*
1226 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1227 */
1228 level = be16_to_cpu(block->bb_level);
1229 ASSERT(level > 0);
1230 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1231 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001232
Dave Chinner9e5987a72013-02-25 12:31:26 +11001233 /*
1234 * Go down the tree until leaf level is reached, following the first
1235 * pointer (leftmost) at each level.
1236 */
1237 while (level-- > 0) {
1238 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1239 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1240 if (error)
1241 return error;
1242 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001243 if (level == 0)
1244 break;
1245 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1246 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001247 XFS_WANT_CORRUPTED_GOTO(mp,
1248 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001249 xfs_trans_brelse(tp, bp);
1250 }
1251 /*
1252 * Here with bp and block set to the leftmost leaf node in the tree.
1253 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001254 room = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001255 i = 0;
1256 /*
1257 * Loop over all leaf nodes. Copy information to the extent records.
1258 */
1259 for (;;) {
1260 xfs_bmbt_rec_t *frp;
1261 xfs_fsblock_t nextbno;
1262 xfs_extnum_t num_recs;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001263
1264 num_recs = xfs_btree_get_numrecs(block);
1265 if (unlikely(i + num_recs > room)) {
1266 ASSERT(i + num_recs <= room);
1267 xfs_warn(ip->i_mount,
1268 "corrupt dinode %Lu, (btree extents).",
1269 (unsigned long long) ip->i_ino);
1270 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1271 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1272 goto error0;
1273 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001274 /*
1275 * Read-ahead the next leaf block, if any.
1276 */
1277 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1278 if (nextbno != NULLFSBLOCK)
1279 xfs_btree_reada_bufl(mp, nextbno, 1,
1280 &xfs_bmbt_buf_ops);
1281 /*
1282 * Copy records into the extent records.
1283 */
1284 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001285 for (j = 0; j < num_recs; j++, i++, frp++) {
1286 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1287 trp->l0 = be64_to_cpu(frp->l0);
1288 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001289 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001290 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001291 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001292 goto error0;
1293 }
1294 }
1295 xfs_trans_brelse(tp, bp);
1296 bno = nextbno;
1297 /*
1298 * If we've reached the end, stop.
1299 */
1300 if (bno == NULLFSBLOCK)
1301 break;
1302 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1303 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1304 if (error)
1305 return error;
1306 block = XFS_BUF_TO_BLOCK(bp);
1307 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001308 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1309 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001310 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11001311 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1312 return 0;
1313error0:
1314 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001315 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001316}
1317
Dave Chinner9e5987a72013-02-25 12:31:26 +11001318/*
1319 * Returns the file-relative block number of the first unused block(s)
1320 * in the file with at least "len" logically contiguous blocks free.
1321 * This is the lowest-address hole if the file has holes, else the first block
1322 * past the end of file.
1323 * Return 0 if the file is currently local (in-inode).
1324 */
1325int /* error */
1326xfs_bmap_first_unused(
1327 xfs_trans_t *tp, /* transaction pointer */
1328 xfs_inode_t *ip, /* incore inode */
1329 xfs_extlen_t len, /* size of hole to find */
1330 xfs_fileoff_t *first_unused, /* unused block */
1331 int whichfork) /* data or attr fork */
1332{
1333 int error; /* error return value */
1334 int idx; /* extent record index */
1335 xfs_ifork_t *ifp; /* inode fork pointer */
1336 xfs_fileoff_t lastaddr; /* last block number seen */
1337 xfs_fileoff_t lowest; /* lowest useful block */
1338 xfs_fileoff_t max; /* starting useful block */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001339 xfs_extnum_t nextents; /* number of extent entries */
1340
1341 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1342 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1343 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1344 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1345 *first_unused = 0;
1346 return 0;
1347 }
1348 ifp = XFS_IFORK_PTR(ip, whichfork);
1349 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1350 (error = xfs_iread_extents(tp, ip, whichfork)))
1351 return error;
1352 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001353 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001354 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001355 struct xfs_bmbt_irec got;
1356
1357 xfs_iext_get_extent(ifp, idx, &got);
1358
Dave Chinner9e5987a72013-02-25 12:31:26 +11001359 /*
1360 * See if the hole before this extent will work.
1361 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001362 if (got.br_startoff >= lowest + len &&
1363 got.br_startoff - max >= len) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001364 *first_unused = max;
1365 return 0;
1366 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001367 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001368 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1369 }
1370 *first_unused = max;
1371 return 0;
1372}
1373
1374/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001375 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001376 * last_block (input value) in the file.
1377 * This is not based on i_size, it is based on the extent records.
1378 * Returns 0 for local files, as they do not have extent records.
1379 */
1380int /* error */
1381xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001382 struct xfs_trans *tp, /* transaction pointer */
1383 struct xfs_inode *ip, /* incore inode */
1384 xfs_fileoff_t *last_block, /* last block */
1385 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001386{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001387 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1388 struct xfs_bmbt_irec got;
1389 xfs_extnum_t idx;
1390 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001391
Christoph Hellwig86685f72016-11-24 11:39:38 +11001392 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1393 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001394 *last_block = 0;
1395 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001396 case XFS_DINODE_FMT_BTREE:
1397 case XFS_DINODE_FMT_EXTENTS:
1398 break;
1399 default:
1400 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001401 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001402
1403 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1404 error = xfs_iread_extents(tp, ip, whichfork);
1405 if (error)
1406 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001407 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001408
1409 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1410 if (got.br_startoff <= *last_block - 1)
1411 return 0;
1412 }
1413
1414 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1415 *last_block = got.br_startoff + got.br_blockcount;
1416 return 0;
1417 }
1418
1419 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001420 return 0;
1421}
1422
Dave Chinner68988112013-08-12 20:49:42 +10001423int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001424xfs_bmap_last_extent(
1425 struct xfs_trans *tp,
1426 struct xfs_inode *ip,
1427 int whichfork,
1428 struct xfs_bmbt_irec *rec,
1429 int *is_empty)
1430{
1431 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1432 int error;
1433 int nextents;
1434
1435 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1436 error = xfs_iread_extents(tp, ip, whichfork);
1437 if (error)
1438 return error;
1439 }
1440
Eric Sandeen5d829302016-11-08 12:59:42 +11001441 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001442 if (nextents == 0) {
1443 *is_empty = 1;
1444 return 0;
1445 }
1446
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001447 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001448 *is_empty = 0;
1449 return 0;
1450}
1451
1452/*
1453 * Check the last inode extent to determine whether this allocation will result
1454 * in blocks being allocated at the end of the file. When we allocate new data
1455 * blocks at the end of the file which do not start at the previous data block,
1456 * we will try to align the new blocks at stripe unit boundaries.
1457 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001458 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001459 * at, or past the EOF.
1460 */
1461STATIC int
1462xfs_bmap_isaeof(
1463 struct xfs_bmalloca *bma,
1464 int whichfork)
1465{
1466 struct xfs_bmbt_irec rec;
1467 int is_empty;
1468 int error;
1469
Thomas Meyer749f24f2017-10-09 11:38:54 -07001470 bma->aeof = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001471 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1472 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001473 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001474 return error;
1475
Dave Chinner6e708bc2013-11-22 10:41:16 +11001476 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001477 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001478 return 0;
1479 }
1480
Dave Chinner9e5987a72013-02-25 12:31:26 +11001481 /*
1482 * Check if we are allocation or past the last extent, or at least into
1483 * the last delayed allocated extent.
1484 */
1485 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1486 (bma->offset >= rec.br_startoff &&
1487 isnullstartblock(rec.br_startblock));
1488 return 0;
1489}
1490
1491/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001492 * Returns the file-relative block number of the first block past eof in
1493 * the file. This is not based on i_size, it is based on the extent records.
1494 * Returns 0 for local files, as they do not have extent records.
1495 */
1496int
1497xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001498 struct xfs_inode *ip,
1499 xfs_fileoff_t *last_block,
1500 int whichfork)
1501{
1502 struct xfs_bmbt_irec rec;
1503 int is_empty;
1504 int error;
1505
1506 *last_block = 0;
1507
1508 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1509 return 0;
1510
1511 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1512 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001513 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001514
1515 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1516 if (error || is_empty)
1517 return error;
1518
1519 *last_block = rec.br_startoff + rec.br_blockcount;
1520 return 0;
1521}
1522
1523/*
1524 * Returns whether the selected fork of the inode has exactly one
1525 * block or not. For the data fork we check this matches di_size,
1526 * implying the file's range is 0..bsize-1.
1527 */
1528int /* 1=>1 block, 0=>otherwise */
1529xfs_bmap_one_block(
1530 xfs_inode_t *ip, /* incore inode */
1531 int whichfork) /* data or attr fork */
1532{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001533 xfs_ifork_t *ifp; /* inode fork pointer */
1534 int rval; /* return value */
1535 xfs_bmbt_irec_t s; /* internal version of extent */
1536
1537#ifndef DEBUG
1538 if (whichfork == XFS_DATA_FORK)
1539 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1540#endif /* !DEBUG */
1541 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1542 return 0;
1543 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1544 return 0;
1545 ifp = XFS_IFORK_PTR(ip, whichfork);
1546 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001547 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001548 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1549 if (rval && whichfork == XFS_DATA_FORK)
1550 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1551 return rval;
1552}
1553
1554/*
1555 * Extent tree manipulation functions used during allocation.
1556 */
1557
1558/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 */
1561STATIC int /* error */
1562xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001563 struct xfs_bmalloca *bma,
1564 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001566 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001569 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 xfs_fileoff_t new_endoff; /* end offset of new entry */
1571 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1572 /* left is 0, right is 1, prev is 2 */
1573 int rval=0; /* return value (logging flags) */
1574 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001575 xfs_filblks_t da_new; /* new count del alloc blocks used */
1576 xfs_filblks_t da_old; /* old count del alloc blocks used */
1577 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001579 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001580 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001581 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001583 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001584 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001585 ASSERT(whichfork != XFS_ATTR_FORK);
1586 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1587 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001588
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001589 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001590 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001591 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001592 ASSERT(!bma->cur ||
1593 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001594
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001595 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597#define LEFT r[0]
1598#define RIGHT r[1]
1599#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Darrick J. Wong60b49842016-10-03 09:11:34 -07001601 if (whichfork == XFS_COW_FORK)
1602 state |= BMAP_COWFORK;
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /*
1605 * Set up a bunch of variables to make the tests simpler.
1606 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001607 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001609 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ASSERT(PREV.br_startoff <= new->br_startoff);
1611 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001612
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001613 da_old = startblockval(PREV.br_startblock);
1614 da_new = 0;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /*
1617 * Set flags determining what part of the previous delayed allocation
1618 * extent is being replaced by a real allocation.
1619 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001620 if (PREV.br_startoff == new->br_startoff)
1621 state |= BMAP_LEFT_FILLING;
1622 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1623 state |= BMAP_RIGHT_FILLING;
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 /*
1626 * Check and set flags if this segment has a left neighbor.
1627 * Don't set contiguous if the combined extent would be too large.
1628 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001629 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001630 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001631 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001632
1633 if (isnullstartblock(LEFT.br_startblock))
1634 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001636
1637 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1638 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1639 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1640 LEFT.br_state == new->br_state &&
1641 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1642 state |= BMAP_LEFT_CONTIG;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * Check and set flags if this segment has a right neighbor.
1646 * Don't set contiguous if the combined extent would be too large.
1647 * Also check for all-three-contiguous being too large.
1648 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001649 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001650 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001651 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001652
1653 if (isnullstartblock(RIGHT.br_startblock))
1654 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001656
1657 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1658 new_endoff == RIGHT.br_startoff &&
1659 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1660 new->br_state == RIGHT.br_state &&
1661 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1662 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1663 BMAP_RIGHT_FILLING)) !=
1664 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1665 BMAP_RIGHT_FILLING) ||
1666 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1667 <= MAXEXTLEN))
1668 state |= BMAP_RIGHT_CONTIG;
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 error = 0;
1671 /*
1672 * Switch out based on the FILLING and CONTIG state bits.
1673 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001674 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1675 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1676 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1677 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /*
1679 * Filling in all of a previously delayed allocation extent.
1680 * The left and right neighbors are both contiguous with new.
1681 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001682 bma->idx--;
1683 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001684 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1685 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001686 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001687
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001688 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001689 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001690 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1692 else {
1693 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001694 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001695 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001697 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 error = xfs_btree_delete(bma->cur, &i);
1699 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001701 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001702 error = xfs_btree_decrement(bma->cur, 0, &i);
1703 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001705 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001706 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001707 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto done;
1709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 break;
1711
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001712 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
1714 * Filling in all of a previously delayed allocation extent.
1715 * The left neighbor is contiguous, the right is not.
1716 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001717 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001718
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001719 old = LEFT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001720 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001721 LEFT.br_blockcount += PREV.br_blockcount;
1722 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001723 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001724
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1726 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 rval = XFS_ILOG_DEXT;
1728 else {
1729 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001730 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001731 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001733 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001734 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001735 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 goto done;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 break;
1739
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001740 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /*
1742 * Filling in all of a previously delayed allocation extent.
1743 * The right neighbor is contiguous, the left is not.
1744 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001746 PREV.br_startblock = new->br_startblock;
1747 PREV.br_blockcount += RIGHT.br_blockcount;
1748 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001749 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001750
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001751 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1752 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 rval = XFS_ILOG_DEXT;
1754 else {
1755 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001756 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001757 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001759 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001760 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001761 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto done;
1763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 break;
1765
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001766 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /*
1768 * Filling in all of a previously delayed allocation extent.
1769 * Neither the left nor right neighbors are contiguous with
1770 * the new one.
1771 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001773 PREV.br_startblock = new->br_startblock;
1774 PREV.br_state = new->br_state;
1775 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001776 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001777
Darrick J. Wong60b49842016-10-03 09:11:34 -07001778 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1781 else {
1782 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001783 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001786 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 error = xfs_btree_insert(bma->cur, &i);
1788 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001790 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001794 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /*
1796 * Filling in the first part of a previous delayed allocation.
1797 * The left neighbor is contiguous.
1798 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001799 old = LEFT;
1800 temp = PREV.br_blockcount - new->br_blockcount;
1801 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1802 startblockval(PREV.br_startblock));
1803
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001804 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001805 LEFT.br_blockcount += new->br_blockcount;
1806 xfs_iext_update_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001807 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001808
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001810 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1811 PREV.br_startoff += new->br_blockcount;
1812 PREV.br_startblock = nullstartblock(da_new);
1813 xfs_iext_update_extent(ifp, bma->idx, &PREV);
1814 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1815
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 rval = XFS_ILOG_DEXT;
1818 else {
1819 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001820 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001821 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001823 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001824 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001825 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto done;
1827 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001828
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 break;
1831
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001832 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /*
1834 * Filling in the first part of a previous delayed allocation.
1835 * The left neighbor is not contiguous.
1836 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001838 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1841 else {
1842 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001843 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001846 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 error = xfs_btree_insert(bma->cur, &i);
1848 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001850 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001852
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001853 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001855 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001856 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 rval |= tmp_rval;
1858 if (error)
1859 goto done;
1860 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001861
1862 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001864 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001866
1867 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1868 PREV.br_startoff = new_endoff;
1869 PREV.br_blockcount = temp;
1870 PREV.br_startblock = nullstartblock(da_new);
1871 xfs_iext_update_extent(ifp, bma->idx + 1, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 break;
1875
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001876 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /*
1878 * Filling in the last part of a previous delayed allocation.
1879 * The right neighbor is contiguous with the new allocation.
1880 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001881 old = RIGHT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001883 RIGHT.br_startoff = new->br_startoff;
1884 RIGHT.br_startblock = new->br_startblock;
1885 RIGHT.br_blockcount += new->br_blockcount;
1886 xfs_iext_update_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001888
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001889 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 rval = XFS_ILOG_DEXT;
1891 else {
1892 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001893 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001896 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001897 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 goto done;
1900 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001901
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001902 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001904 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001905
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001907 PREV.br_blockcount = temp;
1908 PREV.br_startblock = nullstartblock(da_new);
1909 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001911
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001912 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001915 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /*
1917 * Filling in the last part of a previous delayed allocation.
1918 * The right neighbor is not contiguous.
1919 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001920 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001921 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001922 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1924 else {
1925 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001926 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001929 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 error = xfs_btree_insert(bma->cur, &i);
1931 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001933 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001935
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001936 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001938 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001939 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 rval |= tmp_rval;
1941 if (error)
1942 goto done;
1943 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001944
1945 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001946 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001947 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001949
1950 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1951 PREV.br_startblock = nullstartblock(da_new);
1952 PREV.br_blockcount = temp;
1953 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001954 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001955
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 break;
1958
1959 case 0:
1960 /*
1961 * Filling in the middle part of a previous delayed allocation.
1962 * Contiguity is impossible here.
1963 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001964 *
1965 * We start with a delayed allocation:
1966 *
1967 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1968 * PREV @ idx
1969 *
1970 * and we are allocating:
1971 * +rrrrrrrrrrrrrrrrr+
1972 * new
1973 *
1974 * and we set it up for insertion as:
1975 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1976 * new
1977 * PREV @ idx LEFT RIGHT
1978 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001980 old = PREV;
1981
1982 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001983 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001984
1985 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001986 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001987 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001988 RIGHT.br_blockcount =
1989 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1990 RIGHT.br_startblock =
1991 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1992 RIGHT.br_blockcount));
1993
1994 /* truncate PREV */
1995 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
1996 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1997 PREV.br_startblock =
1998 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1999 PREV.br_blockcount));
2000 xfs_iext_update_extent(ifp, bma->idx, &PREV);
2001 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2002
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002003 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002005 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002006
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2009 else {
2010 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002011 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002012 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002014 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 error = xfs_btree_insert(bma->cur, &i);
2016 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002018 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002020
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002021 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002022 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002023 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002024 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 rval |= tmp_rval;
2026 if (error)
2027 goto done;
2028 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002029
2030 da_new = startblockval(PREV.br_startblock) +
2031 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002032 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 break;
2034
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002035 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2036 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2037 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2038 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2039 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2040 case BMAP_LEFT_CONTIG:
2041 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * These cases are all impossible.
2044 */
2045 ASSERT(0);
2046 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002047
Darrick J. Wong9c194642016-08-03 12:16:05 +10002048 /* add reverse mapping */
2049 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2050 if (error)
2051 goto done;
2052
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002053 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002054 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002055 int tmp_logflags; /* partial log flag return val */
2056
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002057 ASSERT(bma->cur == NULL);
2058 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002059 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002060 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002061 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002062 if (error)
2063 goto done;
2064 }
2065
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002066 if (bma->cur) {
2067 da_new += bma->cur->bc_private.b.allocated;
2068 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002069 }
2070
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002071 /* adjust for changes in reserved delayed indirect blocks */
2072 if (da_new != da_old) {
2073 ASSERT(state == 0 || da_new < da_old);
2074 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2075 false);
2076 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002077
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002078 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002080 if (whichfork != XFS_COW_FORK)
2081 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return error;
2083#undef LEFT
2084#undef RIGHT
2085#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
2088/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002089 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
2091STATIC int /* error */
2092xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002093 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002095 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002096 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002098 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002099 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002100 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002101 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002106 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2109 /* left is 0, right is 1, prev is 2 */
2110 int rval=0; /* return value (logging flags) */
2111 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002112 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002113 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002115 *logflagsp = 0;
2116
2117 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002118 ifp = XFS_IFORK_PTR(ip, whichfork);
2119 if (whichfork == XFS_COW_FORK)
2120 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002121
2122 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002123 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002124 ASSERT(!isnullstartblock(new->br_startblock));
2125
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002126 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128#define LEFT r[0]
2129#define RIGHT r[1]
2130#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /*
2133 * Set up a bunch of variables to make the tests simpler.
2134 */
2135 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002136 xfs_iext_get_extent(ifp, *idx, &PREV);
2137 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 new_endoff = new->br_startoff + new->br_blockcount;
2139 ASSERT(PREV.br_startoff <= new->br_startoff);
2140 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * Set flags determining what part of the previous oldext allocation
2144 * extent is being replaced by a newext allocation.
2145 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002146 if (PREV.br_startoff == new->br_startoff)
2147 state |= BMAP_LEFT_FILLING;
2148 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2149 state |= BMAP_RIGHT_FILLING;
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /*
2152 * Check and set flags if this segment has a left neighbor.
2153 * Don't set contiguous if the combined extent would be too large.
2154 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002155 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002156 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002157 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002158
2159 if (isnullstartblock(LEFT.br_startblock))
2160 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002162
2163 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2164 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2165 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002166 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002167 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2168 state |= BMAP_LEFT_CONTIG;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * Check and set flags if this segment has a right neighbor.
2172 * Don't set contiguous if the combined extent would be too large.
2173 * Also check for all-three-contiguous being too large.
2174 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002175 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002176 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002177 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002178 if (isnullstartblock(RIGHT.br_startblock))
2179 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002181
2182 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2183 new_endoff == RIGHT.br_startoff &&
2184 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002185 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002186 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2187 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2188 BMAP_RIGHT_FILLING)) !=
2189 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2190 BMAP_RIGHT_FILLING) ||
2191 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2192 <= MAXEXTLEN))
2193 state |= BMAP_RIGHT_CONTIG;
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /*
2196 * Switch out based on the FILLING and CONTIG state bits.
2197 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002198 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2199 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2200 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2201 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /*
2203 * Setting all of a previous oldext extent to newext.
2204 * The left and right neighbors are both contiguous with new.
2205 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002206 --*idx;
2207
2208 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002209 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2210 xfs_iext_update_extent(ifp, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002211 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002212
Christoph Hellwigec90c552011-05-23 08:52:53 +00002213 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002214 XFS_IFORK_NEXT_SET(ip, whichfork,
2215 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 if (cur == NULL)
2217 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2218 else {
2219 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002220 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2221 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002223 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002224 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002226 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002227 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002229 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002230 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002232 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002233 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002235 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002236 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002237 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto done;
2239 }
2240 break;
2241
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002242 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /*
2244 * Setting all of a previous oldext extent to newext.
2245 * The left neighbor is contiguous, the right is not.
2246 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002247 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002248
Christoph Hellwigec90c552011-05-23 08:52:53 +00002249 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002250 LEFT.br_blockcount += PREV.br_blockcount;
2251 xfs_iext_update_extent(ifp, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002252 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2253
2254 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002255 XFS_IFORK_NEXT_SET(ip, whichfork,
2256 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (cur == NULL)
2258 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2259 else {
2260 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002261 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2262 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002264 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002265 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002267 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002268 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002270 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002271 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002272 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 goto done;
2274 }
2275 break;
2276
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002277 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /*
2279 * Setting all of a previous oldext extent to newext.
2280 * The right neighbor is contiguous, the left is not.
2281 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002282 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002283 PREV.br_blockcount += RIGHT.br_blockcount;
2284 PREV.br_state = new->br_state;
2285 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002286 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002287
Christoph Hellwigec90c552011-05-23 08:52:53 +00002288 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002289 XFS_IFORK_NEXT_SET(ip, whichfork,
2290 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (cur == NULL)
2292 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2293 else {
2294 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002295 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002298 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002299 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002301 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002302 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002304 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002305 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002306 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 goto done;
2308 }
2309 break;
2310
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002311 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Setting all of a previous oldext extent to newext.
2314 * Neither the left nor right neighbors are contiguous with
2315 * the new one.
2316 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002317 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002318 PREV.br_state = new->br_state;
2319 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002320 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (cur == NULL)
2323 rval = XFS_ILOG_DEXT;
2324 else {
2325 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002326 error = xfs_bmbt_lookup_eq(cur, new, &i);
2327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002329 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002330 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002331 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 goto done;
2333 }
2334 break;
2335
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002336 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /*
2338 * Setting the first part of a previous oldext extent to newext.
2339 * The left neighbor is contiguous.
2340 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002341 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002342 LEFT.br_blockcount += new->br_blockcount;
2343 xfs_iext_update_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002344 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002345
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002346 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002347 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002348 PREV.br_startoff += new->br_blockcount;
2349 PREV.br_startblock += new->br_blockcount;
2350 PREV.br_blockcount -= new->br_blockcount;
2351 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002352 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002353
Christoph Hellwigec90c552011-05-23 08:52:53 +00002354 --*idx;
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (cur == NULL)
2357 rval = XFS_ILOG_DEXT;
2358 else {
2359 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002360 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002363 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002364 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002365 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002367 error = xfs_btree_decrement(cur, 0, &i);
2368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002370 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002371 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 goto done;
2373 }
2374 break;
2375
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002376 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /*
2378 * Setting the first part of a previous oldext extent to newext.
2379 * The left neighbor is not contiguous.
2380 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002381 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002382 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002383 PREV.br_startoff += new->br_blockcount;
2384 PREV.br_startblock += new->br_blockcount;
2385 PREV.br_blockcount -= new->br_blockcount;
2386 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002387 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002388
Christoph Hellwigec90c552011-05-23 08:52:53 +00002389 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002390 XFS_IFORK_NEXT_SET(ip, whichfork,
2391 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (cur == NULL)
2393 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2394 else {
2395 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002396 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002397 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002399 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002400 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002401 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto done;
2403 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002404 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002406 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408 break;
2409
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002410 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /*
2412 * Setting the last part of a previous oldext extent to newext.
2413 * The right neighbor is contiguous with the new allocation.
2414 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002415 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002416 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 PREV.br_blockcount -= new->br_blockcount;
2418 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002419 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2420
2421 ++*idx;
2422
2423 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002424 RIGHT.br_startoff = new->br_startoff;
2425 RIGHT.br_startblock = new->br_startblock;
2426 RIGHT.br_blockcount += new->br_blockcount;
2427 xfs_iext_update_extent(ifp, *idx, &RIGHT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002428 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (cur == NULL)
2431 rval = XFS_ILOG_DEXT;
2432 else {
2433 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002434 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002435 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002437 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002438 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002439 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002441 error = xfs_btree_increment(cur, 0, &i);
2442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002444 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002445 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 goto done;
2447 }
2448 break;
2449
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002450 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /*
2452 * Setting the last part of a previous oldext extent to newext.
2453 * The right neighbor is not contiguous.
2454 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002455 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002456 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002457 PREV.br_blockcount -= new->br_blockcount;
2458 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002459 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002460
Christoph Hellwigec90c552011-05-23 08:52:53 +00002461 ++*idx;
2462 xfs_iext_insert(ip, *idx, 1, new, state);
2463
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002464 XFS_IFORK_NEXT_SET(ip, whichfork,
2465 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (cur == NULL)
2467 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2468 else {
2469 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002470 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002471 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002473 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002474 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002475 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002477 error = xfs_bmbt_lookup_eq(cur, new, &i);
2478 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002480 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002481 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002483 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485 break;
2486
2487 case 0:
2488 /*
2489 * Setting the middle part of a previous oldext extent to
2490 * newext. Contiguity is impossible here.
2491 * One extent becomes three extents.
2492 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002493 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002494 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002495 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2496 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002497 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 r[0] = *new;
2500 r[1].br_startoff = new_endoff;
2501 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002502 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002504 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002505
2506 ++*idx;
2507 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2508
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002509 XFS_IFORK_NEXT_SET(ip, whichfork,
2510 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (cur == NULL)
2512 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2513 else {
2514 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002515 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002516 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002518 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002520 error = xfs_bmbt_update(cur, &r[1]);
2521 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 goto done;
2523 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002525 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002527 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002528 /*
2529 * Reset the cursor to the position of the new extent
2530 * we are about to insert as we can't trust it after
2531 * the previous insert.
2532 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002533 error = xfs_bmbt_lookup_eq(cur, new, &i);
2534 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002536 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002538 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002540 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
2542 break;
2543
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002544 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2545 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2546 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2547 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2548 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2549 case BMAP_LEFT_CONTIG:
2550 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 /*
2552 * These cases are all impossible.
2553 */
2554 ASSERT(0);
2555 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002556
Darrick J. Wong9c194642016-08-03 12:16:05 +10002557 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002558 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002559 if (error)
2560 goto done;
2561
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002562 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002563 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002564 int tmp_logflags; /* partial log flag return val */
2565
2566 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002567 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002568 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002569 *logflagsp |= tmp_logflags;
2570 if (error)
2571 goto done;
2572 }
2573
2574 /* clear out the allocated field, done with it now in any case. */
2575 if (cur) {
2576 cur->bc_private.b.allocated = 0;
2577 *curp = cur;
2578 }
2579
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002580 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002582 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return error;
2584#undef LEFT
2585#undef RIGHT
2586#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
2589/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002590 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002592STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593xfs_bmap_add_extent_hole_delay(
2594 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002595 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002596 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002597 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002599 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2601 xfs_filblks_t newlen=0; /* new indirect size */
2602 xfs_filblks_t oldlen=0; /* old indirect size */
2603 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2604 int state; /* state bits, accessed thru macros */
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002605 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002607 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002609 if (whichfork == XFS_COW_FORK)
2610 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002611 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /*
2614 * Check and set flags if this segment has a left neighbor
2615 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002616 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002617 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002618 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002619
2620 if (isnullstartblock(left.br_startblock))
2621 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /*
2625 * Check and set flags if the current (right) segment exists.
2626 * If it doesn't exist, we're converting the hole at end-of-file.
2627 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002628 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002629 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002630 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002631
2632 if (isnullstartblock(right.br_startblock))
2633 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 /*
2637 * Set contiguity flags on the left and right neighbors.
2638 * Don't let extents get too large, even if the pieces are contiguous.
2639 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002640 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2641 left.br_startoff + left.br_blockcount == new->br_startoff &&
2642 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2643 state |= BMAP_LEFT_CONTIG;
2644
2645 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2646 new->br_startoff + new->br_blockcount == right.br_startoff &&
2647 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2648 (!(state & BMAP_LEFT_CONTIG) ||
2649 (left.br_blockcount + new->br_blockcount +
2650 right.br_blockcount <= MAXEXTLEN)))
2651 state |= BMAP_RIGHT_CONTIG;
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 /*
2654 * Switch out based on the contiguity flags.
2655 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002656 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2657 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /*
2659 * New allocation is contiguous with delayed allocations
2660 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002661 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002663 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 temp = left.br_blockcount + new->br_blockcount +
2665 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002666
Christoph Hellwigec90c552011-05-23 08:52:53 +00002667 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002668 oldlen = startblockval(left.br_startblock) +
2669 startblockval(new->br_startblock) +
2670 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002671 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2672 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002673 left.br_startblock = nullstartblock(newlen);
2674 left.br_blockcount = temp;
2675 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002676 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002677
Christoph Hellwigec90c552011-05-23 08:52:53 +00002678 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 break;
2680
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002681 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 /*
2683 * New allocation is contiguous with a delayed allocation
2684 * on the left.
2685 * Merge the new allocation with the left neighbor.
2686 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002687 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002689
2690 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002691 oldlen = startblockval(left.br_startblock) +
2692 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002693 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2694 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002695 left.br_blockcount = temp;
2696 left.br_startblock = nullstartblock(newlen);
2697 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002698 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 break;
2700
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002701 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 /*
2703 * New allocation is contiguous with a delayed allocation
2704 * on the right.
2705 * Merge the new allocation with the right neighbor.
2706 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002707 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002709 oldlen = startblockval(new->br_startblock) +
2710 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002711 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2712 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002713 right.br_startoff = new->br_startoff;
2714 right.br_startblock = nullstartblock(newlen);
2715 right.br_blockcount = temp;
2716 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002717 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 break;
2719
2720 case 0:
2721 /*
2722 * New allocation is not contiguous with another
2723 * delayed allocation.
2724 * Insert a new entry.
2725 */
2726 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002727 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 break;
2729 }
2730 if (oldlen != newlen) {
2731 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002732 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2733 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * Nothing to do for disk quota accounting here.
2736 */
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002741 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 */
2743STATIC int /* error */
2744xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002745 struct xfs_trans *tp,
2746 struct xfs_inode *ip,
2747 int whichfork,
2748 xfs_extnum_t *idx,
2749 struct xfs_btree_cur **curp,
2750 struct xfs_bmbt_irec *new,
2751 xfs_fsblock_t *first,
2752 struct xfs_defer_ops *dfops,
2753 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002755 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2756 struct xfs_mount *mp = ip->i_mount;
2757 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2761 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002762 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 int state; /* state bits, accessed thru macros */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002764 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Christoph Hellwig6d045582017-04-11 16:45:54 -07002766 ASSERT(*idx >= 0);
2767 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002768 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002769 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002770
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002771 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002772
2773 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002774 if (whichfork == XFS_ATTR_FORK)
2775 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002776 if (whichfork == XFS_COW_FORK)
2777 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 /*
2780 * Check and set flags if this segment has a left neighbor.
2781 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002782 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002783 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002784 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002785 if (isnullstartblock(left.br_startblock))
2786 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * Check and set flags if this segment has a current value.
2791 * Not true if we're inserting into the "hole" at eof.
2792 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002793 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002794 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002795 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002796 if (isnullstartblock(right.br_startblock))
2797 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /*
2801 * We're inserting a real allocation between "left" and "right".
2802 * Set the contiguity flags. Don't let extents get too large.
2803 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002804 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2805 left.br_startoff + left.br_blockcount == new->br_startoff &&
2806 left.br_startblock + left.br_blockcount == new->br_startblock &&
2807 left.br_state == new->br_state &&
2808 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2809 state |= BMAP_LEFT_CONTIG;
2810
2811 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2812 new->br_startoff + new->br_blockcount == right.br_startoff &&
2813 new->br_startblock + new->br_blockcount == right.br_startblock &&
2814 new->br_state == right.br_state &&
2815 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2816 (!(state & BMAP_LEFT_CONTIG) ||
2817 left.br_blockcount + new->br_blockcount +
2818 right.br_blockcount <= MAXEXTLEN))
2819 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002821 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 /*
2823 * Select which case we're in here, and implement it.
2824 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002825 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2826 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 /*
2828 * New allocation is contiguous with real allocations on the
2829 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002830 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002832 --*idx;
2833 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002834 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2835 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002836 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002837
Christoph Hellwig6d045582017-04-11 16:45:54 -07002838 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002839
Christoph Hellwig6d045582017-04-11 16:45:54 -07002840 XFS_IFORK_NEXT_SET(ip, whichfork,
2841 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2842 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002843 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002844 } else {
2845 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002846 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002847 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002848 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002851 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002852 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002853 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002854 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002855 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002856 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002857 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002858 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002859 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002860 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002864 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 /*
2866 * New allocation is contiguous with a real allocation
2867 * on the left.
2868 * Merge the new allocation with the left neighbor.
2869 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002870 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002871 old = left;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002872 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002873 left.br_blockcount += new->br_blockcount;
2874 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002875 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002876
Christoph Hellwig6d045582017-04-11 16:45:54 -07002877 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002878 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002879 } else {
2880 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002881 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002882 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002883 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002884 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002885 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002886 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002887 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002889 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002891 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 /*
2893 * New allocation is contiguous with a real allocation
2894 * on the right.
2895 * Merge the new allocation with the right neighbor.
2896 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002897 old = right;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002898 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002899 right.br_startoff = new->br_startoff;
2900 right.br_startblock = new->br_startblock;
2901 right.br_blockcount += new->br_blockcount;
2902 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002903 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002904
Christoph Hellwig6d045582017-04-11 16:45:54 -07002905 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002906 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002907 } else {
2908 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002909 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002910 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002911 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002912 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002913 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002914 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002915 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 case 0:
2920 /*
2921 * New allocation is not contiguous with another
2922 * real allocation.
2923 * Insert a new entry.
2924 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 xfs_iext_insert(ip, *idx, 1, new, state);
2926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2928 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002929 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002930 } else {
2931 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002932 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002933 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002934 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002935 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002936 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002937 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002938 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002939 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002943
Darrick J. Wong9c194642016-08-03 12:16:05 +10002944 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002945 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002946 if (error)
2947 goto done;
2948
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002949 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002950 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002951 int tmp_logflags; /* partial log flag return val */
2952
Christoph Hellwig6d045582017-04-11 16:45:54 -07002953 ASSERT(cur == NULL);
2954 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002955 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002956 *logflagsp |= tmp_logflags;
2957 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002958 if (error)
2959 goto done;
2960 }
2961
2962 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002963 if (cur)
2964 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002965
Christoph Hellwig6d045582017-04-11 16:45:54 -07002966 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002967done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002968 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002969 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
Nathan Scottdd9f4382006-01-11 15:28:28 +11002972/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11002973 * Functions used in the extent read, allocate and remove paths
2974 */
2975
2976/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002977 * Adjust the size of the new extent based on di_extsize and rt extsize.
2978 */
Dave Chinner68988112013-08-12 20:49:42 +10002979int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002980xfs_bmap_extsize_align(
2981 xfs_mount_t *mp,
2982 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2983 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2984 xfs_extlen_t extsz, /* align to this extent size */
2985 int rt, /* is this a realtime inode? */
2986 int eof, /* is extent at end-of-file? */
2987 int delay, /* creating delalloc extent? */
2988 int convert, /* overwriting unwritten extent? */
2989 xfs_fileoff_t *offp, /* in/out: aligned offset */
2990 xfs_extlen_t *lenp) /* in/out: aligned length */
2991{
2992 xfs_fileoff_t orig_off; /* original offset */
2993 xfs_extlen_t orig_alen; /* original length */
2994 xfs_fileoff_t orig_end; /* original off+len */
2995 xfs_fileoff_t nexto; /* next file offset */
2996 xfs_fileoff_t prevo; /* previous file offset */
2997 xfs_fileoff_t align_off; /* temp for offset */
2998 xfs_extlen_t align_alen; /* temp for length */
2999 xfs_extlen_t temp; /* temp for calculations */
3000
3001 if (convert)
3002 return 0;
3003
3004 orig_off = align_off = *offp;
3005 orig_alen = align_alen = *lenp;
3006 orig_end = orig_off + orig_alen;
3007
3008 /*
3009 * If this request overlaps an existing extent, then don't
3010 * attempt to perform any additional alignment.
3011 */
3012 if (!delay && !eof &&
3013 (orig_off >= gotp->br_startoff) &&
3014 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3015 return 0;
3016 }
3017
3018 /*
3019 * If the file offset is unaligned vs. the extent size
3020 * we need to align it. This will be possible unless
3021 * the file was previously written with a kernel that didn't
3022 * perform this alignment, or if a truncate shot us in the
3023 * foot.
3024 */
3025 temp = do_mod(orig_off, extsz);
3026 if (temp) {
3027 align_alen += temp;
3028 align_off -= temp;
3029 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003030
3031 /* Same adjustment for the end of the requested area. */
3032 temp = (align_alen % extsz);
3033 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003034 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003035
3036 /*
3037 * For large extent hint sizes, the aligned extent might be larger than
3038 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3039 * the length back under MAXEXTLEN. The outer allocation loops handle
3040 * short allocation just fine, so it is safe to do this. We only want to
3041 * do it when we are forced to, though, because it means more allocation
3042 * operations are required.
3043 */
3044 while (align_alen > MAXEXTLEN)
3045 align_alen -= extsz;
3046 ASSERT(align_alen <= MAXEXTLEN);
3047
Nathan Scottdd9f4382006-01-11 15:28:28 +11003048 /*
3049 * If the previous block overlaps with this proposed allocation
3050 * then move the start forward without adjusting the length.
3051 */
3052 if (prevp->br_startoff != NULLFILEOFF) {
3053 if (prevp->br_startblock == HOLESTARTBLOCK)
3054 prevo = prevp->br_startoff;
3055 else
3056 prevo = prevp->br_startoff + prevp->br_blockcount;
3057 } else
3058 prevo = 0;
3059 if (align_off != orig_off && align_off < prevo)
3060 align_off = prevo;
3061 /*
3062 * If the next block overlaps with this proposed allocation
3063 * then move the start back without adjusting the length,
3064 * but not before offset 0.
3065 * This may of course make the start overlap previous block,
3066 * and if we hit the offset 0 limit then the next block
3067 * can still overlap too.
3068 */
3069 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3070 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3071 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3072 nexto = gotp->br_startoff + gotp->br_blockcount;
3073 else
3074 nexto = gotp->br_startoff;
3075 } else
3076 nexto = NULLFILEOFF;
3077 if (!eof &&
3078 align_off + align_alen != orig_end &&
3079 align_off + align_alen > nexto)
3080 align_off = nexto > align_alen ? nexto - align_alen : 0;
3081 /*
3082 * If we're now overlapping the next or previous extent that
3083 * means we can't fit an extsz piece in this hole. Just move
3084 * the start forward to the first valid spot and set
3085 * the length so we hit the end.
3086 */
3087 if (align_off != orig_off && align_off < prevo)
3088 align_off = prevo;
3089 if (align_off + align_alen != orig_end &&
3090 align_off + align_alen > nexto &&
3091 nexto != NULLFILEOFF) {
3092 ASSERT(nexto > prevo);
3093 align_alen = nexto - align_off;
3094 }
3095
3096 /*
3097 * If realtime, and the result isn't a multiple of the realtime
3098 * extent size we need to remove blocks until it is.
3099 */
3100 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3101 /*
3102 * We're not covering the original request, or
3103 * we won't be able to once we fix the length.
3104 */
3105 if (orig_off < align_off ||
3106 orig_end > align_off + align_alen ||
3107 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003108 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003109 /*
3110 * Try to fix it by moving the start up.
3111 */
3112 if (align_off + temp <= orig_off) {
3113 align_alen -= temp;
3114 align_off += temp;
3115 }
3116 /*
3117 * Try to fix it by moving the end in.
3118 */
3119 else if (align_off + align_alen - temp >= orig_end)
3120 align_alen -= temp;
3121 /*
3122 * Set the start to the minimum then trim the length.
3123 */
3124 else {
3125 align_alen -= orig_off - align_off;
3126 align_off = orig_off;
3127 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3128 }
3129 /*
3130 * Result doesn't cover the request, fail it.
3131 */
3132 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003133 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003134 } else {
3135 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003136 /* see MAXEXTLEN handling above */
3137 ASSERT(orig_end <= align_off + align_alen ||
3138 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003139 }
3140
3141#ifdef DEBUG
3142 if (!eof && gotp->br_startoff != NULLFILEOFF)
3143 ASSERT(align_off + align_alen <= gotp->br_startoff);
3144 if (prevp->br_startoff != NULLFILEOFF)
3145 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3146#endif
3147
3148 *lenp = align_alen;
3149 *offp = align_off;
3150 return 0;
3151}
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153#define XFS_ALLOC_GAP_UNITS 4
3154
Dave Chinner68988112013-08-12 20:49:42 +10003155void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003156xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003157 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
3159 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3161 xfs_mount_t *mp; /* mount point structure */
3162 int nullfb; /* true if ap->firstblock isn't set */
3163 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
3165#define ISVALID(x,y) \
3166 (rt ? \
3167 (x) < mp->m_sb.sb_rblocks : \
3168 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3169 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3170 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003173 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003174 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3175 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003176 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 /*
3178 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003179 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003181 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3182 !isnullstartblock(ap->prev.br_startblock) &&
3183 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3184 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003185 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 /*
3187 * Adjust for the gap between prevp and us.
3188 */
Dave Chinner3a756672011-09-18 20:40:58 +00003189 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003190 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003192 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3193 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
3195 /*
3196 * If not at eof, then compare the two neighbor blocks.
3197 * Figure out whether either one gives us a good starting point,
3198 * and pick the better one.
3199 */
3200 else if (!ap->eof) {
3201 xfs_fsblock_t gotbno; /* right side block number */
3202 xfs_fsblock_t gotdiff=0; /* right side difference */
3203 xfs_fsblock_t prevbno; /* left side block number */
3204 xfs_fsblock_t prevdiff=0; /* left side difference */
3205
3206 /*
3207 * If there's a previous (left) block, select a requested
3208 * start block based on it.
3209 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003210 if (ap->prev.br_startoff != NULLFILEOFF &&
3211 !isnullstartblock(ap->prev.br_startblock) &&
3212 (prevbno = ap->prev.br_startblock +
3213 ap->prev.br_blockcount) &&
3214 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 /*
3216 * Calculate gap to end of previous block.
3217 */
Dave Chinner3a756672011-09-18 20:40:58 +00003218 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003219 (ap->prev.br_startoff +
3220 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 /*
3222 * Figure the startblock based on the previous block's
3223 * end and the gap size.
3224 * Heuristic!
3225 * If the gap is large relative to the piece we're
3226 * allocating, or using it gives us an invalid block
3227 * number, then just use the end of the previous block.
3228 */
Dave Chinner3a756672011-09-18 20:40:58 +00003229 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003231 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 prevbno += adjust;
3233 else
3234 prevdiff += adjust;
3235 /*
3236 * If the firstblock forbids it, can't use it,
3237 * must use default.
3238 */
3239 if (!rt && !nullfb &&
3240 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3241 prevbno = NULLFSBLOCK;
3242 }
3243 /*
3244 * No previous block or can't follow it, just default.
3245 */
3246 else
3247 prevbno = NULLFSBLOCK;
3248 /*
3249 * If there's a following (right) block, select a requested
3250 * start block based on it.
3251 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003252 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 /*
3254 * Calculate gap to start of next block.
3255 */
Dave Chinner3a756672011-09-18 20:40:58 +00003256 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 /*
3258 * Figure the startblock based on the next block's
3259 * start and the gap size.
3260 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003261 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 /*
3263 * Heuristic!
3264 * If the gap is large relative to the piece we're
3265 * allocating, or using it gives us an invalid block
3266 * number, then just use the start of the next block
3267 * offset by our length.
3268 */
Dave Chinner3a756672011-09-18 20:40:58 +00003269 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 ISVALID(gotbno - gotdiff, gotbno))
3271 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003272 else if (ISVALID(gotbno - ap->length, gotbno)) {
3273 gotbno -= ap->length;
3274 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 } else
3276 gotdiff += adjust;
3277 /*
3278 * If the firstblock forbids it, can't use it,
3279 * must use default.
3280 */
3281 if (!rt && !nullfb &&
3282 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3283 gotbno = NULLFSBLOCK;
3284 }
3285 /*
3286 * No next block, just default.
3287 */
3288 else
3289 gotbno = NULLFSBLOCK;
3290 /*
3291 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003292 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 */
3294 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003295 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003297 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003299 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003301#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003302}
3303
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003304static int
3305xfs_bmap_longest_free_extent(
3306 struct xfs_trans *tp,
3307 xfs_agnumber_t ag,
3308 xfs_extlen_t *blen,
3309 int *notinit)
3310{
3311 struct xfs_mount *mp = tp->t_mountp;
3312 struct xfs_perag *pag;
3313 xfs_extlen_t longest;
3314 int error = 0;
3315
3316 pag = xfs_perag_get(mp, ag);
3317 if (!pag->pagf_init) {
3318 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3319 if (error)
3320 goto out;
3321
3322 if (!pag->pagf_init) {
3323 *notinit = 1;
3324 goto out;
3325 }
3326 }
3327
Dave Chinner50adbcb2015-06-22 10:04:31 +10003328 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003329 xfs_alloc_min_freelist(mp, pag),
3330 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003331 if (*blen < longest)
3332 *blen = longest;
3333
3334out:
3335 xfs_perag_put(pag);
3336 return error;
3337}
3338
3339static void
3340xfs_bmap_select_minlen(
3341 struct xfs_bmalloca *ap,
3342 struct xfs_alloc_arg *args,
3343 xfs_extlen_t *blen,
3344 int notinit)
3345{
3346 if (notinit || *blen < ap->minlen) {
3347 /*
3348 * Since we did a BUF_TRYLOCK above, it is possible that
3349 * there is space for this request.
3350 */
3351 args->minlen = ap->minlen;
3352 } else if (*blen < args->maxlen) {
3353 /*
3354 * If the best seen length is less than the request length,
3355 * use the best as the minimum.
3356 */
3357 args->minlen = *blen;
3358 } else {
3359 /*
3360 * Otherwise we've seen an extent as big as maxlen, use that
3361 * as the minimum.
3362 */
3363 args->minlen = args->maxlen;
3364 }
3365}
3366
Nathan Scotta365bdd2006-03-14 13:34:16 +11003367STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003368xfs_bmap_btalloc_nullfb(
3369 struct xfs_bmalloca *ap,
3370 struct xfs_alloc_arg *args,
3371 xfs_extlen_t *blen)
3372{
3373 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003374 xfs_agnumber_t ag, startag;
3375 int notinit = 0;
3376 int error;
3377
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003378 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003379 args->total = ap->total;
3380
Christoph Hellwigc467c042010-02-15 23:34:42 +00003381 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3382 if (startag == NULLAGNUMBER)
3383 startag = ag = 0;
3384
Dave Chinner14b064c2011-01-27 12:16:28 +11003385 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003386 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3387 &notinit);
3388 if (error)
3389 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003390
Christoph Hellwigc467c042010-02-15 23:34:42 +00003391 if (++ag == mp->m_sb.sb_agcount)
3392 ag = 0;
3393 if (ag == startag)
3394 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003395 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003396
3397 xfs_bmap_select_minlen(ap, args, blen, notinit);
3398 return 0;
3399}
3400
3401STATIC int
3402xfs_bmap_btalloc_filestreams(
3403 struct xfs_bmalloca *ap,
3404 struct xfs_alloc_arg *args,
3405 xfs_extlen_t *blen)
3406{
3407 struct xfs_mount *mp = ap->ip->i_mount;
3408 xfs_agnumber_t ag;
3409 int notinit = 0;
3410 int error;
3411
3412 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3413 args->total = ap->total;
3414
3415 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3416 if (ag == NULLAGNUMBER)
3417 ag = 0;
3418
3419 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3420 if (error)
3421 return error;
3422
3423 if (*blen < args->maxlen) {
3424 error = xfs_filestream_new_ag(ap, &ag);
3425 if (error)
3426 return error;
3427
3428 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3429 &notinit);
3430 if (error)
3431 return error;
3432
3433 }
3434
3435 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003436
3437 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003438 * Set the failure fallback case to look in the selected AG as stream
3439 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003440 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003441 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003442 return 0;
3443}
3444
3445STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003446xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003447 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003448{
3449 xfs_mount_t *mp; /* mount point structure */
3450 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003451 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003452 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003453 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003454 xfs_alloc_arg_t args;
3455 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003456 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003457 int nullfb; /* true if ap->firstblock isn't set */
3458 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003459 int tryagain;
3460 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003461 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003462
Dave Chinnera99ebf42011-12-01 11:24:20 +00003463 ASSERT(ap->length);
3464
Nathan Scotta365bdd2006-03-14 13:34:16 +11003465 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003466
3467 /* stripe alignment for allocation is determined by mount parameters */
3468 stripe_align = 0;
3469 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3470 stripe_align = mp->m_swidth;
3471 else if (mp->m_dalign)
3472 stripe_align = mp->m_dalign;
3473
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003474 if (ap->flags & XFS_BMAPI_COWFORK)
3475 align = xfs_get_cowextsz_hint(ap->ip);
3476 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003477 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003478 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003479 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003480 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003481 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003482 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003483 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003484 }
Dave Chinner33177f052013-12-12 16:34:36 +11003485
3486
Dave Chinner0937e0f2011-09-18 20:40:57 +00003487 nullfb = *ap->firstblock == NULLFSBLOCK;
3488 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003489 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003490 if (xfs_alloc_is_userdata(ap->datatype) &&
3491 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003492 ag = xfs_filestream_lookup_ag(ap->ip);
3493 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003494 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003495 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003496 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003497 }
3498 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003499 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003500
3501 xfs_bmap_adjacent(ap);
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003504 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 * it's in the right allocation group.
3506 */
Dave Chinner3a756672011-09-18 20:40:58 +00003507 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 ;
3509 else
Dave Chinner3a756672011-09-18 20:40:58 +00003510 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 * Normal allocation, done through xfs_alloc_vextent.
3513 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003514 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003515 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003516 args.tp = ap->tp;
3517 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003518 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003519 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003520
3521 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003522 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003523 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003524 blen = 0;
3525 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003526 /*
3527 * Search for an allocation group with a single extent large
3528 * enough for the request. If one isn't found, then adjust
3529 * the minimum allocation size to the largest space found.
3530 */
Dave Chinner292378e2016-09-26 08:21:28 +10003531 if (xfs_alloc_is_userdata(ap->datatype) &&
3532 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003533 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3534 else
3535 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003536 if (error)
3537 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003538 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003539 if (xfs_inode_is_filestream(ap->ip))
3540 args.type = XFS_ALLOCTYPE_FIRST_AG;
3541 else
3542 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003543 args.total = args.minlen = ap->minlen;
3544 } else {
3545 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3546 args.total = ap->total;
3547 args.minlen = ap->minlen;
3548 }
David Chinner957d0eb2007-06-18 16:50:37 +10003549 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003550 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003551 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003552 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003553 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003554 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003555 args.prod = 1;
3556 args.mod = 0;
3557 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003558 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003559 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003560 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3561 }
3562 /*
3563 * If we are not low on available data blocks, and the
3564 * underlying logical volume manager is a stripe, and
3565 * the file offset is zero then try to allocate data
3566 * blocks on stripe unit boundary.
3567 * NOTE: ap->aeof is only set if the allocation length
3568 * is >= the stripe unit and the allocation offset is
3569 * at the end of file.
3570 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003571 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003572 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003573 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003574 atype = args.type;
3575 isaligned = 1;
3576 /*
3577 * Adjust for alignment
3578 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003579 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003580 args.minlen = blen - args.alignment;
3581 args.minalignslop = 0;
3582 } else {
3583 /*
3584 * First try an exact bno allocation.
3585 * If it fails then do a near or start bno
3586 * allocation with alignment turned on.
3587 */
3588 atype = args.type;
3589 tryagain = 1;
3590 args.type = XFS_ALLOCTYPE_THIS_BNO;
3591 args.alignment = 1;
3592 /*
3593 * Compute the minlen+alignment for the
3594 * next case. Set slop so that the value
3595 * of minlen+alignment+slop doesn't go up
3596 * between the calls.
3597 */
Dave Chinner33177f052013-12-12 16:34:36 +11003598 if (blen > stripe_align && blen <= args.maxlen)
3599 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 else
3601 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003602 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003603 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003604 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003605 args.minlen - 1;
3606 else
3607 args.minalignslop = 0;
3608 }
3609 } else {
3610 args.alignment = 1;
3611 args.minalignslop = 0;
3612 }
3613 args.minleft = ap->minleft;
3614 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003615 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003616 args.datatype = ap->datatype;
3617 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003618 args.ip = ap->ip;
3619
3620 error = xfs_alloc_vextent(&args);
3621 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003622 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003623
Nathan Scotta365bdd2006-03-14 13:34:16 +11003624 if (tryagain && args.fsbno == NULLFSBLOCK) {
3625 /*
3626 * Exact allocation failed. Now try with alignment
3627 * turned on.
3628 */
3629 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003630 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003631 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 args.minlen = nextminlen;
3633 args.minalignslop = 0;
3634 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 if ((error = xfs_alloc_vextent(&args)))
3636 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003637 }
3638 if (isaligned && args.fsbno == NULLFSBLOCK) {
3639 /*
3640 * allocation failed, so turn off alignment and
3641 * try again.
3642 */
3643 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003644 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003645 args.alignment = 0;
3646 if ((error = xfs_alloc_vextent(&args)))
3647 return error;
3648 }
3649 if (args.fsbno == NULLFSBLOCK && nullfb &&
3650 args.minlen > ap->minlen) {
3651 args.minlen = ap->minlen;
3652 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003653 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 if ((error = xfs_alloc_vextent(&args)))
3655 return error;
3656 }
3657 if (args.fsbno == NULLFSBLOCK && nullfb) {
3658 args.fsbno = 0;
3659 args.type = XFS_ALLOCTYPE_FIRST_AG;
3660 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661 if ((error = xfs_alloc_vextent(&args)))
3662 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003663 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003664 }
3665 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003666 /*
3667 * check the allocation happened at the same or higher AG than
3668 * the first block that was allocated.
3669 */
3670 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003671 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3672 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003673
Dave Chinner3a756672011-09-18 20:40:58 +00003674 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003675 if (*ap->firstblock == NULLFSBLOCK)
3676 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003677 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003678 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003679 if (!(ap->flags & XFS_BMAPI_COWFORK))
3680 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003681 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3682 if (ap->wasdel)
3683 ap->ip->i_delayed_blks -= args.len;
3684 /*
3685 * Adjust the disk quota also. This was reserved
3686 * earlier.
3687 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003688 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003689 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3690 XFS_TRANS_DQ_BCOUNT,
3691 (long) args.len);
3692 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003693 ap->blkno = NULLFSBLOCK;
3694 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
3696 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003697}
3698
3699/*
3700 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3701 * It figures out where to ask the underlying allocator to put the new extent.
3702 */
3703STATIC int
3704xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003705 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003706{
Dave Chinner292378e2016-09-26 08:21:28 +10003707 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3708 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003709 return xfs_bmap_rtalloc(ap);
3710 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711}
3712
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003713/* Trim extent to fit a logical block range. */
3714void
3715xfs_trim_extent(
3716 struct xfs_bmbt_irec *irec,
3717 xfs_fileoff_t bno,
3718 xfs_filblks_t len)
3719{
3720 xfs_fileoff_t distance;
3721 xfs_fileoff_t end = bno + len;
3722
3723 if (irec->br_startoff + irec->br_blockcount <= bno ||
3724 irec->br_startoff >= end) {
3725 irec->br_blockcount = 0;
3726 return;
3727 }
3728
3729 if (irec->br_startoff < bno) {
3730 distance = bno - irec->br_startoff;
3731 if (isnullstartblock(irec->br_startblock))
3732 irec->br_startblock = DELAYSTARTBLOCK;
3733 if (irec->br_startblock != DELAYSTARTBLOCK &&
3734 irec->br_startblock != HOLESTARTBLOCK)
3735 irec->br_startblock += distance;
3736 irec->br_startoff += distance;
3737 irec->br_blockcount -= distance;
3738 }
3739
3740 if (end < irec->br_startoff + irec->br_blockcount) {
3741 distance = irec->br_startoff + irec->br_blockcount - end;
3742 irec->br_blockcount -= distance;
3743 }
3744}
3745
Brian Foster40214d12017-10-13 09:47:46 -07003746/* trim extent to within eof */
3747void
3748xfs_trim_extent_eof(
3749 struct xfs_bmbt_irec *irec,
3750 struct xfs_inode *ip)
3751
3752{
3753 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3754 i_size_read(VFS_I(ip))));
3755}
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003758 * Trim the returned map to the required bounds
3759 */
3760STATIC void
3761xfs_bmapi_trim_map(
3762 struct xfs_bmbt_irec *mval,
3763 struct xfs_bmbt_irec *got,
3764 xfs_fileoff_t *bno,
3765 xfs_filblks_t len,
3766 xfs_fileoff_t obno,
3767 xfs_fileoff_t end,
3768 int n,
3769 int flags)
3770{
3771 if ((flags & XFS_BMAPI_ENTIRE) ||
3772 got->br_startoff + got->br_blockcount <= obno) {
3773 *mval = *got;
3774 if (isnullstartblock(got->br_startblock))
3775 mval->br_startblock = DELAYSTARTBLOCK;
3776 return;
3777 }
3778
3779 if (obno > *bno)
3780 *bno = obno;
3781 ASSERT((*bno >= obno) || (n == 0));
3782 ASSERT(*bno < end);
3783 mval->br_startoff = *bno;
3784 if (isnullstartblock(got->br_startblock))
3785 mval->br_startblock = DELAYSTARTBLOCK;
3786 else
3787 mval->br_startblock = got->br_startblock +
3788 (*bno - got->br_startoff);
3789 /*
3790 * Return the minimum of what we got and what we asked for for
3791 * the length. We can use the len variable here because it is
3792 * modified below and we could have been there before coming
3793 * here if the first part of the allocation didn't overlap what
3794 * was asked for.
3795 */
3796 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3797 got->br_blockcount - (*bno - got->br_startoff));
3798 mval->br_state = got->br_state;
3799 ASSERT(mval->br_blockcount <= len);
3800 return;
3801}
3802
3803/*
3804 * Update and validate the extent map to return
3805 */
3806STATIC void
3807xfs_bmapi_update_map(
3808 struct xfs_bmbt_irec **map,
3809 xfs_fileoff_t *bno,
3810 xfs_filblks_t *len,
3811 xfs_fileoff_t obno,
3812 xfs_fileoff_t end,
3813 int *n,
3814 int flags)
3815{
3816 xfs_bmbt_irec_t *mval = *map;
3817
3818 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3819 ((mval->br_startoff + mval->br_blockcount) <= end));
3820 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3821 (mval->br_startoff < obno));
3822
3823 *bno = mval->br_startoff + mval->br_blockcount;
3824 *len = end - *bno;
3825 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3826 /* update previous map with new information */
3827 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3828 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3829 ASSERT(mval->br_state == mval[-1].br_state);
3830 mval[-1].br_blockcount = mval->br_blockcount;
3831 mval[-1].br_state = mval->br_state;
3832 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3833 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3834 mval[-1].br_startblock != HOLESTARTBLOCK &&
3835 mval->br_startblock == mval[-1].br_startblock +
3836 mval[-1].br_blockcount &&
3837 ((flags & XFS_BMAPI_IGSTATE) ||
3838 mval[-1].br_state == mval->br_state)) {
3839 ASSERT(mval->br_startoff ==
3840 mval[-1].br_startoff + mval[-1].br_blockcount);
3841 mval[-1].br_blockcount += mval->br_blockcount;
3842 } else if (*n > 0 &&
3843 mval->br_startblock == DELAYSTARTBLOCK &&
3844 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3845 mval->br_startoff ==
3846 mval[-1].br_startoff + mval[-1].br_blockcount) {
3847 mval[-1].br_blockcount += mval->br_blockcount;
3848 mval[-1].br_state = mval->br_state;
3849 } else if (!((*n == 0) &&
3850 ((mval->br_startoff + mval->br_blockcount) <=
3851 obno))) {
3852 mval++;
3853 (*n)++;
3854 }
3855 *map = mval;
3856}
3857
3858/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003859 * Map file blocks to filesystem blocks without allocation.
3860 */
3861int
3862xfs_bmapi_read(
3863 struct xfs_inode *ip,
3864 xfs_fileoff_t bno,
3865 xfs_filblks_t len,
3866 struct xfs_bmbt_irec *mval,
3867 int *nmap,
3868 int flags)
3869{
3870 struct xfs_mount *mp = ip->i_mount;
3871 struct xfs_ifork *ifp;
3872 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003873 xfs_fileoff_t obno;
3874 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003875 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003876 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003877 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003878 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003879 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003880
3881 ASSERT(*nmap >= 1);
3882 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003883 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003884 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003885
3886 if (unlikely(XFS_TEST_ERROR(
3887 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3888 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003889 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003890 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003891 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003892 }
3893
3894 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003895 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003896
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003897 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003898
3899 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003900
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003901 /* No CoW fork? Return a hole. */
3902 if (whichfork == XFS_COW_FORK && !ifp) {
3903 mval->br_startoff = bno;
3904 mval->br_startblock = HOLESTARTBLOCK;
3905 mval->br_blockcount = len;
3906 mval->br_state = XFS_EXT_NORM;
3907 *nmap = 1;
3908 return 0;
3909 }
3910
Dave Chinner5c8ed202011-09-18 20:40:45 +00003911 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3912 error = xfs_iread_extents(NULL, ip, whichfork);
3913 if (error)
3914 return error;
3915 }
3916
Christoph Hellwig334f3422016-11-24 11:39:43 +11003917 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3918 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003919 end = bno + len;
3920 obno = bno;
3921
3922 while (bno < end && n < *nmap) {
3923 /* Reading past eof, act as though there's a hole up to end. */
3924 if (eof)
3925 got.br_startoff = end;
3926 if (got.br_startoff > bno) {
3927 /* Reading in a hole. */
3928 mval->br_startoff = bno;
3929 mval->br_startblock = HOLESTARTBLOCK;
3930 mval->br_blockcount =
3931 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3932 mval->br_state = XFS_EXT_NORM;
3933 bno += mval->br_blockcount;
3934 len -= mval->br_blockcount;
3935 mval++;
3936 n++;
3937 continue;
3938 }
3939
3940 /* set up the extent map to return. */
3941 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3942 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3943
3944 /* If we're done, stop now. */
3945 if (bno >= end || n >= *nmap)
3946 break;
3947
3948 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003949 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3950 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003951 }
3952 *nmap = n;
3953 return 0;
3954}
3955
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003956/*
3957 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3958 * global pool and the extent inserted into the inode in-core extent tree.
3959 *
3960 * On entry, got refers to the first extent beyond the offset of the extent to
3961 * allocate or eof is specified if no such extent exists. On return, got refers
3962 * to the extent record that was inserted to the inode fork.
3963 *
3964 * Note that the allocated extent may have been merged with contiguous extents
3965 * during insertion into the inode fork. Thus, got does not reflect the current
3966 * state of the inode fork on return. If necessary, the caller can use lastx to
3967 * look up the updated record in the inode fork.
3968 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003969int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003970xfs_bmapi_reserve_delalloc(
3971 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003972 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003973 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003974 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003975 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003976 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003977 xfs_extnum_t *lastx,
3978 int eof)
3979{
3980 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003981 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003982 xfs_extlen_t alen;
3983 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003984 char rt = XFS_IS_REALTIME_INODE(ip);
3985 xfs_extlen_t extsz;
3986 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003987 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003988
Brian Foster974ae922016-11-28 14:57:42 +11003989 /*
3990 * Cap the alloc length. Keep track of prealloc so we know whether to
3991 * tag the inode before we return.
3992 */
3993 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003994 if (!eof)
3995 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003996 if (prealloc && alen >= len)
3997 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003998
3999 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004000 if (whichfork == XFS_COW_FORK)
4001 extsz = xfs_get_cowextsz_hint(ip);
4002 else
4003 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004004 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004005 struct xfs_bmbt_irec prev;
4006
4007 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4008 prev.br_startoff = NULLFILEOFF;
4009
4010 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004011 1, 0, &aoff, &alen);
4012 ASSERT(!error);
4013 }
4014
4015 if (rt)
4016 extsz = alen / mp->m_sb.sb_rextsize;
4017
4018 /*
4019 * Make a transaction-less quota reservation for delayed allocation
4020 * blocks. This number gets adjusted later. We return if we haven't
4021 * allocated blocks already inside this loop.
4022 */
4023 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4024 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4025 if (error)
4026 return error;
4027
4028 /*
4029 * Split changing sb for alen and indlen since they could be coming
4030 * from different places.
4031 */
4032 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4033 ASSERT(indlen > 0);
4034
4035 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004036 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004037 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004038 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004039 }
4040
4041 if (error)
4042 goto out_unreserve_quota;
4043
Dave Chinner0d485ad2015-02-23 21:22:03 +11004044 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004045 if (error)
4046 goto out_unreserve_blocks;
4047
4048
4049 ip->i_delayed_blks += alen;
4050
4051 got->br_startoff = aoff;
4052 got->br_startblock = nullstartblock(indlen);
4053 got->br_blockcount = alen;
4054 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004055
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004056 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004057
Brian Foster974ae922016-11-28 14:57:42 +11004058 /*
4059 * Tag the inode if blocks were preallocated. Note that COW fork
4060 * preallocation can occur at the start or end of the extent, even when
4061 * prealloc == 0, so we must also check the aligned offset and length.
4062 */
4063 if (whichfork == XFS_DATA_FORK && prealloc)
4064 xfs_inode_set_eofblocks_tag(ip);
4065 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4066 xfs_inode_set_cowblocks_tag(ip);
4067
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004068 return 0;
4069
4070out_unreserve_blocks:
4071 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004072 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004073 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004074 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004075out_unreserve_quota:
4076 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004077 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004078 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4079 return error;
4080}
4081
Dave Chinnercf11da92014-07-15 07:08:24 +10004082static int
4083xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004084 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004085{
4086 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004087 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004088 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004089 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004090 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004091
Dave Chinnera99ebf42011-12-01 11:24:20 +00004092 ASSERT(bma->length > 0);
4093
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004094 /*
4095 * For the wasdelay case, we could also just allocate the stuff asked
4096 * for in this bmap call but that wouldn't be as good.
4097 */
4098 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004099 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4100 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004101 if (bma->idx)
4102 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004103 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004104 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004105 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004106 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004107 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004108 }
4109
4110 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004111 * Set the data type being allocated. For the data fork, the first data
4112 * in the file is treated differently to all other allocations. For the
4113 * attribute fork, we only need to ensure the allocated range is not on
4114 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004115 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004116 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004117 bma->datatype = XFS_ALLOC_NOBUSY;
4118 if (whichfork == XFS_DATA_FORK) {
4119 if (bma->offset == 0)
4120 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4121 else
4122 bma->datatype |= XFS_ALLOC_USERDATA;
4123 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004124 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004125 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004126 }
4127
Dave Chinnere04426b2012-10-05 11:06:59 +10004128 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004129
4130 /*
4131 * Only want to do the alignment at the eof if it is userdata and
4132 * allocation length is larger than a stripe unit.
4133 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004134 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004135 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004136 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004137 if (error)
4138 return error;
4139 }
4140
4141 error = xfs_bmap_alloc(bma);
4142 if (error)
4143 return error;
4144
Dave Chinner29c8d172011-09-18 20:41:00 +00004145 if (bma->cur)
4146 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004147 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004148 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004149 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4150 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4151 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004152 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004153 }
4154 /*
4155 * Bump the number of extents we've allocated
4156 * in this call.
4157 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004158 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004159
Dave Chinner29c8d172011-09-18 20:41:00 +00004160 if (bma->cur)
4161 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004162 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4163
Dave Chinner963c30c2011-09-18 20:40:59 +00004164 bma->got.br_startoff = bma->offset;
4165 bma->got.br_startblock = bma->blkno;
4166 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004167 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004168
4169 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004170 * In the data fork, a wasdelay extent has been initialized, so
4171 * shouldn't be flagged as unwritten.
4172 *
4173 * For the cow fork, however, we convert delalloc reservations
4174 * (extents allocated for speculative preallocation) to
4175 * allocated unwritten extents, and only convert the unwritten
4176 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004177 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004178 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4179 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004180 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004181 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004182
Christoph Hellwigc6534242011-09-18 20:41:05 +00004183 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004184 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004185 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004186 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4187 whichfork, &bma->idx, &bma->cur, &bma->got,
4188 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004189
Christoph Hellwigc315c902011-09-18 20:41:02 +00004190 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004191 if (error)
4192 return error;
4193
4194 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004195 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4196 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4197 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004198 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004199 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004200
Dave Chinner963c30c2011-09-18 20:40:59 +00004201 ASSERT(bma->got.br_startoff <= bma->offset);
4202 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4203 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004204 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4205 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004206 return 0;
4207}
4208
Dave Chinnerb447fe52011-09-18 20:40:51 +00004209STATIC int
4210xfs_bmapi_convert_unwritten(
4211 struct xfs_bmalloca *bma,
4212 struct xfs_bmbt_irec *mval,
4213 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004214 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004215{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004216 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004217 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004218 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004219 int error;
4220
Dave Chinnerb447fe52011-09-18 20:40:51 +00004221 /* check if we need to do unwritten->real conversion */
4222 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4223 (flags & XFS_BMAPI_PREALLOC))
4224 return 0;
4225
4226 /* check if we need to do real->unwritten conversion */
4227 if (mval->br_state == XFS_EXT_NORM &&
4228 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4229 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4230 return 0;
4231
4232 /*
4233 * Modify (by adding) the state flag, if writing.
4234 */
4235 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004236 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4237 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004238 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004239 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004240 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004241 }
4242 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4243 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4244
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004245 /*
4246 * Before insertion into the bmbt, zero the range being converted
4247 * if required.
4248 */
4249 if (flags & XFS_BMAPI_ZERO) {
4250 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4251 mval->br_blockcount);
4252 if (error)
4253 return error;
4254 }
4255
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004256 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4257 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004258 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004259 /*
4260 * Log the inode core unconditionally in the unwritten extent conversion
4261 * path because the conversion might not have done so (e.g., if the
4262 * extent count hasn't changed). We need to make sure the inode is dirty
4263 * in the transaction for the sake of fsync(), even if nothing has
4264 * changed, because fsync() will not force the log for this transaction
4265 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004266 *
4267 * Note: If we're only converting cow fork extents, there aren't
4268 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004269 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004270 if (whichfork != XFS_COW_FORK)
4271 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004272 if (error)
4273 return error;
4274
4275 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004276 * Update our extent pointer, given that
4277 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4278 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004279 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004280 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004281
4282 /*
4283 * We may have combined previously unwritten space with written space,
4284 * so generate another request.
4285 */
4286 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004287 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004288 return 0;
4289}
4290
Christoph Hellwig44032802011-09-18 20:40:48 +00004291/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004292 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4293 * extent state if necessary. Details behaviour is controlled by the flags
4294 * parameter. Only allocates blocks from a single allocation group, to avoid
4295 * locking problems.
4296 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 * The returned value in "firstblock" from the first call in a transaction
4298 * must be remembered and presented to subsequent calls in "firstblock".
4299 * An upper bound for the number of blocks to be allocated is supplied to
4300 * the first call in "total"; if no allocation group has that many free
4301 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4302 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004303int
4304xfs_bmapi_write(
4305 struct xfs_trans *tp, /* transaction pointer */
4306 struct xfs_inode *ip, /* incore inode */
4307 xfs_fileoff_t bno, /* starting file offs. mapped */
4308 xfs_filblks_t len, /* length to map in file */
4309 int flags, /* XFS_BMAPI_... */
4310 xfs_fsblock_t *firstblock, /* first allocated block
4311 controls a.g. for allocs */
4312 xfs_extlen_t total, /* total blocks needed */
4313 struct xfs_bmbt_irec *mval, /* output: map values */
4314 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004315 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004317 struct xfs_mount *mp = ip->i_mount;
4318 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004319 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004320 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004321 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004322 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004323 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004324 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004325 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004328 xfs_fileoff_t orig_bno; /* original block number value */
4329 int orig_flags; /* original flags arg value */
4330 xfs_filblks_t orig_len; /* original value of len arg */
4331 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4332 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
4334 orig_bno = bno;
4335 orig_len = len;
4336 orig_flags = flags;
4337 orig_mval = mval;
4338 orig_nmap = *nmap;
4339#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004340 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004343 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4344 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004345 ASSERT(tp != NULL ||
4346 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4347 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004348 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004349 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004350 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004351 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004352
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004353 /* zeroing is for currently only for data extents, not metadata */
4354 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4355 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4356 /*
4357 * we can allocate unwritten extents or pre-zero allocated blocks,
4358 * but it makes no sense to do both at once. This would result in
4359 * zeroing the unwritten extent twice, but it still being an
4360 * unwritten extent....
4361 */
4362 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4363 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 if (unlikely(XFS_TEST_ERROR(
4366 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004367 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004368 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004369 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004370 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004374 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004377
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004378 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004379
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004380 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004382 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004384 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004385 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004386 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004387 }
4388
4389 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4390 error = xfs_iread_extents(tp, ip, whichfork);
4391 if (error)
4392 goto error0;
4393 }
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 n = 0;
4396 end = bno + len;
4397 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004398
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004399 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4400 eof = true;
4401 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4402 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004403 bma.tp = tp;
4404 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004405 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004406 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004407 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004408 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004409
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004411 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004412
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004413 /* in hole or beyoned EOF? */
4414 if (eof || bma.got.br_startoff > bno) {
4415 if (flags & XFS_BMAPI_DELALLOC) {
4416 /*
4417 * For the COW fork we can reasonably get a
4418 * request for converting an extent that races
4419 * with other threads already having converted
4420 * part of it, as there converting COW to
4421 * regular blocks is not protected using the
4422 * IOLOCK.
4423 */
4424 ASSERT(flags & XFS_BMAPI_COWFORK);
4425 if (!(flags & XFS_BMAPI_COWFORK)) {
4426 error = -EIO;
4427 goto error0;
4428 }
4429
4430 if (eof || bno >= end)
4431 break;
4432 } else {
4433 need_alloc = true;
4434 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004435 } else if (isnullstartblock(bma.got.br_startblock)) {
4436 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004437 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004438
4439 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 * First, deal with the hole before the allocated space
4441 * that we found, if any.
4442 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004443 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004444 bma.eof = eof;
4445 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4446 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004447 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004448 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004449
Dave Chinnera99ebf42011-12-01 11:24:20 +00004450 /*
4451 * There's a 32/64 bit type mismatch between the
4452 * allocation length request (which can be 64 bits in
4453 * length) and the bma length request, which is
4454 * xfs_extlen_t and therefore 32 bits. Hence we have to
4455 * check for 32-bit overflows and handle them here.
4456 */
4457 if (len > (xfs_filblks_t)MAXEXTLEN)
4458 bma.length = MAXEXTLEN;
4459 else
4460 bma.length = len;
4461
4462 ASSERT(len > 0);
4463 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004464 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 if (error)
4466 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004467 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004468 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004469
4470 /*
4471 * If this is a CoW allocation, record the data in
4472 * the refcount btree for orphan recovery.
4473 */
4474 if (whichfork == XFS_COW_FORK) {
4475 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4476 bma.blkno, bma.length);
4477 if (error)
4478 goto error0;
4479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004481
Dave Chinneraef9a892011-09-18 20:40:44 +00004482 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004483 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4484 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Dave Chinnerb447fe52011-09-18 20:40:51 +00004486 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004487 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004488 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004489 continue;
4490 if (error)
4491 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Dave Chinneraef9a892011-09-18 20:40:44 +00004493 /* update the extent map to return */
4494 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 /*
4497 * If we're done, stop now. Stop when we've allocated
4498 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4499 * the transaction may get too big.
4500 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004501 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004503
4504 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004505 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004506 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4507 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 /*
4512 * Transform from btree to extents, give it cur.
4513 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004514 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004515 int tmp_logflags = 0;
4516
Dave Chinner29c8d172011-09-18 20:41:00 +00004517 ASSERT(bma.cur);
4518 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004520 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 if (error)
4522 goto error0;
4523 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004526 XFS_IFORK_NEXTENTS(ip, whichfork) >
4527 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529error0:
4530 /*
4531 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004532 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004534 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004536 bma.logflags &= ~xfs_ilog_fext(whichfork);
4537 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004539 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 /*
4541 * Log whatever the flags say, even if error. Otherwise we might miss
4542 * detecting a case where the data is changed, there's an error,
4543 * and it's not logged so we don't shutdown when we should.
4544 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004545 if (bma.logflags)
4546 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004547
Dave Chinner29c8d172011-09-18 20:41:00 +00004548 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (!error) {
4550 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004551 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004553 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004554 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004556 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4558 }
4559 if (!error)
4560 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4561 orig_nmap, *nmap);
4562 return error;
4563}
4564
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004565static int
4566xfs_bmapi_remap(
4567 struct xfs_trans *tp,
4568 struct xfs_inode *ip,
4569 xfs_fileoff_t bno,
4570 xfs_filblks_t len,
4571 xfs_fsblock_t startblock,
4572 struct xfs_defer_ops *dfops)
4573{
4574 struct xfs_mount *mp = ip->i_mount;
4575 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4576 struct xfs_btree_cur *cur = NULL;
4577 xfs_fsblock_t firstblock = NULLFSBLOCK;
4578 struct xfs_bmbt_irec got;
4579 xfs_extnum_t idx;
4580 int logflags = 0, error;
4581
4582 ASSERT(len > 0);
4583 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4584 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4585
4586 if (unlikely(XFS_TEST_ERROR(
4587 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4588 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004589 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004590 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4591 return -EFSCORRUPTED;
4592 }
4593
4594 if (XFS_FORCED_SHUTDOWN(mp))
4595 return -EIO;
4596
4597 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4598 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4599 if (error)
4600 return error;
4601 }
4602
4603 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4604 /* make sure we only reflink into a hole. */
4605 ASSERT(got.br_startoff > bno);
4606 ASSERT(got.br_startoff - bno >= len);
4607 }
4608
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004609 ip->i_d.di_nblocks += len;
4610 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004611
4612 if (ifp->if_flags & XFS_IFBROOT) {
4613 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4614 cur->bc_private.b.firstblock = firstblock;
4615 cur->bc_private.b.dfops = dfops;
4616 cur->bc_private.b.flags = 0;
4617 }
4618
4619 got.br_startoff = bno;
4620 got.br_startblock = startblock;
4621 got.br_blockcount = len;
4622 got.br_state = XFS_EXT_NORM;
4623
4624 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4625 &got, &firstblock, dfops, &logflags);
4626 if (error)
4627 goto error0;
4628
4629 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4630 int tmp_logflags = 0;
4631
4632 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4633 &tmp_logflags, XFS_DATA_FORK);
4634 logflags |= tmp_logflags;
4635 }
4636
4637error0:
4638 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4639 logflags &= ~XFS_ILOG_DEXT;
4640 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4641 logflags &= ~XFS_ILOG_DBROOT;
4642
4643 if (logflags)
4644 xfs_trans_log_inode(tp, ip, logflags);
4645 if (cur) {
4646 xfs_btree_del_cursor(cur,
4647 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4648 }
4649 return error;
4650}
4651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004653 * When a delalloc extent is split (e.g., due to a hole punch), the original
4654 * indlen reservation must be shared across the two new extents that are left
4655 * behind.
4656 *
4657 * Given the original reservation and the worst case indlen for the two new
4658 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004659 * reservation fairly across the two new extents. If necessary, steal available
4660 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4661 * ores == 1). The number of stolen blocks is returned. The availability and
4662 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004663 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004664static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004665xfs_bmap_split_indlen(
4666 xfs_filblks_t ores, /* original res. */
4667 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004668 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4669 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004670{
4671 xfs_filblks_t len1 = *indlen1;
4672 xfs_filblks_t len2 = *indlen2;
4673 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004674 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004675 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004676
4677 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004678 * Steal as many blocks as we can to try and satisfy the worst case
4679 * indlen for both new extents.
4680 */
Brian Foster75d65362017-02-13 22:48:30 -08004681 if (ores < nres && avail)
4682 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4683 ores += stolen;
4684
4685 /* nothing else to do if we've satisfied the new reservation */
4686 if (ores >= nres)
4687 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004688
4689 /*
Brian Foster75d65362017-02-13 22:48:30 -08004690 * We can't meet the total required reservation for the two extents.
4691 * Calculate the percent of the overall shortage between both extents
4692 * and apply this percentage to each of the requested indlen values.
4693 * This distributes the shortage fairly and reduces the chances that one
4694 * of the two extents is left with nothing when extents are repeatedly
4695 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004696 */
Brian Foster75d65362017-02-13 22:48:30 -08004697 resfactor = (ores * 100);
4698 do_div(resfactor, nres);
4699 len1 *= resfactor;
4700 do_div(len1, 100);
4701 len2 *= resfactor;
4702 do_div(len2, 100);
4703 ASSERT(len1 + len2 <= ores);
4704 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4705
4706 /*
4707 * Hand out the remainder to each extent. If one of the two reservations
4708 * is zero, we want to make sure that one gets a block first. The loop
4709 * below starts with len1, so hand len2 a block right off the bat if it
4710 * is zero.
4711 */
4712 ores -= (len1 + len2);
4713 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4714 if (ores && !len2 && *indlen2) {
4715 len2++;
4716 ores--;
4717 }
4718 while (ores) {
4719 if (len1 < *indlen1) {
4720 len1++;
4721 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004722 }
Brian Foster75d65362017-02-13 22:48:30 -08004723 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004724 break;
Brian Foster75d65362017-02-13 22:48:30 -08004725 if (len2 < *indlen2) {
4726 len2++;
4727 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004728 }
4729 }
4730
4731 *indlen1 = len1;
4732 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004733
4734 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004735}
4736
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004737int
4738xfs_bmap_del_extent_delay(
4739 struct xfs_inode *ip,
4740 int whichfork,
4741 xfs_extnum_t *idx,
4742 struct xfs_bmbt_irec *got,
4743 struct xfs_bmbt_irec *del)
4744{
4745 struct xfs_mount *mp = ip->i_mount;
4746 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4747 struct xfs_bmbt_irec new;
4748 int64_t da_old, da_new, da_diff = 0;
4749 xfs_fileoff_t del_endoff, got_endoff;
4750 xfs_filblks_t got_indlen, new_indlen, stolen;
4751 int error = 0, state = 0;
4752 bool isrt;
4753
4754 XFS_STATS_INC(mp, xs_del_exlist);
4755
4756 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4757 del_endoff = del->br_startoff + del->br_blockcount;
4758 got_endoff = got->br_startoff + got->br_blockcount;
4759 da_old = startblockval(got->br_startblock);
4760 da_new = 0;
4761
4762 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004763 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004764 ASSERT(del->br_blockcount > 0);
4765 ASSERT(got->br_startoff <= del->br_startoff);
4766 ASSERT(got_endoff >= del_endoff);
4767
4768 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004769 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004770
4771 do_div(rtexts, mp->m_sb.sb_rextsize);
4772 xfs_mod_frextents(mp, rtexts);
4773 }
4774
4775 /*
4776 * Update the inode delalloc counter now and wait to update the
4777 * sb counters as we might have to borrow some blocks for the
4778 * indirect block accounting.
4779 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004780 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4781 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004782 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004783 if (error)
4784 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004785 ip->i_delayed_blks -= del->br_blockcount;
4786
4787 if (whichfork == XFS_COW_FORK)
4788 state |= BMAP_COWFORK;
4789
4790 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004791 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004792 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004793 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004794
Christoph Hellwig0173c682017-10-17 14:16:22 -07004795 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4796 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004797 /*
4798 * Matches the whole extent. Delete the entry.
4799 */
4800 xfs_iext_remove(ip, *idx, 1, state);
4801 --*idx;
4802 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004803 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804 /*
4805 * Deleting the first part of the extent.
4806 */
4807 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4808 got->br_startoff = del_endoff;
4809 got->br_blockcount -= del->br_blockcount;
4810 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4811 got->br_blockcount), da_old);
4812 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004813 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004814 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4815 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004816 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004817 /*
4818 * Deleting the last part of the extent.
4819 */
4820 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4821 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4822 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4823 got->br_blockcount), da_old);
4824 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004825 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004826 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4827 break;
4828 case 0:
4829 /*
4830 * Deleting the middle of the extent.
4831 *
4832 * Distribute the original indlen reservation across the two new
4833 * extents. Steal blocks from the deleted extent if necessary.
4834 * Stealing blocks simply fudges the fdblocks accounting below.
4835 * Warn if either of the new indlen reservations is zero as this
4836 * can lead to delalloc problems.
4837 */
4838 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4839
4840 got->br_blockcount = del->br_startoff - got->br_startoff;
4841 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4842
4843 new.br_blockcount = got_endoff - del_endoff;
4844 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4845
4846 WARN_ON_ONCE(!got_indlen || !new_indlen);
4847 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4848 del->br_blockcount);
4849
4850 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004851 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004852 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4853
4854 new.br_startoff = del_endoff;
4855 new.br_state = got->br_state;
4856 new.br_startblock = nullstartblock((int)new_indlen);
4857
4858 ++*idx;
4859 xfs_iext_insert(ip, *idx, 1, &new, state);
4860
4861 da_new = got_indlen + new_indlen - stolen;
4862 del->br_blockcount -= stolen;
4863 break;
4864 }
4865
4866 ASSERT(da_old >= da_new);
4867 da_diff = da_old - da_new;
4868 if (!isrt)
4869 da_diff += del->br_blockcount;
4870 if (da_diff)
4871 xfs_mod_fdblocks(mp, da_diff, false);
4872 return error;
4873}
4874
4875void
4876xfs_bmap_del_extent_cow(
4877 struct xfs_inode *ip,
4878 xfs_extnum_t *idx,
4879 struct xfs_bmbt_irec *got,
4880 struct xfs_bmbt_irec *del)
4881{
4882 struct xfs_mount *mp = ip->i_mount;
4883 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4884 struct xfs_bmbt_irec new;
4885 xfs_fileoff_t del_endoff, got_endoff;
4886 int state = BMAP_COWFORK;
4887
4888 XFS_STATS_INC(mp, xs_del_exlist);
4889
4890 del_endoff = del->br_startoff + del->br_blockcount;
4891 got_endoff = got->br_startoff + got->br_blockcount;
4892
4893 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004894 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004895 ASSERT(del->br_blockcount > 0);
4896 ASSERT(got->br_startoff <= del->br_startoff);
4897 ASSERT(got_endoff >= del_endoff);
4898 ASSERT(!isnullstartblock(got->br_startblock));
4899
4900 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004901 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004902 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004903 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004904
Christoph Hellwig0173c682017-10-17 14:16:22 -07004905 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4906 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004907 /*
4908 * Matches the whole extent. Delete the entry.
4909 */
4910 xfs_iext_remove(ip, *idx, 1, state);
4911 --*idx;
4912 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004913 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004914 /*
4915 * Deleting the first part of the extent.
4916 */
4917 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4918 got->br_startoff = del_endoff;
4919 got->br_blockcount -= del->br_blockcount;
4920 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004921 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004922 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4923 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004924 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004925 /*
4926 * Deleting the last part of the extent.
4927 */
4928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4929 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004930 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004931 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4932 break;
4933 case 0:
4934 /*
4935 * Deleting the middle of the extent.
4936 */
4937 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4938 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004939 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004940 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4941
4942 new.br_startoff = del_endoff;
4943 new.br_blockcount = got_endoff - del_endoff;
4944 new.br_state = got->br_state;
4945 new.br_startblock = del->br_startblock + del->br_blockcount;
4946
4947 ++*idx;
4948 xfs_iext_insert(ip, *idx, 1, &new, state);
4949 break;
4950 }
4951}
4952
Brian Fostera9bd24a2016-03-15 11:42:46 +11004953/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004954 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004955 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11004956 */
4957STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004958xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11004959 xfs_inode_t *ip, /* incore inode pointer */
4960 xfs_trans_t *tp, /* current transaction pointer */
4961 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004962 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004963 xfs_btree_cur_t *cur, /* if null, not a btree */
4964 xfs_bmbt_irec_t *del, /* data to remove from extents */
4965 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004966 int whichfork, /* data or attr fork */
4967 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004968{
Dave Chinner9e5987a72013-02-25 12:31:26 +11004969 xfs_fsblock_t del_endblock=0; /* first block past del */
4970 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004971 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004972 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004973 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004974 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004975 xfs_fileoff_t got_endoff; /* first offset past got */
4976 int i; /* temp state */
4977 xfs_ifork_t *ifp; /* inode fork pointer */
4978 xfs_mount_t *mp; /* mount structure */
4979 xfs_filblks_t nblks; /* quota/sb block count */
4980 xfs_bmbt_irec_t new; /* new record to be inserted */
4981 /* REFERENCED */
4982 uint qfield; /* quota field to update */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004983 int state = 0;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004984 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004985
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004986 mp = ip->i_mount;
4987 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004988
4989 if (whichfork == XFS_ATTR_FORK)
4990 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004991 else if (whichfork == XFS_COW_FORK)
4992 state |= BMAP_COWFORK;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004993
Dave Chinner9e5987a72013-02-25 12:31:26 +11004994 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004995 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a72013-02-25 12:31:26 +11004996 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004997 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004998 ASSERT(got.br_startoff <= del->br_startoff);
4999 del_endoff = del->br_startoff + del->br_blockcount;
5000 got_endoff = got.br_startoff + got.br_blockcount;
5001 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005002 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005003 qfield = 0;
5004 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005005
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005006 /*
5007 * If it's the case where the directory code is running with no block
5008 * reservation, and the deleted block is in the middle of its extent,
5009 * and the resulting insert of an extent would cause transformation to
5010 * btree format, then reject it. The calling code will then swap blocks
5011 * around instead. We have to do this now, rather than waiting for the
5012 * conversion to btree format, since the transaction will be dirty then.
5013 */
5014 if (tp->t_blk_res == 0 &&
5015 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5016 XFS_IFORK_NEXTENTS(ip, whichfork) >=
5017 XFS_IFORK_MAXEXT(ip, whichfork) &&
5018 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5019 return -ENOSPC;
5020
5021 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005022 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5023 xfs_fsblock_t bno;
5024 xfs_filblks_t len;
5025
5026 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
5027 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
5028 bno = del->br_startblock;
5029 len = del->br_blockcount;
5030 do_div(bno, mp->m_sb.sb_rextsize);
5031 do_div(len, mp->m_sb.sb_rextsize);
5032 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5033 if (error)
5034 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005035 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005036 nblks = len * mp->m_sb.sb_rextsize;
5037 qfield = XFS_TRANS_DQ_RTBCOUNT;
5038 } else {
5039 do_fx = 1;
5040 nblks = del->br_blockcount;
5041 qfield = XFS_TRANS_DQ_BCOUNT;
5042 }
5043
5044 del_endblock = del->br_startblock + del->br_blockcount;
5045 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005046 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005047 if (error)
5048 goto done;
5049 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005050 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005051
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005052 if (got.br_startoff == del->br_startoff)
5053 state |= BMAP_LEFT_FILLING;
5054 if (got_endoff == del_endoff)
5055 state |= BMAP_RIGHT_FILLING;
5056
5057 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5058 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005059 /*
5060 * Matches the whole extent. Delete the entry.
5061 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005062 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005063 xfs_iext_remove(ip, *idx, 1,
5064 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5065 --*idx;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005066
5067 XFS_IFORK_NEXT_SET(ip, whichfork,
5068 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5069 flags |= XFS_ILOG_CORE;
5070 if (!cur) {
5071 flags |= xfs_ilog_fext(whichfork);
5072 break;
5073 }
5074 if ((error = xfs_btree_delete(cur, &i)))
5075 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005076 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005077 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005078 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005079 /*
5080 * Deleting the first part of the extent.
5081 */
5082 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005083 got.br_startoff = del_endoff;
5084 got.br_startblock = del_endblock;
5085 got.br_blockcount -= del->br_blockcount;
5086 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005087 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5088 if (!cur) {
5089 flags |= xfs_ilog_fext(whichfork);
5090 break;
5091 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005092 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005093 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005094 goto done;
5095 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005096 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005097 /*
5098 * Deleting the last part of the extent.
5099 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005100 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005101 got.br_blockcount -= del->br_blockcount;
5102 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005103 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5104 if (!cur) {
5105 flags |= xfs_ilog_fext(whichfork);
5106 break;
5107 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005108 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005109 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005110 goto done;
5111 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005112 case 0:
5113 /*
5114 * Deleting the middle of the extent.
5115 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005116 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005117
5118 old = got;
5119 got.br_blockcount = del->br_startoff - got.br_startoff;
5120 xfs_iext_update_extent(ifp, *idx, &got);
5121
Dave Chinner9e5987a72013-02-25 12:31:26 +11005122 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005123 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005124 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005125 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005126
Christoph Hellwige1d75532017-10-17 14:16:21 -07005127 flags |= XFS_ILOG_CORE;
5128 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005129 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005130 if (error)
5131 goto done;
5132 error = xfs_btree_increment(cur, 0, &i);
5133 if (error)
5134 goto done;
5135 cur->bc_rec.b = new;
5136 error = xfs_btree_insert(cur, &i);
5137 if (error && error != -ENOSPC)
5138 goto done;
5139 /*
5140 * If get no-space back from btree insert, it tried a
5141 * split, and we have a zero block reservation. Fix up
5142 * our state and return the error.
5143 */
5144 if (error == -ENOSPC) {
5145 /*
5146 * Reset the cursor, don't trust it after any
5147 * insert operation.
5148 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005149 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005150 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005151 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005152 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5153 /*
5154 * Update the btree record back
5155 * to the original value.
5156 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005157 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005158 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005159 goto done;
5160 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005161 * Reset the extent record back
5162 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005163 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005164 xfs_iext_update_extent(ifp, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005165 flags = 0;
5166 error = -ENOSPC;
5167 goto done;
5168 }
5169 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5170 } else
5171 flags |= xfs_ilog_fext(whichfork);
5172 XFS_IFORK_NEXT_SET(ip, whichfork,
5173 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005174 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5175 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5176 ++*idx;
5177 break;
5178 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005179
5180 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005181 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5182 if (error)
5183 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005184
Dave Chinner9e5987a72013-02-25 12:31:26 +11005185 /*
5186 * If we need to, add to list of extents to delete.
5187 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005188 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005189 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5190 error = xfs_refcount_decrease_extent(mp, dfops, del);
5191 if (error)
5192 goto done;
5193 } else
5194 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5195 del->br_blockcount, NULL);
5196 }
5197
Dave Chinner9e5987a72013-02-25 12:31:26 +11005198 /*
5199 * Adjust inode # blocks in the file.
5200 */
5201 if (nblks)
5202 ip->i_d.di_nblocks -= nblks;
5203 /*
5204 * Adjust quota data.
5205 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005206 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005207 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5208
Dave Chinner9e5987a72013-02-25 12:31:26 +11005209done:
5210 *logflagsp = flags;
5211 return error;
5212}
5213
5214/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 * Unmap (remove) blocks from a file.
5216 * If nexts is nonzero then the number of extents to remove is limited to
5217 * that value. If not all extents in the block range can be removed then
5218 * *done is set.
5219 */
5220int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005221__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 xfs_trans_t *tp, /* transaction pointer */
5223 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005224 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005225 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 int flags, /* misc flags */
5227 xfs_extnum_t nexts, /* number of extents max */
5228 xfs_fsblock_t *firstblock, /* first allocated block
5229 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005230 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231{
5232 xfs_btree_cur_t *cur; /* bmap btree cursor */
5233 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 int error; /* error return value */
5235 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005236 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 xfs_ifork_t *ifp; /* inode fork pointer */
5238 int isrt; /* freeing in rt area */
5239 xfs_extnum_t lastx; /* last extent index used */
5240 int logflags; /* transaction logging flags */
5241 xfs_extlen_t mod; /* rt extent offset */
5242 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 int tmp_logflags; /* partial logging flags */
5244 int wasdel; /* was a delayed alloc extent */
5245 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005247 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005248 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005249 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005250 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005252 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005253
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005254 whichfork = xfs_bmapi_whichfork(flags);
5255 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 ifp = XFS_IFORK_PTR(ip, whichfork);
5257 if (unlikely(
5258 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5259 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5260 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5261 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005262 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
5264 mp = ip->i_mount;
5265 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005266 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005267
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005268 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 ASSERT(len > 0);
5270 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005271
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005272 /*
5273 * Guesstimate how many blocks we can unmap without running the risk of
5274 * blowing out the transaction with a mix of EFIs and reflink
5275 * adjustments.
5276 */
5277 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5278 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5279 else
5280 max_len = len;
5281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5283 (error = xfs_iread_extents(tp, ip, whichfork)))
5284 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005285 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005286 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 return 0;
5288 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005289 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005290 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005291 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005292
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 /*
5294 * Check to see if the given block number is past the end of the
5295 * file, back up to the last block if so...
5296 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005297 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005298 ASSERT(lastx > 0);
5299 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005300 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005302
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 logflags = 0;
5304 if (ifp->if_flags & XFS_IFBROOT) {
5305 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005306 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005308 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 cur->bc_private.b.flags = 0;
5310 } else
5311 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005312
5313 if (isrt) {
5314 /*
5315 * Synchronize by locking the bitmap inode.
5316 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005317 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005318 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005319 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5320 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005321 }
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005324 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005325 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005327 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 * Just back up to the previous extent, if so.
5329 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005330 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 if (--lastx < 0)
5332 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005333 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 }
5335 /*
5336 * Is the last block of this extent before the range
5337 * we're supposed to delete? If so, we're done.
5338 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005339 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005341 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 break;
5343 /*
5344 * Then deal with the (possibly delayed) allocated space
5345 * we found.
5346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005348 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005349
5350 /*
5351 * Make sure we don't touch multiple AGF headers out of order
5352 * in a single transaction, as that could cause AB-BA deadlocks.
5353 */
5354 if (!wasdel) {
5355 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5356 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5357 break;
5358 prev_agno = agno;
5359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 if (got.br_startoff < start) {
5361 del.br_startoff = start;
5362 del.br_blockcount -= start - got.br_startoff;
5363 if (!wasdel)
5364 del.br_startblock += start - got.br_startoff;
5365 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005366 if (del.br_startoff + del.br_blockcount > end + 1)
5367 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005368
5369 /* How much can we safely unmap? */
5370 if (max_len < del.br_blockcount) {
5371 del.br_startoff += del.br_blockcount - max_len;
5372 if (!wasdel)
5373 del.br_startblock += del.br_blockcount - max_len;
5374 del.br_blockcount = max_len;
5375 }
5376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 sum = del.br_startblock + del.br_blockcount;
5378 if (isrt &&
5379 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5380 /*
5381 * Realtime extent not lined up at the end.
5382 * The extent could have been split into written
5383 * and unwritten pieces, or we could just be
5384 * unmapping part of it. But we can't really
5385 * get rid of part of a realtime extent.
5386 */
5387 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005388 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 /*
5390 * This piece is unwritten, or we're not
5391 * using unwritten extents. Skip over it.
5392 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005393 ASSERT(end >= mod);
5394 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005396 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005398 xfs_iext_get_extent(ifp, lastx,
5399 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 }
5401 continue;
5402 }
5403 /*
5404 * It's written, turn it unwritten.
5405 * This is better than zeroing it.
5406 */
5407 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005408 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 /*
5410 * If this spans a realtime extent boundary,
5411 * chop it back to the start of the one we end at.
5412 */
5413 if (del.br_blockcount > mod) {
5414 del.br_startoff += del.br_blockcount - mod;
5415 del.br_startblock += del.br_blockcount - mod;
5416 del.br_blockcount = mod;
5417 }
5418 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005419 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005420 whichfork, &lastx, &cur, &del,
5421 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 if (error)
5423 goto error0;
5424 goto nodelete;
5425 }
5426 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5427 /*
5428 * Realtime extent is lined up at the end but not
5429 * at the front. We'll get rid of full extents if
5430 * we can.
5431 */
5432 mod = mp->m_sb.sb_rextsize - mod;
5433 if (del.br_blockcount > mod) {
5434 del.br_blockcount -= mod;
5435 del.br_startoff += mod;
5436 del.br_startblock += mod;
5437 } else if ((del.br_startoff == start &&
5438 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005439 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005440 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 /*
5442 * Can't make it unwritten. There isn't
5443 * a full extent here so just skip it.
5444 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005445 ASSERT(end >= del.br_blockcount);
5446 end -= del.br_blockcount;
5447 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005448 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 continue;
5450 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005451 struct xfs_bmbt_irec prev;
5452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 /*
5454 * This one is already unwritten.
5455 * It must have a written left neighbor.
5456 * Unwrite the killed part of that one and
5457 * try again.
5458 */
5459 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005460 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005462 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 ASSERT(del.br_startblock ==
5464 prev.br_startblock + prev.br_blockcount);
5465 if (prev.br_startoff < start) {
5466 mod = start - prev.br_startoff;
5467 prev.br_blockcount -= mod;
5468 prev.br_startblock += mod;
5469 prev.br_startoff = start;
5470 }
5471 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005472 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005473 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005474 ip, whichfork, &lastx, &cur,
5475 &prev, firstblock, dfops,
5476 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 if (error)
5478 goto error0;
5479 goto nodelete;
5480 } else {
5481 ASSERT(del.br_state == XFS_EXT_NORM);
5482 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005483 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005484 ip, whichfork, &lastx, &cur,
5485 &del, firstblock, dfops,
5486 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 if (error)
5488 goto error0;
5489 goto nodelete;
5490 }
5491 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005492
Brian Fosterb2706a02016-03-15 11:42:46 +11005493 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005494 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5495 &got, &del);
5496 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005497 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5498 cur, &del, &tmp_logflags, whichfork,
5499 flags);
5500 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005501 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 if (error)
5504 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005505
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005506 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005507 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 /*
5510 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005512 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005513 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005514 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005515 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005516 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 extno++;
5519 }
5520 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005521 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005522 *rlen = 0;
5523 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005524 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 /*
5527 * Convert to a btree if necessary.
5528 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005529 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005531 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 &cur, 0, &tmp_logflags, whichfork);
5533 logflags |= tmp_logflags;
5534 if (error)
5535 goto error0;
5536 }
5537 /*
5538 * transform from btree to extents, give it cur
5539 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005540 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 ASSERT(cur != NULL);
5542 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5543 whichfork);
5544 logflags |= tmp_logflags;
5545 if (error)
5546 goto error0;
5547 }
5548 /*
5549 * transform from extents to local?
5550 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 error = 0;
5552error0:
5553 /*
5554 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005555 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005557 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005559 logflags &= ~xfs_ilog_fext(whichfork);
5560 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005562 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 /*
5564 * Log inode even in the error case, if the transaction
5565 * is dirty we'll need to shut down the filesystem.
5566 */
5567 if (logflags)
5568 xfs_trans_log_inode(tp, ip, logflags);
5569 if (cur) {
5570 if (!error) {
5571 *firstblock = cur->bc_private.b.firstblock;
5572 cur->bc_private.b.allocated = 0;
5573 }
5574 xfs_btree_del_cursor(cur,
5575 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5576 }
5577 return error;
5578}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005579
Darrick J. Wong44535932016-10-03 09:11:29 -07005580/* Unmap a range of a file. */
5581int
5582xfs_bunmapi(
5583 xfs_trans_t *tp,
5584 struct xfs_inode *ip,
5585 xfs_fileoff_t bno,
5586 xfs_filblks_t len,
5587 int flags,
5588 xfs_extnum_t nexts,
5589 xfs_fsblock_t *firstblock,
5590 struct xfs_defer_ops *dfops,
5591 int *done)
5592{
5593 int error;
5594
5595 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5596 dfops);
5597 *done = (len == 0);
5598 return error;
5599}
5600
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005601/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005602 * Determine whether an extent shift can be accomplished by a merge with the
5603 * extent that precedes the target hole of the shift.
5604 */
5605STATIC bool
5606xfs_bmse_can_merge(
5607 struct xfs_bmbt_irec *left, /* preceding extent */
5608 struct xfs_bmbt_irec *got, /* current extent to shift */
5609 xfs_fileoff_t shift) /* shift fsb */
5610{
5611 xfs_fileoff_t startoff;
5612
5613 startoff = got->br_startoff - shift;
5614
5615 /*
5616 * The extent, once shifted, must be adjacent in-file and on-disk with
5617 * the preceding extent.
5618 */
5619 if ((left->br_startoff + left->br_blockcount != startoff) ||
5620 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5621 (left->br_state != got->br_state) ||
5622 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5623 return false;
5624
5625 return true;
5626}
5627
5628/*
5629 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5630 * hole in the file. If an extent shift would result in the extent being fully
5631 * adjacent to the extent that currently precedes the hole, we can merge with
5632 * the preceding extent rather than do the shift.
5633 *
5634 * This function assumes the caller has verified a shift-by-merge is possible
5635 * with the provided extents via xfs_bmse_can_merge().
5636 */
5637STATIC int
5638xfs_bmse_merge(
5639 struct xfs_inode *ip,
5640 int whichfork,
5641 xfs_fileoff_t shift, /* shift fsb */
5642 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005643 struct xfs_bmbt_irec *got, /* extent to shift */
5644 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005645 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005646 int *logflags, /* output */
5647 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005648{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005649 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5650 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005651 xfs_filblks_t blockcount;
5652 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005653 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005654
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005655 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005656
5657 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5658 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005659 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005660
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005661 new = *left;
5662 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005663
5664 /*
5665 * Update the on-disk extent count, the btree if necessary and log the
5666 * inode.
5667 */
5668 XFS_IFORK_NEXT_SET(ip, whichfork,
5669 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5670 *logflags |= XFS_ILOG_CORE;
5671 if (!cur) {
5672 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005673 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005674 }
5675
5676 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005677 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005678 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005679 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005680 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005681
5682 error = xfs_btree_delete(cur, &i);
5683 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005684 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005685 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005686
5687 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005688 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005689 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005690 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005691 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005692
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005693 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005694 if (error)
5695 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005696
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005697done:
5698 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5699 xfs_iext_remove(ip, current_ext, 1, 0);
5700
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005701 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005702 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5703 if (error)
5704 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005705 memcpy(&new, got, sizeof(new));
5706 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005707 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005708}
5709
5710/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005711 * Shift a single extent.
5712 */
5713STATIC int
5714xfs_bmse_shift_one(
5715 struct xfs_inode *ip,
5716 int whichfork,
5717 xfs_fileoff_t offset_shift_fsb,
5718 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005719 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005720 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005721 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005722 enum shift_direction direction,
5723 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005724{
5725 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005726 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005727 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005728 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005729 int error;
5730 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005731 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005732
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005733 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005734 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005735 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005736
Brian Fosterf71721d2014-09-23 15:39:05 +10005737 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005738 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005739
Namjae Jeona904b1c2015-03-25 15:08:56 +11005740 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005741 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005742
Namjae Jeona904b1c2015-03-25 15:08:56 +11005743 /*
5744 * Check for merge if we've got an extent to the left,
5745 * otherwise make sure there's enough room at the start
5746 * of the file for the shift.
5747 */
5748 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005749 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005750 return -EINVAL;
5751 goto update_current_ext;
5752 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005753
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005754 /*
5755 * grab the left extent and check for a large enough hole.
5756 */
5757 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
5758 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005759 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005760
Dave Chinnerb11bd672014-12-04 09:42:24 +11005761 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005762 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
5763 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5764 *current_ext, got, &adj_irec,
5765 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005766 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005767 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005768 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005769 /* nothing to move if this is the last extent */
5770 if (*current_ext >= (total_extents - 1))
5771 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005772
Namjae Jeona904b1c2015-03-25 15:08:56 +11005773 /*
5774 * If this is not the last extent in the file, make sure there
5775 * is enough room between current extent and next extent for
5776 * accommodating the shift.
5777 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005778 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
5779 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005780 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005781
Namjae Jeona904b1c2015-03-25 15:08:56 +11005782 /*
5783 * Unlike a left shift (which involves a hole punch),
5784 * a right shift does not modify extent neighbors
5785 * in any way. We should never find mergeable extents
5786 * in this scenario. Check anyways and warn if we
5787 * encounter two extents that could be one.
5788 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005789 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11005790 WARN_ON_ONCE(1);
5791 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005792
Brian Fostera979bdf2014-09-23 15:39:04 +10005793 /*
5794 * Increment the extent index for the next iteration, update the start
5795 * offset of the in-core extent and update the btree if applicable.
5796 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005797update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005798 *logflags |= XFS_ILOG_CORE;
5799
5800 new = *got;
5801 new.br_startoff = startoff;
5802
5803 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005804 error = xfs_bmbt_lookup_eq(cur, got, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005805 if (error)
5806 return error;
5807 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5808
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005809 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005810 if (error)
5811 return error;
5812 } else {
5813 *logflags |= XFS_ILOG_DEXT;
5814 }
5815
5816 xfs_iext_update_extent(ifp, *current_ext, &new);
5817
Namjae Jeona904b1c2015-03-25 15:08:56 +11005818 if (direction == SHIFT_LEFT)
5819 (*current_ext)++;
5820 else
5821 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005822
Darrick J. Wong9c194642016-08-03 12:16:05 +10005823 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005824 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005825 if (error)
5826 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005827 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10005828}
5829
5830/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005831 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005832 *
Brian Foster2c845f52014-09-23 15:37:09 +10005833 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005834 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005835 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5836 * is the length by which each extent is shifted. If there is no hole to shift
5837 * the extents into, this will be considered invalid operation and we abort
5838 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005839 */
5840int
5841xfs_bmap_shift_extents(
5842 struct xfs_trans *tp,
5843 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005844 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005845 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005846 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005847 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005848 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005849 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005850 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005851 int num_exts)
5852{
Brian Fosterca446d82014-09-02 12:12:53 +10005853 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005854 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005855 struct xfs_mount *mp = ip->i_mount;
5856 struct xfs_ifork *ifp;
5857 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005858 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005859 xfs_extnum_t total_extents;
5860 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005861 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005862 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005863 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005864
5865 if (unlikely(XFS_TEST_ERROR(
5866 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5867 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005868 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005869 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5870 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005871 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005872 }
5873
5874 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005875 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005876
Brian Foster2c845f52014-09-23 15:37:09 +10005877 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5878 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005879 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005880
5881 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005882 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5883 /* Read in all the extents */
5884 error = xfs_iread_extents(tp, ip, whichfork);
5885 if (error)
5886 return error;
5887 }
5888
Brian Fosterddb19e32014-09-23 15:38:09 +10005889 if (ifp->if_flags & XFS_IFBROOT) {
5890 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5891 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005892 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005893 cur->bc_private.b.flags = 0;
5894 }
5895
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005896 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005897 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005898 * are collapsing out, so we cannot use the count of real extents here.
5899 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005900 */
Eric Sandeen5d829302016-11-08 12:59:42 +11005901 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005902 if (total_extents == 0) {
5903 *done = 1;
5904 goto del_cursor;
5905 }
5906
5907 /*
5908 * In case of first right shift, we need to initialize next_fsb
5909 */
5910 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005911 ASSERT(direction == SHIFT_RIGHT);
5912
5913 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005914 xfs_iext_get_extent(ifp, current_ext, &got);
5915 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005916 *done = 1;
5917 goto del_cursor;
5918 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005919 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005920 } else {
5921 /*
5922 * Look up the extent index for the fsb where we start shifting. We can
5923 * henceforth iterate with current_ext as extent list changes are locked
5924 * out via ilock.
5925 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005926 * If next_fsb lies in a hole beyond which there are no extents we are
5927 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005928 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005929 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5930 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005931 *done = 1;
5932 goto del_cursor;
5933 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 }
5935
5936 /* Lookup the extent index at which we have to stop */
5937 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005938 struct xfs_bmbt_irec s;
5939
5940 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005941 /* Make stop_extent exclusive of shift range */
5942 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005943 if (current_ext <= stop_extent) {
5944 error = -EIO;
5945 goto del_cursor;
5946 }
5947 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005948 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005949 if (current_ext >= stop_extent) {
5950 error = -EIO;
5951 goto del_cursor;
5952 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005953 }
5954
5955 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005956 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005957 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005958 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10005959 if (error)
5960 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005961 /*
5962 * If there was an extent merge during the shift, the extent
5963 * count can change. Update the total and grade the next record.
5964 */
5965 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11005966 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005967 stop_extent = total_extents;
5968 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005969
Namjae Jeona904b1c2015-03-25 15:08:56 +11005970 if (current_ext == stop_extent) {
5971 *done = 1;
5972 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005973 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005974 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005975 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005976 }
5977
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005978 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10005979 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005980
5981del_cursor:
5982 if (cur)
5983 xfs_btree_del_cursor(cur,
5984 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5985
Brian Fosterca446d82014-09-02 12:12:53 +10005986 if (logflags)
5987 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005988
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005989 return error;
5990}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005991
5992/*
5993 * Splits an extent into two extents at split_fsb block such that it is
5994 * the first block of the current_ext. @current_ext is a target extent
5995 * to be split. @split_fsb is a block where the extents is split.
5996 * If split_fsb lies in a hole or the first block of extents, just return 0.
5997 */
5998STATIC int
5999xfs_bmap_split_extent_at(
6000 struct xfs_trans *tp,
6001 struct xfs_inode *ip,
6002 xfs_fileoff_t split_fsb,
6003 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006004 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006005{
6006 int whichfork = XFS_DATA_FORK;
6007 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006008 struct xfs_bmbt_irec got;
6009 struct xfs_bmbt_irec new; /* split extent */
6010 struct xfs_mount *mp = ip->i_mount;
6011 struct xfs_ifork *ifp;
6012 xfs_fsblock_t gotblkcnt; /* new block count for got */
6013 xfs_extnum_t current_ext;
6014 int error = 0;
6015 int logflags = 0;
6016 int i = 0;
6017
6018 if (unlikely(XFS_TEST_ERROR(
6019 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6020 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006021 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006022 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6023 XFS_ERRLEVEL_LOW, mp);
6024 return -EFSCORRUPTED;
6025 }
6026
6027 if (XFS_FORCED_SHUTDOWN(mp))
6028 return -EIO;
6029
6030 ifp = XFS_IFORK_PTR(ip, whichfork);
6031 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6032 /* Read in all the extents */
6033 error = xfs_iread_extents(tp, ip, whichfork);
6034 if (error)
6035 return error;
6036 }
6037
6038 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006039 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006040 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006041 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6042 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006043 return 0;
6044
6045 gotblkcnt = split_fsb - got.br_startoff;
6046 new.br_startoff = split_fsb;
6047 new.br_startblock = got.br_startblock + gotblkcnt;
6048 new.br_blockcount = got.br_blockcount - gotblkcnt;
6049 new.br_state = got.br_state;
6050
6051 if (ifp->if_flags & XFS_IFBROOT) {
6052 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6053 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006054 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006055 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006056 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006057 if (error)
6058 goto del_cursor;
6059 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6060 }
6061
Namjae Jeona904b1c2015-03-25 15:08:56 +11006062 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006063 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006064
6065 logflags = XFS_ILOG_CORE;
6066 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006067 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006068 if (error)
6069 goto del_cursor;
6070 } else
6071 logflags |= XFS_ILOG_DEXT;
6072
6073 /* Add new extent */
6074 current_ext++;
6075 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6076 XFS_IFORK_NEXT_SET(ip, whichfork,
6077 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6078
6079 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006080 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006081 if (error)
6082 goto del_cursor;
6083 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006084 error = xfs_btree_insert(cur, &i);
6085 if (error)
6086 goto del_cursor;
6087 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6088 }
6089
6090 /*
6091 * Convert to a btree if necessary.
6092 */
6093 if (xfs_bmap_needs_btree(ip, whichfork)) {
6094 int tmp_logflags; /* partial log flag return val */
6095
6096 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006097 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006098 &cur, 0, &tmp_logflags, whichfork);
6099 logflags |= tmp_logflags;
6100 }
6101
6102del_cursor:
6103 if (cur) {
6104 cur->bc_private.b.allocated = 0;
6105 xfs_btree_del_cursor(cur,
6106 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6107 }
6108
6109 if (logflags)
6110 xfs_trans_log_inode(tp, ip, logflags);
6111 return error;
6112}
6113
6114int
6115xfs_bmap_split_extent(
6116 struct xfs_inode *ip,
6117 xfs_fileoff_t split_fsb)
6118{
6119 struct xfs_mount *mp = ip->i_mount;
6120 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006121 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006122 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006123 int error;
6124
Christoph Hellwig253f4912016-04-06 09:19:55 +10006125 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6126 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6127 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006128 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006129
6130 xfs_ilock(ip, XFS_ILOCK_EXCL);
6131 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6132
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006133 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006134
6135 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006136 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006137 if (error)
6138 goto out;
6139
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006140 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006141 if (error)
6142 goto out;
6143
Christoph Hellwig70393312015-06-04 13:48:08 +10006144 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006145
6146out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006147 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006148 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006149 return error;
6150}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006151
6152/* Deferred mapping is only for real extents in the data fork. */
6153static bool
6154xfs_bmap_is_update_needed(
6155 struct xfs_bmbt_irec *bmap)
6156{
6157 return bmap->br_startblock != HOLESTARTBLOCK &&
6158 bmap->br_startblock != DELAYSTARTBLOCK;
6159}
6160
6161/* Record a bmap intent. */
6162static int
6163__xfs_bmap_add(
6164 struct xfs_mount *mp,
6165 struct xfs_defer_ops *dfops,
6166 enum xfs_bmap_intent_type type,
6167 struct xfs_inode *ip,
6168 int whichfork,
6169 struct xfs_bmbt_irec *bmap)
6170{
6171 int error;
6172 struct xfs_bmap_intent *bi;
6173
6174 trace_xfs_bmap_defer(mp,
6175 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6176 type,
6177 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6178 ip->i_ino, whichfork,
6179 bmap->br_startoff,
6180 bmap->br_blockcount,
6181 bmap->br_state);
6182
6183 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6184 INIT_LIST_HEAD(&bi->bi_list);
6185 bi->bi_type = type;
6186 bi->bi_owner = ip;
6187 bi->bi_whichfork = whichfork;
6188 bi->bi_bmap = *bmap;
6189
Christoph Hellwig882d8782017-08-28 10:21:03 -07006190 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006191 if (error) {
6192 kmem_free(bi);
6193 return error;
6194 }
6195
6196 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6197 return 0;
6198}
6199
6200/* Map an extent into a file. */
6201int
6202xfs_bmap_map_extent(
6203 struct xfs_mount *mp,
6204 struct xfs_defer_ops *dfops,
6205 struct xfs_inode *ip,
6206 struct xfs_bmbt_irec *PREV)
6207{
6208 if (!xfs_bmap_is_update_needed(PREV))
6209 return 0;
6210
6211 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6212 XFS_DATA_FORK, PREV);
6213}
6214
6215/* Unmap an extent out of a file. */
6216int
6217xfs_bmap_unmap_extent(
6218 struct xfs_mount *mp,
6219 struct xfs_defer_ops *dfops,
6220 struct xfs_inode *ip,
6221 struct xfs_bmbt_irec *PREV)
6222{
6223 if (!xfs_bmap_is_update_needed(PREV))
6224 return 0;
6225
6226 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6227 XFS_DATA_FORK, PREV);
6228}
6229
6230/*
6231 * Process one of the deferred bmap operations. We pass back the
6232 * btree cursor to maintain our lock on the bmapbt between calls.
6233 */
6234int
6235xfs_bmap_finish_one(
6236 struct xfs_trans *tp,
6237 struct xfs_defer_ops *dfops,
6238 struct xfs_inode *ip,
6239 enum xfs_bmap_intent_type type,
6240 int whichfork,
6241 xfs_fileoff_t startoff,
6242 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006243 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006244 xfs_exntst_t state)
6245{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006246 xfs_fsblock_t firstfsb;
6247 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006248
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006249 /*
6250 * firstfsb is tied to the transaction lifetime and is used to
6251 * ensure correct AG locking order and schedule work item
6252 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6253 * to only making one bmap call per transaction, so it should
6254 * be safe to have it as a local variable here.
6255 */
6256 firstfsb = NULLFSBLOCK;
6257
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006258 trace_xfs_bmap_deferred(tp->t_mountp,
6259 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6260 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006261 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006262
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006263 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006264 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006265
6266 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006267 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006268 return -EIO;
6269
6270 switch (type) {
6271 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006272 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006273 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006274 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006275 break;
6276 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006277 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6278 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006279 break;
6280 default:
6281 ASSERT(0);
6282 error = -EFSCORRUPTED;
6283 }
6284
6285 return error;
6286}