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