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