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