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