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