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