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