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