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