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