Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 : * All Rights Reserved.
5 : */
6 : #include "xfs.h"
7 : #include "xfs_fs.h"
8 : #include "xfs_shared.h"
9 : #include "xfs_format.h"
10 : #include "xfs_log_format.h"
11 : #include "xfs_trans_resv.h"
12 : #include "xfs_bit.h"
13 : #include "xfs_sb.h"
14 : #include "xfs_mount.h"
15 : #include "xfs_defer.h"
16 : #include "xfs_dir2.h"
17 : #include "xfs_inode.h"
18 : #include "xfs_btree.h"
19 : #include "xfs_trans.h"
20 : #include "xfs_alloc.h"
21 : #include "xfs_bmap.h"
22 : #include "xfs_bmap_util.h"
23 : #include "xfs_bmap_btree.h"
24 : #include "xfs_rtbitmap.h"
25 : #include "xfs_errortag.h"
26 : #include "xfs_error.h"
27 : #include "xfs_quota.h"
28 : #include "xfs_trans_space.h"
29 : #include "xfs_buf_item.h"
30 : #include "xfs_trace.h"
31 : #include "xfs_attr_leaf.h"
32 : #include "xfs_filestream.h"
33 : #include "xfs_rmap.h"
34 : #include "xfs_ag.h"
35 : #include "xfs_ag_resv.h"
36 : #include "xfs_refcount.h"
37 : #include "xfs_icache.h"
38 : #include "xfs_iomap.h"
39 : #include "xfs_health.h"
40 : #include "xfs_symlink_remote.h"
41 : #include "xfs_inode_util.h"
42 : #include "xfs_rtalloc.h"
43 :
44 : struct kmem_cache *xfs_bmap_intent_cache;
45 :
46 : /*
47 : * Miscellaneous helper functions
48 : */
49 :
50 : /*
51 : * Compute and fill in the value of the maximum depth of a bmap btree
52 : * in this filesystem. Done once, during mount.
53 : */
54 : void
55 133944 : xfs_bmap_compute_maxlevels(
56 : xfs_mount_t *mp, /* file system mount structure */
57 : int whichfork) /* data or attr fork */
58 : {
59 133944 : uint64_t maxblocks; /* max blocks at this level */
60 133944 : xfs_extnum_t maxleafents; /* max leaf entries possible */
61 133944 : int level; /* btree level */
62 133944 : int maxrootrecs; /* max records in root block */
63 133944 : int minleafrecs; /* min records in leaf block */
64 133944 : int minnoderecs; /* min records in node block */
65 133944 : int sz; /* root block size */
66 :
67 : /*
68 : * The maximum number of extents in a fork, hence the maximum number of
69 : * leaf entries, is controlled by the size of the on-disk extent count.
70 : *
71 : * Note that we can no longer assume that if we are in ATTR1 that the
72 : * fork offset of all the inodes will be
73 : * (xfs_default_attroffset(ip) >> 3) because we could have mounted with
74 : * ATTR2 and then mounted back with ATTR1, keeping the i_forkoff's fixed
75 : * but probably at various positions. Therefore, for both ATTR1 and
76 : * ATTR2 we have to assume the worst case scenario of a minimum size
77 : * available.
78 : */
79 133944 : maxleafents = xfs_iext_max_nextents(xfs_has_large_extent_counts(mp),
80 : whichfork);
81 133944 : if (whichfork == XFS_DATA_FORK)
82 : sz = xfs_bmdr_space_calc(MINDBTPTRS);
83 : else
84 66972 : sz = xfs_bmdr_space_calc(MINABTPTRS);
85 :
86 133944 : maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
87 133944 : minleafrecs = mp->m_bmap_dmnr[0];
88 133944 : minnoderecs = mp->m_bmap_dmnr[1];
89 133944 : maxblocks = howmany_64(maxleafents, minleafrecs);
90 942126 : for (level = 1; maxblocks > 1; level++) {
91 674238 : if (maxblocks <= maxrootrecs)
92 : maxblocks = 1;
93 : else
94 674177 : maxblocks = howmany_64(maxblocks, minnoderecs);
95 : }
96 133944 : mp->m_bm_maxlevels[whichfork] = level;
97 133944 : ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
98 133944 : }
99 :
100 : unsigned int
101 66972 : xfs_bmap_compute_attr_offset(
102 : struct xfs_mount *mp)
103 : {
104 66972 : if (mp->m_sb.sb_inodesize == 256)
105 428 : return XFS_LITINO(mp) - xfs_bmdr_space_calc(MINABTPTRS);
106 : return xfs_bmdr_space_calc(6 * MINABTPTRS);
107 : }
108 :
109 : STATIC int /* error */
110 479816592 : xfs_bmbt_lookup_eq(
111 : struct xfs_btree_cur *cur,
112 : struct xfs_bmbt_irec *irec,
113 : int *stat) /* success/failure */
114 : {
115 479816592 : cur->bc_rec.b = *irec;
116 479816592 : return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
117 : }
118 :
119 : STATIC int /* error */
120 0 : xfs_bmbt_lookup_first(
121 : struct xfs_btree_cur *cur,
122 : int *stat) /* success/failure */
123 : {
124 0 : cur->bc_rec.b.br_startoff = 0;
125 0 : cur->bc_rec.b.br_startblock = 0;
126 0 : cur->bc_rec.b.br_blockcount = 0;
127 0 : return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
128 : }
129 :
130 : /*
131 : * Check if the inode needs to be converted to btree format.
132 : */
133 631392233 : static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
134 : {
135 631392233 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
136 :
137 1244493332 : return whichfork != XFS_COW_FORK &&
138 631191315 : ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
139 273465173 : ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork);
140 : }
141 :
142 : /*
143 : * Check if the inode should be converted to extent format.
144 : */
145 624847949 : static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
146 : {
147 624847949 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
148 :
149 1244845963 : return whichfork != XFS_COW_FORK &&
150 624869137 : ifp->if_format == XFS_DINODE_FMT_BTREE &&
151 345385791 : ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork);
152 : }
153 :
154 : /*
155 : * Update the record referred to by cur to the value given by irec
156 : * This either works (return 0) or gets an EFSCORRUPTED error.
157 : */
158 : STATIC int
159 215294883 : xfs_bmbt_update(
160 : struct xfs_btree_cur *cur,
161 : struct xfs_bmbt_irec *irec)
162 : {
163 215294883 : union xfs_btree_rec rec;
164 :
165 215294883 : xfs_bmbt_disk_set_all(&rec.bmbt, irec);
166 215294391 : return xfs_btree_update(cur, &rec);
167 : }
168 :
169 : /*
170 : * Compute the worst-case number of indirect blocks that will be used
171 : * for ip's delayed extent of length "len".
172 : */
173 : STATIC xfs_filblks_t
174 84334040 : xfs_bmap_worst_indlen(
175 : xfs_inode_t *ip, /* incore inode pointer */
176 : xfs_filblks_t len) /* delayed extent length */
177 : {
178 84334040 : int level; /* btree level number */
179 84334040 : int maxrecs; /* maximum record count at this level */
180 84334040 : xfs_mount_t *mp; /* mount structure */
181 84334040 : xfs_filblks_t rval; /* return value */
182 :
183 84334040 : mp = ip->i_mount;
184 84334040 : maxrecs = mp->m_bmap_dmxr[0];
185 84334040 : for (level = 0, rval = 0;
186 87823399 : level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
187 3489359 : level++) {
188 87811005 : len += maxrecs - 1;
189 87811005 : do_div(len, maxrecs);
190 87811005 : rval += len;
191 87811005 : if (len == 1)
192 84321646 : return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
193 84321646 : level - 1;
194 3489359 : if (level == 0)
195 3107582 : maxrecs = mp->m_bmap_dmxr[1];
196 : }
197 : return rval;
198 : }
199 :
200 : /*
201 : * Calculate the default attribute fork offset for newly created inodes.
202 : */
203 : uint
204 114432496 : xfs_default_attroffset(
205 : struct xfs_inode *ip)
206 : {
207 114432496 : if (ip->i_df.if_format == XFS_DINODE_FMT_DEV)
208 : return roundup(sizeof(xfs_dev_t), 8);
209 104205367 : return M_IGEO(ip->i_mount)->attr_fork_offset;
210 : }
211 :
212 : /*
213 : * Helper routine to reset inode i_forkoff field when switching attribute fork
214 : * from local to extent format - we reset it where possible to make space
215 : * available for inline data fork extents.
216 : */
217 : STATIC void
218 8889074 : xfs_bmap_forkoff_reset(
219 : xfs_inode_t *ip,
220 : int whichfork)
221 : {
222 8889074 : if (whichfork == XFS_ATTR_FORK &&
223 8236307 : ip->i_df.if_format != XFS_DINODE_FMT_DEV &&
224 : ip->i_df.if_format != XFS_DINODE_FMT_BTREE) {
225 8077403 : uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
226 :
227 8077403 : if (dfl_forkoff > ip->i_forkoff)
228 8040203 : ip->i_forkoff = dfl_forkoff;
229 : }
230 8889074 : }
231 :
232 : #ifdef DEBUG
233 : STATIC struct xfs_buf *
234 744487374 : xfs_bmap_get_bp(
235 : struct xfs_btree_cur *cur,
236 : xfs_fsblock_t bno)
237 : {
238 744487374 : struct xfs_log_item *lip;
239 744487374 : int i;
240 :
241 744487374 : if (!cur)
242 : return NULL;
243 :
244 1789726173 : for (i = 0; i < cur->bc_maxlevels; i++) {
245 1789726221 : if (!cur->bc_levels[i].bp)
246 : break;
247 1216559396 : if (xfs_buf_daddr(cur->bc_levels[i].bp) == bno)
248 171320597 : return cur->bc_levels[i].bp;
249 : }
250 :
251 : /* Chase down all the log items to see if the bp is there */
252 3295125023 : list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
253 2750502677 : struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
254 :
255 2750502677 : if (bip->bli_item.li_type == XFS_LI_BUF &&
256 1663924100 : xfs_buf_daddr(bip->bli_buf) == bno)
257 28544431 : return bip->bli_buf;
258 : }
259 :
260 : return NULL;
261 : }
262 :
263 : STATIC void
264 174721607 : xfs_check_block(
265 : struct xfs_btree_block *block,
266 : xfs_mount_t *mp,
267 : int root,
268 : short sz)
269 : {
270 174721607 : int i, j, dmxr;
271 174721607 : __be64 *pp, *thispa; /* pointer to block address */
272 174721607 : xfs_bmbt_key_t *prevp, *keyp;
273 :
274 174721607 : ASSERT(be16_to_cpu(block->bb_level) > 0);
275 :
276 : prevp = NULL;
277 919187366 : for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
278 744465463 : dmxr = mp->m_bmap_dmxr[0];
279 744465463 : keyp = xfs_bmbt_key_addr(mp, block, i);
280 :
281 744465463 : if (prevp) {
282 569747338 : ASSERT(be64_to_cpu(prevp->br_startoff) <
283 : be64_to_cpu(keyp->br_startoff));
284 : }
285 744465463 : prevp = keyp;
286 :
287 : /*
288 : * Compare the block numbers to see if there are dups.
289 : */
290 744465463 : if (root)
291 268968194 : pp = xfs_bmap_broot_ptr_addr(mp, block, i, sz);
292 : else
293 475497269 : pp = xfs_bmbt_ptr_addr(mp, block, i, dmxr);
294 :
295 7580889888 : for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
296 6836424129 : if (root)
297 360646118 : thispa = xfs_bmap_broot_ptr_addr(mp, block, j, sz);
298 : else
299 6475778011 : thispa = xfs_bmbt_ptr_addr(mp, block, j, dmxr);
300 6836426874 : if (*thispa == *pp) {
301 0 : xfs_warn(mp, "%s: thispa(%d) == pp(%d) %lld",
302 : __func__, j, i,
303 : (unsigned long long)be64_to_cpu(*thispa));
304 0 : xfs_err(mp, "%s: ptrs are equal in node\n",
305 : __func__);
306 0 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
307 : }
308 : }
309 : }
310 174721903 : }
311 :
312 : /*
313 : * Check that the extents for the inode ip are in the right order in all
314 : * btree leaves. THis becomes prohibitively expensive for large extent count
315 : * files, so don't bother with inodes that have more than 10,000 extents in
316 : * them. The btree record ordering checks will still be done, so for such large
317 : * bmapbt constructs that is going to catch most corruptions.
318 : */
319 : STATIC void
320 429891857 : xfs_bmap_check_leaf_extents(
321 : struct xfs_btree_cur *cur, /* btree cursor or null */
322 : xfs_inode_t *ip, /* incore inode pointer */
323 : int whichfork) /* data or attr fork */
324 : {
325 429891857 : struct xfs_mount *mp = ip->i_mount;
326 429891857 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
327 429852277 : struct xfs_btree_block *block; /* current btree block */
328 429852277 : xfs_fsblock_t bno; /* block # of "block" */
329 429852277 : struct xfs_buf *bp; /* buffer for "block" */
330 429852277 : int error; /* error return value */
331 429852277 : xfs_extnum_t i=0, j; /* index into the extents list */
332 429852277 : int level; /* btree level, for checking */
333 429852277 : __be64 *pp; /* pointer to block address */
334 429852277 : xfs_bmbt_rec_t *ep; /* pointer to current extent */
335 429852277 : xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
336 429852277 : xfs_bmbt_rec_t *nextp; /* pointer to next extent */
337 429852277 : int bp_release = 0;
338 :
339 429852277 : if (ifp->if_format != XFS_DINODE_FMT_BTREE)
340 : return;
341 :
342 : /* skip large extent count inodes */
343 238925459 : if (ip->i_df.if_nextents > 10000)
344 : return;
345 :
346 155942652 : bno = NULLFSBLOCK;
347 155942652 : block = ifp->if_broot;
348 : /*
349 : * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
350 : */
351 155942652 : level = be16_to_cpu(block->bb_level);
352 155942652 : ASSERT(level > 0);
353 155942652 : xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
354 155946390 : pp = xfs_bmap_broot_ptr_addr(mp, block, 1, ifp->if_broot_bytes);
355 155945949 : bno = be64_to_cpu(*pp);
356 :
357 155945949 : ASSERT(bno != NULLFSBLOCK);
358 155945949 : ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
359 155945949 : ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
360 :
361 : /*
362 : * Go down the tree until leaf level is reached, following the first
363 : * pointer (leftmost) at each level.
364 : */
365 174721850 : while (level-- > 0) {
366 : /* See if buf is in cur first */
367 174722882 : bp_release = 0;
368 174722882 : bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
369 174721947 : if (!bp) {
370 48512271 : bp_release = 1;
371 48512271 : error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
372 : XFS_BMAP_BTREE_REF,
373 : &xfs_bmbt_buf_ops);
374 48512223 : if (xfs_metadata_is_sick(error))
375 0 : xfs_btree_mark_sick(cur);
376 48512223 : if (error)
377 1 : goto error_norelse;
378 : }
379 174721898 : block = XFS_BUF_TO_BLOCK(bp);
380 174721898 : if (level == 0)
381 : break;
382 :
383 : /*
384 : * Check this block for basic sanity (increasing keys and
385 : * no duplicate blocks).
386 : */
387 :
388 18776510 : xfs_check_block(block, mp, 0, 0);
389 18777125 : pp = xfs_bmbt_ptr_addr(mp, block, 1, mp->m_bmap_dmxr[1]);
390 18777125 : bno = be64_to_cpu(*pp);
391 18777125 : if (XFS_IS_CORRUPT(mp, !xfs_verify_fsbno(mp, bno))) {
392 0 : xfs_btree_mark_sick(cur);
393 0 : error = -EFSCORRUPTED;
394 0 : goto error0;
395 : }
396 18777057 : if (bp_release) {
397 0 : bp_release = 0;
398 0 : xfs_trans_brelse(NULL, bp);
399 : }
400 : }
401 :
402 : /*
403 : * Here with bp and block set to the leftmost leaf node in the tree.
404 : */
405 : i = 0;
406 :
407 : /*
408 : * Loop over all leaf nodes checking that all extents are in the right order.
409 : */
410 1295445362 : for (;;) {
411 725694859 : xfs_fsblock_t nextbno;
412 725694859 : xfs_extnum_t num_recs;
413 :
414 :
415 725694859 : num_recs = xfs_btree_get_numrecs(block);
416 :
417 : /*
418 : * Read-ahead the next leaf block, if any.
419 : */
420 :
421 725694859 : nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
422 :
423 : /*
424 : * Check all the extents to make sure they are OK.
425 : * If we had a previous block, the last entry should
426 : * conform with the first entry in this one.
427 : */
428 :
429 725694859 : ep = xfs_bmbt_rec_addr(mp, block, 1);
430 725694859 : if (i) {
431 569750171 : ASSERT(xfs_bmbt_disk_get_startoff(&last) +
432 : xfs_bmbt_disk_get_blockcount(&last) <=
433 : xfs_bmbt_disk_get_startoff(ep));
434 : }
435 >15027*10^7 : for (j = 1; j < num_recs; j++) {
436 >14955*10^7 : nextp = xfs_bmbt_rec_addr(mp, block, j + 1);
437 >14955*10^7 : ASSERT(xfs_bmbt_disk_get_startoff(ep) +
438 : xfs_bmbt_disk_get_blockcount(ep) <=
439 : xfs_bmbt_disk_get_startoff(nextp));
440 >14955*10^7 : ep = nextp;
441 : }
442 :
443 725704078 : last = *ep;
444 725704078 : i += num_recs;
445 725704078 : if (bp_release) {
446 544610169 : bp_release = 0;
447 544610169 : xfs_trans_brelse(NULL, bp);
448 : }
449 725720362 : bno = nextbno;
450 : /*
451 : * If we've reached the end, stop.
452 : */
453 725720362 : if (bno == NULLFSBLOCK)
454 : break;
455 :
456 569765984 : bp_release = 0;
457 569765984 : bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
458 569760465 : if (!bp) {
459 496112621 : bp_release = 1;
460 496112621 : error = xfs_btree_read_bufl(mp, NULL, bno, &bp,
461 : XFS_BMAP_BTREE_REF,
462 : &xfs_bmbt_buf_ops);
463 496102714 : if (xfs_metadata_is_sick(error))
464 0 : xfs_btree_mark_sick(cur);
465 496102714 : if (error)
466 55 : goto error_norelse;
467 : }
468 569750503 : block = XFS_BUF_TO_BLOCK(bp);
469 : }
470 :
471 : return;
472 :
473 : error0:
474 0 : xfs_warn(mp, "%s: at error0", __func__);
475 0 : if (bp_release)
476 0 : xfs_trans_brelse(NULL, bp);
477 0 : error_norelse:
478 56 : xfs_warn(mp, "%s: BAD after btree leaves for %llu extents",
479 : __func__, i);
480 56 : xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
481 56 : xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
482 56 : return;
483 : }
484 :
485 : /*
486 : * Validate that the bmbt_irecs being returned from bmapi are valid
487 : * given the caller's original parameters. Specifically check the
488 : * ranges of the returned irecs to ensure that they only extend beyond
489 : * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
490 : */
491 : STATIC void
492 236090087 : xfs_bmap_validate_ret(
493 : xfs_fileoff_t bno,
494 : xfs_filblks_t len,
495 : uint32_t flags,
496 : xfs_bmbt_irec_t *mval,
497 : int nmap,
498 : int ret_nmap)
499 : {
500 236090087 : int i; /* index to map values */
501 :
502 236090087 : ASSERT(ret_nmap <= nmap);
503 :
504 472134995 : for (i = 0; i < ret_nmap; i++) {
505 236069607 : ASSERT(mval[i].br_blockcount > 0);
506 236069607 : if (!(flags & XFS_BMAPI_ENTIRE)) {
507 236069607 : ASSERT(mval[i].br_startoff >= bno);
508 236069607 : ASSERT(mval[i].br_blockcount <= len);
509 236069607 : ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
510 : bno + len);
511 : } else {
512 0 : ASSERT(mval[i].br_startoff < bno + len);
513 0 : ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
514 : bno);
515 : }
516 236069607 : ASSERT(i == 0 ||
517 : mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
518 : mval[i].br_startoff);
519 236069607 : ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
520 : mval[i].br_startblock != HOLESTARTBLOCK);
521 236069607 : ASSERT(mval[i].br_state == XFS_EXT_NORM ||
522 : mval[i].br_state == XFS_EXT_UNWRITTEN);
523 : }
524 236065388 : }
525 :
526 : #else
527 : #define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
528 : #define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
529 : #endif /* DEBUG */
530 :
531 : /*
532 : * Inode fork format manipulation functions
533 : */
534 :
535 : /*
536 : * Convert the inode format to extent format if it currently is in btree format,
537 : * but the extent list is small enough that it fits into the extent format.
538 : *
539 : * Since the extents are already in-core, all we have to do is give up the space
540 : * for the btree root and pitch the leaf block.
541 : */
542 : STATIC int /* error */
543 624999003 : xfs_bmap_btree_to_extents(
544 : struct xfs_trans *tp, /* transaction pointer */
545 : struct xfs_inode *ip, /* incore inode pointer */
546 : struct xfs_btree_cur *cur, /* btree cursor */
547 : int *logflagsp, /* inode logging flags */
548 : int whichfork) /* data or attr fork */
549 : {
550 624999003 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
551 624955376 : struct xfs_mount *mp = ip->i_mount;
552 624955376 : struct xfs_btree_block *rblock = ifp->if_broot;
553 624955376 : struct xfs_btree_block *cblock;/* child btree block */
554 624955376 : xfs_fsblock_t cbno; /* child block number */
555 624955376 : struct xfs_buf *cbp; /* child block's buffer */
556 624955376 : int error; /* error return value */
557 624955376 : __be64 *pp; /* ptr to block address */
558 624955376 : struct xfs_owner_info oinfo;
559 :
560 : /* check if we actually need the extent format first: */
561 624955376 : if (!xfs_bmap_wants_extents(ip, whichfork))
562 : return 0;
563 :
564 2336607 : ASSERT(cur);
565 2336607 : ASSERT(whichfork != XFS_COW_FORK);
566 2336607 : ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
567 2336607 : ASSERT(be16_to_cpu(rblock->bb_level) == 1);
568 2336607 : ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
569 2336607 : ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false) == 1);
570 :
571 2336602 : pp = xfs_bmap_broot_ptr_addr(mp, rblock, 1, ifp->if_broot_bytes);
572 2336609 : cbno = be64_to_cpu(*pp);
573 : #ifdef DEBUG
574 2336609 : if (XFS_IS_CORRUPT(cur->bc_mp, !xfs_btree_check_lptr(cur, cbno, 1))) {
575 0 : xfs_btree_mark_sick(cur);
576 0 : return -EFSCORRUPTED;
577 : }
578 : #endif
579 2336612 : error = xfs_btree_read_bufl(mp, tp, cbno, &cbp, XFS_BMAP_BTREE_REF,
580 : &xfs_bmbt_buf_ops);
581 2336613 : if (xfs_metadata_is_sick(error))
582 0 : xfs_btree_mark_sick(cur);
583 2336613 : if (error)
584 : return error;
585 2336612 : cblock = XFS_BUF_TO_BLOCK(cbp);
586 2336612 : if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
587 : return error;
588 :
589 2336617 : xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
590 2336617 : error = xfs_free_extent_later(cur->bc_tp, cbno, 1, &oinfo,
591 : XFS_AG_RESV_NONE, 0);
592 2336619 : if (error)
593 : return error;
594 :
595 2336619 : ip->i_nblocks--;
596 2336619 : xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
597 2336616 : xfs_trans_binval(tp, cbp);
598 2336621 : if (cur->bc_levels[0].bp == cbp)
599 2336621 : cur->bc_levels[0].bp = NULL;
600 2336621 : xfs_iroot_free(ip, whichfork);
601 2336620 : ASSERT(ifp->if_broot == NULL);
602 2336620 : ifp->if_format = XFS_DINODE_FMT_EXTENTS;
603 2336620 : *logflagsp |= XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
604 2336620 : return 0;
605 : }
606 :
607 : /*
608 : * Convert an extents-format file into a btree-format file.
609 : * The new file will have a root block (in the inode) and a single child block.
610 : */
611 : STATIC int /* error */
612 3746204 : xfs_bmap_extents_to_btree(
613 : struct xfs_trans *tp, /* transaction pointer */
614 : struct xfs_inode *ip, /* incore inode pointer */
615 : struct xfs_btree_cur **curp, /* cursor returned to caller */
616 : int wasdel, /* converting a delayed alloc */
617 : int *logflagsp, /* inode logging flags */
618 : int whichfork) /* data or attr fork */
619 : {
620 3746204 : struct xfs_btree_block *ablock; /* allocated (child) bt block */
621 3746204 : struct xfs_buf *abp; /* buffer for ablock */
622 3746204 : struct xfs_alloc_arg args; /* allocation arguments */
623 3746204 : struct xfs_bmbt_rec *arp; /* child record pointer */
624 3746204 : struct xfs_btree_block *block; /* btree root block */
625 3746204 : struct xfs_btree_cur *cur; /* bmap btree cursor */
626 3746204 : int error; /* error return value */
627 3746204 : struct xfs_ifork *ifp; /* inode fork pointer */
628 3746204 : struct xfs_bmbt_key *kp; /* root block key pointer */
629 3746204 : struct xfs_mount *mp; /* mount structure */
630 3746204 : xfs_bmbt_ptr_t *pp; /* root block address pointer */
631 3746204 : struct xfs_iext_cursor icur;
632 3746204 : struct xfs_bmbt_irec rec;
633 3746204 : xfs_extnum_t cnt = 0;
634 :
635 3746204 : mp = ip->i_mount;
636 3746204 : ASSERT(whichfork != XFS_COW_FORK);
637 3746204 : ifp = xfs_ifork_ptr(ip, whichfork);
638 3746197 : ASSERT(ifp->if_format == XFS_DINODE_FMT_EXTENTS);
639 :
640 : /*
641 : * Need a cursor. Can't allocate until bb_level is filled in.
642 : */
643 3746197 : xfs_bmbt_iroot_alloc(ip, whichfork);
644 3746199 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
645 3746200 : cur->bc_ino.flags = wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
646 : /*
647 : * Convert to a btree with two levels, one record in root.
648 : */
649 3746200 : ifp->if_format = XFS_DINODE_FMT_BTREE;
650 3746200 : memset(&args, 0, sizeof(args));
651 3746200 : args.tp = tp;
652 3746200 : args.mp = mp;
653 3746200 : xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
654 :
655 3746200 : args.minlen = args.maxlen = args.prod = 1;
656 3746200 : args.wasdel = wasdel;
657 3746200 : *logflagsp = 0;
658 18731000 : error = xfs_alloc_vextent_start_ag(&args,
659 3746200 : XFS_INO_TO_FSB(mp, ip->i_ino));
660 3746194 : if (error)
661 49 : goto out_root_realloc;
662 :
663 : /*
664 : * Allocation can't fail, the space was reserved.
665 : */
666 3746145 : if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
667 0 : error = -ENOSPC;
668 0 : goto out_root_realloc;
669 : }
670 :
671 3746145 : cur->bc_ino.allocated++;
672 3746145 : ip->i_nblocks++;
673 3746145 : xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
674 11238451 : error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
675 3746151 : XFS_FSB_TO_DADDR(mp, args.fsbno),
676 : mp->m_bsize, 0, &abp);
677 3746120 : if (error)
678 0 : goto out_unreserve_dquot;
679 :
680 : /*
681 : * Fill in the child block.
682 : */
683 3746120 : ablock = XFS_BUF_TO_BLOCK(abp);
684 3746120 : xfs_btree_init_buf(mp, abp, &xfs_bmbt_ops, 0, 0, ip->i_ino);
685 :
686 54253359 : for_each_xfs_iext(ifp, &icur, &rec) {
687 50507270 : if (isnullstartblock(rec.br_startblock))
688 1620670 : continue;
689 48886600 : arp = xfs_bmbt_rec_addr(mp, ablock, 1 + cnt);
690 48886600 : xfs_bmbt_disk_set_all(arp, &rec);
691 48886558 : cnt++;
692 : }
693 3746142 : ASSERT(cnt == ifp->if_nextents);
694 3746142 : xfs_btree_set_numrecs(ablock, cnt);
695 :
696 : /*
697 : * Fill in the root key and pointer.
698 : */
699 3746142 : block = ifp->if_broot;
700 3746142 : kp = xfs_bmbt_key_addr(mp, block, 1);
701 3746142 : arp = xfs_bmbt_rec_addr(mp, ablock, 1);
702 3746142 : kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
703 3746102 : pp = xfs_bmbt_ptr_addr(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
704 3746126 : be16_to_cpu(block->bb_level)));
705 3746102 : *pp = cpu_to_be64(args.fsbno);
706 :
707 : /*
708 : * Do all this logging at the end so that
709 : * the root is at the right level.
710 : */
711 3746102 : xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
712 3746149 : xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
713 3746128 : ASSERT(*curp == NULL);
714 3746128 : *curp = cur;
715 3746128 : *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
716 3746128 : return 0;
717 :
718 : out_unreserve_dquot:
719 0 : xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
720 49 : out_root_realloc:
721 49 : xfs_iroot_free(ip, whichfork);
722 49 : ifp->if_format = XFS_DINODE_FMT_EXTENTS;
723 49 : ASSERT(ifp->if_broot == NULL);
724 49 : xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
725 :
726 49 : return error;
727 : }
728 :
729 : /*
730 : * Convert a local file to an extents file.
731 : * This code is out of bounds for data forks of regular files,
732 : * since the file data needs to get logged so things will stay consistent.
733 : * (The bmap-level manipulations are ok, though).
734 : */
735 : void
736 8890015 : xfs_bmap_local_to_extents_empty(
737 : struct xfs_trans *tp,
738 : struct xfs_inode *ip,
739 : int whichfork)
740 : {
741 8890015 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
742 :
743 8889518 : ASSERT(whichfork != XFS_COW_FORK);
744 8889518 : ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
745 8889518 : ASSERT(ifp->if_bytes == 0);
746 8889518 : ASSERT(ifp->if_nextents == 0);
747 :
748 8889518 : xfs_bmap_forkoff_reset(ip, whichfork);
749 8889301 : ifp->if_u1.if_root = NULL;
750 8889301 : ifp->if_height = 0;
751 8889301 : ifp->if_format = XFS_DINODE_FMT_EXTENTS;
752 8889301 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
753 8890286 : }
754 :
755 :
756 : int /* error */
757 3141 : xfs_bmap_local_to_extents(
758 : xfs_trans_t *tp, /* transaction pointer */
759 : xfs_inode_t *ip, /* incore inode pointer */
760 : xfs_extlen_t total, /* total blocks needed by transaction */
761 : int *logflagsp, /* inode logging flags */
762 : int whichfork,
763 : void (*init_fn)(struct xfs_trans *tp,
764 : struct xfs_buf *bp,
765 : struct xfs_inode *ip,
766 : struct xfs_ifork *ifp, void *priv),
767 : void *priv)
768 : {
769 3141 : int error = 0;
770 3141 : int flags; /* logging flags returned */
771 3141 : struct xfs_ifork *ifp; /* inode fork pointer */
772 3141 : xfs_alloc_arg_t args; /* allocation arguments */
773 3141 : struct xfs_buf *bp; /* buffer for extent block */
774 3141 : struct xfs_bmbt_irec rec;
775 3141 : struct xfs_iext_cursor icur;
776 :
777 : /*
778 : * We don't want to deal with the case of keeping inode data inline yet.
779 : * So sending the data fork of a regular inode is invalid.
780 : */
781 3141 : ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
782 3141 : ifp = xfs_ifork_ptr(ip, whichfork);
783 3141 : ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
784 :
785 3141 : if (!ifp->if_bytes) {
786 0 : xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
787 0 : flags = XFS_ILOG_CORE;
788 0 : goto done;
789 : }
790 :
791 3141 : flags = 0;
792 3141 : error = 0;
793 3141 : memset(&args, 0, sizeof(args));
794 3141 : args.tp = tp;
795 3141 : args.mp = ip->i_mount;
796 3141 : args.total = total;
797 3141 : args.minlen = args.maxlen = args.prod = 1;
798 3141 : xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
799 :
800 : /*
801 : * Allocate a block. We know we need only one, since the
802 : * file currently fits in an inode.
803 : */
804 3141 : args.total = total;
805 3141 : args.minlen = args.maxlen = args.prod = 1;
806 15705 : error = xfs_alloc_vextent_start_ag(&args,
807 3141 : XFS_INO_TO_FSB(args.mp, ip->i_ino));
808 3141 : if (error)
809 0 : goto done;
810 :
811 : /* Can't fail, the space was reserved. */
812 3141 : ASSERT(args.fsbno != NULLFSBLOCK);
813 3141 : ASSERT(args.len == 1);
814 9423 : error = xfs_trans_get_buf(tp, args.mp->m_ddev_targp,
815 3141 : XFS_FSB_TO_DADDR(args.mp, args.fsbno),
816 3141 : args.mp->m_bsize, 0, &bp);
817 3141 : if (error)
818 0 : goto done;
819 :
820 : /*
821 : * Initialize the block, copy the data and log the remote buffer.
822 : *
823 : * The callout is responsible for logging because the remote format
824 : * might differ from the local format and thus we don't know how much to
825 : * log here. Note that init_fn must also set the buffer log item type
826 : * correctly.
827 : */
828 3141 : init_fn(tp, bp, ip, ifp, priv);
829 :
830 : /* account for the change in fork size */
831 3141 : xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
832 3141 : xfs_bmap_local_to_extents_empty(tp, ip, whichfork);
833 3141 : flags |= XFS_ILOG_CORE;
834 :
835 3141 : ifp->if_u1.if_root = NULL;
836 3141 : ifp->if_height = 0;
837 :
838 3141 : rec.br_startoff = 0;
839 3141 : rec.br_startblock = args.fsbno;
840 3141 : rec.br_blockcount = 1;
841 3141 : rec.br_state = XFS_EXT_NORM;
842 3141 : xfs_iext_first(ifp, &icur);
843 3141 : xfs_iext_insert(ip, &icur, &rec, 0);
844 :
845 3141 : ifp->if_nextents = 1;
846 3141 : ip->i_nblocks = 1;
847 3141 : xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
848 3141 : flags |= xfs_ilog_fext(whichfork);
849 :
850 3141 : done:
851 3141 : *logflagsp = flags;
852 3141 : return error;
853 : }
854 :
855 : /*
856 : * Called from xfs_bmap_add_attrfork to handle btree format files.
857 : */
858 : STATIC int /* error */
859 2235 : xfs_bmap_add_attrfork_btree(
860 : xfs_trans_t *tp, /* transaction pointer */
861 : xfs_inode_t *ip, /* incore inode pointer */
862 : int *flags) /* inode logging flags */
863 : {
864 2235 : struct xfs_btree_block *block = ip->i_df.if_broot;
865 2235 : struct xfs_btree_cur *cur; /* btree cursor */
866 2235 : int error; /* error return value */
867 2235 : xfs_mount_t *mp; /* file system mount struct */
868 2235 : int stat; /* newroot status */
869 :
870 2235 : mp = ip->i_mount;
871 :
872 2235 : if (xfs_bmap_bmdr_space(block) <= xfs_inode_data_fork_size(ip))
873 2235 : *flags |= XFS_ILOG_DBROOT;
874 : else {
875 0 : cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
876 0 : error = xfs_bmbt_lookup_first(cur, &stat);
877 0 : if (error)
878 0 : goto error0;
879 : /* must be at least one entry */
880 0 : if (XFS_IS_CORRUPT(mp, stat != 1)) {
881 0 : xfs_btree_mark_sick(cur);
882 0 : error = -EFSCORRUPTED;
883 0 : goto error0;
884 : }
885 0 : if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
886 0 : goto error0;
887 0 : if (stat == 0) {
888 0 : xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
889 0 : return -ENOSPC;
890 : }
891 0 : cur->bc_ino.allocated = 0;
892 0 : xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
893 : }
894 : return 0;
895 0 : error0:
896 0 : xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
897 0 : return error;
898 : }
899 :
900 : /*
901 : * Called from xfs_bmap_add_attrfork to handle extents format files.
902 : */
903 : STATIC int /* error */
904 4696393 : xfs_bmap_add_attrfork_extents(
905 : struct xfs_trans *tp, /* transaction pointer */
906 : struct xfs_inode *ip, /* incore inode pointer */
907 : int *flags) /* inode logging flags */
908 : {
909 4696393 : struct xfs_btree_cur *cur; /* bmap btree cursor */
910 4696393 : int error; /* error return value */
911 :
912 4696393 : if (ip->i_df.if_nextents * sizeof(struct xfs_bmbt_rec) <=
913 4696393 : xfs_inode_data_fork_size(ip))
914 : return 0;
915 1615 : cur = NULL;
916 1615 : error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
917 : XFS_DATA_FORK);
918 1615 : if (cur) {
919 1615 : cur->bc_ino.allocated = 0;
920 1615 : xfs_btree_del_cursor(cur, error);
921 : }
922 : return error;
923 : }
924 :
925 : /*
926 : * Called from xfs_bmap_add_attrfork to handle local format files. Each
927 : * different data fork content type needs a different callout to do the
928 : * conversion. Some are basic and only require special block initialisation
929 : * callouts for the data formating, others (directories) are so specialised they
930 : * handle everything themselves.
931 : *
932 : * XXX (dgc): investigate whether directory conversion can use the generic
933 : * formatting callout. It should be possible - it's just a very complex
934 : * formatter.
935 : */
936 : STATIC int /* error */
937 68922 : xfs_bmap_add_attrfork_local(
938 : struct xfs_trans *tp, /* transaction pointer */
939 : struct xfs_inode *ip, /* incore inode pointer */
940 : int *flags) /* inode logging flags */
941 : {
942 68922 : struct xfs_da_args dargs; /* args for dir/attr code */
943 :
944 68922 : if (ip->i_df.if_bytes <= xfs_inode_data_fork_size(ip))
945 : return 0;
946 :
947 68 : if (S_ISDIR(VFS_I(ip)->i_mode)) {
948 66 : memset(&dargs, 0, sizeof(dargs));
949 66 : dargs.geo = ip->i_mount->m_dir_geo;
950 66 : dargs.dp = ip;
951 66 : dargs.total = dargs.geo->fsbcount;
952 66 : dargs.whichfork = XFS_DATA_FORK;
953 66 : dargs.trans = tp;
954 66 : dargs.owner = ip->i_ino;
955 66 : return xfs_dir2_sf_to_block(&dargs);
956 : }
957 :
958 2 : if (S_ISLNK(VFS_I(ip)->i_mode))
959 2 : return xfs_bmap_local_to_extents(tp, ip, 1, flags,
960 : XFS_DATA_FORK, xfs_symlink_local_to_remote,
961 : NULL);
962 :
963 : /* should only be called for types that support local format data */
964 0 : ASSERT(0);
965 0 : xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
966 0 : return -EFSCORRUPTED;
967 : }
968 :
969 : /*
970 : * Set an inode attr fork offset based on the format of the data fork.
971 : */
972 : static int
973 4768958 : xfs_bmap_set_attrforkoff(
974 : struct xfs_inode *ip,
975 : int size,
976 : int *version)
977 : {
978 4768958 : int default_size = xfs_default_attroffset(ip) >> 3;
979 :
980 4768958 : switch (ip->i_df.if_format) {
981 7 : case XFS_DINODE_FMT_DEV:
982 7 : ip->i_forkoff = default_size;
983 7 : break;
984 4768951 : case XFS_DINODE_FMT_LOCAL:
985 : case XFS_DINODE_FMT_EXTENTS:
986 : case XFS_DINODE_FMT_BTREE:
987 4768951 : ip->i_forkoff = xfs_attr_shortform_bytesfit(ip, size);
988 4763418 : if (!ip->i_forkoff)
989 76 : ip->i_forkoff = default_size;
990 4763342 : else if (xfs_has_attr2(ip->i_mount) && version)
991 4763342 : *version = 2;
992 : break;
993 0 : default:
994 0 : ASSERT(0);
995 0 : return -EINVAL;
996 : }
997 :
998 : return 0;
999 : }
1000 :
1001 : /*
1002 : * Convert inode from non-attributed to attributed. Caller must hold the
1003 : * ILOCK_EXCL and the file cannot have an attr fork.
1004 : */
1005 : int /* error code */
1006 9290943 : xfs_bmap_add_attrfork(
1007 : struct xfs_trans *tp,
1008 : struct xfs_inode *ip, /* incore inode pointer */
1009 : int size, /* space new attribute needs */
1010 : int rsvd) /* xact may use reserved blks */
1011 9290943 : {
1012 9290943 : struct xfs_mount *mp = tp->t_mountp;
1013 9290943 : int version = 1; /* superblock attr version */
1014 9290943 : int logflags; /* logging flags */
1015 9290943 : int error; /* error return value */
1016 9290943 :
1017 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1018 9290943 : if (xfs_is_metadir_inode(ip))
1019 9290943 : ASSERT(XFS_IS_DQDETACHED(ip->i_mount, ip));
1020 4968 : else
1021 : ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1022 9285975 : ASSERT(!xfs_inode_has_attr_fork(ip));
1023 :
1024 9290943 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1025 : error = xfs_bmap_set_attrforkoff(ip, size, &version);
1026 9290943 : if (error)
1027 : return error;
1028 9294731 :
1029 : xfs_ifork_init_attr(ip, XFS_DINODE_FMT_EXTENTS, 0);
1030 9294274 : logflags = 0;
1031 4523643 : switch (ip->i_df.if_format) {
1032 : case XFS_DINODE_FMT_LOCAL:
1033 4770631 : error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1034 4769131 : break;
1035 4763381 : case XFS_DINODE_FMT_EXTENTS:
1036 0 : error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1037 : break;
1038 4763381 : case XFS_DINODE_FMT_BTREE:
1039 4763568 : error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1040 4763568 : break;
1041 68925 : default:
1042 68925 : error = 0;
1043 68925 : break;
1044 4692401 : }
1045 4692401 : if (logflags)
1046 4692401 : xfs_trans_log_inode(tp, ip, logflags);
1047 2235 : if (error)
1048 2235 : return error;
1049 2235 : if (!xfs_has_attr(mp) ||
1050 : (!xfs_has_attr2(mp) && version == 2)) {
1051 : bool log_sb = false;
1052 :
1053 : spin_lock(&mp->m_sb_lock);
1054 4763293 : if (!xfs_has_attr(mp)) {
1055 3852 : xfs_add_attr(mp);
1056 4763284 : log_sb = true;
1057 0 : }
1058 4763284 : if (!xfs_has_attr2(mp) && version == 2) {
1059 0 : xfs_add_attr2(mp);
1060 0 : log_sb = true;
1061 : }
1062 0 : spin_unlock(&mp->m_sb_lock);
1063 0 : if (log_sb)
1064 0 : xfs_log_sb(tp);
1065 0 : }
1066 :
1067 0 : return 0;
1068 0 : }
1069 0 :
1070 : /*
1071 0 : * Internal and external extent tree search functions.
1072 0 : */
1073 0 :
1074 : struct xfs_iread_state {
1075 : struct xfs_iext_cursor icur;
1076 4763284 : xfs_extnum_t loaded;
1077 4771142 : };
1078 4771142 :
1079 : int
1080 4523643 : xfs_bmap_complain_bad_rec(
1081 4523643 : struct xfs_inode *ip,
1082 4521776 : int whichfork,
1083 4521776 : xfs_failaddr_t fa,
1084 : const struct xfs_bmbt_irec *irec)
1085 : {
1086 : struct xfs_mount *mp = ip->i_mount;
1087 : const char *forkname;
1088 :
1089 : switch (whichfork) {
1090 : case XFS_DATA_FORK: forkname = "data"; break;
1091 : case XFS_ATTR_FORK: forkname = "attr"; break;
1092 : case XFS_COW_FORK: forkname = "CoW"; break;
1093 : default: forkname = "???"; break;
1094 : }
1095 :
1096 0 : xfs_warn(mp,
1097 : "Bmap BTree record corruption in inode 0x%llx %s fork detected at %pS!",
1098 : ip->i_ino, forkname, fa);
1099 : xfs_warn(mp,
1100 : "Offset 0x%llx, start block 0x%llx, block count 0x%llx state 0x%x",
1101 : irec->br_startoff, irec->br_startblock, irec->br_blockcount,
1102 0 : irec->br_state);
1103 0 :
1104 : return -EFSCORRUPTED;
1105 0 : }
1106 :
1107 : /* Stuff every bmbt record from this block into the incore extent map. */
1108 : static int
1109 : xfs_iread_bmbt_block(
1110 : struct xfs_btree_cur *cur,
1111 : int level,
1112 0 : void *priv)
1113 : {
1114 : struct xfs_iread_state *ir = priv;
1115 0 : struct xfs_mount *mp = cur->bc_mp;
1116 : struct xfs_inode *ip = cur->bc_ino.ip;
1117 : struct xfs_btree_block *block;
1118 : struct xfs_buf *bp;
1119 : struct xfs_bmbt_rec *frp;
1120 0 : xfs_extnum_t num_recs;
1121 : xfs_extnum_t j;
1122 : int whichfork = cur->bc_ino.whichfork;
1123 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1124 :
1125 5008219 : block = xfs_btree_get_block(cur, level, &bp);
1126 :
1127 : /* Abort if we find more records than nextents. */
1128 : num_recs = xfs_btree_get_numrecs(block);
1129 : if (unlikely(ir->loaded + num_recs > ifp->if_nextents)) {
1130 5008219 : xfs_warn(ip->i_mount, "corrupt dinode %llu, (btree extents).",
1131 5008219 : (unsigned long long)ip->i_ino);
1132 5008219 : xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, block,
1133 5008219 : sizeof(*block), __this_address);
1134 5008219 : xfs_bmap_mark_sick(ip, whichfork);
1135 5008219 : return -EFSCORRUPTED;
1136 5008219 : }
1137 5008219 :
1138 5008219 : /* Copy records into the incore cache. */
1139 5008219 : frp = xfs_bmbt_rec_addr(mp, block, 1);
1140 : for (j = 0; j < num_recs; j++, frp++, ir->loaded++) {
1141 5008191 : struct xfs_bmbt_irec new;
1142 : xfs_failaddr_t fa;
1143 :
1144 5008192 : xfs_bmbt_disk_get_all(frp, &new);
1145 5008192 : fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1146 0 : if (fa) {
1147 : xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1148 0 : "xfs_iread_extents(2)", frp,
1149 0 : sizeof(*frp), fa);
1150 0 : xfs_bmap_mark_sick(ip, whichfork);
1151 0 : return xfs_bmap_complain_bad_rec(ip, whichfork, fa,
1152 : &new);
1153 : }
1154 : xfs_iext_insert(ip, &ir->icur, &new,
1155 5008192 : xfs_bmap_fork_to_state(whichfork));
1156 1010791195 : trace_xfs_read_extent(ip, &ir->icur,
1157 1005782901 : xfs_bmap_fork_to_state(whichfork), _THIS_IP_);
1158 1005782901 : xfs_iext_next(ifp, &ir->icur);
1159 : }
1160 1005782901 :
1161 1005785998 : return 0;
1162 1005791278 : }
1163 0 :
1164 : /*
1165 : * Read in extents from a btree-format inode.
1166 0 : */
1167 0 : int
1168 : xfs_iread_extents(
1169 : struct xfs_trans *tp,
1170 2011568135 : struct xfs_inode *ip,
1171 : int whichfork)
1172 1005788485 : {
1173 1005788485 : struct xfs_iread_state ir;
1174 1005786234 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1175 : struct xfs_mount *mp = ip->i_mount;
1176 : struct xfs_btree_cur *cur;
1177 : int error;
1178 :
1179 : if (!xfs_need_iread_extents(ifp))
1180 : return 0;
1181 :
1182 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1183 :
1184 16459152654 : ir.loaded = 0;
1185 : xfs_iext_first(ifp, &ir.icur);
1186 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1187 : error = xfs_btree_visit_blocks(cur, xfs_iread_bmbt_block,
1188 : XFS_BTREE_VISIT_RECORDS, &ir);
1189 16459152654 : xfs_btree_del_cursor(cur, error);
1190 16459152654 : if (error)
1191 16450652579 : goto out;
1192 16450652579 :
1193 16450652579 : if (XFS_IS_CORRUPT(mp, ir.loaded != ifp->if_nextents)) {
1194 : xfs_bmap_mark_sick(ip, whichfork);
1195 16450652579 : error = -EFSCORRUPTED;
1196 : goto out;
1197 : }
1198 227014 : ASSERT(ir.loaded == xfs_iext_count(ifp));
1199 : /*
1200 226998 : * Use release semantics so that we can use acquire semantics in
1201 226998 : * xfs_need_iread_extents and be guaranteed to see a valid mapping tree
1202 227002 : * after that load.
1203 227062 : */
1204 : smp_store_release(&ifp->if_needextents, 0);
1205 227061 : return 0;
1206 227057 : out:
1207 234 : if (xfs_metadata_is_sick(error))
1208 : xfs_bmap_mark_sick(ip, whichfork);
1209 226823 : xfs_iext_destroy(ifp);
1210 0 : return error;
1211 0 : }
1212 0 :
1213 : /*
1214 226823 : * Returns the relative block number of the first unused block(s) in the given
1215 : * fork with at least "len" logically contiguous blocks free. This is the
1216 : * lowest-address hole if the fork has holes, else the first block past the end
1217 : * of fork. Return 0 if the fork is currently local (in-inode).
1218 : */
1219 : int /* error */
1220 226811 : xfs_bmap_first_unused(
1221 226811 : struct xfs_trans *tp, /* transaction pointer */
1222 234 : struct xfs_inode *ip, /* incore inode */
1223 234 : xfs_extlen_t len, /* size of hole to find */
1224 10 : xfs_fileoff_t *first_unused, /* unused block */
1225 234 : int whichfork) /* data or attr fork */
1226 234 : {
1227 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1228 : struct xfs_bmbt_irec got;
1229 : struct xfs_iext_cursor icur;
1230 : xfs_fileoff_t lastaddr = 0;
1231 : xfs_fileoff_t lowest, max;
1232 : int error;
1233 :
1234 : if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
1235 : *first_unused = 0;
1236 9501426 : return 0;
1237 : }
1238 :
1239 : ASSERT(xfs_ifork_has_extents(ifp));
1240 :
1241 : error = xfs_iread_extents(tp, ip, whichfork);
1242 : if (error)
1243 9501426 : return error;
1244 9500655 :
1245 9500655 : lowest = max = *first_unused;
1246 9500655 : for_each_xfs_iext(ifp, &icur, &got) {
1247 9500655 : /*
1248 9500655 : * See if the hole before this extent will work.
1249 : */
1250 9500655 : if (got.br_startoff >= lowest + len &&
1251 0 : got.br_startoff - max >= len)
1252 0 : break;
1253 : lastaddr = got.br_startoff + got.br_blockcount;
1254 : max = XFS_FILEOFF_MAX(lastaddr, lowest);
1255 9500655 : }
1256 :
1257 9500655 : *first_unused = max;
1258 9501290 : return 0;
1259 : }
1260 :
1261 9501416 : /*
1262 81452169 : * Returns the file-relative block number of the last block - 1 before
1263 : * last_block (input value) in the file.
1264 : * This is not based on i_size, it is based on the extent records.
1265 : * Returns 0 for local files, as they do not have extent records.
1266 72253466 : */
1267 52439026 : int /* error */
1268 : xfs_bmap_last_before(
1269 71951543 : struct xfs_trans *tp, /* transaction pointer */
1270 71951543 : struct xfs_inode *ip, /* incore inode */
1271 : xfs_fileoff_t *last_block, /* last block */
1272 : int whichfork) /* data or attr fork */
1273 9500301 : {
1274 9500301 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1275 : struct xfs_bmbt_irec got;
1276 : struct xfs_iext_cursor icur;
1277 : int error;
1278 :
1279 : switch (ifp->if_format) {
1280 : case XFS_DINODE_FMT_LOCAL:
1281 : *last_block = 0;
1282 : return 0;
1283 : case XFS_DINODE_FMT_BTREE:
1284 507692 : case XFS_DINODE_FMT_EXTENTS:
1285 : break;
1286 : default:
1287 : ASSERT(0);
1288 : xfs_bmap_mark_sick(ip, whichfork);
1289 : return -EFSCORRUPTED;
1290 507692 : }
1291 507691 :
1292 507691 : error = xfs_iread_extents(tp, ip, whichfork);
1293 507691 : if (error)
1294 : return error;
1295 507691 :
1296 0 : if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1297 0 : *last_block = 0;
1298 0 : return 0;
1299 : }
1300 :
1301 507691 : int
1302 0 : xfs_bmap_last_extent(
1303 0 : struct xfs_trans *tp,
1304 0 : struct xfs_inode *ip,
1305 0 : int whichfork,
1306 : struct xfs_bmbt_irec *rec,
1307 : int *is_empty)
1308 507691 : {
1309 507692 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1310 : struct xfs_iext_cursor icur;
1311 : int error;
1312 507691 :
1313 451177 : error = xfs_iread_extents(tp, ip, whichfork);
1314 : if (error)
1315 : return error;
1316 :
1317 : xfs_iext_last(ifp, &icur);
1318 975641610 : if (!xfs_iext_get_extent(ifp, &icur, rec))
1319 : *is_empty = 1;
1320 : else
1321 : *is_empty = 0;
1322 : return 0;
1323 : }
1324 :
1325 975641610 : /*
1326 975785735 : * Check the last inode extent to determine whether this allocation will result
1327 975785735 : * in blocks being allocated at the end of the file. When we allocate new data
1328 : * blocks at the end of the file which do not start at the previous data block,
1329 975785735 : * we will try to align the new blocks at stripe unit boundaries.
1330 976924951 : *
1331 : * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1332 : * at, or past the EOF.
1333 977050365 : */
1334 979086207 : STATIC int
1335 50503093 : xfs_bmap_isaeof(
1336 : struct xfs_bmalloca *bma,
1337 927519777 : int whichfork)
1338 : {
1339 : struct xfs_bmbt_irec rec;
1340 : int is_empty;
1341 : int error;
1342 :
1343 : bma->aeof = false;
1344 : error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1345 : &is_empty);
1346 : if (error)
1347 : return error;
1348 :
1349 : if (is_empty) {
1350 : bma->aeof = true;
1351 1040890 : return 0;
1352 : }
1353 :
1354 : /*
1355 1040890 : * Check if we are allocation or past the last extent, or at least into
1356 1040890 : * the last delayed allocated extent.
1357 1040890 : */
1358 : bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1359 1040890 : (bma->offset >= rec.br_startoff &&
1360 1040890 : isnullstartblock(rec.br_startblock));
1361 : return 0;
1362 1041238 : }
1363 :
1364 : /*
1365 1041238 : * Returns the file-relative block number of the first block past eof in
1366 43687 : * the file. This is not based on i_size, it is based on the extent records.
1367 43687 : * Returns 0 for local files, as they do not have extent records.
1368 : */
1369 : int
1370 : xfs_bmap_last_offset(
1371 : struct xfs_inode *ip,
1372 : xfs_fileoff_t *last_block,
1373 : int whichfork)
1374 997551 : {
1375 242764 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
1376 242764 : struct xfs_bmbt_irec rec;
1377 997551 : int is_empty;
1378 : int error;
1379 :
1380 : *last_block = 0;
1381 :
1382 : if (ifp->if_format == XFS_DINODE_FMT_LOCAL)
1383 : return 0;
1384 :
1385 : if (XFS_IS_CORRUPT(ip->i_mount, !xfs_ifork_has_extents(ifp))) {
1386 973023792 : xfs_bmap_mark_sick(ip, whichfork);
1387 : return -EFSCORRUPTED;
1388 : }
1389 :
1390 : error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1391 973023792 : if (error || is_empty)
1392 972352112 : return error;
1393 972352112 :
1394 972352112 : *last_block = rec.br_startoff + rec.br_blockcount;
1395 : return 0;
1396 972352112 : }
1397 :
1398 972352112 : /*
1399 : * Extent tree manipulation functions used during allocation.
1400 : */
1401 972352112 :
1402 0 : /*
1403 0 : * Convert a delayed allocation to a real allocation.
1404 : */
1405 : STATIC int /* error */
1406 972352112 : xfs_bmap_add_extent_delay_real(
1407 975278018 : struct xfs_bmalloca *bma,
1408 : int whichfork)
1409 : {
1410 924814269 : struct xfs_mount *mp = bma->ip->i_mount;
1411 924814269 : struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
1412 : struct xfs_bmbt_irec *new = &bma->got;
1413 : int error; /* error return value */
1414 : int i; /* temp state */
1415 : xfs_fileoff_t new_endoff; /* end offset of new entry */
1416 : xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1417 : /* left is 0, right is 1, prev is 2 */
1418 : int rval=0; /* return value (logging flags) */
1419 : uint32_t state = xfs_bmap_fork_to_state(whichfork);
1420 : xfs_filblks_t da_new; /* new count del alloc blocks used */
1421 : xfs_filblks_t da_old; /* old count del alloc blocks used */
1422 29293986 : xfs_filblks_t temp=0; /* value for da_new calculations */
1423 : int tmp_rval; /* partial logging flags */
1424 : struct xfs_bmbt_irec old;
1425 :
1426 29293986 : ASSERT(whichfork != XFS_ATTR_FORK);
1427 29293986 : ASSERT(!isnullstartblock(new->br_startblock));
1428 29293601 : ASSERT(!bma->cur ||
1429 29293601 : (bma->cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
1430 29293601 :
1431 29293601 : XFS_STATS_INC(mp, xs_add_exlist);
1432 29293601 :
1433 : #define LEFT r[0]
1434 29293601 : #define RIGHT r[1]
1435 29293601 : #define PREV r[2]
1436 29293601 :
1437 29293601 : /*
1438 29293601 : * Set up a bunch of variables to make the tests simpler.
1439 29293601 : */
1440 29293601 : xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1441 : new_endoff = new->br_startoff + new->br_blockcount;
1442 29293601 : ASSERT(isnullstartblock(PREV.br_startblock));
1443 29293601 : ASSERT(PREV.br_startoff <= new->br_startoff);
1444 29293601 : ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1445 :
1446 : da_old = startblockval(PREV.br_startblock);
1447 29293601 : da_new = 0;
1448 :
1449 : /*
1450 : * Set flags determining what part of the previous delayed allocation
1451 : * extent is being replaced by a real allocation.
1452 : */
1453 : if (PREV.br_startoff == new->br_startoff)
1454 : state |= BMAP_LEFT_FILLING;
1455 : if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1456 29293763 : state |= BMAP_RIGHT_FILLING;
1457 29293898 :
1458 29293898 : /*
1459 29293898 : * Check and set flags if this segment has a left neighbor.
1460 29293898 : * Don't set contiguous if the combined extent would be too large.
1461 : */
1462 29293898 : if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1463 29293898 : state |= BMAP_LEFT_VALID;
1464 : if (isnullstartblock(LEFT.br_startblock))
1465 : state |= BMAP_LEFT_DELAY;
1466 : }
1467 :
1468 : if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1469 29293898 : LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1470 29293919 : LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1471 29293898 : LEFT.br_state == new->br_state &&
1472 26915176 : LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
1473 : state |= BMAP_LEFT_CONTIG;
1474 :
1475 : /*
1476 : * Check and set flags if this segment has a right neighbor.
1477 : * Don't set contiguous if the combined extent would be too large.
1478 29293898 : * Also check for all-three-contiguous being too large.
1479 21841048 : */
1480 21841048 : if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1481 152058 : state |= BMAP_RIGHT_VALID;
1482 : if (isnullstartblock(RIGHT.br_startblock))
1483 : state |= BMAP_RIGHT_DELAY;
1484 29293950 : }
1485 21689006 :
1486 4891128 : if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1487 597933 : new_endoff == RIGHT.br_startoff &&
1488 389236 : new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1489 370077 : new->br_state == RIGHT.br_state &&
1490 : new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
1491 : ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1492 : BMAP_RIGHT_FILLING)) !=
1493 : (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1494 : BMAP_RIGHT_FILLING) ||
1495 : LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1496 29293950 : <= XFS_MAX_BMBT_EXTLEN))
1497 15705316 : state |= BMAP_RIGHT_CONTIG;
1498 15705316 :
1499 4913615 : error = 0;
1500 : /*
1501 : * Switch out based on the FILLING and CONTIG state bits.
1502 29293697 : */
1503 10791785 : switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1504 2280995 : BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1505 135265 : case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1506 79918 : BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1507 79918 : /*
1508 : * Filling in all of a previously delayed allocation extent.
1509 : * The left and right neighbors are both contiguous with new.
1510 48127 : */
1511 48127 : LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1512 :
1513 79918 : xfs_iext_remove(bma->ip, &bma->icur, state);
1514 : xfs_iext_remove(bma->ip, &bma->icur, state);
1515 29293697 : xfs_iext_prev(ifp, &bma->icur);
1516 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1517 : ifp->if_nextents--;
1518 :
1519 29293697 : if (bma->cur == NULL)
1520 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1521 48127 : else {
1522 : rval = XFS_ILOG_CORE;
1523 : error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1524 : if (error)
1525 : goto done;
1526 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1527 48127 : xfs_btree_mark_sick(bma->cur);
1528 : error = -EFSCORRUPTED;
1529 48127 : goto done;
1530 48127 : }
1531 48127 : error = xfs_btree_delete(bma->cur, &i);
1532 48127 : if (error)
1533 48127 : goto done;
1534 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1535 48127 : xfs_btree_mark_sick(bma->cur);
1536 : error = -EFSCORRUPTED;
1537 : goto done;
1538 21589 : }
1539 21589 : error = xfs_btree_decrement(bma->cur, 0, &i);
1540 21589 : if (error)
1541 0 : goto done;
1542 21589 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1543 0 : xfs_btree_mark_sick(bma->cur);
1544 0 : error = -EFSCORRUPTED;
1545 0 : goto done;
1546 : }
1547 21589 : error = xfs_bmbt_update(bma->cur, &LEFT);
1548 21589 : if (error)
1549 0 : goto done;
1550 21589 : }
1551 0 : break;
1552 0 :
1553 0 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1554 : /*
1555 21589 : * Filling in all of a previously delayed allocation extent.
1556 21589 : * The left neighbor is contiguous, the right is not.
1557 0 : */
1558 21589 : old = LEFT;
1559 0 : LEFT.br_blockcount += PREV.br_blockcount;
1560 0 :
1561 0 : xfs_iext_remove(bma->ip, &bma->icur, state);
1562 : xfs_iext_prev(ifp, &bma->icur);
1563 21589 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1564 21589 :
1565 0 : if (bma->cur == NULL)
1566 : rval = XFS_ILOG_DEXT;
1567 : else {
1568 : rval = 0;
1569 198700 : error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1570 : if (error)
1571 : goto done;
1572 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1573 : xfs_btree_mark_sick(bma->cur);
1574 198700 : error = -EFSCORRUPTED;
1575 198700 : goto done;
1576 : }
1577 198700 : error = xfs_bmbt_update(bma->cur, &LEFT);
1578 198699 : if (error)
1579 198699 : goto done;
1580 : }
1581 198699 : break;
1582 :
1583 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1584 69691 : /*
1585 69691 : * Filling in all of a previously delayed allocation extent.
1586 69691 : * The right neighbor is contiguous, the left is not. Take care
1587 0 : * with delay -> unwritten extent allocation here because the
1588 69691 : * delalloc record we are overwriting is always written.
1589 0 : */
1590 0 : PREV.br_startblock = new->br_startblock;
1591 0 : PREV.br_blockcount += RIGHT.br_blockcount;
1592 : PREV.br_state = new->br_state;
1593 69691 :
1594 69691 : xfs_iext_next(ifp, &bma->icur);
1595 0 : xfs_iext_remove(bma->ip, &bma->icur, state);
1596 : xfs_iext_prev(ifp, &bma->icur);
1597 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1598 :
1599 31791 : if (bma->cur == NULL)
1600 : rval = XFS_ILOG_DEXT;
1601 : else {
1602 : rval = 0;
1603 : error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1604 : if (error)
1605 : goto done;
1606 31791 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1607 31791 : xfs_btree_mark_sick(bma->cur);
1608 31791 : error = -EFSCORRUPTED;
1609 : goto done;
1610 31791 : }
1611 31791 : error = xfs_bmbt_update(bma->cur, &PREV);
1612 31791 : if (error)
1613 31791 : goto done;
1614 : }
1615 31791 : break;
1616 :
1617 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1618 14078 : /*
1619 14078 : * Filling in all of a previously delayed allocation extent.
1620 14078 : * Neither the left nor right neighbors are contiguous with
1621 0 : * the new one.
1622 14078 : */
1623 0 : PREV.br_startblock = new->br_startblock;
1624 0 : PREV.br_state = new->br_state;
1625 0 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1626 : ifp->if_nextents++;
1627 14078 :
1628 14078 : if (bma->cur == NULL)
1629 0 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1630 : else {
1631 : rval = XFS_ILOG_CORE;
1632 : error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1633 26636433 : if (error)
1634 : goto done;
1635 : if (XFS_IS_CORRUPT(mp, i != 0)) {
1636 : xfs_btree_mark_sick(bma->cur);
1637 : error = -EFSCORRUPTED;
1638 : goto done;
1639 26636433 : }
1640 26636433 : error = xfs_btree_insert(bma->cur, &i);
1641 26636433 : if (error)
1642 26636499 : goto done;
1643 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1644 26636499 : xfs_btree_mark_sick(bma->cur);
1645 : error = -EFSCORRUPTED;
1646 : goto done;
1647 7769070 : }
1648 7769070 : }
1649 7769094 : break;
1650 2 :
1651 7769092 : case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1652 0 : /*
1653 0 : * Filling in the first part of a previous delayed allocation.
1654 0 : * The left neighbor is contiguous.
1655 : */
1656 7769092 : old = LEFT;
1657 7769079 : temp = PREV.br_blockcount - new->br_blockcount;
1658 0 : da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1659 7769079 : startblockval(PREV.br_startblock));
1660 0 :
1661 0 : LEFT.br_blockcount += new->br_blockcount;
1662 0 :
1663 : PREV.br_blockcount = temp;
1664 : PREV.br_startoff += new->br_blockcount;
1665 : PREV.br_startblock = nullstartblock(da_new);
1666 :
1667 123250 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1668 : xfs_iext_prev(ifp, &bma->icur);
1669 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1670 :
1671 : if (bma->cur == NULL)
1672 123250 : rval = XFS_ILOG_DEXT;
1673 123250 : else {
1674 123250 : rval = 0;
1675 : error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1676 : if (error)
1677 123250 : goto done;
1678 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1679 123250 : xfs_btree_mark_sick(bma->cur);
1680 123250 : error = -EFSCORRUPTED;
1681 123250 : goto done;
1682 : }
1683 123250 : error = xfs_bmbt_update(bma->cur, &LEFT);
1684 123250 : if (error)
1685 123250 : goto done;
1686 : }
1687 123250 : break;
1688 :
1689 : case BMAP_LEFT_FILLING:
1690 84898 : /*
1691 84898 : * Filling in the first part of a previous delayed allocation.
1692 84898 : * The left neighbor is not contiguous.
1693 0 : */
1694 84898 : xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1695 0 : ifp->if_nextents++;
1696 0 :
1697 0 : if (bma->cur == NULL)
1698 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1699 84898 : else {
1700 84898 : rval = XFS_ILOG_CORE;
1701 0 : error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1702 : if (error)
1703 : goto done;
1704 : if (XFS_IS_CORRUPT(mp, i != 0)) {
1705 2255396 : xfs_btree_mark_sick(bma->cur);
1706 : error = -EFSCORRUPTED;
1707 : goto done;
1708 : }
1709 : error = xfs_btree_insert(bma->cur, &i);
1710 2255396 : if (error)
1711 2255456 : goto done;
1712 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1713 2255456 : xfs_btree_mark_sick(bma->cur);
1714 : error = -EFSCORRUPTED;
1715 : goto done;
1716 1389243 : }
1717 1389243 : }
1718 1389333 :
1719 0 : if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1720 1389333 : error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1721 0 : &bma->cur, 1, &tmp_rval, whichfork);
1722 0 : rval |= tmp_rval;
1723 0 : if (error)
1724 : goto done;
1725 1389333 : }
1726 1389207 :
1727 0 : temp = PREV.br_blockcount - new->br_blockcount;
1728 1389207 : da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1729 0 : startblockval(PREV.br_startblock) -
1730 0 : (bma->cur ? bma->cur->bc_ino.allocated : 0));
1731 0 :
1732 : PREV.br_startoff = new_endoff;
1733 : PREV.br_blockcount = temp;
1734 : PREV.br_startblock = nullstartblock(da_new);
1735 2255420 : xfs_iext_next(ifp, &bma->icur);
1736 13651 : xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1737 : xfs_iext_prev(ifp, &bma->icur);
1738 13651 : break;
1739 13651 :
1740 0 : case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1741 : /*
1742 : * Filling in the last part of a previous delayed allocation.
1743 2255218 : * The right neighbor is contiguous with the new allocation.
1744 2255218 : */
1745 : old = RIGHT;
1746 : RIGHT.br_startoff = new->br_startoff;
1747 : RIGHT.br_startblock = new->br_startblock;
1748 2255146 : RIGHT.br_blockcount += new->br_blockcount;
1749 2255146 :
1750 2255146 : if (bma->cur == NULL)
1751 2255039 : rval = XFS_ILOG_DEXT;
1752 2255262 : else {
1753 2255336 : rval = 0;
1754 2255336 : error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1755 : if (error)
1756 0 : goto done;
1757 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1758 : xfs_btree_mark_sick(bma->cur);
1759 : error = -EFSCORRUPTED;
1760 : goto done;
1761 0 : }
1762 0 : error = xfs_bmbt_update(bma->cur, &RIGHT);
1763 0 : if (error)
1764 0 : goto done;
1765 : }
1766 0 :
1767 : temp = PREV.br_blockcount - new->br_blockcount;
1768 : da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1769 0 : startblockval(PREV.br_startblock));
1770 0 :
1771 0 : PREV.br_blockcount = temp;
1772 0 : PREV.br_startblock = nullstartblock(da_new);
1773 0 :
1774 0 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1775 0 : xfs_iext_next(ifp, &bma->icur);
1776 0 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1777 : break;
1778 0 :
1779 0 : case BMAP_RIGHT_FILLING:
1780 0 : /*
1781 : * Filling in the last part of a previous delayed allocation.
1782 : * The right neighbor is not contiguous.
1783 0 : */
1784 0 : xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1785 : ifp->if_nextents++;
1786 :
1787 0 : if (bma->cur == NULL)
1788 0 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1789 : else {
1790 0 : rval = XFS_ILOG_CORE;
1791 0 : error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1792 0 : if (error)
1793 0 : goto done;
1794 : if (XFS_IS_CORRUPT(mp, i != 0)) {
1795 0 : xfs_btree_mark_sick(bma->cur);
1796 : error = -EFSCORRUPTED;
1797 : goto done;
1798 : }
1799 : error = xfs_btree_insert(bma->cur, &i);
1800 0 : if (error)
1801 0 : goto done;
1802 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1803 0 : xfs_btree_mark_sick(bma->cur);
1804 : error = -EFSCORRUPTED;
1805 : goto done;
1806 0 : }
1807 0 : }
1808 0 :
1809 0 : if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1810 0 : error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1811 0 : &bma->cur, 1, &tmp_rval, whichfork);
1812 0 : rval |= tmp_rval;
1813 0 : if (error)
1814 : goto done;
1815 0 : }
1816 0 :
1817 0 : temp = PREV.br_blockcount - new->br_blockcount;
1818 0 : da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1819 0 : startblockval(PREV.br_startblock) -
1820 0 : (bma->cur ? bma->cur->bc_ino.allocated : 0));
1821 0 :
1822 : PREV.br_startblock = nullstartblock(da_new);
1823 : PREV.br_blockcount = temp;
1824 : xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1825 0 : xfs_iext_next(ifp, &bma->icur);
1826 0 : break;
1827 :
1828 0 : case 0:
1829 0 : /*
1830 0 : * Filling in the middle part of a previous delayed allocation.
1831 : * Contiguity is impossible here.
1832 : * This case is avoided almost all the time.
1833 0 : *
1834 0 : * We start with a delayed allocation:
1835 : *
1836 : * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1837 : * PREV @ idx
1838 0 : *
1839 0 : * and we are allocating:
1840 0 : * +rrrrrrrrrrrrrrrrr+
1841 0 : * new
1842 0 : *
1843 : * and we set it up for insertion as:
1844 0 : * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1845 : * new
1846 : * PREV @ idx LEFT RIGHT
1847 : * inserted at idx + 1
1848 : */
1849 : old = PREV;
1850 :
1851 : /* LEFT is the new middle */
1852 : LEFT = *new;
1853 :
1854 : /* RIGHT is the new right */
1855 : RIGHT.br_state = PREV.br_state;
1856 : RIGHT.br_startoff = new_endoff;
1857 : RIGHT.br_blockcount =
1858 : PREV.br_startoff + PREV.br_blockcount - new_endoff;
1859 : RIGHT.br_startblock =
1860 : nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1861 : RIGHT.br_blockcount));
1862 :
1863 : /* truncate PREV */
1864 : PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1865 0 : PREV.br_startblock =
1866 : nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1867 : PREV.br_blockcount));
1868 0 : xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1869 :
1870 : xfs_iext_next(ifp, &bma->icur);
1871 0 : xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1872 0 : xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1873 0 : ifp->if_nextents++;
1874 0 :
1875 0 : if (bma->cur == NULL)
1876 0 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1877 : else {
1878 : rval = XFS_ILOG_CORE;
1879 : error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1880 0 : if (error)
1881 0 : goto done;
1882 0 : if (XFS_IS_CORRUPT(mp, i != 0)) {
1883 : xfs_btree_mark_sick(bma->cur);
1884 0 : error = -EFSCORRUPTED;
1885 : goto done;
1886 0 : }
1887 0 : error = xfs_btree_insert(bma->cur, &i);
1888 0 : if (error)
1889 0 : goto done;
1890 : if (XFS_IS_CORRUPT(mp, i != 1)) {
1891 0 : xfs_btree_mark_sick(bma->cur);
1892 : error = -EFSCORRUPTED;
1893 : goto done;
1894 0 : }
1895 0 : }
1896 0 :
1897 0 : if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1898 0 : error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1899 0 : &bma->cur, 1, &tmp_rval, whichfork);
1900 0 : rval |= tmp_rval;
1901 0 : if (error)
1902 : goto done;
1903 0 : }
1904 0 :
1905 0 : da_new = startblockval(PREV.br_startblock) +
1906 0 : startblockval(RIGHT.br_startblock);
1907 0 : break;
1908 0 :
1909 0 : case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1910 : case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1911 : case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1912 : case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1913 0 : case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1914 0 : case BMAP_LEFT_CONTIG:
1915 : case BMAP_RIGHT_CONTIG:
1916 0 : /*
1917 0 : * These cases are all impossible.
1918 0 : */
1919 : ASSERT(0);
1920 : }
1921 0 :
1922 0 : /* add reverse mapping unless caller opted out */
1923 0 : if (!(bma->flags & XFS_BMAPI_NORMAP))
1924 : xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1925 0 :
1926 : /* convert to a btree if necessary */
1927 : if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1928 : int tmp_logflags; /* partial log flag return val */
1929 :
1930 : ASSERT(bma->cur == NULL);
1931 : error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1932 : &bma->cur, da_old > 0, &tmp_logflags,
1933 : whichfork);
1934 : bma->logflags |= tmp_logflags;
1935 0 : if (error)
1936 : goto done;
1937 : }
1938 :
1939 29293817 : if (da_new != da_old)
1940 29293616 : xfs_mod_delalloc(mp, (int64_t)da_new - da_old);
1941 :
1942 : if (bma->cur) {
1943 29294016 : da_new += bma->cur->bc_ino.allocated;
1944 490435 : bma->cur->bc_ino.allocated = 0;
1945 : }
1946 490435 :
1947 490435 : /* adjust for changes in reserved delayed indirect blocks */
1948 : if (da_new != da_old) {
1949 : ASSERT(state == 0 || da_new < da_old);
1950 490435 : error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
1951 490435 : false);
1952 30 : }
1953 :
1954 : xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1955 29293344 : done:
1956 26932991 : if (whichfork != XFS_COW_FORK)
1957 : bma->logflags |= rval;
1958 29293504 : return error;
1959 9852376 : #undef LEFT
1960 9852376 : #undef RIGHT
1961 : #undef PREV
1962 : }
1963 :
1964 29293504 : /*
1965 26916622 : * Convert an unwritten allocation to a real allocation or vice versa.
1966 26916622 : */
1967 : int /* error */
1968 : xfs_bmap_add_extent_unwritten_real(
1969 : struct xfs_trans *tp,
1970 29293577 : xfs_inode_t *ip, /* incore inode pointer */
1971 29293925 : int whichfork,
1972 29293925 : struct xfs_iext_cursor *icur,
1973 25625759 : struct xfs_btree_cur **curp, /* if *curp is null, not a btree */
1974 29293925 : xfs_bmbt_irec_t *new, /* new data to add to file extents */
1975 : int *logflagsp) /* inode logging flags */
1976 : {
1977 : struct xfs_btree_cur *cur; /* btree cursor */
1978 : int error; /* error return value */
1979 : int i; /* temp state */
1980 : struct xfs_ifork *ifp; /* inode fork pointer */
1981 : xfs_fileoff_t new_endoff; /* end offset of new entry */
1982 : xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1983 : /* left is 0, right is 1, prev is 2 */
1984 91332761 : int rval=0; /* return value (logging flags) */
1985 : uint32_t state = xfs_bmap_fork_to_state(whichfork);
1986 : struct xfs_mount *mp = ip->i_mount;
1987 : struct xfs_bmbt_irec old;
1988 :
1989 : *logflagsp = 0;
1990 :
1991 : cur = *curp;
1992 : ifp = xfs_ifork_ptr(ip, whichfork);
1993 91332761 :
1994 91332761 : ASSERT(!isnullstartblock(new->br_startblock));
1995 91332761 :
1996 91332761 : XFS_STATS_INC(mp, xs_add_exlist);
1997 91332761 :
1998 91332761 : #define LEFT r[0]
1999 : #define RIGHT r[1]
2000 91332761 : #define PREV r[2]
2001 91332761 :
2002 91332761 : /*
2003 91332761 : * Set up a bunch of variables to make the tests simpler.
2004 : */
2005 91332761 : error = 0;
2006 : xfs_iext_get_extent(ifp, icur, &PREV);
2007 91332761 : ASSERT(new->br_state != PREV.br_state);
2008 91332761 : new_endoff = new->br_startoff + new->br_blockcount;
2009 : ASSERT(PREV.br_startoff <= new->br_startoff);
2010 91331033 : ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2011 :
2012 91331033 : /*
2013 : * Set flags determining what part of the previous oldext allocation
2014 : * extent is being replaced by a newext allocation.
2015 : */
2016 : if (PREV.br_startoff == new->br_startoff)
2017 : state |= BMAP_LEFT_FILLING;
2018 : if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2019 : state |= BMAP_RIGHT_FILLING;
2020 :
2021 91331169 : /*
2022 91331169 : * Check and set flags if this segment has a left neighbor.
2023 91332849 : * Don't set contiguous if the combined extent would be too large.
2024 91332849 : */
2025 91332849 : if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2026 91332849 : state |= BMAP_LEFT_VALID;
2027 : if (isnullstartblock(LEFT.br_startblock))
2028 : state |= BMAP_LEFT_DELAY;
2029 : }
2030 :
2031 : if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2032 91332849 : LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2033 76016923 : LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2034 91332849 : LEFT.br_state == new->br_state &&
2035 69890772 : LEFT.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2036 : state |= BMAP_LEFT_CONTIG;
2037 :
2038 : /*
2039 : * Check and set flags if this segment has a right neighbor.
2040 : * Don't set contiguous if the combined extent would be too large.
2041 91332849 : * Also check for all-three-contiguous being too large.
2042 73663054 : */
2043 73663054 : if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2044 367338 : state |= BMAP_RIGHT_VALID;
2045 : if (isnullstartblock(RIGHT.br_startblock))
2046 : state |= BMAP_RIGHT_DELAY;
2047 91333103 : }
2048 73295752 :
2049 25425606 : if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2050 5028143 : new_endoff == RIGHT.br_startoff &&
2051 5008974 : new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2052 5008973 : new->br_state == RIGHT.br_state &&
2053 : new->br_blockcount + RIGHT.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2054 : ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2055 : BMAP_RIGHT_FILLING)) !=
2056 : (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2057 : BMAP_RIGHT_FILLING) ||
2058 : LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2059 91333103 : <= XFS_MAX_BMBT_EXTLEN))
2060 59054210 : state |= BMAP_RIGHT_CONTIG;
2061 59054210 :
2062 557305 : /*
2063 : * Switch out based on the FILLING and CONTIG state bits.
2064 : */
2065 91333547 : switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2066 58496989 : BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2067 23587327 : case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2068 3206796 : BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2069 3206761 : /*
2070 3206761 : * Setting all of a previous oldext extent to newext.
2071 : * The left and right neighbors are both contiguous with new.
2072 : */
2073 728212 : LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2074 728212 :
2075 : xfs_iext_remove(ip, icur, state);
2076 3206760 : xfs_iext_remove(ip, icur, state);
2077 : xfs_iext_prev(ifp, icur);
2078 : xfs_iext_update_extent(ip, state, icur, &LEFT);
2079 : ifp->if_nextents -= 2;
2080 : if (cur == NULL)
2081 91333547 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2082 : else {
2083 728211 : rval = XFS_ILOG_CORE;
2084 : error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2085 : if (error)
2086 : goto done;
2087 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2088 : xfs_btree_mark_sick(cur);
2089 728211 : error = -EFSCORRUPTED;
2090 : goto done;
2091 728211 : }
2092 728211 : if ((error = xfs_btree_delete(cur, &i)))
2093 728211 : goto done;
2094 728211 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2095 728211 : xfs_btree_mark_sick(cur);
2096 728211 : error = -EFSCORRUPTED;
2097 : goto done;
2098 : }
2099 514237 : if ((error = xfs_btree_decrement(cur, 0, &i)))
2100 514237 : goto done;
2101 514237 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2102 0 : xfs_btree_mark_sick(cur);
2103 514237 : error = -EFSCORRUPTED;
2104 0 : goto done;
2105 0 : }
2106 0 : if ((error = xfs_btree_delete(cur, &i)))
2107 : goto done;
2108 514237 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2109 0 : xfs_btree_mark_sick(cur);
2110 514237 : error = -EFSCORRUPTED;
2111 0 : goto done;
2112 0 : }
2113 0 : if ((error = xfs_btree_decrement(cur, 0, &i)))
2114 : goto done;
2115 514237 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2116 0 : xfs_btree_mark_sick(cur);
2117 514237 : error = -EFSCORRUPTED;
2118 0 : goto done;
2119 0 : }
2120 0 : error = xfs_bmbt_update(cur, &LEFT);
2121 : if (error)
2122 514237 : goto done;
2123 0 : }
2124 514237 : break;
2125 0 :
2126 0 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2127 0 : /*
2128 : * Setting all of a previous oldext extent to newext.
2129 514237 : * The left neighbor is contiguous, the right is not.
2130 0 : */
2131 514237 : LEFT.br_blockcount += PREV.br_blockcount;
2132 0 :
2133 0 : xfs_iext_remove(ip, icur, state);
2134 0 : xfs_iext_prev(ifp, icur);
2135 : xfs_iext_update_extent(ip, state, icur, &LEFT);
2136 514237 : ifp->if_nextents--;
2137 514237 : if (cur == NULL)
2138 0 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2139 : else {
2140 : rval = XFS_ILOG_CORE;
2141 : error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2142 2924135 : if (error)
2143 : goto done;
2144 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2145 : xfs_btree_mark_sick(cur);
2146 : error = -EFSCORRUPTED;
2147 2924135 : goto done;
2148 : }
2149 2924135 : if ((error = xfs_btree_delete(cur, &i)))
2150 2924134 : goto done;
2151 2924133 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2152 2924134 : xfs_btree_mark_sick(cur);
2153 2924134 : error = -EFSCORRUPTED;
2154 : goto done;
2155 : }
2156 1352656 : if ((error = xfs_btree_decrement(cur, 0, &i)))
2157 1352656 : goto done;
2158 1352657 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2159 0 : xfs_btree_mark_sick(cur);
2160 1352657 : error = -EFSCORRUPTED;
2161 0 : goto done;
2162 0 : }
2163 0 : error = xfs_bmbt_update(cur, &LEFT);
2164 : if (error)
2165 1352657 : goto done;
2166 0 : }
2167 1352654 : break;
2168 0 :
2169 0 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2170 0 : /*
2171 : * Setting all of a previous oldext extent to newext.
2172 1352654 : * The right neighbor is contiguous, the left is not.
2173 0 : */
2174 1352655 : PREV.br_blockcount += RIGHT.br_blockcount;
2175 0 : PREV.br_state = new->br_state;
2176 0 :
2177 0 : xfs_iext_next(ifp, icur);
2178 : xfs_iext_remove(ip, icur, state);
2179 1352655 : xfs_iext_prev(ifp, icur);
2180 1352655 : xfs_iext_update_extent(ip, state, icur, &PREV);
2181 0 : ifp->if_nextents--;
2182 :
2183 : if (cur == NULL)
2184 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2185 1446013 : else {
2186 : rval = XFS_ILOG_CORE;
2187 : error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2188 : if (error)
2189 : goto done;
2190 1446013 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2191 1446013 : xfs_btree_mark_sick(cur);
2192 : error = -EFSCORRUPTED;
2193 1446013 : goto done;
2194 1446012 : }
2195 1446013 : if ((error = xfs_btree_delete(cur, &i)))
2196 1446013 : goto done;
2197 1446013 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2198 : xfs_btree_mark_sick(cur);
2199 1446013 : error = -EFSCORRUPTED;
2200 : goto done;
2201 : }
2202 493811 : if ((error = xfs_btree_decrement(cur, 0, &i)))
2203 493811 : goto done;
2204 493811 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2205 0 : xfs_btree_mark_sick(cur);
2206 493811 : error = -EFSCORRUPTED;
2207 0 : goto done;
2208 0 : }
2209 0 : error = xfs_bmbt_update(cur, &PREV);
2210 : if (error)
2211 493811 : goto done;
2212 0 : }
2213 493811 : break;
2214 0 :
2215 0 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2216 0 : /*
2217 : * Setting all of a previous oldext extent to newext.
2218 493811 : * Neither the left nor right neighbors are contiguous with
2219 0 : * the new one.
2220 493809 : */
2221 0 : PREV.br_state = new->br_state;
2222 0 : xfs_iext_update_extent(ip, state, icur, &PREV);
2223 0 :
2224 : if (cur == NULL)
2225 493809 : rval = XFS_ILOG_DEXT;
2226 493810 : else {
2227 0 : rval = 0;
2228 : error = xfs_bmbt_lookup_eq(cur, new, &i);
2229 : if (error)
2230 : goto done;
2231 59612343 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2232 : xfs_btree_mark_sick(cur);
2233 : error = -EFSCORRUPTED;
2234 : goto done;
2235 : }
2236 : error = xfs_bmbt_update(cur, &PREV);
2237 59612343 : if (error)
2238 59612343 : goto done;
2239 : }
2240 59611284 : break;
2241 :
2242 : case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2243 29754839 : /*
2244 29754839 : * Setting the first part of a previous oldext extent to newext.
2245 29755221 : * The left neighbor is contiguous.
2246 0 : */
2247 29755221 : LEFT.br_blockcount += new->br_blockcount;
2248 0 :
2249 0 : old = PREV;
2250 0 : PREV.br_startoff += new->br_blockcount;
2251 : PREV.br_startblock += new->br_blockcount;
2252 29755221 : PREV.br_blockcount -= new->br_blockcount;
2253 29754857 :
2254 0 : xfs_iext_update_extent(ip, state, icur, &PREV);
2255 : xfs_iext_prev(ifp, icur);
2256 : xfs_iext_update_extent(ip, state, icur, &LEFT);
2257 :
2258 1356628 : if (cur == NULL)
2259 : rval = XFS_ILOG_DEXT;
2260 : else {
2261 : rval = 0;
2262 : error = xfs_bmbt_lookup_eq(cur, &old, &i);
2263 1356628 : if (error)
2264 : goto done;
2265 1356628 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2266 1356628 : xfs_btree_mark_sick(cur);
2267 1356628 : error = -EFSCORRUPTED;
2268 1356628 : goto done;
2269 : }
2270 1356628 : error = xfs_bmbt_update(cur, &PREV);
2271 1356628 : if (error)
2272 1356629 : goto done;
2273 : error = xfs_btree_decrement(cur, 0, &i);
2274 1356628 : if (error)
2275 : goto done;
2276 : error = xfs_bmbt_update(cur, &LEFT);
2277 515246 : if (error)
2278 515246 : goto done;
2279 515246 : }
2280 0 : break;
2281 515246 :
2282 0 : case BMAP_LEFT_FILLING:
2283 0 : /*
2284 0 : * Setting the first part of a previous oldext extent to newext.
2285 : * The left neighbor is not contiguous.
2286 515246 : */
2287 515246 : old = PREV;
2288 0 : PREV.br_startoff += new->br_blockcount;
2289 515246 : PREV.br_startblock += new->br_blockcount;
2290 515246 : PREV.br_blockcount -= new->br_blockcount;
2291 0 :
2292 515246 : xfs_iext_update_extent(ip, state, icur, &PREV);
2293 515246 : xfs_iext_insert(ip, icur, new, state);
2294 0 : ifp->if_nextents++;
2295 :
2296 : if (cur == NULL)
2297 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2298 9949290 : else {
2299 : rval = XFS_ILOG_CORE;
2300 : error = xfs_bmbt_lookup_eq(cur, &old, &i);
2301 : if (error)
2302 : goto done;
2303 9949290 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2304 9949290 : xfs_btree_mark_sick(cur);
2305 9949290 : error = -EFSCORRUPTED;
2306 9949290 : goto done;
2307 : }
2308 9949290 : error = xfs_bmbt_update(cur, &PREV);
2309 9949293 : if (error)
2310 9949291 : goto done;
2311 : cur->bc_rec.b = *new;
2312 9949291 : if ((error = xfs_btree_insert(cur, &i)))
2313 : goto done;
2314 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2315 1949783 : xfs_btree_mark_sick(cur);
2316 1949783 : error = -EFSCORRUPTED;
2317 1949783 : goto done;
2318 0 : }
2319 1949783 : }
2320 0 : break;
2321 0 :
2322 0 : case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2323 : /*
2324 1949783 : * Setting the last part of a previous oldext extent to newext.
2325 1949783 : * The right neighbor is contiguous with the new allocation.
2326 0 : */
2327 1949783 : old = PREV;
2328 1949783 : PREV.br_blockcount -= new->br_blockcount;
2329 0 :
2330 1949783 : RIGHT.br_startoff = new->br_startoff;
2331 0 : RIGHT.br_startblock = new->br_startblock;
2332 0 : RIGHT.br_blockcount += new->br_blockcount;
2333 0 :
2334 : xfs_iext_update_extent(ip, state, icur, &PREV);
2335 : xfs_iext_next(ifp, icur);
2336 : xfs_iext_update_extent(ip, state, icur, &RIGHT);
2337 :
2338 1032536 : if (cur == NULL)
2339 : rval = XFS_ILOG_DEXT;
2340 : else {
2341 : rval = 0;
2342 : error = xfs_bmbt_lookup_eq(cur, &old, &i);
2343 1032536 : if (error)
2344 1032536 : goto done;
2345 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2346 1032536 : xfs_btree_mark_sick(cur);
2347 1032536 : error = -EFSCORRUPTED;
2348 1032536 : goto done;
2349 : }
2350 1032536 : error = xfs_bmbt_update(cur, &PREV);
2351 1032536 : if (error)
2352 1032536 : goto done;
2353 : error = xfs_btree_increment(cur, 0, &i);
2354 1032536 : if (error)
2355 : goto done;
2356 : error = xfs_bmbt_update(cur, &RIGHT);
2357 565680 : if (error)
2358 565680 : goto done;
2359 565680 : }
2360 0 : break;
2361 565680 :
2362 0 : case BMAP_RIGHT_FILLING:
2363 0 : /*
2364 0 : * Setting the last part of a previous oldext extent to newext.
2365 : * The right neighbor is not contiguous.
2366 565680 : */
2367 565680 : old = PREV;
2368 0 : PREV.br_blockcount -= new->br_blockcount;
2369 565680 :
2370 565680 : xfs_iext_update_extent(ip, state, icur, &PREV);
2371 0 : xfs_iext_next(ifp, icur);
2372 565680 : xfs_iext_insert(ip, icur, new, state);
2373 565680 : ifp->if_nextents++;
2374 0 :
2375 : if (cur == NULL)
2376 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2377 : else {
2378 4147781 : rval = XFS_ILOG_CORE;
2379 : error = xfs_bmbt_lookup_eq(cur, &old, &i);
2380 : if (error)
2381 : goto done;
2382 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2383 4147781 : xfs_btree_mark_sick(cur);
2384 4147781 : error = -EFSCORRUPTED;
2385 : goto done;
2386 4147781 : }
2387 4147779 : error = xfs_bmbt_update(cur, &PREV);
2388 4147767 : if (error)
2389 4147780 : goto done;
2390 : error = xfs_bmbt_lookup_eq(cur, new, &i);
2391 4147780 : if (error)
2392 : goto done;
2393 : if (XFS_IS_CORRUPT(mp, i != 0)) {
2394 1208533 : xfs_btree_mark_sick(cur);
2395 1208533 : error = -EFSCORRUPTED;
2396 1208533 : goto done;
2397 0 : }
2398 1208533 : if ((error = xfs_btree_insert(cur, &i)))
2399 0 : goto done;
2400 0 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2401 0 : xfs_btree_mark_sick(cur);
2402 : error = -EFSCORRUPTED;
2403 1208533 : goto done;
2404 1208531 : }
2405 0 : }
2406 1208531 : break;
2407 1208533 :
2408 0 : case 0:
2409 1208533 : /*
2410 0 : * Setting the middle part of a previous oldext extent to
2411 0 : * newext. Contiguity is impossible here.
2412 0 : * One extent becomes three extents.
2413 : */
2414 1208533 : old = PREV;
2415 0 : PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2416 1208531 :
2417 0 : r[0] = *new;
2418 0 : r[1].br_startoff = new_endoff;
2419 0 : r[1].br_blockcount =
2420 : old.br_startoff + old.br_blockcount - new_endoff;
2421 : r[1].br_startblock = new->br_startblock + new->br_blockcount;
2422 : r[1].br_state = PREV.br_state;
2423 :
2424 10136610 : xfs_iext_update_extent(ip, state, icur, &PREV);
2425 : xfs_iext_next(ifp, icur);
2426 : xfs_iext_insert(ip, icur, &r[1], state);
2427 : xfs_iext_insert(ip, icur, &r[0], state);
2428 : ifp->if_nextents += 2;
2429 :
2430 10136610 : if (cur == NULL)
2431 10136610 : rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2432 : else {
2433 10136610 : rval = XFS_ILOG_CORE;
2434 10136610 : error = xfs_bmbt_lookup_eq(cur, &old, &i);
2435 10136610 : if (error)
2436 10136610 : goto done;
2437 10136610 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2438 10136610 : xfs_btree_mark_sick(cur);
2439 : error = -EFSCORRUPTED;
2440 10136610 : goto done;
2441 10136568 : }
2442 10136511 : /* new right extent - oldext */
2443 10136648 : error = xfs_bmbt_update(cur, &r[1]);
2444 10136674 : if (error)
2445 : goto done;
2446 10136674 : /* new left extent - oldext */
2447 : cur->bc_rec.b = PREV;
2448 : if ((error = xfs_btree_insert(cur, &i)))
2449 4104818 : goto done;
2450 4104818 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2451 4104814 : xfs_btree_mark_sick(cur);
2452 0 : error = -EFSCORRUPTED;
2453 4104814 : goto done;
2454 0 : }
2455 0 : /*
2456 0 : * Reset the cursor to the position of the new extent
2457 : * we are about to insert as we can't trust it after
2458 : * the previous insert.
2459 4104814 : */
2460 4104798 : error = xfs_bmbt_lookup_eq(cur, new, &i);
2461 0 : if (error)
2462 : goto done;
2463 4104798 : if (XFS_IS_CORRUPT(mp, i != 0)) {
2464 4104798 : xfs_btree_mark_sick(cur);
2465 0 : error = -EFSCORRUPTED;
2466 4104808 : goto done;
2467 0 : }
2468 0 : /* new middle extent - newext */
2469 0 : if ((error = xfs_btree_insert(cur, &i)))
2470 : goto done;
2471 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2472 : xfs_btree_mark_sick(cur);
2473 : error = -EFSCORRUPTED;
2474 : goto done;
2475 : }
2476 4104808 : }
2477 4104814 : break;
2478 0 :
2479 4104814 : case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2480 0 : case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2481 0 : case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2482 0 : case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2483 : case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2484 : case BMAP_LEFT_CONTIG:
2485 4104814 : case BMAP_RIGHT_CONTIG:
2486 0 : /*
2487 4104817 : * These cases are all impossible.
2488 0 : */
2489 0 : ASSERT(0);
2490 0 : }
2491 :
2492 : /* update reverse mappings */
2493 : xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2494 :
2495 0 : /* convert to a btree if necessary */
2496 : if (xfs_bmap_needs_btree(ip, whichfork)) {
2497 : int tmp_logflags; /* partial log flag return val */
2498 :
2499 : ASSERT(cur == NULL);
2500 : error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2501 : &tmp_logflags, whichfork);
2502 : *logflagsp |= tmp_logflags;
2503 : if (error)
2504 : goto done;
2505 0 : }
2506 :
2507 : /* clear out the allocated field, done with it now in any case. */
2508 : if (cur) {
2509 91332564 : cur->bc_ino.allocated = 0;
2510 : *curp = cur;
2511 : }
2512 91333343 :
2513 462199 : xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2514 : done:
2515 462199 : *logflagsp |= rval;
2516 462199 : return error;
2517 : #undef LEFT
2518 462199 : #undef RIGHT
2519 462199 : #undef PREV
2520 14 : }
2521 :
2522 : /*
2523 : * Convert a hole to a delayed allocation.
2524 91331103 : */
2525 40921441 : STATIC void
2526 40921441 : xfs_bmap_add_extent_hole_delay(
2527 : xfs_inode_t *ip, /* incore inode pointer */
2528 : int whichfork,
2529 91331103 : struct xfs_iext_cursor *icur,
2530 91331948 : xfs_bmbt_irec_t *new) /* new data to add to file extents */
2531 91331948 : {
2532 91331948 : struct xfs_ifork *ifp; /* inode fork pointer */
2533 : xfs_bmbt_irec_t left; /* left neighbor extent entry */
2534 : xfs_filblks_t newlen=0; /* new indirect size */
2535 : xfs_filblks_t oldlen=0; /* old indirect size */
2536 : xfs_bmbt_irec_t right; /* right neighbor extent entry */
2537 : uint32_t state = xfs_bmap_fork_to_state(whichfork);
2538 : xfs_filblks_t temp; /* temp for indirect calculations */
2539 :
2540 : ifp = xfs_ifork_ptr(ip, whichfork);
2541 : ASSERT(isnullstartblock(new->br_startblock));
2542 54927794 :
2543 : /*
2544 : * Check and set flags if this segment has a left neighbor
2545 : */
2546 : if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2547 : state |= BMAP_LEFT_VALID;
2548 54927794 : if (isnullstartblock(left.br_startblock))
2549 54927794 : state |= BMAP_LEFT_DELAY;
2550 54927794 : }
2551 54927794 :
2552 54927794 : /*
2553 54927794 : * Check and set flags if the current (right) segment exists.
2554 54927794 : * If it doesn't exist, we're converting the hole at end-of-file.
2555 : */
2556 54927794 : if (xfs_iext_get_extent(ifp, icur, &right)) {
2557 54817482 : state |= BMAP_RIGHT_VALID;
2558 : if (isnullstartblock(right.br_startblock))
2559 : state |= BMAP_RIGHT_DELAY;
2560 : }
2561 :
2562 54817482 : /*
2563 41709046 : * Set contiguity flags on the left and right neighbors.
2564 41709046 : * Don't let extents get too large, even if the pieces are contiguous.
2565 22886925 : */
2566 : if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2567 : left.br_startoff + left.br_blockcount == new->br_startoff &&
2568 : left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2569 : state |= BMAP_LEFT_CONTIG;
2570 :
2571 : if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2572 54866540 : new->br_startoff + new->br_blockcount == right.br_startoff &&
2573 27874417 : new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2574 27874417 : (!(state & BMAP_LEFT_CONTIG) ||
2575 8155679 : (left.br_blockcount + new->br_blockcount +
2576 : right.br_blockcount <= XFS_MAX_BMBT_EXTLEN)))
2577 : state |= BMAP_RIGHT_CONTIG;
2578 :
2579 : /*
2580 : * Switch out based on the contiguity flags.
2581 : */
2582 54898274 : switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2583 22885696 : case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2584 18636293 : /*
2585 18635478 : * New allocation is contiguous with delayed allocations
2586 : * on the left and on the right.
2587 54898274 : * Merge all three into a single extent record.
2588 8155678 : */
2589 3174348 : temp = left.br_blockcount + new->br_blockcount +
2590 3155016 : right.br_blockcount;
2591 61938 :
2592 : oldlen = startblockval(left.br_startblock) +
2593 3155016 : startblockval(new->br_startblock) +
2594 : startblockval(right.br_startblock);
2595 : newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2596 : oldlen);
2597 : left.br_startblock = nullstartblock(newlen);
2598 54898274 : left.br_blockcount = temp;
2599 61938 :
2600 : xfs_iext_remove(ip, icur, state);
2601 : xfs_iext_prev(ifp, icur);
2602 : xfs_iext_update_extent(ip, state, icur, &left);
2603 : break;
2604 :
2605 61938 : case BMAP_LEFT_CONTIG:
2606 61938 : /*
2607 : * New allocation is contiguous with a delayed allocation
2608 61938 : * on the left.
2609 61938 : * Merge the new allocation with the left neighbor.
2610 61938 : */
2611 61938 : temp = left.br_blockcount + new->br_blockcount;
2612 :
2613 61938 : oldlen = startblockval(left.br_startblock) +
2614 61938 : startblockval(new->br_startblock);
2615 : newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2616 61938 : oldlen);
2617 61938 : left.br_blockcount = temp;
2618 61938 : left.br_startblock = nullstartblock(newlen);
2619 61938 :
2620 : xfs_iext_prev(ifp, icur);
2621 18564670 : xfs_iext_update_extent(ip, state, icur, &left);
2622 : break;
2623 :
2624 : case BMAP_RIGHT_CONTIG:
2625 : /*
2626 : * New allocation is contiguous with a delayed allocation
2627 18564670 : * on the right.
2628 : * Merge the new allocation with the right neighbor.
2629 18564670 : */
2630 18564670 : temp = new->br_blockcount + right.br_blockcount;
2631 18564670 : oldlen = startblockval(new->br_startblock) +
2632 : startblockval(right.br_startblock);
2633 18565498 : newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2634 18565498 : oldlen);
2635 : right.br_startoff = new->br_startoff;
2636 18560005 : right.br_startblock = nullstartblock(newlen);
2637 18570600 : right.br_blockcount = temp;
2638 18570600 : xfs_iext_update_extent(ip, state, icur, &right);
2639 : break;
2640 3093078 :
2641 : case 0:
2642 : /*
2643 : * New allocation is not contiguous with another
2644 : * delayed allocation.
2645 : * Insert a new entry.
2646 3093078 : */
2647 3093078 : oldlen = newlen = 0;
2648 3093078 : xfs_iext_insert(ip, icur, new, state);
2649 3093078 : break;
2650 : }
2651 3093079 : if (oldlen != newlen) {
2652 3093079 : ASSERT(oldlen > newlen);
2653 3093079 : xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2654 3093079 : false);
2655 3093079 : /*
2656 : * Nothing to do for disk quota accounting here.
2657 33178588 : */
2658 : xfs_mod_delalloc(ip->i_mount, (int64_t)newlen - oldlen);
2659 : }
2660 : }
2661 :
2662 : /*
2663 33178588 : * Convert a hole to a real allocation.
2664 33178588 : */
2665 33178588 : STATIC int /* error */
2666 : xfs_bmap_add_extent_hole_real(
2667 54870804 : struct xfs_trans *tp,
2668 21723259 : struct xfs_inode *ip,
2669 21723259 : int whichfork,
2670 : struct xfs_iext_cursor *icur,
2671 : struct xfs_btree_cur **curp,
2672 : struct xfs_bmbt_irec *new,
2673 : int *logflagsp,
2674 21729720 : uint32_t flags)
2675 : {
2676 54879762 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
2677 : struct xfs_mount *mp = ip->i_mount;
2678 : struct xfs_btree_cur *cur = *curp;
2679 : int error; /* error return value */
2680 : int i; /* temp state */
2681 : xfs_bmbt_irec_t left; /* left neighbor extent entry */
2682 309314264 : xfs_bmbt_irec_t right; /* right neighbor extent entry */
2683 : int rval=0; /* return value (logging flags) */
2684 : uint32_t state = xfs_bmap_fork_to_state(whichfork);
2685 : struct xfs_bmbt_irec old;
2686 :
2687 : ASSERT(!isnullstartblock(new->br_startblock));
2688 : ASSERT(!cur || !(cur->bc_ino.flags & XFS_BTCUR_BMBT_WASDEL));
2689 :
2690 : XFS_STATS_INC(mp, xs_add_exlist);
2691 :
2692 309314264 : /*
2693 309282985 : * Check and set flags if this segment has a left neighbor.
2694 309282985 : */
2695 309282985 : if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2696 309282985 : state |= BMAP_LEFT_VALID;
2697 309282985 : if (isnullstartblock(left.br_startblock))
2698 309282985 : state |= BMAP_LEFT_DELAY;
2699 309282985 : }
2700 309282985 :
2701 309282985 : /*
2702 : * Check and set flags if this segment has a current value.
2703 309282985 : * Not true if we're inserting into the "hole" at eof.
2704 309282985 : */
2705 : if (xfs_iext_get_extent(ifp, icur, &right)) {
2706 309282985 : state |= BMAP_RIGHT_VALID;
2707 : if (isnullstartblock(right.br_startblock))
2708 : state |= BMAP_RIGHT_DELAY;
2709 : }
2710 :
2711 309296573 : /*
2712 256237225 : * We're inserting a real allocation between "left" and "right".
2713 256237225 : * Set the contiguity flags. Don't let extents get too large.
2714 250713 : */
2715 : if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2716 : left.br_startoff + left.br_blockcount == new->br_startoff &&
2717 : left.br_startblock + left.br_blockcount == new->br_startblock &&
2718 : left.br_state == new->br_state &&
2719 : left.br_blockcount + new->br_blockcount <= XFS_MAX_BMBT_EXTLEN)
2720 : state |= BMAP_LEFT_CONTIG;
2721 309297316 :
2722 87107003 : if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2723 87107003 : new->br_startoff + new->br_blockcount == right.br_startoff &&
2724 478303 : new->br_startblock + new->br_blockcount == right.br_startblock &&
2725 : new->br_state == right.br_state &&
2726 : new->br_blockcount + right.br_blockcount <= XFS_MAX_BMBT_EXTLEN &&
2727 : (!(state & BMAP_LEFT_CONTIG) ||
2728 : left.br_blockcount + new->br_blockcount +
2729 : right.br_blockcount <= XFS_MAX_BMBT_EXTLEN))
2730 : state |= BMAP_RIGHT_CONTIG;
2731 309302751 :
2732 255985822 : error = 0;
2733 192248806 : /*
2734 51569583 : * Select which case we're in here, and implement it.
2735 49403557 : */
2736 49403429 : switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2737 : case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2738 309302751 : /*
2739 86627750 : * New allocation is contiguous with real allocations on the
2740 28922097 : * left and on the right.
2741 4769809 : * Merge all three into a single extent record.
2742 4413883 : */
2743 4413883 : left.br_blockcount += new->br_blockcount + right.br_blockcount;
2744 2045615 :
2745 : xfs_iext_remove(ip, icur, state);
2746 4413883 : xfs_iext_prev(ifp, icur);
2747 : xfs_iext_update_extent(ip, state, icur, &left);
2748 309302751 : ifp->if_nextents--;
2749 :
2750 : if (cur == NULL) {
2751 : rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2752 309302751 : } else {
2753 2045615 : rval = XFS_ILOG_CORE;
2754 : error = xfs_bmbt_lookup_eq(cur, &right, &i);
2755 : if (error)
2756 : goto done;
2757 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2758 : xfs_btree_mark_sick(cur);
2759 2045615 : error = -EFSCORRUPTED;
2760 : goto done;
2761 2045615 : }
2762 2045615 : error = xfs_btree_delete(cur, &i);
2763 2045615 : if (error)
2764 2045615 : goto done;
2765 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2766 2045615 : xfs_btree_mark_sick(cur);
2767 426503 : error = -EFSCORRUPTED;
2768 : goto done;
2769 1623652 : }
2770 1623652 : error = xfs_btree_decrement(cur, 0, &i);
2771 1623652 : if (error)
2772 0 : goto done;
2773 1623652 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2774 0 : xfs_btree_mark_sick(cur);
2775 0 : error = -EFSCORRUPTED;
2776 0 : goto done;
2777 : }
2778 1623652 : error = xfs_bmbt_update(cur, &left);
2779 1623652 : if (error)
2780 0 : goto done;
2781 1623652 : }
2782 0 : break;
2783 0 :
2784 0 : case BMAP_LEFT_CONTIG:
2785 : /*
2786 1623652 : * New allocation is contiguous with a real allocation
2787 1623652 : * on the left.
2788 0 : * Merge the new allocation with the left neighbor.
2789 1623652 : */
2790 0 : old = left;
2791 0 : left.br_blockcount += new->br_blockcount;
2792 0 :
2793 : xfs_iext_prev(ifp, icur);
2794 1623652 : xfs_iext_update_extent(ip, state, icur, &left);
2795 1623652 :
2796 0 : if (cur == NULL) {
2797 : rval = xfs_ilog_fext(whichfork);
2798 : } else {
2799 : rval = 0;
2800 47357815 : error = xfs_bmbt_lookup_eq(cur, &old, &i);
2801 : if (error)
2802 : goto done;
2803 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2804 : xfs_btree_mark_sick(cur);
2805 : error = -EFSCORRUPTED;
2806 47357815 : goto done;
2807 47357815 : }
2808 : error = xfs_bmbt_update(cur, &left);
2809 47357815 : if (error)
2810 47357810 : goto done;
2811 : }
2812 47357815 : break;
2813 35000403 :
2814 : case BMAP_RIGHT_CONTIG:
2815 12357412 : /*
2816 12357412 : * New allocation is contiguous with a real allocation
2817 12357415 : * on the right.
2818 0 : * Merge the new allocation with the right neighbor.
2819 12357415 : */
2820 0 : old = right;
2821 0 :
2822 0 : right.br_startoff = new->br_startoff;
2823 : right.br_startblock = new->br_startblock;
2824 12357415 : right.br_blockcount += new->br_blockcount;
2825 12357410 : xfs_iext_update_extent(ip, state, icur, &right);
2826 0 :
2827 : if (cur == NULL) {
2828 : rval = xfs_ilog_fext(whichfork);
2829 : } else {
2830 2368268 : rval = 0;
2831 : error = xfs_bmbt_lookup_eq(cur, &old, &i);
2832 : if (error)
2833 : goto done;
2834 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2835 : xfs_btree_mark_sick(cur);
2836 2368268 : error = -EFSCORRUPTED;
2837 : goto done;
2838 2368268 : }
2839 2368268 : error = xfs_bmbt_update(cur, &right);
2840 2368268 : if (error)
2841 2368268 : goto done;
2842 : }
2843 2368268 : break;
2844 1390217 :
2845 : case 0:
2846 978051 : /*
2847 978051 : * New allocation is not contiguous with another
2848 978051 : * real allocation.
2849 0 : * Insert a new entry.
2850 978051 : */
2851 0 : xfs_iext_insert(ip, icur, new, state);
2852 0 : ifp->if_nextents++;
2853 0 :
2854 : if (cur == NULL) {
2855 978051 : rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2856 978051 : } else {
2857 0 : rval = XFS_ILOG_CORE;
2858 : error = xfs_bmbt_lookup_eq(cur, new, &i);
2859 : if (error)
2860 : goto done;
2861 257531053 : if (XFS_IS_CORRUPT(mp, i != 0)) {
2862 : xfs_btree_mark_sick(cur);
2863 : error = -EFSCORRUPTED;
2864 : goto done;
2865 : }
2866 : error = xfs_btree_insert(cur, &i);
2867 257531053 : if (error)
2868 257505047 : goto done;
2869 : if (XFS_IS_CORRUPT(mp, i != 1)) {
2870 257505047 : xfs_btree_mark_sick(cur);
2871 96383173 : error = -EFSCORRUPTED;
2872 : goto done;
2873 170768294 : }
2874 170768294 : }
2875 170771474 : break;
2876 1092 : }
2877 170770382 :
2878 0 : /* add reverse mapping unless caller opted out */
2879 0 : if (!(flags & XFS_BMAPI_NORMAP))
2880 0 : xfs_rmap_map_extent(tp, ip, whichfork, new);
2881 :
2882 170770382 : /* convert to a btree if necessary */
2883 170769080 : if (xfs_bmap_needs_btree(ip, whichfork)) {
2884 189 : int tmp_logflags; /* partial log flag return val */
2885 170768891 :
2886 0 : ASSERT(cur == NULL);
2887 0 : error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2888 0 : &tmp_logflags, whichfork);
2889 : *logflagsp |= tmp_logflags;
2890 : cur = *curp;
2891 : if (error)
2892 : goto done;
2893 : }
2894 :
2895 309277340 : /* clear out the allocated field, done with it now in any case. */
2896 309284564 : if (cur)
2897 : cur->bc_ino.allocated = 0;
2898 :
2899 309294559 : xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2900 2434025 : done:
2901 : *logflagsp |= rval;
2902 2434025 : return error;
2903 2434025 : }
2904 :
2905 2433998 : /*
2906 2433998 : * Functions used in the extent read, allocate and remove paths
2907 2433998 : */
2908 2 :
2909 : /*
2910 : * Adjust the size of the new extent based on i_extsize and rt extsize.
2911 : */
2912 309252699 : int
2913 188158509 : xfs_bmap_extsize_align(
2914 : xfs_mount_t *mp,
2915 309252699 : xfs_bmbt_irec_t *gotp, /* next extent pointer */
2916 309272654 : xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2917 309272654 : xfs_extlen_t extsz, /* align to this extent size */
2918 309272654 : int rt, /* is this a realtime inode? */
2919 : int eof, /* is extent at end-of-file? */
2920 : int delay, /* creating delalloc extent? */
2921 : int convert, /* overwriting unwritten extent? */
2922 : xfs_fileoff_t *offp, /* in/out: aligned offset */
2923 : xfs_extlen_t *lenp) /* in/out: aligned length */
2924 : {
2925 : xfs_fileoff_t orig_off; /* original offset */
2926 : xfs_extlen_t orig_alen; /* original length */
2927 : xfs_fileoff_t orig_end; /* original off+len */
2928 : xfs_fileoff_t nexto; /* next file offset */
2929 103873548 : xfs_fileoff_t prevo; /* previous file offset */
2930 : xfs_fileoff_t align_off; /* temp for offset */
2931 : xfs_extlen_t align_alen; /* temp for length */
2932 : xfs_extlen_t temp; /* temp for calculations */
2933 :
2934 : if (convert)
2935 : return 0;
2936 :
2937 : orig_off = align_off = *offp;
2938 : orig_alen = align_alen = *lenp;
2939 : orig_end = orig_off + orig_alen;
2940 :
2941 103873548 : /*
2942 103873548 : * If this request overlaps an existing extent, then don't
2943 103873548 : * attempt to perform any additional alignment.
2944 103873548 : */
2945 103873548 : if (!delay && !eof &&
2946 103873548 : (orig_off >= gotp->br_startoff) &&
2947 103873548 : (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2948 103873548 : return 0;
2949 : }
2950 103873548 :
2951 : /*
2952 : * If the file offset is unaligned vs. the extent size
2953 103873523 : * we need to align it. This will be possible unless
2954 103873523 : * the file was previously written with a kernel that didn't
2955 103873523 : * perform this alignment, or if a truncate shot us in the
2956 : * foot.
2957 : */
2958 : div_u64_rem(orig_off, extsz, &temp);
2959 : if (temp) {
2960 : align_alen += temp;
2961 103873523 : align_off -= temp;
2962 39909272 : }
2963 3668215 :
2964 : /* Same adjustment for the end of the requested area. */
2965 : temp = (align_alen % extsz);
2966 : if (temp)
2967 : align_alen += extsz - temp;
2968 :
2969 : /*
2970 : * For large extent hint sizes, the aligned extent might be larger than
2971 : * XFS_BMBT_MAX_EXTLEN. In that case, reduce the size by an extsz so
2972 : * that it pulls the length back under XFS_BMBT_MAX_EXTLEN. The outer
2973 : * allocation loops handle short allocation just fine, so it is safe to
2974 100205359 : * do this. We only want to do it when we are forced to, though, because
2975 100138631 : * it means more allocation operations are required.
2976 5972408 : */
2977 5972408 : while (align_alen > XFS_MAX_BMBT_EXTLEN)
2978 : align_alen -= extsz;
2979 : ASSERT(align_alen <= XFS_MAX_BMBT_EXTLEN);
2980 :
2981 100138631 : /*
2982 100138631 : * If the previous block overlaps with this proposed allocation
2983 7007256 : * then move the start forward without adjusting the length.
2984 : */
2985 : if (prevp->br_startoff != NULLFILEOFF) {
2986 : if (prevp->br_startblock == HOLESTARTBLOCK)
2987 : prevo = prevp->br_startoff;
2988 : else
2989 : prevo = prevp->br_startoff + prevp->br_blockcount;
2990 : } else
2991 : prevo = 0;
2992 : if (align_off != orig_off && align_off < prevo)
2993 100139294 : align_off = prevo;
2994 663 : /*
2995 100138631 : * If the next block overlaps with this proposed allocation
2996 : * then move the start back without adjusting the length,
2997 : * but not before offset 0.
2998 : * This may of course make the start overlap previous block,
2999 : * and if we hit the offset 0 limit then the next block
3000 : * can still overlap too.
3001 100138631 : */
3002 85967106 : if (!eof && gotp->br_startoff != NULLFILEOFF) {
3003 : if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3004 : (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3005 85967106 : nexto = gotp->br_startoff + gotp->br_blockcount;
3006 : else
3007 : nexto = gotp->br_startoff;
3008 100138631 : } else
3009 719722 : nexto = NULLFILEOFF;
3010 : if (!eof &&
3011 : align_off + align_alen != orig_end &&
3012 : align_off + align_alen > nexto)
3013 : align_off = nexto > align_alen ? nexto - align_alen : 0;
3014 : /*
3015 : * If we're now overlapping the next or previous extent that
3016 : * means we can't fit an extsz piece in this hole. Just move
3017 : * the start forward to the first valid spot and set
3018 100138631 : * the length so we hit the end.
3019 37504997 : */
3020 36238299 : if (align_off != orig_off && align_off < prevo)
3021 0 : align_off = prevo;
3022 : if (align_off + align_alen != orig_end &&
3023 : align_off + align_alen > nexto &&
3024 : nexto != NULLFILEOFF) {
3025 : ASSERT(nexto > prevo);
3026 100138631 : align_alen = nexto - align_off;
3027 37504916 : }
3028 :
3029 629887 : /*
3030 : * If realtime, and the result isn't a multiple of the realtime
3031 : * extent size we need to remove blocks until it is.
3032 : */
3033 : if (rt && (temp = xfs_extlen_to_rtxmod(mp, align_alen))) {
3034 : /*
3035 : * We're not covering the original request, or
3036 100138631 : * we won't be able to once we fix the length.
3037 559699 : */
3038 100138631 : if (orig_off < align_off ||
3039 : orig_end > align_off + align_alen ||
3040 : align_alen - temp < orig_alen)
3041 583994 : return -EINVAL;
3042 583994 : /*
3043 : * Try to fix it by moving the start up.
3044 : */
3045 : if (align_off + temp <= orig_off) {
3046 : align_alen -= temp;
3047 : align_off += temp;
3048 : }
3049 100138631 : /*
3050 : * Try to fix it by moving the end in.
3051 : */
3052 : else if (align_off + align_alen - temp >= orig_end)
3053 : align_alen -= temp;
3054 0 : /*
3055 0 : * Set the start to the minimum then trim the length.
3056 0 : */
3057 : else {
3058 : align_alen -= orig_off - align_off;
3059 : align_off = orig_off;
3060 : align_alen -= xfs_extlen_to_rtxmod(mp, align_alen);
3061 0 : }
3062 : /*
3063 : * Result doesn't cover the request, fail it.
3064 : */
3065 : if (orig_off < align_off || orig_end > align_off + align_alen)
3066 : return -EINVAL;
3067 : } else {
3068 0 : ASSERT(orig_off >= align_off);
3069 : /* see XFS_BMBT_MAX_EXTLEN handling above */
3070 : ASSERT(orig_end <= align_off + align_alen ||
3071 : align_alen + extsz > XFS_MAX_BMBT_EXTLEN);
3072 : }
3073 :
3074 0 : #ifdef DEBUG
3075 0 : if (!eof && gotp->br_startoff != NULLFILEOFF)
3076 0 : ASSERT(align_off + align_alen <= gotp->br_startoff);
3077 : if (prevp->br_startoff != NULLFILEOFF)
3078 : ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3079 : #endif
3080 :
3081 0 : *lenp = align_alen;
3082 : *offp = align_off;
3083 : return 0;
3084 100122843 : }
3085 :
3086 100122843 : #define XFS_ALLOC_GAP_UNITS 4
3087 :
3088 : void
3089 : xfs_bmap_adjacent(
3090 : struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3091 100122843 : {
3092 37503794 : xfs_fsblock_t adjust; /* adjustment to block numbers */
3093 100122843 : xfs_mount_t *mp; /* mount point structure */
3094 85970097 : int rt; /* true if inode is realtime */
3095 :
3096 : #define ISVALID(x,y) \
3097 100122843 : (rt ? \
3098 100122843 : (x) < mp->m_sb.sb_rblocks : \
3099 100122843 : XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3100 : XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3101 : XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3102 :
3103 : mp = ap->ip->i_mount;
3104 : rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3105 174436854 : (ap->datatype & XFS_ALLOC_USERDATA);
3106 : /*
3107 : * If allocating at eof, and there's a previous real block,
3108 174436854 : * try to use its last block as our starting point.
3109 174436854 : */
3110 174436854 : if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3111 : !isnullstartblock(ap->prev.br_startblock) &&
3112 : ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3113 : ap->prev.br_startblock)) {
3114 : ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3115 : /*
3116 : * Adjust for the gap between prevp and us.
3117 : */
3118 : adjust = ap->offset -
3119 174436854 : (ap->prev.br_startoff + ap->prev.br_blockcount);
3120 174436854 : if (adjust &&
3121 90503552 : ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3122 : ap->blkno += adjust;
3123 : }
3124 : /*
3125 : * If not at eof, then compare the two neighbor blocks.
3126 174436854 : * Figure out whether either one gives us a good starting point,
3127 64964022 : * and pick the better one.
3128 52994826 : */
3129 : else if (!ap->eof) {
3130 52994085 : xfs_fsblock_t gotbno; /* right side block number */
3131 : xfs_fsblock_t gotdiff=0; /* right side difference */
3132 : xfs_fsblock_t prevbno; /* left side block number */
3133 : xfs_fsblock_t prevdiff=0; /* left side difference */
3134 52994085 :
3135 52994085 : /*
3136 61720637 : * If there's a previous (left) block, select a requested
3137 8726552 : * start block based on it.
3138 8664118 : */
3139 : if (ap->prev.br_startoff != NULLFILEOFF &&
3140 : !isnullstartblock(ap->prev.br_startblock) &&
3141 : (prevbno = ap->prev.br_startblock +
3142 : ap->prev.br_blockcount) &&
3143 : ISVALID(prevbno, ap->prev.br_startblock)) {
3144 : /*
3145 121442822 : * Calculate gap to end of previous block.
3146 75632052 : */
3147 75632052 : adjust = prevdiff = ap->offset -
3148 75632052 : (ap->prev.br_startoff +
3149 75632052 : ap->prev.br_blockcount);
3150 : /*
3151 : * Figure the startblock based on the previous block's
3152 : * end and the gap size.
3153 : * Heuristic!
3154 : * If the gap is large relative to the piece we're
3155 140851447 : * allocating, or using it gives us an invalid block
3156 65438009 : * number, then just use the end of the previous block.
3157 65221364 : */
3158 96497740 : if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3159 65219395 : ISVALID(prevbno + prevdiff,
3160 : ap->prev.br_startblock))
3161 : prevbno += adjust;
3162 : else
3163 65221936 : prevdiff += adjust;
3164 65221936 : }
3165 65221936 : /*
3166 : * No previous block or can't follow it, just default.
3167 : */
3168 : else
3169 : prevbno = NULLFSBLOCK;
3170 : /*
3171 : * If there's a following (right) block, select a requested
3172 : * start block based on it.
3173 : */
3174 79584226 : if (!isnullstartblock(ap->got.br_startblock)) {
3175 39481793 : /*
3176 : * Calculate gap to start of next block.
3177 39481583 : */
3178 : adjust = gotdiff = ap->got.br_startoff - ap->offset;
3179 25740360 : /*
3180 : * Figure the startblock based on the next block's
3181 : * start and the gap size.
3182 : */
3183 : gotbno = ap->got.br_startblock;
3184 : /*
3185 : * Heuristic!
3186 : * If the gap is large relative to the piece we're
3187 : * allocating, or using it gives us an invalid block
3188 : * number, then just use the start of the next block
3189 : * offset by our length.
3190 75632487 : */
3191 : if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3192 : ISVALID(gotbno - gotdiff, gotbno))
3193 : gotbno -= adjust;
3194 47699045 : else if (ISVALID(gotbno - ap->length, gotbno)) {
3195 : gotbno -= ap->length;
3196 : gotdiff += adjust - ap->length;
3197 : } else
3198 : gotdiff += adjust;
3199 47699045 : }
3200 : /*
3201 : * No next block, just default.
3202 : */
3203 : else
3204 : gotbno = NULLFSBLOCK;
3205 : /*
3206 : * If both valid, pick the better one, else the only good
3207 52308858 : * one, else ap->blkno is already set (to 0 or the inode block).
3208 20154233 : */
3209 20105473 : if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3210 27593580 : ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3211 27508728 : else if (prevbno != NULLFSBLOCK)
3212 27508728 : ap->blkno = prevbno;
3213 : else if (gotbno != NULLFSBLOCK)
3214 84317 : ap->blkno = gotbno;
3215 : }
3216 : #undef ISVALID
3217 : }
3218 :
3219 : int
3220 : xfs_bmap_longest_free_extent(
3221 : struct xfs_perag *pag,
3222 : struct xfs_trans *tp,
3223 : xfs_extlen_t *blen)
3224 : {
3225 75631960 : xfs_extlen_t longest;
3226 55705820 : int error = 0;
3227 30710967 :
3228 20301020 : if (!xfs_perag_initialised_agf(pag)) {
3229 10409947 : error = xfs_alloc_read_agf(pag, tp, XFS_ALLOC_FLAG_TRYLOCK,
3230 2776445 : NULL);
3231 : if (error)
3232 : return error;
3233 174436815 : }
3234 :
3235 : longest = xfs_alloc_longest_free_extent(pag,
3236 91408178 : xfs_alloc_min_freelist(pag->pag_mount, pag),
3237 : xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
3238 : if (*blen < longest)
3239 : *blen = longest;
3240 :
3241 91408178 : return 0;
3242 91408178 : }
3243 :
3244 182816356 : static xfs_extlen_t
3245 2635 : xfs_bmap_select_minlen(
3246 : struct xfs_bmalloca *ap,
3247 2635 : struct xfs_alloc_arg *args,
3248 : xfs_extlen_t blen)
3249 : {
3250 :
3251 91405630 : /*
3252 : * Since we used XFS_ALLOC_FLAG_TRYLOCK in _longest_free_extent(), it is
3253 : * possible that there is enough contiguous free space for this request.
3254 91323733 : */
3255 89164206 : if (blen < ap->minlen)
3256 : return ap->minlen;
3257 :
3258 : /*
3259 : * If the best seen length is less than the request length,
3260 : * use the best as the minimum, otherwise we've got the maxlen we
3261 : * were asked for.
3262 : */
3263 : if (blen < args->maxlen)
3264 : return blen;
3265 : return args->maxlen;
3266 : }
3267 :
3268 : static int
3269 : xfs_bmap_btalloc_select_lengths(
3270 : struct xfs_bmalloca *ap,
3271 87712415 : struct xfs_alloc_arg *args,
3272 : xfs_extlen_t *blen)
3273 : {
3274 : struct xfs_mount *mp = args->mp;
3275 : struct xfs_perag *pag;
3276 : xfs_agnumber_t agno, startag;
3277 : int error = 0;
3278 :
3279 87712298 : if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3280 : args->total = ap->minlen;
3281 : args->minlen = ap->minlen;
3282 : return 0;
3283 : }
3284 :
3285 87554951 : args->total = ap->total;
3286 : startag = XFS_FSB_TO_AGNO(mp, ap->blkno);
3287 : if (startag == NULLAGNUMBER)
3288 : startag = 0;
3289 :
3290 87554951 : *blen = 0;
3291 87554951 : for_each_perag_wrap(mp, startag, agno, pag) {
3292 87554951 : error = xfs_bmap_longest_free_extent(pag, args->tp, blen);
3293 87554951 : if (error && error != -EAGAIN)
3294 : break;
3295 87554951 : error = 0;
3296 548 : if (*blen >= args->maxlen)
3297 548 : break;
3298 548 : }
3299 : if (pag)
3300 : xfs_perag_rele(pag);
3301 87554403 :
3302 87554403 : args->minlen = xfs_bmap_select_minlen(ap, args, *blen);
3303 87554403 : return error;
3304 0 : }
3305 :
3306 87554403 : /* Update all inode and quota accounting for the allocation we just did. */
3307 91580066 : static void
3308 90969015 : xfs_bmap_btalloc_accounting(
3309 90849977 : struct xfs_bmalloca *ap,
3310 : struct xfs_alloc_arg *args)
3311 90849977 : {
3312 90849977 : if (ap->flags & XFS_BMAPI_COWFORK) {
3313 : /*
3314 : * COW fork blocks are in-core only and thus are treated as
3315 87553395 : * in-core quota reservation (like delalloc blocks) even when
3316 86847857 : * converted to real blocks. The quota reservation is not
3317 : * accounted to disk until blocks are remapped to the data
3318 87650905 : * fork. So if these blocks were previously delalloc, we
3319 87650905 : * already have quota reservation and there's nothing to do
3320 : * yet.
3321 : */
3322 : if (ap->wasdel) {
3323 : xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3324 90348629 : return;
3325 : }
3326 :
3327 : /*
3328 90348629 : * Otherwise, we've allocated blocks in a hole. The transaction
3329 : * has acquired in-core quota reservation for this extent.
3330 : * Rather than account these as real blocks, however, we reduce
3331 : * the transaction quota reservation based on the allocation.
3332 : * This essentially transfers the transaction quota reservation
3333 : * to that of a delalloc extent.
3334 : */
3335 : ap->ip->i_delayed_blks += args->len;
3336 : xfs_trans_mod_dquot_byino(ap->tp, ap->ip, XFS_TRANS_DQ_RES_BLKS,
3337 : -(long)args->len);
3338 4021391 : return;
3339 3668234 : }
3340 3668234 :
3341 : /* data/attr fork only */
3342 : ap->ip->i_nblocks += args->len;
3343 : xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3344 : if (ap->wasdel) {
3345 : ap->ip->i_delayed_blks -= args->len;
3346 : xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)args->len);
3347 : }
3348 : xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3349 : ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT : XFS_TRANS_DQ_BCOUNT,
3350 : args->len);
3351 353157 : }
3352 353157 :
3353 353157 : static int
3354 353157 : xfs_bmap_compute_alignments(
3355 : struct xfs_bmalloca *ap,
3356 : struct xfs_alloc_arg *args)
3357 : {
3358 86327238 : struct xfs_mount *mp = args->mp;
3359 86327238 : xfs_extlen_t align = 0; /* minimum allocation alignment */
3360 86361585 : int stripe_align = 0;
3361 25625886 :
3362 25625886 : /* stripe alignment for allocation is determined by mount parameters */
3363 : if (mp->m_swidth && xfs_has_swalloc(mp))
3364 86361511 : stripe_align = mp->m_swidth;
3365 86361511 : else if (mp->m_dalign)
3366 86361511 : stripe_align = mp->m_dalign;
3367 :
3368 : if (ap->flags & XFS_BMAPI_COWFORK)
3369 : align = xfs_get_cowextsz_hint(ap->ip);
3370 90274370 : else if (ap->datatype & XFS_ALLOC_USERDATA)
3371 : align = xfs_get_extsz_hint(ap->ip);
3372 : if (align) {
3373 : if (xfs_bmap_extsize_align(mp, &ap->got, &ap->prev, align, 0,
3374 90274370 : ap->eof, 0, ap->conv, &ap->offset,
3375 90274370 : &ap->length))
3376 90274370 : ASSERT(0);
3377 : ASSERT(ap->length);
3378 : }
3379 90274370 :
3380 : /* apply extent size hints if obtained earlier */
3381 90274018 : if (align) {
3382 5367816 : args->prod = align;
3383 : div_u64_rem(ap->offset, args->prod, &args->mod);
3384 90274370 : if (args->mod)
3385 4021432 : args->mod = args->prod - args->mod;
3386 86252938 : } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
3387 45207627 : args->prod = 1;
3388 49227760 : args->mod = 0;
3389 12405539 : } else {
3390 12405627 : args->prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
3391 : div_u64_rem(ap->offset, args->prod, &args->mod);
3392 0 : if (args->mod)
3393 12405539 : args->mod = args->prod - args->mod;
3394 : }
3395 :
3396 : return stripe_align;
3397 90272983 : }
3398 12405560 :
3399 12405560 : static void
3400 12405516 : xfs_bmap_process_allocated_extent(
3401 1365326 : struct xfs_bmalloca *ap,
3402 77867423 : struct xfs_alloc_arg *args,
3403 77845375 : xfs_fileoff_t orig_offset,
3404 77845375 : xfs_extlen_t orig_length)
3405 : {
3406 22048 : ap->blkno = args->fsbno;
3407 22048 : ap->length = args->len;
3408 22048 : /*
3409 15608 : * If the extent size hint is active, we tried to round the
3410 : * caller's allocation request offset down to extsz and the
3411 : * length up to another extsz boundary. If we found a free
3412 90272939 : * extent we mapped it in starting at this new offset. If the
3413 : * newly mapped space isn't long enough to cover any of the
3414 : * range of offsets that was originally requested, move the
3415 : * mapping up so that we can fill as much of the caller's
3416 90361654 : * original request as possible. Free space is apparently
3417 : * very fragmented so we're unlikely to be able to satisfy the
3418 : * hints anyway.
3419 : */
3420 : if (ap->length <= orig_length)
3421 : ap->offset = orig_offset;
3422 90361654 : else if (ap->offset + ap->length < orig_offset + orig_length)
3423 90361654 : ap->offset = orig_offset + orig_length - ap->length;
3424 : xfs_bmap_btalloc_accounting(ap, args);
3425 : }
3426 :
3427 : #ifdef DEBUG
3428 : static int
3429 : xfs_bmap_exact_minlen_extent_alloc(
3430 : struct xfs_bmalloca *ap)
3431 : {
3432 : struct xfs_mount *mp = ap->ip->i_mount;
3433 : struct xfs_alloc_arg args = { .tp = ap->tp, .mp = mp };
3434 : xfs_fileoff_t orig_offset;
3435 : xfs_extlen_t orig_length;
3436 90361654 : int error;
3437 89931156 :
3438 430498 : ASSERT(ap->length);
3439 15 :
3440 90361654 : if (ap->minlen != 1) {
3441 90358296 : ap->blkno = NULLFSBLOCK;
3442 : ap->length = 0;
3443 : return 0;
3444 : }
3445 2638195 :
3446 : orig_offset = ap->offset;
3447 : orig_length = ap->length;
3448 2638195 :
3449 2638195 : args.alloc_minlen_only = 1;
3450 2638195 :
3451 2638195 : xfs_bmap_compute_alignments(ap, &args);
3452 2638195 :
3453 : /*
3454 2638195 : * Unlike the longest extent available in an AG, we don't track
3455 : * the length of an AG's shortest extent.
3456 2638195 : * XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT is a debug only knob and
3457 0 : * hence we can afford to start traversing from the 0th AG since
3458 0 : * we need not be concerned about a drop in performance in
3459 0 : * "debug only" code paths.
3460 : */
3461 : ap->blkno = XFS_AGB_TO_FSB(mp, 0, 0);
3462 2638195 :
3463 2638195 : args.oinfo = XFS_RMAP_OINFO_SKIP_UPDATE;
3464 : args.minlen = args.maxlen = ap->minlen;
3465 2638195 : args.total = ap->total;
3466 :
3467 2638195 : args.alignment = 1;
3468 : args.minalignslop = 0;
3469 :
3470 : args.minleft = ap->minleft;
3471 : args.wasdel = ap->wasdel;
3472 : args.resv = XFS_AG_RESV_NONE;
3473 : args.datatype = ap->datatype;
3474 :
3475 : error = xfs_alloc_vextent_first_ag(&args, ap->blkno);
3476 : if (error)
3477 2635811 : return error;
3478 :
3479 2635811 : if (args.fsbno != NULLFSBLOCK) {
3480 2635811 : xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3481 2635811 : orig_length);
3482 : } else {
3483 2635811 : ap->blkno = NULLFSBLOCK;
3484 2635811 : ap->length = 0;
3485 : }
3486 2635811 :
3487 2635811 : return 0;
3488 2635811 : }
3489 2635811 : #else
3490 :
3491 2635811 : #define xfs_bmap_exact_minlen_extent_alloc(bma) (-EFSCORRUPTED)
3492 2638391 :
3493 : #endif
3494 :
3495 2638391 : /*
3496 2638389 : * If we are not low on available data blocks and we are allocating at
3497 : * EOF, optimise allocation for contiguous file extension and/or stripe
3498 : * alignment of the new extent.
3499 2 : *
3500 2 : * NOTE: ap->aeof is only set if the allocation length is >= the
3501 : * stripe unit and the allocation offset is at the end of file.
3502 : */
3503 : static int
3504 : xfs_bmap_btalloc_at_eof(
3505 : struct xfs_bmalloca *ap,
3506 : struct xfs_alloc_arg *args,
3507 : xfs_extlen_t blen,
3508 : int stripe_align,
3509 : bool ag_only)
3510 : {
3511 : struct xfs_mount *mp = args->mp;
3512 : struct xfs_perag *caller_pag = args->pag;
3513 : int error;
3514 :
3515 : /*
3516 : * If there are already extents in the file, try an exact EOF block
3517 : * allocation to extend the file as a contiguous extent. If that fails,
3518 : * or it's the first allocation in a file, just try for a stripe aligned
3519 : * allocation.
3520 447022 : */
3521 : if (ap->offset) {
3522 : xfs_extlen_t nextminlen = 0;
3523 :
3524 : /*
3525 : * Compute the minlen+alignment for the next case. Set slop so
3526 : * that the value of minlen+alignment+slop doesn't go up between
3527 447022 : * the calls.
3528 447022 : */
3529 447022 : args->alignment = 1;
3530 : if (blen > stripe_align && blen <= args->maxlen)
3531 : nextminlen = blen - stripe_align;
3532 : else
3533 : nextminlen = args->minlen;
3534 : if (nextminlen + stripe_align > args->minlen + 1)
3535 : args->minalignslop = nextminlen + stripe_align -
3536 : args->minlen - 1;
3537 447022 : else
3538 419598 : args->minalignslop = 0;
3539 :
3540 : if (!caller_pag)
3541 : args->pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, ap->blkno));
3542 : error = xfs_alloc_vextent_exact_bno(args, ap->blkno);
3543 : if (!caller_pag) {
3544 : xfs_perag_put(args->pag);
3545 419598 : args->pag = NULL;
3546 419598 : }
3547 4718 : if (error)
3548 : return error;
3549 414880 :
3550 419598 : if (args->fsbno != NULLFSBLOCK)
3551 414880 : return 0;
3552 414880 : /*
3553 : * Exact allocation failed. Reset to try an aligned allocation
3554 4718 : * according to the original allocation specification.
3555 : */
3556 419598 : args->alignment = stripe_align;
3557 418756 : args->minlen = nextminlen;
3558 419643 : args->minalignslop = 0;
3559 419603 : } else {
3560 418766 : /*
3561 418797 : * Adjust minlen to try and preserve alignment if we
3562 : * can't guarantee an aligned maxlen extent.
3563 419634 : */
3564 : args->alignment = stripe_align;
3565 : if (blen > args->alignment &&
3566 419634 : blen <= args->maxlen + args->alignment)
3567 : args->minlen = blen - args->alignment;
3568 : args->minalignslop = 0;
3569 : }
3570 :
3571 : if (ag_only) {
3572 304336 : error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3573 304336 : } else {
3574 304336 : args->pag = NULL;
3575 : error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3576 : ASSERT(args->pag == NULL);
3577 : args->pag = caller_pag;
3578 : }
3579 : if (error)
3580 27424 : return error;
3581 27424 :
3582 27354 : if (args->fsbno != NULLFSBLOCK)
3583 999 : return 0;
3584 27424 :
3585 : /*
3586 : * Allocation failed, so turn return the allocation args to their
3587 331760 : * original non-aligned state so the caller can proceed on allocation
3588 5609 : * failure as if this function was never called.
3589 : */
3590 326151 : args->alignment = 1;
3591 326151 : return 0;
3592 326172 : }
3593 326172 :
3594 : /*
3595 331768 : * We have failed multiple allocation attempts so now are in a low space
3596 : * allocation situation. Try a locality first full filesystem minimum length
3597 : * allocation whilst still maintaining necessary total block reservation
3598 331768 : * requirements.
3599 : *
3600 : * If that fails, we are now critically low on space, so perform a last resort
3601 : * allocation attempt: no reserve, no locality, blocking, minimum length, full
3602 : * filesystem free space scan. We also indicate to future allocations in this
3603 : * transaction that we are critically low on space so they don't waste time on
3604 : * allocation modes that are unlikely to succeed.
3605 : */
3606 17601 : int
3607 17601 : xfs_bmap_btalloc_low_space(
3608 : struct xfs_bmalloca *ap,
3609 : struct xfs_alloc_arg *args)
3610 : {
3611 : int error;
3612 :
3613 : if (args->minlen > ap->minlen) {
3614 : args->minlen = ap->minlen;
3615 : error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3616 : if (error || args->fsbno != NULLFSBLOCK)
3617 : return error;
3618 : }
3619 :
3620 : /* Last ditch attempt before failure is declared. */
3621 : args->total = ap->minlen;
3622 : error = xfs_alloc_vextent_first_ag(args, 0);
3623 594300 : if (error)
3624 : return error;
3625 : ap->tp->t_flags |= XFS_TRANS_LOWMODE;
3626 : return 0;
3627 594300 : }
3628 :
3629 594300 : static int
3630 594140 : xfs_bmap_btalloc_filestreams(
3631 594140 : struct xfs_bmalloca *ap,
3632 594140 : struct xfs_alloc_arg *args,
3633 : int stripe_align)
3634 : {
3635 : xfs_extlen_t blen = 0;
3636 : int error = 0;
3637 160 :
3638 160 :
3639 160 : error = xfs_filestream_select_ag(ap, args, &blen);
3640 : if (error)
3641 160 : return error;
3642 160 : ASSERT(args->pag);
3643 :
3644 : /*
3645 : * If we are in low space mode, then optimal allocation will fail so
3646 61500 : * prepare for minimal allocation and jump to the low space algorithm
3647 : * immediately.
3648 : */
3649 : if (ap->tp->t_flags & XFS_TRANS_LOWMODE) {
3650 : args->minlen = ap->minlen;
3651 61500 : ASSERT(args->fsbno == NULLFSBLOCK);
3652 61500 : goto out_low_space;
3653 : }
3654 :
3655 61500 : args->minlen = xfs_bmap_select_minlen(ap, args, blen);
3656 61510 : if (ap->aeof)
3657 : error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3658 61510 : true);
3659 :
3660 : if (!error && args->fsbno == NULLFSBLOCK)
3661 : error = xfs_alloc_vextent_near_bno(args, ap->blkno);
3662 :
3663 : out_low_space:
3664 : /*
3665 61510 : * We are now done with the perag reference for the filestreams
3666 0 : * association provided by xfs_filestream_select_ag(). Release it now as
3667 0 : * we've either succeeded, had a fatal error or we are out of space and
3668 0 : * need to do a full filesystem scan for free space which will take it's
3669 : * own references.
3670 : */
3671 61510 : xfs_perag_rele(args->pag);
3672 61510 : args->pag = NULL;
3673 5610 : if (error || args->fsbno != NULLFSBLOCK)
3674 : return error;
3675 :
3676 61510 : return xfs_bmap_btalloc_low_space(ap, args);
3677 55900 : }
3678 :
3679 5610 : static int
3680 : xfs_bmap_btalloc_best_length(
3681 : struct xfs_bmalloca *ap,
3682 : struct xfs_alloc_arg *args,
3683 : int stripe_align)
3684 : {
3685 : xfs_extlen_t blen = 0;
3686 : int error;
3687 61518 :
3688 61541 : ap->blkno = XFS_INO_TO_FSB(args->mp, ap->ip->i_ino);
3689 61541 : xfs_bmap_adjacent(ap);
3690 :
3691 : /*
3692 1 : * Search for an allocation group with a single extent large enough for
3693 : * the request. If one isn't found, then adjust the minimum allocation
3694 : * size to the largest space found.
3695 : */
3696 87597878 : error = xfs_bmap_btalloc_select_lengths(ap, args, &blen);
3697 : if (error)
3698 : return error;
3699 :
3700 : /*
3701 87597878 : * Don't attempt optimal EOF allocation if previous allocations barely
3702 87597878 : * succeeded due to being near ENOSPC. It is highly unlikely we'll get
3703 : * optimal or even aligned allocations in this case, so don't waste time
3704 87597878 : * trying.
3705 87597878 : */
3706 : if (ap->aeof && !(ap->tp->t_flags & XFS_TRANS_LOWMODE)) {
3707 : error = xfs_bmap_btalloc_at_eof(ap, args, blen, stripe_align,
3708 : false);
3709 : if (error || args->fsbno != NULLFSBLOCK)
3710 : return error;
3711 : }
3712 87569421 :
3713 87639694 : error = xfs_alloc_vextent_start_ag(args, ap->blkno);
3714 : if (error || args->fsbno != NULLFSBLOCK)
3715 : return error;
3716 :
3717 : return xfs_bmap_btalloc_low_space(ap, args);
3718 : }
3719 :
3720 : static int
3721 : xfs_bmap_btalloc(
3722 87639694 : struct xfs_bmalloca *ap)
3723 441452 : {
3724 : struct xfs_mount *mp = ap->ip->i_mount;
3725 441456 : struct xfs_alloc_arg args = {
3726 : .tp = ap->tp,
3727 : .mp = mp,
3728 : .fsbno = NULLFSBLOCK,
3729 87215845 : .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
3730 87257874 : .minleft = ap->minleft,
3731 : .wasdel = ap->wasdel,
3732 : .resv = XFS_AG_RESV_NONE,
3733 594299 : .datatype = ap->datatype,
3734 : .alignment = 1,
3735 : .minalignslop = 0,
3736 : };
3737 87649491 : xfs_fileoff_t orig_offset;
3738 : xfs_extlen_t orig_length;
3739 : int error;
3740 87649491 : int stripe_align;
3741 175298982 :
3742 87649491 : ASSERT(ap->length);
3743 : orig_offset = ap->offset;
3744 : orig_length = ap->length;
3745 :
3746 87649491 : stripe_align = xfs_bmap_compute_alignments(ap, &args);
3747 0 :
3748 : /* Trim the allocation back to the maximum an AG can fit. */
3749 87649491 : args.maxlen = min(ap->length, mp->m_ag_max_usable);
3750 :
3751 : if ((ap->datatype & XFS_ALLOC_USERDATA) &&
3752 : xfs_inode_is_filestream(ap->ip))
3753 87649491 : error = xfs_bmap_btalloc_filestreams(ap, &args, stripe_align);
3754 87649491 : else
3755 87649491 : error = xfs_bmap_btalloc_best_length(ap, &args, stripe_align);
3756 87649491 : if (error)
3757 : return error;
3758 87649491 :
3759 87649491 : if (args.fsbno != NULLFSBLOCK) {
3760 87649491 : xfs_bmap_process_allocated_extent(ap, &args, orig_offset,
3761 : orig_length);
3762 87649491 : } else {
3763 : ap->blkno = NULLFSBLOCK;
3764 : ap->length = 0;
3765 87618169 : }
3766 : return 0;
3767 87618169 : }
3768 46621474 :
3769 61188 : /* Trim extent to fit a logical block range. */
3770 : void
3771 87557010 : xfs_trim_extent(
3772 87734110 : struct xfs_bmbt_irec *irec,
3773 : xfs_fileoff_t bno,
3774 : xfs_filblks_t len)
3775 87729524 : {
3776 87729364 : xfs_fileoff_t distance;
3777 : xfs_fileoff_t end = bno + len;
3778 :
3779 160 : if (irec->br_startoff + irec->br_blockcount <= bno ||
3780 160 : irec->br_startoff >= end) {
3781 : irec->br_blockcount = 0;
3782 : return;
3783 : }
3784 :
3785 : if (irec->br_startoff < bno) {
3786 : distance = bno - irec->br_startoff;
3787 708126850 : if (isnullstartblock(irec->br_startblock))
3788 : irec->br_startblock = DELAYSTARTBLOCK;
3789 : if (irec->br_startblock != DELAYSTARTBLOCK &&
3790 : irec->br_startblock != HOLESTARTBLOCK)
3791 : irec->br_startblock += distance;
3792 708126850 : irec->br_startoff += distance;
3793 708126850 : irec->br_blockcount -= distance;
3794 : }
3795 708126850 :
3796 : if (end < irec->br_startoff + irec->br_blockcount) {
3797 82877608 : distance = irec->br_startoff + irec->br_blockcount - end;
3798 82877608 : irec->br_blockcount -= distance;
3799 : }
3800 : }
3801 625249242 :
3802 208793859 : /*
3803 208793859 : * Trim the returned map to the required bounds
3804 30722125 : */
3805 208793859 : STATIC void
3806 : xfs_bmapi_trim_map(
3807 177872884 : struct xfs_bmbt_irec *mval,
3808 208793859 : struct xfs_bmbt_irec *got,
3809 208793859 : xfs_fileoff_t *bno,
3810 : xfs_filblks_t len,
3811 : xfs_fileoff_t obno,
3812 625249242 : xfs_fileoff_t end,
3813 54250095 : int n,
3814 54250095 : uint32_t flags)
3815 : {
3816 : if ((flags & XFS_BMAPI_ENTIRE) ||
3817 : got->br_startoff + got->br_blockcount <= obno) {
3818 : *mval = *got;
3819 : if (isnullstartblock(got->br_startblock))
3820 : mval->br_startblock = DELAYSTARTBLOCK;
3821 : return;
3822 9391731107 : }
3823 :
3824 : if (obno > *bno)
3825 : *bno = obno;
3826 : ASSERT((*bno >= obno) || (n == 0));
3827 : ASSERT(*bno < end);
3828 : mval->br_startoff = *bno;
3829 : if (isnullstartblock(got->br_startblock))
3830 : mval->br_startblock = DELAYSTARTBLOCK;
3831 : else
3832 9391731107 : mval->br_startblock = got->br_startblock +
3833 9391731107 : (*bno - got->br_startoff);
3834 63 : /*
3835 63 : * Return the minimum of what we got and what we asked for for
3836 0 : * the length. We can use the len variable here because it is
3837 63 : * modified below and we could have been there before coming
3838 : * here if the first part of the allocation didn't overlap what
3839 : * was asked for.
3840 9391731044 : */
3841 0 : mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3842 9391731044 : got->br_blockcount - (*bno - got->br_startoff));
3843 9391731044 : mval->br_state = got->br_state;
3844 9391731044 : ASSERT(mval->br_blockcount <= len);
3845 9391731044 : return;
3846 4113501 : }
3847 :
3848 9387617543 : /*
3849 9387617543 : * Update and validate the extent map to return
3850 : */
3851 : STATIC void
3852 : xfs_bmapi_update_map(
3853 : struct xfs_bmbt_irec **map,
3854 : xfs_fileoff_t *bno,
3855 : xfs_filblks_t *len,
3856 : xfs_fileoff_t obno,
3857 9391731044 : xfs_fileoff_t end,
3858 : int *n,
3859 9391731044 : uint32_t flags)
3860 9391731044 : {
3861 : xfs_bmbt_irec_t *mval = *map;
3862 :
3863 : ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3864 : ((mval->br_startoff + mval->br_blockcount) <= end));
3865 : ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3866 : (mval->br_startoff < obno));
3867 :
3868 9389653919 : *bno = mval->br_startoff + mval->br_blockcount;
3869 : *len = end - *bno;
3870 : if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3871 : /* update previous map with new information */
3872 : ASSERT(mval->br_startblock == mval[-1].br_startblock);
3873 : ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3874 : ASSERT(mval->br_state == mval[-1].br_state);
3875 : mval[-1].br_blockcount = mval->br_blockcount;
3876 : mval[-1].br_state = mval->br_state;
3877 9389653919 : } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3878 : mval[-1].br_startblock != DELAYSTARTBLOCK &&
3879 9389653919 : mval[-1].br_startblock != HOLESTARTBLOCK &&
3880 : mval->br_startblock == mval[-1].br_startblock +
3881 9389653919 : mval[-1].br_blockcount &&
3882 : mval[-1].br_state == mval->br_state) {
3883 : ASSERT(mval->br_startoff ==
3884 9389653919 : mval[-1].br_startoff + mval[-1].br_blockcount);
3885 9389653919 : mval[-1].br_blockcount += mval->br_blockcount;
3886 9389653919 : } else if (*n > 0 &&
3887 : mval->br_startblock == DELAYSTARTBLOCK &&
3888 0 : mval[-1].br_startblock == DELAYSTARTBLOCK &&
3889 0 : mval->br_startoff ==
3890 0 : mval[-1].br_startoff + mval[-1].br_blockcount) {
3891 0 : mval[-1].br_blockcount += mval->br_blockcount;
3892 0 : mval[-1].br_state = mval->br_state;
3893 9389653919 : } else if (!((*n == 0) &&
3894 616799 : ((mval->br_startoff + mval->br_blockcount) <=
3895 612893 : obno))) {
3896 612893 : mval++;
3897 612893 : (*n)++;
3898 0 : }
3899 0 : *map = mval;
3900 : }
3901 0 :
3902 9389653919 : /*
3903 616799 : * Map file blocks to filesystem blocks without allocation.
3904 0 : */
3905 0 : int
3906 0 : xfs_bmapi_read(
3907 0 : struct xfs_inode *ip,
3908 0 : xfs_fileoff_t bno,
3909 9389653919 : xfs_filblks_t len,
3910 9388662736 : struct xfs_bmbt_irec *mval,
3911 : int *nmap,
3912 9387528828 : uint32_t flags)
3913 9387528828 : {
3914 : struct xfs_mount *mp = ip->i_mount;
3915 9389653919 : int whichfork = xfs_bmapi_whichfork(flags);
3916 9389653919 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
3917 : struct xfs_bmbt_irec got;
3918 : xfs_fileoff_t obno;
3919 : xfs_fileoff_t end;
3920 : struct xfs_iext_cursor icur;
3921 : int error;
3922 13557760479 : bool eof = false;
3923 : int n = 0;
3924 :
3925 : ASSERT(*nmap >= 1);
3926 : ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_ENTIRE)));
3927 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
3928 :
3929 : if (WARN_ON_ONCE(!ifp)) {
3930 13557760479 : xfs_bmap_mark_sick(ip, whichfork);
3931 13557760479 : return -EFSCORRUPTED;
3932 13557760479 : }
3933 13549270253 :
3934 13549270253 : if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
3935 13549270253 : XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
3936 13549270253 : xfs_bmap_mark_sick(ip, whichfork);
3937 13549270253 : return -EFSCORRUPTED;
3938 13549270253 : }
3939 13549270253 :
3940 : if (xfs_is_shutdown(mp))
3941 13549270253 : return -EIO;
3942 13549270253 :
3943 13549270253 : XFS_STATS_INC(mp, xs_blk_mapr);
3944 :
3945 13551049093 : error = xfs_iread_extents(NULL, ip, whichfork);
3946 0 : if (error)
3947 0 : return error;
3948 :
3949 : if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got))
3950 27099414665 : eof = true;
3951 13551049093 : end = bno + len;
3952 0 : obno = bno;
3953 0 :
3954 : while (bno < end && n < *nmap) {
3955 : /* Reading past eof, act as though there's a hole up to end. */
3956 27096731144 : if (eof)
3957 : got.br_startoff = end;
3958 : if (got.br_startoff > bno) {
3959 13548356482 : /* Reading in a hole. */
3960 : mval->br_startoff = bno;
3961 13552717870 : mval->br_startblock = HOLESTARTBLOCK;
3962 13553506546 : mval->br_blockcount =
3963 : XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3964 : mval->br_state = XFS_EXT_NORM;
3965 13553918716 : bno += mval->br_blockcount;
3966 855800104 : len -= mval->br_blockcount;
3967 13555439247 : mval++;
3968 13555439247 : n++;
3969 : continue;
3970 18006934323 : }
3971 :
3972 13579756235 : /* set up the extent map to return. */
3973 879398090 : xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3974 13579756235 : xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3975 :
3976 4427239483 : /* If we're done, stop now. */
3977 4427239483 : if (bno >= end || n >= *nmap)
3978 4427239483 : break;
3979 4427239483 :
3980 4427239483 : /* Else go on to the next record. */
3981 4427239483 : if (!xfs_iext_next_extent(ifp, &icur, &got))
3982 4427239483 : eof = true;
3983 4427239483 : }
3984 4427239483 : *nmap = n;
3985 4427239483 : return 0;
3986 : }
3987 :
3988 : /*
3989 9152516752 : * Add a delayed allocation extent to an inode. Blocks are reserved from the
3990 9152759731 : * global pool and the extent inserted into the inode in-core extent tree.
3991 : *
3992 : * On entry, got refers to the first extent beyond the offset of the extent to
3993 9152781475 : * allocate or eof is specified if no such extent exists. On return, got refers
3994 : * to the extent record that was inserted to the inode fork.
3995 : *
3996 : * Note that the allocated extent may have been merged with contiguous extents
3997 24286993 : * during insertion into the inode fork. Thus, got does not reflect the current
3998 23641380 : * state of the inode fork on return. If necessary, the caller can use lastx to
3999 : * look up the updated record in the inode fork.
4000 13555672570 : */
4001 13555672570 : int
4002 : xfs_bmapi_reserve_delalloc(
4003 : struct xfs_inode *ip,
4004 : int whichfork,
4005 : xfs_fileoff_t off,
4006 : xfs_filblks_t len,
4007 : xfs_filblks_t prealloc,
4008 : struct xfs_bmbt_irec *got,
4009 : struct xfs_iext_cursor *icur,
4010 : int eof)
4011 : {
4012 : struct xfs_mount *mp = ip->i_mount;
4013 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4014 : xfs_extlen_t alen;
4015 : xfs_extlen_t indlen;
4016 : int error;
4017 : xfs_fileoff_t aoff = off;
4018 57339660 :
4019 : /*
4020 : * Cap the alloc length. Keep track of prealloc so we know whether to
4021 : * tag the inode before we return.
4022 : */
4023 : alen = XFS_FILBLKS_MIN(len + prealloc, XFS_MAX_BMBT_EXTLEN);
4024 : if (!eof)
4025 : alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4026 : if (prealloc && alen >= len)
4027 : prealloc = alen - len;
4028 57339660 :
4029 57339660 : /* Figure out the extent size, adjust alen */
4030 57256372 : if (whichfork == XFS_COW_FORK) {
4031 57256372 : struct xfs_bmbt_irec prev;
4032 57256372 : xfs_extlen_t extsz = xfs_get_cowextsz_hint(ip);
4033 57256372 :
4034 : if (!xfs_iext_peek_prev_extent(ifp, icur, &prev))
4035 : prev.br_startoff = NULLFILEOFF;
4036 :
4037 : error = xfs_bmap_extsize_align(mp, got, &prev, extsz, 0, eof,
4038 : 1, 0, &aoff, &alen);
4039 57256372 : ASSERT(!error);
4040 57256372 : }
4041 28757993 :
4042 57256372 : /*
4043 7483752 : * Make a transaction-less quota reservation for delayed allocation
4044 : * blocks. This number gets adjusted later. We return if we haven't
4045 : * allocated blocks already inside this loop.
4046 57256372 : */
4047 4761437 : error = xfs_quota_reserve_blkres(ip, alen);
4048 4761437 : if (error)
4049 : return error;
4050 4766129 :
4051 2480920 : /*
4052 : * Split changing sb for alen and indlen since they could be coming
4053 4763252 : * from different places.
4054 : */
4055 4762506 : indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4056 : ASSERT(indlen > 0);
4057 :
4058 : error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
4059 : if (error)
4060 : goto out_unreserve_quota;
4061 :
4062 : error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
4063 57257441 : if (error)
4064 57441641 : goto out_unreserve_blocks;
4065 :
4066 :
4067 : ip->i_delayed_blks += alen;
4068 : xfs_mod_delalloc(ip->i_mount, alen + indlen);
4069 :
4070 : got->br_startoff = aoff;
4071 57436203 : got->br_startblock = nullstartblock(indlen);
4072 57418640 : got->br_blockcount = alen;
4073 : got->br_state = XFS_EXT_NORM;
4074 57418640 :
4075 57373879 : xfs_bmap_add_extent_hole_delay(ip, whichfork, icur, got);
4076 2003210 :
4077 : /*
4078 55370669 : * Tag the inode if blocks were preallocated. Note that COW fork
4079 55398360 : * preallocation can occur at the start or end of the extent, even when
4080 463188 : * prealloc == 0, so we must also check the aligned offset and length.
4081 : */
4082 : if (whichfork == XFS_DATA_FORK && prealloc)
4083 54935172 : xfs_inode_set_eofblocks_tag(ip);
4084 54935172 : if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4085 : xfs_inode_set_cowblocks_tag(ip);
4086 54913469 :
4087 54913469 : return 0;
4088 54917055 :
4089 54917055 : out_unreserve_blocks:
4090 : xfs_mod_fdblocks(mp, alen, false);
4091 54917055 : out_unreserve_quota:
4092 : if (XFS_IS_QUOTA_ON(mp))
4093 : xfs_quota_unreserve_blkres(ip, alen);
4094 : return error;
4095 : }
4096 :
4097 : static int
4098 54864773 : xfs_bmap_alloc_userdata(
4099 6415187 : struct xfs_bmalloca *bma)
4100 54865004 : {
4101 4369358 : struct xfs_mount *mp = bma->ip->i_mount;
4102 : int whichfork = xfs_bmapi_whichfork(bma->flags);
4103 : int error;
4104 :
4105 : /*
4106 463188 : * Set the data type being allocated. For the data fork, the first data
4107 2466352 : * in the file is treated differently to all other allocations. For the
4108 2466352 : * attribute fork, we only need to ensure the allocated range is not on
4109 2315147 : * the busy list.
4110 : */
4111 : bma->datatype = XFS_ALLOC_NOBUSY;
4112 : if (whichfork == XFS_DATA_FORK || whichfork == XFS_COW_FORK) {
4113 : bma->datatype |= XFS_ALLOC_USERDATA;
4114 135855175 : if (bma->offset == 0)
4115 : bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4116 :
4117 135855175 : if (mp->m_dalign && bma->length >= mp->m_dalign) {
4118 135855175 : error = xfs_bmap_isaeof(bma, whichfork);
4119 135855175 : if (error)
4120 : return error;
4121 : }
4122 :
4123 : if (XFS_IS_REALTIME_INODE(bma->ip))
4124 : return xfs_bmap_rtalloc(bma);
4125 : }
4126 :
4127 135855175 : if (unlikely(XFS_TEST_ERROR(false, mp,
4128 135855175 : XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4129 135852013 : return xfs_bmap_exact_minlen_extent_alloc(bma);
4130 135852013 :
4131 9549277 : return xfs_bmap_btalloc(bma);
4132 : }
4133 135852013 :
4134 1040900 : static int
4135 1041193 : xfs_bmapi_allocate(
4136 : struct xfs_bmalloca *bma)
4137 : {
4138 : struct xfs_mount *mp = bma->ip->i_mount;
4139 135852306 : int whichfork = xfs_bmapi_whichfork(bma->flags);
4140 86625033 : struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4141 : int tmp_logflags = 0;
4142 : int error;
4143 49230507 :
4144 : ASSERT(bma->length > 0);
4145 2609953 :
4146 : /*
4147 46622804 : * For the wasdelay case, we could also just allocate the stuff asked
4148 : * for in this bmap call but that wouldn't be as good.
4149 : */
4150 : if (bma->wasdel) {
4151 176929534 : bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4152 : bma->offset = bma->got.br_startoff;
4153 : if (!xfs_iext_peek_prev_extent(ifp, &bma->icur, &bma->prev))
4154 176929534 : bma->prev.br_startoff = NULLFILEOFF;
4155 176929534 : } else {
4156 176929534 : bma->length = XFS_FILBLKS_MIN(bma->length, XFS_MAX_BMBT_EXTLEN);
4157 176873870 : if (!bma->eof)
4158 176873870 : bma->length = XFS_FILBLKS_MIN(bma->length,
4159 : bma->got.br_startoff - bma->offset);
4160 176873870 : }
4161 :
4162 : if (bma->flags & XFS_BMAPI_CONTIG)
4163 : bma->minlen = bma->length;
4164 : else
4165 : bma->minlen = 1;
4166 176873870 :
4167 29297835 : if (bma->flags & XFS_BMAPI_METADATA) {
4168 29297835 : if (unlikely(XFS_TEST_ERROR(false, mp,
4169 29297835 : XFS_ERRTAG_BMAP_ALLOC_MINLEN_EXTENT)))
4170 7456320 : error = xfs_bmap_exact_minlen_extent_alloc(bma);
4171 : else
4172 147576035 : error = xfs_bmap_btalloc(bma);
4173 147576035 : } else {
4174 48237518 : error = xfs_bmap_alloc_userdata(bma);
4175 : }
4176 : if (error || bma->blkno == NULLFSBLOCK)
4177 : return error;
4178 176873958 :
4179 9495114 : if (bma->flags & XFS_BMAPI_ZERO) {
4180 : error = xfs_zero_extent(bma->ip, bma->blkno, bma->length);
4181 167378844 : if (error)
4182 : return error;
4183 176873958 : }
4184 41021006 :
4185 : if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur)
4186 28668 : bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4187 : /*
4188 41023978 : * Bump the number of extents we've allocated
4189 : * in this call.
4190 135852952 : */
4191 : bma->nallocs++;
4192 177101659 :
4193 : if (bma->cur)
4194 : bma->cur->bc_ino.flags =
4195 177096901 : bma->wasdel ? XFS_BTCUR_BMBT_WASDEL : 0;
4196 81958 :
4197 81958 : bma->got.br_startoff = bma->offset;
4198 : bma->got.br_startblock = bma->blkno;
4199 : bma->got.br_blockcount = bma->length;
4200 : bma->got.br_state = XFS_EXT_NORM;
4201 177096901 :
4202 53578117 : if (bma->flags & XFS_BMAPI_PREALLOC)
4203 : bma->got.br_state = XFS_EXT_UNWRITTEN;
4204 :
4205 : if (bma->wasdel)
4206 : error = xfs_bmap_add_extent_delay_real(bma, whichfork);
4207 177101034 : else
4208 : error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4209 177101034 : whichfork, &bma->icur, &bma->cur, &bma->got,
4210 53571122 : &bma->logflags, bma->flags);
4211 53571122 :
4212 : bma->logflags |= tmp_logflags;
4213 177101034 : if (error)
4214 177101034 : return error;
4215 177101034 :
4216 177101034 : /*
4217 : * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4218 177101034 : * or xfs_bmap_add_extent_hole_real might have merged it into one of
4219 135914439 : * the neighbouring ones.
4220 : */
4221 177101034 : xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4222 29293929 :
4223 : ASSERT(bma->got.br_startoff <= bma->offset);
4224 147807105 : ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4225 : bma->offset + bma->length);
4226 : ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4227 : bma->got.br_state == XFS_EXT_UNWRITTEN);
4228 177100696 : return 0;
4229 177100696 : }
4230 :
4231 : STATIC int
4232 : xfs_bmapi_convert_unwritten(
4233 : struct xfs_bmalloca *bma,
4234 : struct xfs_bmbt_irec *mval,
4235 : xfs_filblks_t len,
4236 : uint32_t flags)
4237 177082482 : {
4238 : int whichfork = xfs_bmapi_whichfork(flags);
4239 177088355 : struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4240 177088355 : int tmp_logflags = 0;
4241 : int error;
4242 177088355 :
4243 : /* check if we need to do unwritten->real conversion */
4244 : if (mval->br_state == XFS_EXT_UNWRITTEN &&
4245 : (flags & XFS_BMAPI_PREALLOC))
4246 : return 0;
4247 :
4248 237982159 : /* check if we need to do real->unwritten conversion */
4249 : if (mval->br_state == XFS_EXT_NORM &&
4250 : (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4251 : (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4252 : return 0;
4253 :
4254 237982159 : /*
4255 237982159 : * Modify (by adding) the state flag, if writing.
4256 237952289 : */
4257 237952289 : ASSERT(mval->br_blockcount <= len);
4258 : if (ifp->if_format == XFS_DINODE_FMT_BTREE && !bma->cur) {
4259 : bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4260 237952289 : bma->ip, whichfork);
4261 186779459 : }
4262 : mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4263 : ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4264 :
4265 127731882 : /*
4266 51168279 : * Before insertion into the bmbt, zero the range being converted
4267 : * if required.
4268 : */
4269 : if (flags & XFS_BMAPI_ZERO) {
4270 : error = xfs_zero_extent(bma->ip, mval->br_startblock,
4271 : mval->br_blockcount);
4272 : if (error)
4273 76563603 : return error;
4274 76563603 : }
4275 39360553 :
4276 : error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4277 : &bma->icur, &bma->cur, mval, &tmp_logflags);
4278 76563801 : /*
4279 76563801 : * Log the inode core unconditionally in the unwritten extent conversion
4280 : * path because the conversion might not have done so (e.g., if the
4281 : * extent count hasn't changed). We need to make sure the inode is dirty
4282 : * in the transaction for the sake of fsync(), even if nothing has
4283 : * changed, because fsync() will not force the log for this transaction
4284 : * unless it sees the inode pinned.
4285 76563801 : *
4286 279556 : * Note: If we're only converting cow fork extents, there aren't
4287 279556 : * any on-disk updates to make, so we don't need to log anything.
4288 279555 : */
4289 : if (whichfork != XFS_COW_FORK)
4290 : bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
4291 : if (error)
4292 76563800 : return error;
4293 :
4294 : /*
4295 : * Update our extent pointer, given that
4296 : * xfs_bmap_add_extent_unwritten_real might have merged it into one
4297 : * of the neighbouring ones.
4298 : */
4299 : xfs_iext_get_extent(ifp, &bma->icur, &bma->got);
4300 :
4301 : /*
4302 : * We may have combined previously unwritten space with written space,
4303 : * so generate another request.
4304 : */
4305 76563915 : if (mval->br_blockcount < len)
4306 76563915 : return -EAGAIN;
4307 76563915 : return 0;
4308 : }
4309 :
4310 : xfs_extlen_t
4311 : xfs_bmapi_minleft(
4312 : struct xfs_trans *tp,
4313 : struct xfs_inode *ip,
4314 : int fork)
4315 76563242 : {
4316 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, fork);
4317 :
4318 : if (tp && tp->t_highest_agno != NULLAGNUMBER)
4319 : return 0;
4320 : if (ifp->if_format != XFS_DINODE_FMT_BTREE)
4321 76562612 : return 1;
4322 1916309 : return be16_to_cpu(ifp->if_broot->bb_level) + 1;
4323 : }
4324 :
4325 : /*
4326 : * Log whatever the flags say, even if error. Otherwise we might miss detecting
4327 265758478 : * a case where the data is changed, there's an error, and it's not logged so we
4328 : * don't shutdown when we should. Don't bother logging extents/btree changes if
4329 : * we converted to the other format.
4330 : */
4331 : static void
4332 265758478 : xfs_bmapi_finish(
4333 : struct xfs_bmalloca *bma,
4334 265728267 : int whichfork,
4335 : int error)
4336 265665115 : {
4337 : struct xfs_ifork *ifp = xfs_ifork_ptr(bma->ip, whichfork);
4338 98381771 :
4339 : if ((bma->logflags & xfs_ilog_fext(whichfork)) &&
4340 : ifp->if_format != XFS_DINODE_FMT_EXTENTS)
4341 : bma->logflags &= ~xfs_ilog_fext(whichfork);
4342 : else if ((bma->logflags & xfs_ilog_fbroot(whichfork)) &&
4343 : ifp->if_format != XFS_DINODE_FMT_BTREE)
4344 : bma->logflags &= ~xfs_ilog_fbroot(whichfork);
4345 :
4346 : if (bma->logflags)
4347 : xfs_trans_log_inode(bma->tp, bma->ip, bma->logflags);
4348 265245841 : if (bma->cur)
4349 : xfs_btree_del_cursor(bma->cur, error);
4350 : }
4351 :
4352 : /*
4353 265245841 : * Map file blocks to filesystem blocks, and allocate blocks or convert the
4354 : * extent state if necessary. Details behaviour is controlled by the flags
4355 278607074 : * parameter. Only allocates blocks from a single allocation group, to avoid
4356 157122982 : * locking problems.
4357 2283707 : */
4358 276324347 : int
4359 0 : xfs_bmapi_write(
4360 0 : struct xfs_trans *tp, /* transaction pointer */
4361 : struct xfs_inode *ip, /* incore inode */
4362 265217919 : xfs_fileoff_t bno, /* starting file offs. mapped */
4363 243946118 : xfs_filblks_t len, /* length to map in file */
4364 265322774 : uint32_t flags, /* XFS_BMAPI_... */
4365 95227421 : xfs_extlen_t total, /* total blocks needed */
4366 265319326 : struct xfs_bmbt_irec *mval, /* output: map values */
4367 : int *nmap) /* i/o: mval size/count */
4368 : {
4369 : struct xfs_bmalloca bma = {
4370 : .tp = tp,
4371 : .ip = ip,
4372 : .total = total,
4373 : };
4374 : struct xfs_mount *mp = ip->i_mount;
4375 236075632 : int whichfork = xfs_bmapi_whichfork(flags);
4376 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4377 : xfs_fileoff_t end; /* end of mapped file region */
4378 : bool eof = false; /* after the end of extents */
4379 : int error; /* error return */
4380 : int n; /* current extent index */
4381 : xfs_fileoff_t obno; /* old block number (offset) */
4382 :
4383 : #ifdef DEBUG
4384 : xfs_fileoff_t orig_bno; /* original block number value */
4385 236075632 : int orig_flags; /* original flags arg value */
4386 : xfs_filblks_t orig_len; /* original value of len arg */
4387 : struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4388 : int orig_nmap; /* original value of *nmap */
4389 :
4390 236075632 : orig_bno = bno;
4391 236075632 : orig_len = len;
4392 236075632 : orig_flags = flags;
4393 235990025 : orig_mval = mval;
4394 235990025 : orig_nmap = *nmap;
4395 235990025 : #endif
4396 235990025 :
4397 235990025 : ASSERT(*nmap >= 1);
4398 : ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4399 : ASSERT(tp != NULL);
4400 235990025 : ASSERT(len > 0);
4401 235990025 : ASSERT(ifp->if_format != XFS_DINODE_FMT_LOCAL);
4402 235990025 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4403 235990025 : ASSERT(!(flags & XFS_BMAPI_REMAP));
4404 235990025 :
4405 : /* zeroing is for currently only for data extents, not metadata */
4406 235990025 : ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4407 235990025 : (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4408 235990025 : /*
4409 235990025 : * we can allocate unwritten extents or pre-zero allocated blocks,
4410 235990025 : * but it makes no sense to do both at once. This would result in
4411 : * zeroing the unwritten extent twice, but it still being an
4412 : * unwritten extent....
4413 235990025 : */
4414 235990025 : ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4415 235990025 : (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4416 235990025 :
4417 235990025 : if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4418 235990025 : XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4419 236003712 : xfs_bmap_mark_sick(ip, whichfork);
4420 : return -EFSCORRUPTED;
4421 : }
4422 236003712 :
4423 : if (xfs_is_shutdown(mp))
4424 : return -EIO;
4425 :
4426 : XFS_STATS_INC(mp, xs_blk_mapw);
4427 :
4428 : error = xfs_iread_extents(tp, ip, whichfork);
4429 : if (error)
4430 236003712 : goto error0;
4431 :
4432 : if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.icur, &bma.got))
4433 472002285 : eof = true;
4434 236003712 : if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4435 0 : bma.prev.br_startoff = NULLFILEOFF;
4436 0 : bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4437 :
4438 : n = 0;
4439 471997146 : end = bno + len;
4440 : obno = bno;
4441 : while (bno < end && n < *nmap) {
4442 235998509 : bool need_alloc = false, wasdelay = false;
4443 :
4444 236038848 : /* in hole or beyond EOF? */
4445 236025396 : if (eof || bma.got.br_startoff > bno) {
4446 0 : /*
4447 : * CoW fork conversions should /never/ hit EOF or
4448 236025396 : * holes. There should always be something for us
4449 99485194 : * to work on.
4450 236009985 : */
4451 64182690 : ASSERT(!((flags & XFS_BMAPI_CONVERT) &&
4452 235946742 : (flags & XFS_BMAPI_COWFORK)));
4453 :
4454 235945188 : need_alloc = true;
4455 235945188 : } else if (isnullstartblock(bma.got.br_startblock)) {
4456 235945188 : wasdelay = true;
4457 237825247 : }
4458 237825247 :
4459 : /*
4460 : * First, deal with the hole before the allocated space
4461 237825247 : * that we found, if any.
4462 : */
4463 : if (need_alloc || wasdelay) {
4464 : bma.eof = eof;
4465 : bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4466 : bma.wasdel = wasdelay;
4467 147630458 : bma.offset = bno;
4468 : bma.flags = flags;
4469 :
4470 : /*
4471 90194789 : * There's a 32/64 bit type mismatch between the
4472 105260 : * allocation length request (which can be 64 bits in
4473 : * length) and the bma length request, which is
4474 : * xfs_extlen_t and therefore 32 bits. Hence we have to
4475 : * check for 32-bit overflows and handle them here.
4476 : */
4477 : if (len > (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN)
4478 : bma.length = XFS_MAX_BMBT_EXTLEN;
4479 237825247 : else
4480 147735780 : bma.length = len;
4481 147735780 :
4482 147735780 : ASSERT(len > 0);
4483 147735780 : ASSERT(bma.length > 0);
4484 147735780 : error = xfs_bmapi_allocate(&bma);
4485 : if (error)
4486 : goto error0;
4487 : if (bma.blkno == NULLFSBLOCK)
4488 : break;
4489 :
4490 : /*
4491 : * If this is a CoW allocation, record the data in
4492 : * the refcount btree for orphan recovery.
4493 147735780 : */
4494 112187 : if (whichfork == XFS_COW_FORK)
4495 : xfs_refcount_alloc_cow_extent(tp,
4496 147623593 : XFS_IS_REALTIME_INODE(ip),
4497 : bma.blkno, bma.length);
4498 147735780 : }
4499 147735780 :
4500 147735780 : /* Deal with the allocated space we found. */
4501 147924189 : xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4502 1985 : end, n, flags);
4503 147922204 :
4504 : /* Execute unwritten extent conversion if necessary */
4505 : error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
4506 : if (error == -EAGAIN)
4507 : continue;
4508 : if (error)
4509 : goto error0;
4510 147922042 :
4511 1229963 : /* update the extent map to return */
4512 1229963 : xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4513 :
4514 : /*
4515 : * If we're done, stop now. Stop when we've allocated
4516 : * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4517 238011517 : * the transaction may get too big.
4518 : */
4519 : if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4520 : break;
4521 237955487 :
4522 237964148 : /* Else go on to the next record. */
4523 1916301 : bma.prev = bma.got;
4524 236047847 : if (!xfs_iext_next_extent(ifp, &bma.icur, &bma.got))
4525 14 : eof = true;
4526 : }
4527 : *nmap = n;
4528 236047833 :
4529 : error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4530 : whichfork);
4531 : if (error)
4532 : goto error0;
4533 :
4534 : ASSERT(ifp->if_format != XFS_DINODE_FMT_BTREE ||
4535 236029895 : ifp->if_nextents > XFS_IFORK_MAXEXT(ip, whichfork));
4536 : xfs_bmapi_finish(&bma, whichfork, 0);
4537 : xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4538 : orig_nmap, *nmap);
4539 372 : return 0;
4540 372 : error0:
4541 99 : xfs_bmapi_finish(&bma, whichfork, error);
4542 : return error;
4543 236029685 : }
4544 :
4545 236029685 : /*
4546 : * Convert an existing delalloc extent to real blocks based on file offset. This
4547 236018316 : * attempts to allocate the entire delalloc extent and may require multiple
4548 0 : * invocations to allocate the target offset if a large enough physical extent
4549 : * is not available.
4550 236018316 : */
4551 : int
4552 236018316 : xfs_bmapi_convert_delalloc(
4553 236112653 : struct xfs_inode *ip,
4554 : int whichfork,
4555 236112653 : xfs_off_t offset,
4556 1999 : struct iomap *iomap,
4557 1999 : unsigned int *seq)
4558 1999 : {
4559 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4560 : struct xfs_mount *mp = ip->i_mount;
4561 : xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
4562 : struct xfs_bmalloca bma = { NULL };
4563 : uint16_t flags = 0;
4564 : struct xfs_trans *tp;
4565 : int error;
4566 :
4567 : if (whichfork == XFS_COW_FORK)
4568 31804685 : flags |= IOMAP_F_SHARED;
4569 :
4570 : /*
4571 : * Space for the extent and indirect blocks was reserved when the
4572 : * delalloc extent was created so there's no need to do so here.
4573 : */
4574 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0,
4575 31804685 : XFS_TRANS_RESERVE, &tp);
4576 31804524 : if (error)
4577 31804524 : return error;
4578 31804524 :
4579 31804524 : xfs_ilock(ip, XFS_ILOCK_EXCL);
4580 31804524 : xfs_trans_ijoin(tp, ip, 0);
4581 31804524 :
4582 : error = xfs_iext_count_may_overflow(ip, whichfork,
4583 31804524 : XFS_IEXT_ADD_NOSPLIT_CNT);
4584 6274318 : if (error == -EFBIG)
4585 : error = xfs_iext_count_upgrade(tp, ip,
4586 : XFS_IEXT_ADD_NOSPLIT_CNT);
4587 : if (error)
4588 : goto out_trans_cancel;
4589 :
4590 31804524 : if (!xfs_iext_lookup_extent(ip, ifp, offset_fsb, &bma.icur, &bma.got) ||
4591 : bma.got.br_startoff > offset_fsb) {
4592 31804490 : /*
4593 : * No extent found in the range we are trying to convert. This
4594 : * should only happen for the COW fork, where another thread
4595 31804481 : * might have moved the extent to the data fork in the meantime.
4596 31804488 : */
4597 : WARN_ON_ONCE(whichfork != XFS_COW_FORK);
4598 31804649 : error = -EAGAIN;
4599 : goto out_trans_cancel;
4600 31804489 : }
4601 0 :
4602 : /*
4603 31804489 : * If we find a real extent here we raced with another thread converting
4604 0 : * the extent. Just return the real extent at this offset.
4605 : */
4606 31804489 : if (!isnullstartblock(bma.got.br_startblock)) {
4607 31804719 : xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4608 : xfs_iomap_inode_sequence(ip, flags));
4609 : *seq = READ_ONCE(ifp->if_seq);
4610 : goto out_trans_cancel;
4611 : }
4612 :
4613 0 : bma.tp = tp;
4614 0 : bma.ip = ip;
4615 0 : bma.wasdel = true;
4616 : bma.offset = bma.got.br_startoff;
4617 : bma.length = max_t(xfs_filblks_t, bma.got.br_blockcount,
4618 : XFS_MAX_BMBT_EXTLEN);
4619 : bma.minleft = xfs_bmapi_minleft(tp, ip, whichfork);
4620 :
4621 : /*
4622 31804719 : * When we're converting the delalloc reservations backing dirty pages
4623 2611792 : * in the page cache, we must be careful about how we create the new
4624 : * extents:
4625 2611772 : *
4626 2611772 : * New CoW fork extents are created unwritten, turned into real extents
4627 : * when we're about to write the data to disk, and mapped into the data
4628 : * fork after the write finishes. End of story.
4629 29192927 : *
4630 29192927 : * New data fork extents must be mapped in as unwritten and converted
4631 29192927 : * to real extents after the write succeeds to avoid exposing stale
4632 29192927 : * disk contents if we crash.
4633 29192927 : */
4634 : bma.flags = XFS_BMAPI_PREALLOC;
4635 29192927 : if (whichfork == XFS_COW_FORK)
4636 : bma.flags |= XFS_BMAPI_COWFORK;
4637 :
4638 : if (!xfs_iext_peek_prev_extent(ifp, &bma.icur, &bma.prev))
4639 : bma.prev.br_startoff = NULLFILEOFF;
4640 :
4641 : error = xfs_bmapi_allocate(&bma);
4642 : if (error)
4643 : goto out_finish;
4644 :
4645 : error = -ENOSPC;
4646 : if (WARN_ON_ONCE(bma.blkno == NULLFSBLOCK))
4647 : goto out_finish;
4648 : if (WARN_ON_ONCE(!xfs_valid_startblock(ip, bma.got.br_startblock))) {
4649 : xfs_bmap_mark_sick(ip, whichfork);
4650 29192732 : error = -EFSCORRUPTED;
4651 29192732 : goto out_finish;
4652 3662621 : }
4653 :
4654 29192732 : XFS_STATS_ADD(mp, xs_xstrat_bytes, XFS_FSB_TO_B(mp, bma.length));
4655 7452674 : XFS_STATS_INC(mp, xs_xstrat_quick);
4656 :
4657 29192698 : ASSERT(!isnullstartblock(bma.got.br_startblock));
4658 29192741 : xfs_bmbt_to_iomap(ip, iomap, &bma.got, 0, flags,
4659 3923 : xfs_iomap_inode_sequence(ip, flags));
4660 : *seq = READ_ONCE(ifp->if_seq);
4661 29188818 :
4662 29188818 : if (whichfork == XFS_COW_FORK)
4663 0 : xfs_refcount_alloc_cow_extent(tp, XFS_IS_REALTIME_INODE(ip),
4664 58377636 : bma.blkno, bma.length);
4665 0 :
4666 0 : error = xfs_bmap_btree_to_extents(tp, ip, bma.cur, &bma.logflags,
4667 0 : whichfork);
4668 : if (error)
4669 : goto out_finish;
4670 29188818 :
4671 29188681 : xfs_bmapi_finish(&bma, whichfork, 0);
4672 : error = xfs_trans_commit(tp);
4673 29188781 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
4674 29188781 : return error;
4675 :
4676 29188584 : out_finish:
4677 : xfs_bmapi_finish(&bma, whichfork, error);
4678 29188584 : out_trans_cancel:
4679 7324858 : xfs_trans_cancel(tp);
4680 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
4681 : return error;
4682 29188731 : }
4683 :
4684 29188168 : int
4685 0 : xfs_bmapi_remap(
4686 : struct xfs_trans *tp,
4687 29188168 : struct xfs_inode *ip,
4688 29188445 : xfs_fileoff_t bno,
4689 29188995 : xfs_filblks_t len,
4690 29188995 : xfs_fsblock_t startblock,
4691 : uint32_t flags)
4692 3923 : {
4693 3923 : struct xfs_mount *mp = ip->i_mount;
4694 2615695 : struct xfs_ifork *ifp;
4695 2615695 : struct xfs_btree_cur *cur = NULL;
4696 2615718 : struct xfs_bmbt_irec got;
4697 2615718 : struct xfs_iext_cursor icur;
4698 : int whichfork = xfs_bmapi_whichfork(flags);
4699 : int logflags = 0, error;
4700 :
4701 161479192 : ifp = xfs_ifork_ptr(ip, whichfork);
4702 : ASSERT(len > 0);
4703 : ASSERT(len <= (xfs_filblks_t)XFS_MAX_BMBT_EXTLEN);
4704 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4705 : ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC |
4706 : XFS_BMAPI_NORMAP)));
4707 : ASSERT((flags & (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC)) !=
4708 : (XFS_BMAPI_ATTRFORK | XFS_BMAPI_PREALLOC));
4709 161479192 :
4710 161479192 : if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
4711 161479192 : XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
4712 161479192 : xfs_bmap_mark_sick(ip, whichfork);
4713 161479192 : return -EFSCORRUPTED;
4714 161479192 : }
4715 161479192 :
4716 : if (xfs_is_shutdown(mp))
4717 161479192 : return -EIO;
4718 161479152 :
4719 161479152 : error = xfs_iread_extents(tp, ip, whichfork);
4720 161479152 : if (error)
4721 161479162 : return error;
4722 :
4723 161479162 : if (xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
4724 : /* make sure we only reflink into a hole. */
4725 : ASSERT(got.br_startoff > bno);
4726 322958317 : ASSERT(got.br_startoff - bno >= len);
4727 161479162 : }
4728 0 :
4729 0 : ip->i_nblocks += len;
4730 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4731 :
4732 322958310 : if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
4733 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
4734 : cur->bc_ino.flags = 0;
4735 161479152 : }
4736 161479149 :
4737 : got.br_startoff = bno;
4738 : got.br_startblock = startblock;
4739 161479152 : got.br_blockcount = len;
4740 : if (flags & XFS_BMAPI_PREALLOC)
4741 38859831 : got.br_state = XFS_EXT_UNWRITTEN;
4742 38859831 : else
4743 : got.br_state = XFS_EXT_NORM;
4744 :
4745 161479223 : error = xfs_bmap_add_extent_hole_real(tp, ip, whichfork, &icur,
4746 161479223 : &cur, &got, &logflags, flags);
4747 : if (error)
4748 161479230 : goto error0;
4749 141495236 :
4750 141495240 : error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags, whichfork);
4751 :
4752 : error0:
4753 161479234 : if (ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS)
4754 161479234 : logflags &= ~XFS_ILOG_DEXT;
4755 161479234 : else if (ip->i_df.if_format != XFS_DINODE_FMT_BTREE)
4756 161479234 : logflags &= ~XFS_ILOG_DBROOT;
4757 2662656 :
4758 : if (logflags)
4759 158816578 : xfs_trans_log_inode(tp, ip, logflags);
4760 : if (cur)
4761 161479234 : xfs_btree_del_cursor(cur, error);
4762 : return error;
4763 161479187 : }
4764 3 :
4765 : /*
4766 161479184 : * When a delalloc extent is split (e.g., due to a hole punch), the original
4767 : * indlen reservation must be shared across the two new extents that are left
4768 161479139 : * behind.
4769 161479139 : *
4770 142606978 : * Given the original reservation and the worst case indlen for the two new
4771 18872161 : * extents (as calculated by xfs_bmap_worst_indlen()), split the original
4772 18872161 : * reservation fairly across the two new extents. If necessary, steal available
4773 : * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4774 161479139 : * ores == 1). The number of stolen blocks is returned. The availability and
4775 153962524 : * subsequent accounting of stolen blocks is the responsibility of the caller.
4776 161479241 : */
4777 142611685 : static xfs_filblks_t
4778 : xfs_bmap_split_indlen(
4779 : xfs_filblks_t ores, /* original res. */
4780 : xfs_filblks_t *indlen1, /* ext1 worst indlen */
4781 : xfs_filblks_t *indlen2, /* ext2 worst indlen */
4782 : xfs_filblks_t avail) /* stealable blocks */
4783 : {
4784 : xfs_filblks_t len1 = *indlen1;
4785 : xfs_filblks_t len2 = *indlen2;
4786 : xfs_filblks_t nres = len1 + len2; /* new total res. */
4787 : xfs_filblks_t stolen = 0;
4788 : xfs_filblks_t resfactor;
4789 :
4790 : /*
4791 : * Steal as many blocks as we can to try and satisfy the worst case
4792 : * indlen for both new extents.
4793 : */
4794 26642 : if (ores < nres && avail)
4795 : stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4796 : ores += stolen;
4797 :
4798 : /* nothing else to do if we've satisfied the new reservation */
4799 : if (ores >= nres)
4800 26642 : return stolen;
4801 26642 :
4802 26642 : /*
4803 26642 : * We can't meet the total required reservation for the two extents.
4804 26642 : * Calculate the percent of the overall shortage between both extents
4805 : * and apply this percentage to each of the requested indlen values.
4806 : * This distributes the shortage fairly and reduces the chances that one
4807 : * of the two extents is left with nothing when extents are repeatedly
4808 : * split.
4809 : */
4810 26642 : resfactor = (ores * 100);
4811 26642 : do_div(resfactor, nres);
4812 26642 : len1 *= resfactor;
4813 : do_div(len1, 100);
4814 : len2 *= resfactor;
4815 26642 : do_div(len2, 100);
4816 : ASSERT(len1 + len2 <= ores);
4817 : ASSERT(len1 < *indlen1 && len2 < *indlen2);
4818 :
4819 : /*
4820 : * Hand out the remainder to each extent. If one of the two reservations
4821 : * is zero, we want to make sure that one gets a block first. The loop
4822 : * below starts with len1, so hand len2 a block right off the bat if it
4823 : * is zero.
4824 : */
4825 : ores -= (len1 + len2);
4826 17604 : ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4827 17604 : if (ores && !len2 && *indlen2) {
4828 17604 : len2++;
4829 17604 : ores--;
4830 17604 : }
4831 17604 : while (ores) {
4832 17604 : if (len1 < *indlen1) {
4833 17604 : len1++;
4834 : ores--;
4835 : }
4836 : if (!ores)
4837 : break;
4838 : if (len2 < *indlen2) {
4839 : len2++;
4840 : ores--;
4841 17604 : }
4842 17604 : }
4843 17604 :
4844 0 : *indlen1 = len1;
4845 0 : *indlen2 = len2;
4846 :
4847 27170 : return stolen;
4848 17604 : }
4849 17604 :
4850 17604 : int
4851 : xfs_bmap_del_extent_delay(
4852 17604 : struct xfs_inode *ip,
4853 : int whichfork,
4854 9566 : struct xfs_iext_cursor *icur,
4855 9566 : struct xfs_bmbt_irec *got,
4856 9566 : struct xfs_bmbt_irec *del)
4857 : {
4858 : struct xfs_mount *mp = ip->i_mount;
4859 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
4860 17604 : struct xfs_bmbt_irec new;
4861 17604 : int64_t da_old, da_new, da_diff = 0;
4862 : xfs_fileoff_t del_endoff, got_endoff;
4863 17604 : xfs_filblks_t got_indlen, new_indlen, stolen;
4864 : uint32_t state = xfs_bmap_fork_to_state(whichfork);
4865 : int error = 0;
4866 : bool isrt;
4867 8982362 :
4868 : XFS_STATS_INC(mp, xs_del_exlist);
4869 :
4870 : isrt = xfs_ifork_is_realtime(ip, whichfork);
4871 : del_endoff = del->br_startoff + del->br_blockcount;
4872 : got_endoff = got->br_startoff + got->br_blockcount;
4873 : da_old = startblockval(got->br_startblock);
4874 8982362 : da_new = 0;
4875 8982362 :
4876 8949263 : ASSERT(del->br_blockcount > 0);
4877 8949263 : ASSERT(got->br_startoff <= del->br_startoff);
4878 8949263 : ASSERT(got_endoff >= del_endoff);
4879 8949263 :
4880 8949263 : if (isrt)
4881 8949263 : xfs_mod_frextents(mp, xfs_rtb_to_rtxt(mp, del->br_blockcount));
4882 8949263 :
4883 : /*
4884 8949263 : * Update the inode delalloc counter now and wait to update the
4885 : * sb counters as we might have to borrow some blocks for the
4886 8951787 : * indirect block accounting.
4887 8963361 : */
4888 8963361 : ASSERT(!isrt);
4889 8963361 : error = xfs_quota_unreserve_blkres(ip, del->br_blockcount);
4890 8963361 : if (error)
4891 : return error;
4892 8963361 : ip->i_delayed_blks -= del->br_blockcount;
4893 8963361 :
4894 8963361 : if (got->br_startoff == del->br_startoff)
4895 : state |= BMAP_LEFT_FILLING;
4896 8963361 : if (got_endoff == del_endoff)
4897 0 : state |= BMAP_RIGHT_FILLING;
4898 :
4899 : switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4900 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
4901 : /*
4902 : * Matches the whole extent. Delete the entry.
4903 : */
4904 8963361 : xfs_iext_remove(ip, icur, state);
4905 8963361 : xfs_iext_prev(ifp, icur);
4906 9007669 : break;
4907 : case BMAP_LEFT_FILLING:
4908 9007669 : /*
4909 : * Deleting the first part of the extent.
4910 9007669 : */
4911 6258754 : got->br_startoff = del_endoff;
4912 9007669 : got->br_blockcount -= del->br_blockcount;
4913 8956579 : da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4914 : got->br_blockcount), da_old);
4915 9007669 : got->br_startblock = nullstartblock((int)da_new);
4916 6226929 : xfs_iext_update_extent(ip, state, icur, got);
4917 : break;
4918 : case BMAP_RIGHT_FILLING:
4919 : /*
4920 6226929 : * Deleting the last part of the extent.
4921 6221513 : */
4922 6221513 : got->br_blockcount = got->br_blockcount - del->br_blockcount;
4923 27996 : da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4924 : got->br_blockcount), da_old);
4925 : got->br_startblock = nullstartblock((int)da_new);
4926 : xfs_iext_update_extent(ip, state, icur, got);
4927 27996 : break;
4928 27996 : case 0:
4929 27996 : /*
4930 : * Deleting the middle of the extent.
4931 27996 : *
4932 27996 : * Distribute the original indlen reservation across the two new
4933 27996 : * extents. Steal blocks from the deleted extent if necessary.
4934 2726102 : * Stealing blocks simply fudges the fdblocks accounting below.
4935 : * Warn if either of the new indlen reservations is zero as this
4936 : * can lead to delalloc problems.
4937 : */
4938 2726102 : got->br_blockcount = del->br_startoff - got->br_startoff;
4939 2726102 : got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4940 :
4941 2722111 : new.br_blockcount = got_endoff - del_endoff;
4942 2719624 : new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4943 2719624 :
4944 26642 : WARN_ON_ONCE(!got_indlen || !new_indlen);
4945 : stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4946 : del->br_blockcount);
4947 :
4948 : got->br_startblock = nullstartblock((int)got_indlen);
4949 :
4950 : new.br_startoff = del_endoff;
4951 : new.br_state = got->br_state;
4952 : new.br_startblock = nullstartblock((int)new_indlen);
4953 :
4954 26642 : xfs_iext_update_extent(ip, state, icur, got);
4955 26642 : xfs_iext_next(ifp, icur);
4956 : xfs_iext_insert(ip, icur, &new, state);
4957 26642 :
4958 26642 : da_new = got_indlen + new_indlen - stolen;
4959 : del->br_blockcount -= stolen;
4960 26642 : break;
4961 26642 : }
4962 :
4963 : ASSERT(da_old >= da_new);
4964 26642 : da_diff = da_old - da_new;
4965 : if (!isrt)
4966 26642 : da_diff += del->br_blockcount;
4967 26642 : if (da_diff) {
4968 26642 : xfs_mod_fdblocks(mp, da_diff, false);
4969 : xfs_mod_delalloc(mp, -da_diff);
4970 26642 : }
4971 26642 : return error;
4972 26642 : }
4973 :
4974 26642 : void
4975 26642 : xfs_bmap_del_extent_cow(
4976 26642 : struct xfs_inode *ip,
4977 : struct xfs_iext_cursor *icur,
4978 : struct xfs_bmbt_irec *got,
4979 8975293 : struct xfs_bmbt_irec *del)
4980 8975293 : {
4981 8975293 : struct xfs_mount *mp = ip->i_mount;
4982 8977416 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_COW_FORK);
4983 8975293 : struct xfs_bmbt_irec new;
4984 8956174 : xfs_fileoff_t del_endoff, got_endoff;
4985 8965568 : uint32_t state = BMAP_COWFORK;
4986 :
4987 : XFS_STATS_INC(mp, xs_del_exlist);
4988 :
4989 : del_endoff = del->br_startoff + del->br_blockcount;
4990 : got_endoff = got->br_startoff + got->br_blockcount;
4991 21009864 :
4992 : ASSERT(del->br_blockcount > 0);
4993 : ASSERT(got->br_startoff <= del->br_startoff);
4994 : ASSERT(got_endoff >= del_endoff);
4995 : ASSERT(!isnullstartblock(got->br_startblock));
4996 :
4997 21009864 : if (got->br_startoff == del->br_startoff)
4998 21009864 : state |= BMAP_LEFT_FILLING;
4999 21009864 : if (got_endoff == del_endoff)
5000 21009864 : state |= BMAP_RIGHT_FILLING;
5001 21009864 :
5002 : switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5003 21009864 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5004 : /*
5005 21009507 : * Matches the whole extent. Delete the entry.
5006 21009507 : */
5007 : xfs_iext_remove(ip, icur, state);
5008 21009507 : xfs_iext_prev(ifp, icur);
5009 21009507 : break;
5010 21009507 : case BMAP_LEFT_FILLING:
5011 21009507 : /*
5012 : * Deleting the first part of the extent.
5013 21009507 : */
5014 20400667 : got->br_startoff = del_endoff;
5015 21009507 : got->br_blockcount -= del->br_blockcount;
5016 17820303 : got->br_startblock = del->br_startblock + del->br_blockcount;
5017 : xfs_iext_update_extent(ip, state, icur, got);
5018 21009507 : break;
5019 17267847 : case BMAP_RIGHT_FILLING:
5020 : /*
5021 : * Deleting the last part of the extent.
5022 : */
5023 17267847 : got->br_blockcount -= del->br_blockcount;
5024 17267732 : xfs_iext_update_extent(ip, state, icur, got);
5025 17267732 : break;
5026 3132743 : case 0:
5027 : /*
5028 : * Deleting the middle of the extent.
5029 : */
5030 3132743 : got->br_blockcount = del->br_startoff - got->br_startoff;
5031 3132743 :
5032 3132743 : new.br_startoff = del_endoff;
5033 3132743 : new.br_blockcount = got_endoff - del_endoff;
5034 3132743 : new.br_state = got->br_state;
5035 552417 : new.br_startblock = del->br_startblock + del->br_blockcount;
5036 :
5037 : xfs_iext_update_extent(ip, state, icur, got);
5038 : xfs_iext_next(ifp, icur);
5039 552417 : xfs_iext_insert(ip, icur, &new, state);
5040 552417 : break;
5041 552417 : }
5042 56500 : ip->i_delayed_blks -= del->br_blockcount;
5043 : }
5044 :
5045 : /*
5046 56500 : * Called by xfs_bmapi to update file extent records and the btree
5047 : * after removing space.
5048 56500 : */
5049 56500 : STATIC int /* error */
5050 56500 : xfs_bmap_del_extent_real(
5051 56500 : xfs_inode_t *ip, /* incore inode pointer */
5052 : xfs_trans_t *tp, /* current transaction pointer */
5053 56500 : struct xfs_iext_cursor *icur,
5054 56500 : struct xfs_btree_cur *cur, /* if null, not a btree */
5055 56500 : xfs_bmbt_irec_t *del, /* data to remove from extents */
5056 56500 : int *logflagsp, /* inode logging flags */
5057 : int whichfork, /* data or attr fork */
5058 21010147 : uint32_t bflags) /* bmapi flags */
5059 21010147 : {
5060 : xfs_fsblock_t del_endblock=0; /* first block past del */
5061 : xfs_fileoff_t del_endoff; /* first offset past del */
5062 : int error; /* error return value */
5063 : int flags = 0;/* inode logging flags */
5064 : struct xfs_bmbt_irec got; /* current extent entry */
5065 : xfs_fileoff_t got_endoff; /* first offset past got */
5066 214545786 : int i; /* temp state */
5067 : struct xfs_ifork *ifp; /* inode fork pointer */
5068 : xfs_mount_t *mp; /* mount structure */
5069 : xfs_filblks_t nblks; /* quota/sb block count */
5070 : xfs_bmbt_irec_t new; /* new record to be inserted */
5071 : /* REFERENCED */
5072 : uint qfield; /* quota field to update */
5073 : uint32_t state = xfs_bmap_fork_to_state(whichfork);
5074 : struct xfs_bmbt_irec old;
5075 : bool isrt = xfs_ifork_is_realtime(ip, whichfork);
5076 214545786 : bool want_free = !(bflags & XFS_BMAPI_REMAP);
5077 214545786 :
5078 214545786 : mp = ip->i_mount;
5079 214545786 : XFS_STATS_INC(mp, xs_del_exlist);
5080 214545786 :
5081 214545786 : ifp = xfs_ifork_ptr(ip, whichfork);
5082 214545786 : ASSERT(del->br_blockcount > 0);
5083 214545786 : xfs_iext_get_extent(ifp, icur, &got);
5084 214545786 : ASSERT(got.br_startoff <= del->br_startoff);
5085 214545786 : del_endoff = del->br_startoff + del->br_blockcount;
5086 214545786 : got_endoff = got.br_startoff + got.br_blockcount;
5087 : ASSERT(got_endoff >= del_endoff);
5088 214545786 : ASSERT(!isnullstartblock(got.br_startblock));
5089 214545786 : qfield = 0;
5090 214545786 : error = 0;
5091 214545786 :
5092 214482805 : /*
5093 : * If it's the case where the directory code is running with no block
5094 214482805 : * reservation, and the deleted block is in the middle of its extent,
5095 214482805 : * and the resulting insert of an extent would cause transformation to
5096 : * btree format, then reject it. The calling code will then swap blocks
5097 214502772 : * around instead. We have to do this now, rather than waiting for the
5098 214497546 : * conversion to btree format, since the transaction will be dirty then.
5099 214497546 : */
5100 214491386 : if (tp->t_blk_res == 0 &&
5101 214491386 : ifp->if_format == XFS_DINODE_FMT_EXTENTS &&
5102 214491386 : ifp->if_nextents >= XFS_IFORK_MAXEXT(ip, whichfork) &&
5103 214491386 : del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5104 214491386 : return -ENOSPC;
5105 214491386 :
5106 214491386 : flags = XFS_ILOG_CORE;
5107 : if (isrt) {
5108 : /*
5109 : * Historically, we did not use EFIs to free realtime extents.
5110 : * However, when reverse mapping is enabled, we must maintain
5111 : * the same order of operations as the data device, which is:
5112 : * Remove the file mapping, remove the reverse mapping, and
5113 : * then free the blocks. This means that we must delay the
5114 : * freeing until after we've scheduled the rmap update. If
5115 : * realtime reflink is enabled, use deferred refcount intent
5116 214491386 : * items to decide what to do with the extent, just like we do
5117 109507328 : * for the data device.
5118 52120305 : */
5119 686513 : if (want_free && !xfs_has_rtrmapbt(mp) &&
5120 : !xfs_has_rtreflink(mp)) {
5121 : error = xfs_rtfree_blocks(tp, del->br_startblock,
5122 214491386 : del->br_blockcount);
5123 214491386 : if (error)
5124 : goto done;
5125 : want_free = false;
5126 : }
5127 : qfield = XFS_TRANS_DQ_RTBCOUNT;
5128 : } else {
5129 : qfield = XFS_TRANS_DQ_BCOUNT;
5130 : }
5131 : nblks = del->br_blockcount;
5132 :
5133 : del_endblock = del->br_startblock + del->br_blockcount;
5134 : if (cur) {
5135 64691329 : error = xfs_bmbt_lookup_eq(cur, &got, &i);
5136 8980133 : if (error)
5137 8980133 : goto done;
5138 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5139 8980133 : xfs_btree_mark_sick(cur);
5140 0 : error = -EFSCORRUPTED;
5141 : goto done;
5142 : }
5143 : }
5144 :
5145 : if (got.br_startoff == del->br_startoff)
5146 : state |= BMAP_LEFT_FILLING;
5147 214491386 : if (got_endoff == del_endoff)
5148 : state |= BMAP_RIGHT_FILLING;
5149 214491386 :
5150 214491386 : switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5151 129732930 : case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
5152 129733315 : /*
5153 1 : * Matches the whole extent. Delete the entry.
5154 129733314 : */
5155 0 : xfs_iext_remove(ip, icur, state);
5156 0 : xfs_iext_prev(ifp, icur);
5157 0 : ifp->if_nextents--;
5158 :
5159 : flags |= XFS_ILOG_CORE;
5160 : if (!cur) {
5161 214491770 : flags |= xfs_ilog_fext(whichfork);
5162 164406644 : break;
5163 214491770 : }
5164 159765520 : if ((error = xfs_btree_delete(cur, &i)))
5165 : goto done;
5166 214491770 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5167 147840497 : xfs_btree_mark_sick(cur);
5168 : error = -EFSCORRUPTED;
5169 : goto done;
5170 : }
5171 147840497 : break;
5172 147976490 : case BMAP_LEFT_FILLING:
5173 147926115 : /*
5174 : * Deleting the first part of the extent.
5175 147926115 : */
5176 147926115 : got.br_startoff = del_endoff;
5177 67775381 : got.br_startblock = del_endblock;
5178 67775381 : got.br_blockcount -= del->br_blockcount;
5179 : xfs_iext_update_extent(ip, state, icur, &got);
5180 80150734 : if (!cur) {
5181 1 : flags |= xfs_ilog_fext(whichfork);
5182 80150279 : break;
5183 0 : }
5184 0 : error = xfs_bmbt_update(cur, &got);
5185 0 : if (error)
5186 : goto done;
5187 : break;
5188 16554274 : case BMAP_RIGHT_FILLING:
5189 : /*
5190 : * Deleting the last part of the extent.
5191 : */
5192 16554274 : got.br_blockcount -= del->br_blockcount;
5193 16554274 : xfs_iext_update_extent(ip, state, icur, &got);
5194 16554274 : if (!cur) {
5195 16554274 : flags |= xfs_ilog_fext(whichfork);
5196 16554286 : break;
5197 6672138 : }
5198 6672138 : error = xfs_bmbt_update(cur, &got);
5199 : if (error)
5200 9882148 : goto done;
5201 9882127 : break;
5202 0 : case 0:
5203 : /*
5204 11919916 : * Deleting the middle of the extent.
5205 : */
5206 :
5207 : old = got;
5208 11919916 :
5209 11919916 : got.br_blockcount = del->br_startoff - got.br_startoff;
5210 11919905 : xfs_iext_update_extent(ip, state, icur, &got);
5211 5793985 :
5212 5793985 : new.br_startoff = del_endoff;
5213 : new.br_blockcount = got_endoff - del_endoff;
5214 6125920 : new.br_state = got.br_state;
5215 6125885 : new.br_startblock = del_endblock;
5216 0 :
5217 : flags |= XFS_ILOG_CORE;
5218 38177083 : if (cur) {
5219 : error = xfs_bmbt_update(cur, &got);
5220 : if (error)
5221 : goto done;
5222 : error = xfs_btree_increment(cur, 0, &i);
5223 38177083 : if (error)
5224 : goto done;
5225 38177083 : cur->bc_rec.b = new;
5226 38177083 : error = xfs_btree_insert(cur, &i);
5227 : if (error && error != -ENOSPC)
5228 38176973 : goto done;
5229 38176973 : /*
5230 38176973 : * If get no-space back from btree insert, it tried a
5231 38176973 : * split, and we have a zero block reservation. Fix up
5232 : * our state and return the error.
5233 38176973 : */
5234 38176973 : if (error == -ENOSPC) {
5235 33574236 : /*
5236 33574052 : * Reset the cursor, don't trust it after any
5237 0 : * insert operation.
5238 33574052 : */
5239 33574017 : error = xfs_bmbt_lookup_eq(cur, &got, &i);
5240 0 : if (error)
5241 33574017 : goto done;
5242 33574017 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5243 33574194 : xfs_btree_mark_sick(cur);
5244 0 : error = -EFSCORRUPTED;
5245 : goto done;
5246 : }
5247 : /*
5248 : * Update the btree record back
5249 : * to the original value.
5250 33574194 : */
5251 : error = xfs_bmbt_update(cur, &old);
5252 : if (error)
5253 : goto done;
5254 : /*
5255 0 : * Reset the extent record back
5256 0 : * to the original value.
5257 0 : */
5258 0 : xfs_iext_update_extent(ip, state, icur, &old);
5259 0 : flags = 0;
5260 0 : error = -ENOSPC;
5261 0 : goto done;
5262 : }
5263 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5264 : xfs_btree_mark_sick(cur);
5265 : error = -EFSCORRUPTED;
5266 : goto done;
5267 0 : }
5268 0 : } else
5269 0 : flags |= xfs_ilog_fext(whichfork);
5270 :
5271 : ifp->if_nextents++;
5272 : xfs_iext_next(ifp, icur);
5273 : xfs_iext_insert(ip, icur, &new, state);
5274 0 : break;
5275 0 : }
5276 0 :
5277 0 : /* remove reverse mapping */
5278 : xfs_rmap_unmap_extent(tp, ip, whichfork, del);
5279 33574194 :
5280 0 : /*
5281 0 : * If we need to, add to list of extents to delete.
5282 0 : */
5283 : if (want_free) {
5284 : if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5285 4602813 : xfs_refcount_decrease_extent(tp, isrt, del);
5286 : } else {
5287 38176931 : unsigned int efi_flags = 0;
5288 38176931 :
5289 38176836 : if ((bflags & XFS_BMAPI_NODISCARD) ||
5290 38176836 : del->br_state == XFS_EXT_UNWRITTEN)
5291 : efi_flags |= XFS_FREE_EXTENT_SKIP_DISCARD;
5292 : if (isrt)
5293 : efi_flags |= XFS_FREE_EXTENT_REALTIME;
5294 214576763 :
5295 : error = xfs_free_extent_later(tp, del->br_startblock,
5296 : del->br_blockcount, NULL,
5297 : XFS_AG_RESV_NONE, efi_flags);
5298 : if (error)
5299 214583936 : goto done;
5300 149178469 : }
5301 86323094 : }
5302 :
5303 62855375 : /*
5304 : * Adjust inode # blocks in the file.
5305 62855375 : */
5306 62812219 : if (nblks)
5307 14886055 : ip->i_nblocks -= nblks;
5308 62855375 : /*
5309 13960971 : * Adjust quota data.
5310 : */
5311 62855375 : if (qfield && !(bflags & XFS_BMAPI_REMAP))
5312 : xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5313 :
5314 62932238 : done:
5315 0 : *logflagsp = flags;
5316 : return error;
5317 : }
5318 :
5319 : /*
5320 : * Unmap (remove) blocks from a file.
5321 : * If nexts is nonzero then the number of extents to remove is limited to
5322 214662353 : * that value. If not all extents in the block range can be removed then
5323 214670216 : * *done is set.
5324 : */
5325 : int /* error */
5326 : __xfs_bunmapi(
5327 214662353 : struct xfs_trans *tp, /* transaction pointer */
5328 158229008 : struct xfs_inode *ip, /* incore inode */
5329 : xfs_fileoff_t start, /* first file offset deleted */
5330 56433345 : xfs_filblks_t *rlen, /* i/o: amount remaining */
5331 214586825 : uint32_t flags, /* misc flags */
5332 214586825 : xfs_extnum_t nexts) /* number of extents max */
5333 : {
5334 : struct xfs_btree_cur *cur; /* bmap btree cursor */
5335 : struct xfs_bmbt_irec del; /* extent being deleted */
5336 : int error; /* error return value */
5337 : xfs_extnum_t extno; /* extent number in list */
5338 : struct xfs_bmbt_irec got; /* current extent record */
5339 : struct xfs_ifork *ifp; /* inode fork pointer */
5340 : int isrt; /* freeing in rt area */
5341 : int logflags; /* transaction logging flags */
5342 203780347 : xfs_extlen_t mod; /* rt extent offset */
5343 : struct xfs_mount *mp = ip->i_mount;
5344 : int tmp_logflags; /* partial logging flags */
5345 : int wasdel; /* was a delayed alloc extent */
5346 : int whichfork; /* data or attribute fork */
5347 : xfs_filblks_t len = *rlen; /* length to unmap in file */
5348 : xfs_fileoff_t end;
5349 : struct xfs_iext_cursor icur;
5350 203780347 : bool done = false;
5351 203780347 :
5352 203780347 : trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
5353 203780347 :
5354 203780347 : whichfork = xfs_bmapi_whichfork(flags);
5355 203780347 : ASSERT(whichfork != XFS_COW_FORK);
5356 203780347 : ifp = xfs_ifork_ptr(ip, whichfork);
5357 203780347 : if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp))) {
5358 203780347 : xfs_bmap_mark_sick(ip, whichfork);
5359 203780347 : return -EFSCORRUPTED;
5360 203780347 : }
5361 203780347 : if (xfs_is_shutdown(mp))
5362 203780347 : return -EIO;
5363 203780347 :
5364 203780347 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5365 203780347 : ASSERT(len > 0);
5366 203780347 : ASSERT(nexts >= 0);
5367 :
5368 203780347 : error = xfs_iread_extents(tp, ip, whichfork);
5369 : if (error)
5370 203605505 : return error;
5371 203605505 :
5372 203605505 : if (xfs_iext_count(ifp) == 0) {
5373 203620743 : *rlen = 0;
5374 0 : return 0;
5375 0 : }
5376 : XFS_STATS_INC(mp, xs_blk_unmap);
5377 407241486 : isrt = xfs_ifork_is_realtime(ip, whichfork);
5378 : end = start + len;
5379 :
5380 203620528 : if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &icur, &got)) {
5381 203643329 : *rlen = 0;
5382 203643329 : return 0;
5383 : }
5384 203643329 : end--;
5385 203703551 :
5386 : logflags = 0;
5387 : if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5388 203708583 : ASSERT(ifp->if_format == XFS_DINODE_FMT_BTREE);
5389 4675002 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5390 4675002 : cur->bc_ino.flags = 0;
5391 : } else
5392 199024490 : cur = NULL;
5393 199062133 :
5394 198996002 : if (isrt) {
5395 : /*
5396 198996002 : * Synchronize by locking the realtime bitmap.
5397 318631 : */
5398 318631 : xfs_rtbitmap_lock(tp, mp);
5399 : }
5400 198667073 :
5401 : extno = 0;
5402 198667073 : while (end != (xfs_fileoff_t)-1 && end >= start &&
5403 198667073 : (nexts == 0 || extno < nexts)) {
5404 102707353 : /*
5405 102707353 : * Is the found extent after a hole in which end lives?
5406 102707760 : * Just back up to the previous extent, if so.
5407 : */
5408 95959720 : if (got.br_startoff > end &&
5409 : !xfs_iext_prev_extent(ifp, &icur, &got)) {
5410 198667480 : done = true;
5411 : break;
5412 : }
5413 : /*
5414 58112806 : * Is the last block of this extent before the range
5415 : * we're supposed to delete? If so, we're done.
5416 : */
5417 : end = XFS_FILEOFF_MIN(end,
5418 420276029 : got.br_startoff + got.br_blockcount - 1);
5419 : if (end < start)
5420 : break;
5421 : /*
5422 : * Then deal with the (possibly delayed) allocated space
5423 : * we found.
5424 226186042 : */
5425 0 : del = got;
5426 : wasdel = isnullstartblock(del.br_startblock);
5427 :
5428 : if (got.br_startoff < start) {
5429 : del.br_startoff = start;
5430 : del.br_blockcount -= start - got.br_startoff;
5431 : if (!wasdel)
5432 : del.br_startblock += start - got.br_startoff;
5433 226186042 : }
5434 : if (del.br_startoff + del.br_blockcount > end + 1)
5435 226186042 : del.br_blockcount = end + 1 - del.br_startoff;
5436 :
5437 : if (!isrt || (flags & XFS_BMAPI_REMAP))
5438 : goto delete;
5439 :
5440 : xfs_rtb_to_rtx(mp, del.br_startblock + del.br_blockcount, &mod);
5441 223692693 : if (mod) {
5442 223692693 : /*
5443 : * Realtime extent not lined up at the end.
5444 223692693 : * The extent could have been split into written
5445 51153791 : * and unwritten pieces, or we could just be
5446 51153791 : * unmapping part of it. But we can't really
5447 51153791 : * get rid of part of a realtime extent.
5448 50130613 : */
5449 : if (del.br_state == XFS_EXT_UNWRITTEN) {
5450 223692693 : /*
5451 54776641 : * This piece is unwritten, or we're not
5452 : * using unwritten extents. Skip over it.
5453 223692693 : */
5454 179764767 : ASSERT((flags & XFS_BMAPI_REMAP) || end >= mod);
5455 : end -= mod > del.br_blockcount ?
5456 43927926 : del.br_blockcount : mod;
5457 43927926 : if (end < got.br_startoff &&
5458 : !xfs_iext_prev_extent(ifp, &icur, &got)) {
5459 : done = true;
5460 : break;
5461 : }
5462 : continue;
5463 : }
5464 : /*
5465 0 : * It's written, turn it unwritten.
5466 : * This is better than zeroing it.
5467 : */
5468 : ASSERT(del.br_state == XFS_EXT_NORM);
5469 : ASSERT(tp->t_blk_res > 0);
5470 0 : /*
5471 0 : * If this spans a realtime extent boundary,
5472 0 : * chop it back to the start of the one we end at.
5473 0 : */
5474 0 : if (del.br_blockcount > mod) {
5475 : del.br_startoff += del.br_blockcount - mod;
5476 : del.br_startblock += del.br_blockcount - mod;
5477 : del.br_blockcount = mod;
5478 0 : }
5479 : del.br_state = XFS_EXT_UNWRITTEN;
5480 : error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5481 : whichfork, &icur, &cur, &del,
5482 : &logflags);
5483 : if (error)
5484 0 : goto error0;
5485 0 : goto nodelete;
5486 : }
5487 :
5488 : xfs_rtb_to_rtx(mp, del.br_startblock, &mod);
5489 : if (mod) {
5490 0 : xfs_extlen_t off = mp->m_sb.sb_rextsize - mod;
5491 0 :
5492 0 : /*
5493 0 : * Realtime extent is lined up at the end but not
5494 : * at the front. We'll get rid of full extents if
5495 0 : * we can.
5496 0 : */
5497 : if (del.br_blockcount > off) {
5498 : del.br_blockcount -= off;
5499 0 : del.br_startoff += off;
5500 0 : del.br_startblock += off;
5501 0 : } else if (del.br_startoff == start &&
5502 : (del.br_state == XFS_EXT_UNWRITTEN ||
5503 : tp->t_blk_res == 0)) {
5504 43927926 : /*
5505 43927926 : * Can't make it unwritten. There isn't
5506 3414280 : * a full extent here so just skip it.
5507 : */
5508 : ASSERT(end >= del.br_blockcount);
5509 : end -= del.br_blockcount;
5510 : if (got.br_startoff > end &&
5511 : !xfs_iext_prev_extent(ifp, &icur, &got)) {
5512 : done = true;
5513 3414280 : break;
5514 448986 : }
5515 448986 : continue;
5516 448986 : } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5517 2965294 : struct xfs_bmbt_irec prev;
5518 543491 : xfs_fileoff_t unwrite_start;
5519 143051 :
5520 : /*
5521 : * This one is already unwritten.
5522 : * It must have a written left neighbor.
5523 : * Unwrite the killed part of that one and
5524 543491 : * try again.
5525 543491 : */
5526 658430 : if (!xfs_iext_prev_extent(ifp, &icur, &prev))
5527 114939 : ASSERT(0);
5528 : ASSERT(prev.br_state == XFS_EXT_NORM);
5529 : ASSERT(!isnullstartblock(prev.br_startblock));
5530 : ASSERT(del.br_startblock ==
5531 543491 : prev.br_startblock + prev.br_blockcount);
5532 2421803 : unwrite_start = max3(start,
5533 1600758 : del.br_startoff - mod,
5534 1600758 : prev.br_startoff);
5535 : mod = unwrite_start - prev.br_startoff;
5536 : prev.br_startoff = unwrite_start;
5537 : prev.br_startblock += mod;
5538 : prev.br_blockcount -= mod;
5539 : prev.br_state = XFS_EXT_UNWRITTEN;
5540 : error = xfs_bmap_add_extent_unwritten_real(tp,
5541 : ip, whichfork, &icur, &cur,
5542 1600758 : &prev, &logflags);
5543 0 : if (error)
5544 1600758 : goto error0;
5545 1600758 : goto nodelete;
5546 1600758 : } else {
5547 : ASSERT(del.br_state == XFS_EXT_NORM);
5548 1600758 : del.br_state = XFS_EXT_UNWRITTEN;
5549 : error = xfs_bmap_add_extent_unwritten_real(tp,
5550 : ip, whichfork, &icur, &cur,
5551 1600758 : &del, &logflags);
5552 1600758 : if (error)
5553 1600758 : goto error0;
5554 1600758 : goto nodelete;
5555 1600758 : }
5556 1600758 : }
5557 :
5558 : delete:
5559 1600758 : if (wasdel) {
5560 0 : error = xfs_bmap_del_extent_delay(ip, whichfork, &icur,
5561 1600758 : &got, &del);
5562 : } else {
5563 821045 : error = xfs_bmap_del_extent_real(ip, tp, &icur, cur,
5564 821045 : &del, &tmp_logflags, whichfork,
5565 821045 : flags);
5566 : logflags |= tmp_logflags;
5567 : }
5568 821045 :
5569 0 : if (error)
5570 821045 : goto error0;
5571 :
5572 : end = del.br_startoff - 1;
5573 : nodelete:
5574 40513646 : /*
5575 220727399 : * If not done go on to the next (previous) record.
5576 6160098 : */
5577 : if (end != (xfs_fileoff_t)-1 && end >= start) {
5578 : if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5579 214567301 : (got.br_startoff > end &&
5580 : !xfs_iext_prev_extent(ifp, &icur, &got))) {
5581 : done = true;
5582 214614380 : break;
5583 : }
5584 : extno++;
5585 220786463 : }
5586 2 : }
5587 : if (done || end == (xfs_fileoff_t)-1 || end < start)
5588 220786461 : *rlen = 0;
5589 223208264 : else
5590 : *rlen = end - start + 1;
5591 :
5592 : /*
5593 223208264 : * Convert to a btree if necessary.
5594 88093730 : */
5595 87709892 : if (xfs_bmap_needs_btree(ip, whichfork)) {
5596 1713834 : ASSERT(cur == NULL);
5597 : error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
5598 : &tmp_logflags, whichfork);
5599 : logflags |= tmp_logflags;
5600 85924801 : } else {
5601 : error = xfs_bmap_btree_to_extents(tp, ip, cur, &logflags,
5602 : whichfork);
5603 196583336 : }
5604 158642047 :
5605 : error0:
5606 40110457 : /*
5607 : * Log everything. Do this after conversion, there's no point in
5608 : * logging the extent records if we've converted to btree format.
5609 : */
5610 : if ((logflags & xfs_ilog_fext(whichfork)) &&
5611 198752504 : ifp->if_format != XFS_DINODE_FMT_EXTENTS)
5612 314707 : logflags &= ~xfs_ilog_fext(whichfork);
5613 314707 : else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5614 : ifp->if_format != XFS_DINODE_FMT_BTREE)
5615 314705 : logflags &= ~xfs_ilog_fbroot(whichfork);
5616 : /*
5617 198215202 : * Log inode even in the error case, if the transaction
5618 : * is dirty we'll need to shut down the filesystem.
5619 : */
5620 : if (logflags)
5621 198557164 : xfs_trans_log_inode(tp, ip, logflags);
5622 : if (cur) {
5623 : if (!error)
5624 : cur->bc_ino.allocated = 0;
5625 : xfs_btree_del_cursor(cur, error);
5626 205042746 : }
5627 77039495 : return error;
5628 314784 : }
5629 204726572 :
5630 0 : /* Unmap a range of a file. */
5631 0 : int
5632 : xfs_bunmapi(
5633 : xfs_trans_t *tp,
5634 : struct xfs_inode *ip,
5635 : xfs_fileoff_t bno,
5636 198557164 : xfs_filblks_t len,
5637 174005337 : uint32_t flags,
5638 198717567 : xfs_extnum_t nexts,
5639 103070766 : int *done)
5640 103022642 : {
5641 103070766 : int error;
5642 :
5643 : error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts);
5644 : *done = (len == 0);
5645 : return error;
5646 : }
5647 :
5648 75036065 : /*
5649 : * Determine whether an extent shift can be accomplished by a merge with the
5650 : * extent that precedes the target hole of the shift.
5651 : */
5652 : STATIC bool
5653 : xfs_bmse_can_merge(
5654 : struct xfs_bmbt_irec *left, /* preceding extent */
5655 : struct xfs_bmbt_irec *got, /* current extent to shift */
5656 : xfs_fileoff_t shift) /* shift fsb */
5657 75036065 : {
5658 : xfs_fileoff_t startoff;
5659 75036065 :
5660 74994805 : startoff = got->br_startoff - shift;
5661 74994805 :
5662 : /*
5663 : * The extent, once shifted, must be adjacent in-file and on-disk with
5664 : * the preceding extent.
5665 : */
5666 : if ((left->br_startoff + left->br_blockcount != startoff) ||
5667 : (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5668 : (left->br_state != got->br_state) ||
5669 115025790 : (left->br_blockcount + got->br_blockcount > XFS_MAX_BMBT_EXTLEN))
5670 : return false;
5671 :
5672 : return true;
5673 : }
5674 115025790 :
5675 : /*
5676 115025790 : * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5677 : * hole in the file. If an extent shift would result in the extent being fully
5678 : * adjacent to the extent that currently precedes the hole, we can merge with
5679 : * the preceding extent rather than do the shift.
5680 : *
5681 : * This function assumes the caller has verified a shift-by-merge is possible
5682 115025790 : * with the provided extents via xfs_bmse_can_merge().
5683 107671789 : */
5684 1781485 : STATIC int
5685 8620 : xfs_bmse_merge(
5686 115017170 : struct xfs_trans *tp,
5687 : struct xfs_inode *ip,
5688 : int whichfork,
5689 : xfs_fileoff_t shift, /* shift fsb */
5690 : struct xfs_iext_cursor *icur,
5691 : struct xfs_bmbt_irec *got, /* extent to shift */
5692 : struct xfs_bmbt_irec *left, /* preceding extent */
5693 : struct xfs_btree_cur *cur,
5694 : int *logflags) /* output */
5695 : {
5696 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5697 : struct xfs_bmbt_irec new;
5698 : xfs_filblks_t blockcount;
5699 : int error, i;
5700 : struct xfs_mount *mp = ip->i_mount;
5701 4310 :
5702 : blockcount = left->br_blockcount + got->br_blockcount;
5703 :
5704 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5705 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5706 : ASSERT(xfs_bmse_can_merge(left, got, shift));
5707 :
5708 : new = *left;
5709 : new.br_blockcount = blockcount;
5710 :
5711 : /*
5712 4310 : * Update the on-disk extent count, the btree if necessary and log the
5713 4310 : * inode.
5714 4310 : */
5715 4310 : ifp->if_nextents--;
5716 4310 : *logflags |= XFS_ILOG_CORE;
5717 : if (!cur) {
5718 4310 : *logflags |= XFS_ILOG_DEXT;
5719 : goto done;
5720 4310 : }
5721 4310 :
5722 4310 : /* lookup and remove the extent to merge */
5723 : error = xfs_bmbt_lookup_eq(cur, got, &i);
5724 4310 : if (error)
5725 4310 : return error;
5726 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5727 : xfs_btree_mark_sick(cur);
5728 : return -EFSCORRUPTED;
5729 : }
5730 :
5731 4310 : error = xfs_btree_delete(cur, &i);
5732 4310 : if (error)
5733 4310 : return error;
5734 2927 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5735 2927 : xfs_btree_mark_sick(cur);
5736 : return -EFSCORRUPTED;
5737 : }
5738 :
5739 1383 : /* lookup and update size of the previous extent */
5740 1383 : error = xfs_bmbt_lookup_eq(cur, left, &i);
5741 : if (error)
5742 1383 : return error;
5743 0 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5744 0 : xfs_btree_mark_sick(cur);
5745 : return -EFSCORRUPTED;
5746 : }
5747 1383 :
5748 1383 : error = xfs_bmbt_update(cur, &new);
5749 : if (error)
5750 1383 : return error;
5751 0 :
5752 0 : /* change to extent format if required after extent removal */
5753 : error = xfs_bmap_btree_to_extents(tp, ip, cur, logflags, whichfork);
5754 : if (error)
5755 : return error;
5756 1383 :
5757 1383 : done:
5758 : xfs_iext_remove(ip, icur, 0);
5759 1383 : xfs_iext_prev(ifp, icur);
5760 0 : xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5761 0 : &new);
5762 :
5763 : /* update reverse mapping. rmap functions merge the rmaps for us */
5764 1383 : xfs_rmap_unmap_extent(tp, ip, whichfork, got);
5765 1383 : memcpy(&new, got, sizeof(new));
5766 : new.br_startoff = left->br_startoff + left->br_blockcount;
5767 : xfs_rmap_map_extent(tp, ip, whichfork, &new);
5768 : return 0;
5769 1383 : }
5770 1383 :
5771 : static int
5772 : xfs_bmap_shift_update_extent(
5773 1383 : struct xfs_trans *tp,
5774 4310 : struct xfs_inode *ip,
5775 4310 : int whichfork,
5776 8620 : struct xfs_iext_cursor *icur,
5777 : struct xfs_bmbt_irec *got,
5778 : struct xfs_btree_cur *cur,
5779 : int *logflags,
5780 4310 : xfs_fileoff_t startoff)
5781 4310 : {
5782 4310 : struct xfs_mount *mp = ip->i_mount;
5783 4310 : struct xfs_bmbt_irec prev = *got;
5784 4310 : int error, i;
5785 :
5786 : *logflags |= XFS_ILOG_CORE;
5787 :
5788 116872593 : got->br_startoff = startoff;
5789 :
5790 : if (cur) {
5791 : error = xfs_bmbt_lookup_eq(cur, &prev, &i);
5792 : if (error)
5793 : return error;
5794 : if (XFS_IS_CORRUPT(mp, i != 1)) {
5795 : xfs_btree_mark_sick(cur);
5796 : return -EFSCORRUPTED;
5797 : }
5798 116872593 :
5799 116872593 : error = xfs_bmbt_update(cur, got);
5800 116872593 : if (error)
5801 : return error;
5802 116872593 : } else {
5803 : *logflags |= XFS_ILOG_DEXT;
5804 116872593 : }
5805 :
5806 116872593 : xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), icur,
5807 108807939 : got);
5808 108807946 :
5809 : /* update reverse mapping */
5810 108807946 : xfs_rmap_unmap_extent(tp, ip, whichfork, &prev);
5811 0 : xfs_rmap_map_extent(tp, ip, whichfork, got);
5812 0 : return 0;
5813 : }
5814 :
5815 108807946 : int
5816 108807931 : xfs_bmap_collapse_extents(
5817 : struct xfs_trans *tp,
5818 : struct xfs_inode *ip,
5819 8064654 : xfs_fileoff_t *next_fsb,
5820 : xfs_fileoff_t offset_shift_fsb,
5821 : bool *done)
5822 233745170 : {
5823 : int whichfork = XFS_DATA_FORK;
5824 : struct xfs_mount *mp = ip->i_mount;
5825 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5826 116872546 : struct xfs_btree_cur *cur = NULL;
5827 116872570 : struct xfs_bmbt_irec got, prev;
5828 116872570 : struct xfs_iext_cursor icur;
5829 : xfs_fileoff_t new_startoff;
5830 : int error = 0;
5831 : int logflags = 0;
5832 106555232 :
5833 : if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5834 : XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5835 : xfs_bmap_mark_sick(ip, whichfork);
5836 : return -EFSCORRUPTED;
5837 : }
5838 :
5839 106555232 : if (xfs_is_shutdown(mp))
5840 106555232 : return -EIO;
5841 106555232 :
5842 106555231 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5843 106555231 :
5844 106555231 : error = xfs_iread_extents(tp, ip, whichfork);
5845 106555231 : if (error)
5846 106555231 : return error;
5847 106555231 :
5848 : if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5849 213110465 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5850 106555231 : cur->bc_ino.flags = 0;
5851 0 : }
5852 0 :
5853 : if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5854 : *done = true;
5855 213110468 : goto del_cursor;
5856 : }
5857 : if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5858 106555233 : xfs_bmap_mark_sick(ip, whichfork);
5859 : error = -EFSCORRUPTED;
5860 106555227 : goto del_cursor;
5861 106555228 : }
5862 :
5863 : new_startoff = got.br_startoff - offset_shift_fsb;
5864 106555228 : if (xfs_iext_peek_prev_extent(ifp, &icur, &prev)) {
5865 101960152 : if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5866 101960157 : error = -EINVAL;
5867 : goto del_cursor;
5868 : }
5869 106555233 :
5870 313678 : if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5871 313678 : error = xfs_bmse_merge(tp, ip, whichfork,
5872 : offset_shift_fsb, &icur, &got, &prev,
5873 106241556 : cur, &logflags);
5874 0 : if (error)
5875 0 : goto del_cursor;
5876 0 : goto done;
5877 : }
5878 : } else {
5879 106241556 : if (got.br_startoff < offset_shift_fsb) {
5880 106241556 : error = -EINVAL;
5881 106080327 : goto del_cursor;
5882 0 : }
5883 0 : }
5884 :
5885 : error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
5886 106080327 : cur, &logflags, new_startoff);
5887 4310 : if (error)
5888 : goto del_cursor;
5889 :
5890 4310 : done:
5891 0 : if (!xfs_iext_next_extent(ifp, &icur, &got)) {
5892 4310 : *done = true;
5893 : goto del_cursor;
5894 : }
5895 161223 :
5896 0 : *next_fsb = got.br_startoff;
5897 0 : del_cursor:
5898 : if (cur)
5899 : xfs_btree_del_cursor(cur, error);
5900 : if (logflags)
5901 106237240 : xfs_trans_log_inode(tp, ip, logflags);
5902 : return error;
5903 106237244 : }
5904 0 :
5905 : /* Make sure we won't be right-shifting an extent past the maximum bound. */
5906 106237244 : int
5907 106241554 : xfs_bmap_can_insert_extents(
5908 2145113 : struct xfs_inode *ip,
5909 2145113 : xfs_fileoff_t off,
5910 : xfs_fileoff_t shift)
5911 : {
5912 104096432 : struct xfs_bmbt_irec got;
5913 106555223 : int is_empty;
5914 106555223 : int error = 0;
5915 101960143 :
5916 106555230 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5917 106241548 :
5918 : if (xfs_is_shutdown(ip->i_mount))
5919 : return -EIO;
5920 :
5921 : xfs_ilock(ip, XFS_ILOCK_EXCL);
5922 : error = xfs_bmap_last_extent(NULL, ip, XFS_DATA_FORK, &got, &is_empty);
5923 1860815 : if (!error && !is_empty && got.br_startoff >= off &&
5924 : ((got.br_startoff + shift) & BMBT_STARTOFF_MASK) < got.br_startoff)
5925 : error = -EINVAL;
5926 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
5927 :
5928 1860815 : return error;
5929 1860815 : }
5930 1860815 :
5931 : int
5932 1860815 : xfs_bmap_insert_extents(
5933 : struct xfs_trans *tp,
5934 3721602 : struct xfs_inode *ip,
5935 : xfs_fileoff_t *next_fsb,
5936 : xfs_fileoff_t offset_shift_fsb,
5937 1860800 : bool *done,
5938 1860799 : xfs_fileoff_t stop_fsb)
5939 1860804 : {
5940 1610023 : int whichfork = XFS_DATA_FORK;
5941 0 : struct xfs_mount *mp = ip->i_mount;
5942 1860804 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
5943 : struct xfs_btree_cur *cur = NULL;
5944 1860804 : struct xfs_bmbt_irec got, next;
5945 : struct xfs_iext_cursor icur;
5946 : xfs_fileoff_t new_startoff;
5947 : int error = 0;
5948 10798742 : int logflags = 0;
5949 :
5950 : if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
5951 : XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
5952 : xfs_bmap_mark_sick(ip, whichfork);
5953 : return -EFSCORRUPTED;
5954 : }
5955 :
5956 10798742 : if (xfs_is_shutdown(mp))
5957 10798742 : return -EIO;
5958 10798742 :
5959 10798736 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5960 10798736 :
5961 10798736 : error = xfs_iread_extents(tp, ip, whichfork);
5962 10798736 : if (error)
5963 10798736 : return error;
5964 10798736 :
5965 : if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
5966 21597472 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5967 10798736 : cur->bc_ino.flags = 0;
5968 0 : }
5969 0 :
5970 : if (*next_fsb == NULLFSBLOCK) {
5971 : xfs_iext_last(ifp, &icur);
5972 21597472 : if (!xfs_iext_get_extent(ifp, &icur, &got) ||
5973 : stop_fsb > got.br_startoff) {
5974 : *done = true;
5975 10798735 : goto del_cursor;
5976 : }
5977 10798733 : } else {
5978 10798734 : if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &icur, &got)) {
5979 : *done = true;
5980 : goto del_cursor;
5981 10798734 : }
5982 6899189 : }
5983 6899199 : if (XFS_IS_CORRUPT(mp, isnullstartblock(got.br_startblock))) {
5984 : xfs_bmap_mark_sick(ip, whichfork);
5985 : error = -EFSCORRUPTED;
5986 10798744 : goto del_cursor;
5987 1857564 : }
5988 1857566 :
5989 1831519 : if (XFS_IS_CORRUPT(mp, stop_fsb > got.br_startoff)) {
5990 163378 : xfs_bmap_mark_sick(ip, whichfork);
5991 163378 : error = -EFSCORRUPTED;
5992 : goto del_cursor;
5993 : }
5994 8941180 :
5995 0 : new_startoff = got.br_startoff + offset_shift_fsb;
5996 0 : if (xfs_iext_peek_next_extent(ifp, &icur, &next)) {
5997 : if (new_startoff + got.br_blockcount > next.br_startoff) {
5998 : error = -EINVAL;
5999 10635366 : goto del_cursor;
6000 0 : }
6001 0 :
6002 0 : /*
6003 : * Unlike a left shift (which involves a hole punch), a right
6004 : * shift does not modify extent neighbors in any way. We should
6005 10635366 : * never find mergeable extents in this scenario. Check anyways
6006 0 : * and warn if we encounter two extents that could be one.
6007 0 : */
6008 0 : if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
6009 : WARN_ON_ONCE(1);
6010 : }
6011 10635366 :
6012 10635366 : error = xfs_bmap_shift_update_extent(tp, ip, whichfork, &icur, &got,
6013 8941179 : cur, &logflags, new_startoff);
6014 0 : if (error)
6015 0 : goto del_cursor;
6016 :
6017 : if (!xfs_iext_prev_extent(ifp, &icur, &got) ||
6018 : stop_fsb >= got.br_startoff + got.br_blockcount) {
6019 : *done = true;
6020 : goto del_cursor;
6021 : }
6022 :
6023 : *next_fsb = got.br_startoff;
6024 8941179 : del_cursor:
6025 0 : if (cur)
6026 : xfs_btree_del_cursor(cur, error);
6027 : if (logflags)
6028 10635365 : xfs_trans_log_inode(tp, ip, logflags);
6029 : return error;
6030 10635365 : }
6031 0 :
6032 : /*
6033 10635365 : * Splits an extent into two extents at split_fsb block such that it is the
6034 10477357 : * first block of the current_ext. @ext is a target extent to be split.
6035 1694186 : * @split_fsb is a block where the extents is split. If split_fsb lies in a
6036 1694186 : * hole or the first block of extents, just return 0.
6037 : */
6038 : int
6039 8941180 : xfs_bmap_split_extent(
6040 10798744 : struct xfs_trans *tp,
6041 10798744 : struct xfs_inode *ip,
6042 6899194 : xfs_fileoff_t split_fsb)
6043 10798735 : {
6044 10635353 : int whichfork = XFS_DATA_FORK;
6045 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
6046 : struct xfs_btree_cur *cur = NULL;
6047 : struct xfs_bmbt_irec got;
6048 : struct xfs_bmbt_irec new; /* split extent */
6049 : struct xfs_mount *mp = ip->i_mount;
6050 : xfs_fsblock_t gotblkcnt; /* new block count for got */
6051 : struct xfs_iext_cursor icur;
6052 : int error = 0;
6053 : int logflags = 0;
6054 : int i = 0;
6055 1857568 :
6056 : if (XFS_IS_CORRUPT(mp, !xfs_ifork_has_extents(ifp)) ||
6057 : XFS_TEST_ERROR(false, mp, XFS_ERRTAG_BMAPIFORMAT)) {
6058 : xfs_bmap_mark_sick(ip, whichfork);
6059 : return -EFSCORRUPTED;
6060 1857568 : }
6061 1857568 :
6062 1857564 : if (xfs_is_shutdown(mp))
6063 1857564 : return -EIO;
6064 1857564 :
6065 1857564 : /* Read in all the extents */
6066 1857564 : error = xfs_iread_extents(tp, ip, whichfork);
6067 1857564 : if (error)
6068 1857564 : return error;
6069 1857564 :
6070 1857564 : /*
6071 : * If there are not extents, or split_fsb lies in a hole we are done.
6072 3715128 : */
6073 1857564 : if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &icur, &got) ||
6074 0 : got.br_startoff >= split_fsb)
6075 0 : return 0;
6076 :
6077 : gotblkcnt = split_fsb - got.br_startoff;
6078 3715128 : new.br_startoff = split_fsb;
6079 : new.br_startblock = got.br_startblock + gotblkcnt;
6080 : new.br_blockcount = got.br_blockcount - gotblkcnt;
6081 : new.br_state = got.br_state;
6082 1857563 :
6083 1857563 : if (ifp->if_format == XFS_DINODE_FMT_BTREE) {
6084 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6085 : cur->bc_ino.flags = 0;
6086 : error = xfs_bmbt_lookup_eq(cur, &got, &i);
6087 : if (error)
6088 : goto del_cursor;
6089 1857563 : if (XFS_IS_CORRUPT(mp, i != 1)) {
6090 1694189 : xfs_btree_mark_sick(cur);
6091 : error = -EFSCORRUPTED;
6092 : goto del_cursor;
6093 569837 : }
6094 569837 : }
6095 569837 :
6096 569837 : got.br_blockcount = gotblkcnt;
6097 569837 : xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), &icur,
6098 : &got);
6099 569837 :
6100 213445 : logflags = XFS_ILOG_CORE;
6101 213445 : if (cur) {
6102 213445 : error = xfs_bmbt_update(cur, &got);
6103 213445 : if (error)
6104 0 : goto del_cursor;
6105 213445 : } else
6106 0 : logflags |= XFS_ILOG_DEXT;
6107 0 :
6108 0 : /* Add new extent */
6109 : xfs_iext_next(ifp, &icur);
6110 : xfs_iext_insert(ip, &icur, &new, 0);
6111 : ifp->if_nextents++;
6112 569837 :
6113 569837 : if (cur) {
6114 : error = xfs_bmbt_lookup_eq(cur, &new, &i);
6115 : if (error)
6116 569837 : goto del_cursor;
6117 569837 : if (XFS_IS_CORRUPT(mp, i != 0)) {
6118 213445 : xfs_btree_mark_sick(cur);
6119 213445 : error = -EFSCORRUPTED;
6120 0 : goto del_cursor;
6121 : }
6122 : error = xfs_btree_insert(cur, &i);
6123 : if (error)
6124 : goto del_cursor;
6125 569837 : if (XFS_IS_CORRUPT(mp, i != 1)) {
6126 569837 : xfs_btree_mark_sick(cur);
6127 569837 : error = -EFSCORRUPTED;
6128 : goto del_cursor;
6129 569837 : }
6130 213445 : }
6131 213445 :
6132 0 : /*
6133 213445 : * Convert to a btree if necessary.
6134 0 : */
6135 0 : if (xfs_bmap_needs_btree(ip, whichfork)) {
6136 0 : int tmp_logflags; /* partial log flag return val */
6137 :
6138 213445 : ASSERT(cur == NULL);
6139 213445 : error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
6140 0 : &tmp_logflags, whichfork);
6141 213445 : logflags |= tmp_logflags;
6142 0 : }
6143 0 :
6144 0 : del_cursor:
6145 : if (cur) {
6146 : cur->bc_ino.allocated = 0;
6147 : xfs_btree_del_cursor(cur, error);
6148 : }
6149 :
6150 : if (logflags)
6151 569837 : xfs_trans_log_inode(tp, ip, logflags);
6152 29571 : return error;
6153 : }
6154 29571 :
6155 29571 : /* Record a bmap intent. */
6156 : static void
6157 29571 : __xfs_bmap_add(
6158 : struct xfs_trans *tp,
6159 : enum xfs_bmap_intent_type type,
6160 540266 : struct xfs_inode *ip,
6161 569837 : int whichfork,
6162 243016 : struct xfs_bmbt_irec *bmap)
6163 243016 : {
6164 : struct xfs_bmap_intent *bi;
6165 :
6166 569837 : if ((whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK) ||
6167 569837 : bmap->br_startblock == HOLESTARTBLOCK ||
6168 : bmap->br_startblock == DELAYSTARTBLOCK)
6169 : return;
6170 :
6171 : bi = kmem_cache_alloc(xfs_bmap_intent_cache, GFP_NOFS | __GFP_NOFAIL);
6172 : INIT_LIST_HEAD(&bi->bi_list);
6173 225578122 : bi->bi_type = type;
6174 : bi->bi_owner = ip;
6175 : bi->bi_whichfork = whichfork;
6176 : bi->bi_bmap = *bmap;
6177 :
6178 : trace_xfs_bmap_defer(bi);
6179 :
6180 225578122 : xfs_bmap_update_get_group(tp->t_mountp, bi);
6181 : xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6182 225578122 : }
6183 225578122 :
6184 : /* Map an extent into a file. */
6185 : void
6186 : xfs_bmap_map_extent(
6187 217910280 : struct xfs_trans *tp,
6188 217911510 : struct xfs_inode *ip,
6189 217911510 : int whichfork,
6190 217911510 : struct xfs_bmbt_irec *PREV)
6191 217911510 : {
6192 217911510 : __xfs_bmap_add(tp, XFS_BMAP_MAP, ip, whichfork, PREV);
6193 : }
6194 217911510 :
6195 : /* Unmap an extent out of a file. */
6196 217910889 : void
6197 217912238 : xfs_bmap_unmap_extent(
6198 : struct xfs_trans *tp,
6199 : struct xfs_inode *ip,
6200 : int whichfork,
6201 : struct xfs_bmbt_irec *PREV)
6202 165312058 : {
6203 : __xfs_bmap_add(tp, XFS_BMAP_UNMAP, ip, whichfork, PREV);
6204 : }
6205 :
6206 : /*
6207 : * Process one of the deferred bmap operations. We pass back the
6208 165312058 : * btree cursor to maintain our lock on the bmapbt between calls.
6209 165312414 : */
6210 : int
6211 : xfs_bmap_finish_one(
6212 : struct xfs_trans *tp,
6213 60266406 : struct xfs_bmap_intent *bi)
6214 : {
6215 : struct xfs_bmbt_irec *bmap = &bi->bi_bmap;
6216 : int error = 0;
6217 : int flags = 0;
6218 :
6219 60266406 : if (bi->bi_whichfork == XFS_ATTR_FORK)
6220 60266665 : flags |= XFS_BMAPI_ATTRFORK;
6221 :
6222 : ASSERT(tp->t_highest_agno == NULLAGNUMBER);
6223 :
6224 : trace_xfs_bmap_deferred(bi);
6225 :
6226 : if (XFS_TEST_ERROR(false, tp->t_mountp, XFS_ERRTAG_BMAP_FINISH_ONE))
6227 217912295 : return -EIO;
6228 :
6229 : switch (bi->bi_type) {
6230 : case XFS_BMAP_MAP:
6231 217912295 : if (bi->bi_bmap.br_state == XFS_EXT_UNWRITTEN)
6232 217912295 : flags |= XFS_BMAPI_PREALLOC;
6233 217912295 : error = xfs_bmapi_remap(tp, bi->bi_owner, bmap->br_startoff,
6234 : bmap->br_blockcount, bmap->br_startblock,
6235 217912295 : flags);
6236 450133 : bmap->br_blockcount = 0;
6237 : break;
6238 217912295 : case XFS_BMAP_UNMAP:
6239 : error = __xfs_bunmapi(tp, bi->bi_owner, bmap->br_startoff,
6240 217912295 : &bmap->br_blockcount, flags | XFS_BMAPI_REMAP,
6241 : 1);
6242 217911988 : break;
6243 : default:
6244 : ASSERT(0);
6245 217912051 : xfs_bmap_mark_sick(bi->bi_owner, bi->bi_whichfork);
6246 161479187 : error = -EFSCORRUPTED;
6247 161479187 : }
6248 2662656 :
6249 161479187 : return error;
6250 : }
6251 :
6252 161479170 : /* Check that an extent does not have invalid flags or bad ranges. */
6253 161479170 : xfs_failaddr_t
6254 56432864 : xfs_bmap_validate_extent_raw(
6255 56432864 : struct xfs_mount *mp,
6256 : bool rtfile,
6257 : int whichfork,
6258 56432864 : struct xfs_bmbt_irec *irec)
6259 0 : {
6260 0 : if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
6261 0 : return __this_address;
6262 0 :
6263 : if (rtfile && whichfork == XFS_DATA_FORK) {
6264 : if (!xfs_verify_rtbext(mp, irec->br_startblock,
6265 : irec->br_blockcount))
6266 : return __this_address;
6267 : } else {
6268 : if (!xfs_verify_fsbext(mp, irec->br_startblock,
6269 : irec->br_blockcount))
6270 5198321775 : return __this_address;
6271 : }
6272 : if (irec->br_state != XFS_EXT_NORM && whichfork != XFS_DATA_FORK)
6273 : return __this_address;
6274 : return NULL;
6275 : }
6276 5198321775 :
6277 0 : int __init
6278 : xfs_bmap_intent_init_cache(void)
6279 5198309197 : {
6280 1255153428 : xfs_bmap_intent_cache = kmem_cache_create("xfs_bmap_intent",
6281 : sizeof(struct xfs_bmap_intent),
6282 0 : 0, 0, NULL);
6283 :
6284 3943155769 : return xfs_bmap_intent_cache != NULL ? 0 : -ENOMEM;
6285 : }
6286 0 :
6287 : void
6288 5198418369 : xfs_bmap_intent_destroy_cache(void)
6289 0 : {
6290 : kmem_cache_destroy(xfs_bmap_intent_cache);
6291 : xfs_bmap_intent_cache = NULL;
6292 : }
6293 :
6294 59 : /* Check that an inode's extent does not have invalid flags or bad ranges. */
6295 : xfs_failaddr_t
6296 59 : xfs_bmap_validate_extent(
6297 : struct xfs_inode *ip,
6298 : int whichfork,
6299 : struct xfs_bmbt_irec *irec)
6300 59 : {
6301 : return xfs_bmap_validate_extent_raw(ip->i_mount,
6302 : XFS_IS_REALTIME_INODE(ip), whichfork, irec);
6303 : }
6304 58 :
6305 : /*
6306 58 : * Used in xfs_itruncate_extents(). This is the maximum number of extents
6307 58 : * freed from a file in a single transaction.
6308 58 : */
6309 : #define XFS_ITRUNC_MAX_EXTENTS 2
6310 :
6311 : /*
6312 5198388863 : * Unmap every extent in part of an inode's fork. We don't do any higher level
6313 : * invalidation work at all.
6314 : */
6315 : int
6316 : xfs_bunmapi_range(
6317 5198388863 : struct xfs_trans **tpp,
6318 5198388863 : struct xfs_inode *ip,
6319 : uint32_t flags,
6320 : xfs_fileoff_t startoff,
6321 : xfs_fileoff_t endoff)
6322 : {
6323 : xfs_filblks_t unmap_len = endoff - startoff + 1;
6324 : int error = 0;
6325 :
6326 : ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6327 :
6328 : while (unmap_len > 0) {
6329 : ASSERT((*tpp)->t_highest_agno == NULLAGNUMBER);
6330 : error = __xfs_bunmapi(*tpp, ip, startoff, &unmap_len, flags,
6331 : XFS_ITRUNC_MAX_EXTENTS);
6332 35421535 : if (error)
6333 : goto out;
6334 :
6335 : /* free the just unmapped extents */
6336 : error = xfs_defer_finish(tpp);
6337 : if (error)
6338 : goto out;
6339 35421535 : }
6340 35421535 : out:
6341 : return error;
6342 35421535 : }
6343 :
6344 107593283 : struct xfs_bmap_query_range {
6345 72143068 : xfs_bmap_query_range_fn fn;
6346 72143068 : void *priv;
6347 : };
6348 72204377 :
6349 431 : /* Format btree record and pass to our callback. */
6350 : STATIC int
6351 : xfs_bmap_query_range_helper(
6352 72203946 : struct xfs_btree_cur *cur,
6353 72284421 : const union xfs_btree_rec *rec,
6354 1506 : void *priv)
6355 : {
6356 35450215 : struct xfs_bmap_query_range *query = priv;
6357 35452152 : struct xfs_bmbt_irec irec;
6358 : xfs_failaddr_t fa;
6359 :
6360 : xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
6361 : fa = xfs_bmap_validate_extent(cur->bc_ino.ip, cur->bc_ino.whichfork,
6362 : &irec);
6363 : if (fa) {
6364 : xfs_btree_mark_sick(cur);
6365 : return xfs_bmap_complain_bad_rec(cur->bc_ino.ip,
6366 : cur->bc_ino.whichfork, fa, &irec);
6367 0 : }
6368 :
6369 : return query->fn(cur, &irec, query->priv);
6370 : }
6371 :
6372 0 : /* Find all bmaps. */
6373 0 : int
6374 0 : xfs_bmap_query_all(
6375 : struct xfs_btree_cur *cur,
6376 0 : xfs_bmap_query_range_fn fn,
6377 0 : void *priv)
6378 : {
6379 0 : struct xfs_bmap_query_range query = {
6380 0 : .priv = priv,
6381 0 : .fn = fn,
6382 0 : };
6383 :
6384 : return xfs_btree_query_all(cur, xfs_bmap_query_range_helper, &query);
6385 0 : }
6386 :
6387 : /* Helper function to extract extent size hint from inode */
6388 : xfs_extlen_t
6389 : xfs_get_extsz_hint(
6390 0 : struct xfs_inode *ip)
6391 : {
6392 : /*
6393 : * No point in aligning allocations if we need to COW to actually
6394 : * write to them.
6395 0 : */
6396 : if (!xfs_is_always_cow_inode(ip) &&
6397 : (ip->i_diflags & XFS_DIFLAG_EXTSIZE) && ip->i_extsize)
6398 : return ip->i_extsize;
6399 : if (XFS_IS_REALTIME_INODE(ip))
6400 0 : return ip->i_mount->m_sb.sb_rextsize;
6401 : return 0;
6402 : }
6403 :
6404 : /*
6405 1755007920 : * Helper function to extract CoW extent size hint from inode.
6406 : * Between the extent size hint and the CoW extent size hint, we
6407 : * return the greater of the two. If the value is zero (automatic),
6408 : * use the default size.
6409 : */
6410 : xfs_extlen_t
6411 : xfs_get_cowextsz_hint(
6412 1755007920 : struct xfs_inode *ip)
6413 1685662593 : {
6414 : xfs_extlen_t a, b;
6415 1688571798 :
6416 978936439 : a = 0;
6417 : if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
6418 : a = ip->i_cowextsize;
6419 : if (XFS_IS_REALTIME_INODE(ip)) {
6420 : /*
6421 : * For realtime files, the realtime extent is the fundamental
6422 : * unit of allocation. This means that data sharing and CoW
6423 : * remapping can only be done in those units. For filesystems
6424 : * where the extent size is larger than one block, write
6425 : * requests that are not aligned to an extent boundary employ
6426 : * an unshare-around strategy to ensure that all pages for a
6427 10880815 : * shared extent are fully dirtied.
6428 : *
6429 : * Because the remapping alignment requirement applies equally
6430 10880815 : * to all CoW writes, any regular overwrites that could be
6431 : * turned (by a speculative CoW preallocation) into a CoW write
6432 10880815 : * must either employ this dirty-around strategy, or be smart
6433 10880815 : * enough to ignore the CoW fork mapping unless the entire
6434 217589 : * extent is dirty or becomes shared by writeback time. Doing
6435 10880815 : * the first would dramatically increase write amplification,
6436 : * and the second would require deeper insight into the state
6437 : * of the page cache during a writeback request. For now, we
6438 : * ignore the hint.
6439 : */
6440 : if (ip->i_mount->m_sb.sb_rextsize > 1)
6441 : return ip->i_mount->m_sb.sb_rextsize;
6442 : b = 0;
6443 : if (ip->i_diflags & XFS_DIFLAG_EXTSIZE)
6444 : b = ip->i_extsize;
6445 : } else {
6446 : b = xfs_get_extsz_hint(ip);
6447 : }
6448 :
6449 : a = max(a, b);
6450 : if (a == 0)
6451 : return XFS_DEFAULT_COWEXTSZ_HINT;
6452 : return a;
6453 : }
6454 :
6455 : static inline xfs_fileoff_t
6456 1743928 : xfs_fsblock_to_fileoff(
6457 : struct xfs_mount *mp,
6458 1180299 : xfs_fsblock_t fsbno)
6459 1180299 : {
6460 84 : xfs_daddr_t daddr = XFS_FSB_TO_DADDR(mp, fsbno);
6461 :
6462 9136887 : return XFS_B_TO_FSB(mp, BBTOB(daddr));
6463 : }
6464 :
6465 10314192 : /*
6466 10314192 : * Given a file and a free physical extent, map it into the file at the same
6467 10168011 : * offset if the file were a sparse image of the physical device. Set @mval to
6468 : * whatever mapping we added to the file.
6469 : */
6470 : int
6471 : xfs_bmapi_freesp(
6472 816 : struct xfs_trans *tp,
6473 : struct xfs_inode *ip,
6474 : xfs_fsblock_t fsbno,
6475 : xfs_extlen_t len,
6476 816 : struct xfs_bmbt_irec *mval)
6477 : {
6478 816 : struct xfs_bmbt_irec irec;
6479 : struct xfs_mount *mp = ip->i_mount;
6480 : xfs_fileoff_t startoff;
6481 : bool isrt = XFS_IS_REALTIME_INODE(ip);
6482 : int nimaps;
6483 : int error;
6484 :
6485 : trace_xfs_bmapi_freesp(ip, fsbno, len);
6486 :
6487 1333 : error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
6488 : XFS_IEXT_ADD_NOSPLIT_CNT);
6489 : if (error)
6490 : return error;
6491 :
6492 : if (isrt)
6493 : startoff = fsbno;
6494 1333 : else
6495 1333 : startoff = xfs_fsblock_to_fileoff(mp, fsbno);
6496 1333 :
6497 1333 : /* Make sure the entire range is a hole. */
6498 1333 : nimaps = 1;
6499 1333 : error = xfs_bmapi_read(ip, startoff, len, &irec, &nimaps, 0);
6500 : if (error)
6501 1333 : return error;
6502 :
6503 1333 : if (irec.br_startoff != startoff ||
6504 : irec.br_startblock != HOLESTARTBLOCK ||
6505 1333 : irec.br_blockcount < len)
6506 : return -EINVAL;
6507 :
6508 1333 : /*
6509 : * Allocate the physical extent. We should not have dropped the lock
6510 : * since the scan of the free space metadata, so this should work,
6511 816 : * though the length may be adjusted to play nicely with metadata space
6512 : * reservations.
6513 : */
6514 1333 : if (isrt) {
6515 1333 : xfs_rtxnum_t rtx_in, rtx_out;
6516 1333 : xfs_extlen_t rtxlen_in, rtxlen_out;
6517 : uint32_t mod;
6518 :
6519 1333 : rtx_in = xfs_rtb_to_rtx(mp, fsbno, &mod);
6520 1333 : if (mod) {
6521 1333 : ASSERT(mod == 0);
6522 : return -EFSCORRUPTED;
6523 : }
6524 :
6525 : rtxlen_in = xfs_rtb_to_rtx(mp, len, &mod);
6526 : if (mod) {
6527 : ASSERT(mod == 0);
6528 : return -EFSCORRUPTED;
6529 : }
6530 1333 :
6531 517 : error = xfs_rtallocate_extent(tp, rtx_in, 1, rtxlen_in,
6532 517 : &rtxlen_out, 0, 1, &rtx_out);
6533 517 : if (error)
6534 : return error;
6535 517 : if (rtx_out == NULLRTEXTNO) {
6536 517 : /*
6537 0 : * We were promised the space! In theory there aren't
6538 0 : * any reserve lists that would prevent us from getting
6539 : * the space.
6540 : */
6541 517 : return -ENOSPC;
6542 517 : }
6543 0 : if (rtx_out != rtx_in) {
6544 0 : ASSERT(0);
6545 : xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
6546 : return -EFSCORRUPTED;
6547 517 : }
6548 : mval->br_blockcount = rtxlen_out * mp->m_sb.sb_rextsize;
6549 517 : } else {
6550 : struct xfs_alloc_arg args = {
6551 517 : .mp = mp,
6552 : .tp = tp,
6553 : .oinfo = XFS_RMAP_OINFO_SKIP_UPDATE,
6554 : .resv = XFS_AG_RESV_NONE,
6555 : .prod = 1,
6556 : .datatype = XFS_ALLOC_USERDATA,
6557 : .maxlen = len,
6558 : .minlen = 1,
6559 517 : };
6560 0 : args.pag = xfs_perag_get(mp, XFS_FSB_TO_AGNO(mp, fsbno));
6561 0 : error = xfs_alloc_vextent_exact_bno(&args, fsbno);
6562 0 : xfs_perag_put(args.pag);
6563 : if (error)
6564 517 : return error;
6565 : if (args.fsbno == NULLFSBLOCK) {
6566 816 : /*
6567 : * We were promised the space, but failed to get it.
6568 : * This could be because the space is reserved for
6569 : * metadata expansion, or it could be because the AGFL
6570 : * fixup grabbed the first block we wanted. Either
6571 : * way, if the transaction is dirty we must commit it
6572 : * and tell the caller to try again.
6573 : */
6574 : if (tp->t_flags & XFS_TRANS_DIRTY)
6575 : return -EAGAIN;
6576 816 : return -ENOSPC;
6577 816 : }
6578 816 : if (args.fsbno != fsbno) {
6579 816 : ASSERT(0);
6580 704 : xfs_bmap_mark_sick(ip, XFS_DATA_FORK);
6581 816 : return -EFSCORRUPTED;
6582 : }
6583 : mval->br_blockcount = args.len;
6584 : }
6585 :
6586 : /* Map extent into file, update quota. */
6587 : mval->br_startblock = fsbno;
6588 : mval->br_startoff = startoff;
6589 : mval->br_state = XFS_EXT_UNWRITTEN;
6590 704 :
6591 : trace_xfs_bmapi_freesp_done(ip, mval);
6592 704 :
6593 : xfs_bmap_map_extent(tp, ip, XFS_DATA_FORK, mval);
6594 112 : if (isrt)
6595 0 : xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_RTBCOUNT,
6596 0 : mval->br_blockcount);
6597 0 : else
6598 : xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
6599 112 : mval->br_blockcount);
6600 :
6601 : return 0;
6602 : }
|