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