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