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