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