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