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