xfs: delalloc -> unwritten COW fork allocation can go wrong
[sfrench/cifs-2.6.git] / fs / xfs / libxfs / xfs_bmap.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_sb.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
18 #include "xfs_dir2.h"
19 #include "xfs_inode.h"
20 #include "xfs_btree.h"
21 #include "xfs_trans.h"
22 #include "xfs_inode_item.h"
23 #include "xfs_extfree_item.h"
24 #include "xfs_alloc.h"
25 #include "xfs_bmap.h"
26 #include "xfs_bmap_util.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_rtalloc.h"
29 #include "xfs_errortag.h"
30 #include "xfs_error.h"
31 #include "xfs_quota.h"
32 #include "xfs_trans_space.h"
33 #include "xfs_buf_item.h"
34 #include "xfs_trace.h"
35 #include "xfs_symlink.h"
36 #include "xfs_attr_leaf.h"
37 #include "xfs_filestream.h"
38 #include "xfs_rmap.h"
39 #include "xfs_ag_resv.h"
40 #include "xfs_refcount.h"
41 #include "xfs_icache.h"
42
43
44 kmem_zone_t             *xfs_bmap_free_item_zone;
45
46 /*
47  * Miscellaneous helper functions
48  */
49
50 /*
51  * Compute and fill in the value of the maximum depth of a bmap btree
52  * in this filesystem.  Done once, during mount.
53  */
54 void
55 xfs_bmap_compute_maxlevels(
56         xfs_mount_t     *mp,            /* file system mount structure */
57         int             whichfork)      /* data or attr fork */
58 {
59         int             level;          /* btree level */
60         uint            maxblocks;      /* max blocks at this level */
61         uint            maxleafents;    /* max leaf entries possible */
62         int             maxrootrecs;    /* max records in root block */
63         int             minleafrecs;    /* min records in leaf block */
64         int             minnoderecs;    /* min records in node block */
65         int             sz;             /* root block size */
66
67         /*
68          * The maximum number of extents in a file, hence the maximum
69          * number of leaf entries, is controlled by the type of di_nextents
70          * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
71          * (a signed 16-bit number, xfs_aextnum_t).
72          *
73          * Note that we can no longer assume that if we are in ATTR1 that
74          * the fork offset of all the inodes will be
75          * (xfs_default_attroffset(ip) >> 3) because we could have mounted
76          * with ATTR2 and then mounted back with ATTR1, keeping the
77          * di_forkoff's fixed but probably at various positions. Therefore,
78          * for both ATTR1 and ATTR2 we have to assume the worst case scenario
79          * of a minimum size available.
80          */
81         if (whichfork == XFS_DATA_FORK) {
82                 maxleafents = MAXEXTNUM;
83                 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
84         } else {
85                 maxleafents = MAXAEXTNUM;
86                 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
87         }
88         maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
89         minleafrecs = mp->m_bmap_dmnr[0];
90         minnoderecs = mp->m_bmap_dmnr[1];
91         maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
92         for (level = 1; maxblocks > 1; level++) {
93                 if (maxblocks <= maxrootrecs)
94                         maxblocks = 1;
95                 else
96                         maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
97         }
98         mp->m_bm_maxlevels[whichfork] = level;
99 }
100
101 STATIC int                              /* error */
102 xfs_bmbt_lookup_eq(
103         struct xfs_btree_cur    *cur,
104         struct xfs_bmbt_irec    *irec,
105         int                     *stat)  /* success/failure */
106 {
107         cur->bc_rec.b = *irec;
108         return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
109 }
110
111 STATIC int                              /* error */
112 xfs_bmbt_lookup_first(
113         struct xfs_btree_cur    *cur,
114         int                     *stat)  /* success/failure */
115 {
116         cur->bc_rec.b.br_startoff = 0;
117         cur->bc_rec.b.br_startblock = 0;
118         cur->bc_rec.b.br_blockcount = 0;
119         return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
120 }
121
122 /*
123  * Check if the inode needs to be converted to btree format.
124  */
125 static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
126 {
127         return whichfork != XFS_COW_FORK &&
128                 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
129                 XFS_IFORK_NEXTENTS(ip, whichfork) >
130                         XFS_IFORK_MAXEXT(ip, whichfork);
131 }
132
133 /*
134  * Check if the inode should be converted to extent format.
135  */
136 static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
137 {
138         return whichfork != XFS_COW_FORK &&
139                 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
140                 XFS_IFORK_NEXTENTS(ip, whichfork) <=
141                         XFS_IFORK_MAXEXT(ip, whichfork);
142 }
143
144 /*
145  * Update the record referred to by cur to the value given by irec
146  * This either works (return 0) or gets an EFSCORRUPTED error.
147  */
148 STATIC int
149 xfs_bmbt_update(
150         struct xfs_btree_cur    *cur,
151         struct xfs_bmbt_irec    *irec)
152 {
153         union xfs_btree_rec     rec;
154
155         xfs_bmbt_disk_set_all(&rec.bmbt, irec);
156         return xfs_btree_update(cur, &rec);
157 }
158
159 /*
160  * Compute the worst-case number of indirect blocks that will be used
161  * for ip's delayed extent of length "len".
162  */
163 STATIC xfs_filblks_t
164 xfs_bmap_worst_indlen(
165         xfs_inode_t     *ip,            /* incore inode pointer */
166         xfs_filblks_t   len)            /* delayed extent length */
167 {
168         int             level;          /* btree level number */
169         int             maxrecs;        /* maximum record count at this level */
170         xfs_mount_t     *mp;            /* mount structure */
171         xfs_filblks_t   rval;           /* return value */
172
173         mp = ip->i_mount;
174         maxrecs = mp->m_bmap_dmxr[0];
175         for (level = 0, rval = 0;
176              level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
177              level++) {
178                 len += maxrecs - 1;
179                 do_div(len, maxrecs);
180                 rval += len;
181                 if (len == 1)
182                         return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
183                                 level - 1;
184                 if (level == 0)
185                         maxrecs = mp->m_bmap_dmxr[1];
186         }
187         return rval;
188 }
189
190 /*
191  * Calculate the default attribute fork offset for newly created inodes.
192  */
193 uint
194 xfs_default_attroffset(
195         struct xfs_inode        *ip)
196 {
197         struct xfs_mount        *mp = ip->i_mount;
198         uint                    offset;
199
200         if (mp->m_sb.sb_inodesize == 256) {
201                 offset = XFS_LITINO(mp, ip->i_d.di_version) -
202                                 XFS_BMDR_SPACE_CALC(MINABTPTRS);
203         } else {
204                 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
205         }
206
207         ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
208         return offset;
209 }
210
211 /*
212  * Helper routine to reset inode di_forkoff field when switching
213  * attribute fork from local to extent format - we reset it where
214  * possible to make space available for inline data fork extents.
215  */
216 STATIC void
217 xfs_bmap_forkoff_reset(
218         xfs_inode_t     *ip,
219         int             whichfork)
220 {
221         if (whichfork == XFS_ATTR_FORK &&
222             ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
223             ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
224                 uint    dfl_forkoff = xfs_default_attroffset(ip) >> 3;
225
226                 if (dfl_forkoff > ip->i_d.di_forkoff)
227                         ip->i_d.di_forkoff = dfl_forkoff;
228         }
229 }
230
231 #ifdef DEBUG
232 STATIC struct xfs_buf *
233 xfs_bmap_get_bp(
234         struct xfs_btree_cur    *cur,
235         xfs_fsblock_t           bno)
236 {
237         struct xfs_log_item     *lip;
238         int                     i;
239
240         if (!cur)
241                 return NULL;
242
243         for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
244                 if (!cur->bc_bufs[i])
245                         break;
246                 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
247                         return cur->bc_bufs[i];
248         }
249
250         /* Chase down all the log items to see if the bp is there */
251         list_for_each_entry(lip, &cur->bc_tp->t_items, li_trans) {
252                 struct xfs_buf_log_item *bip = (struct xfs_buf_log_item *)lip;
253
254                 if (bip->bli_item.li_type == XFS_LI_BUF &&
255                     XFS_BUF_ADDR(bip->bli_buf) == bno)
256                         return bip->bli_buf;
257         }
258
259         return NULL;
260 }
261
262 STATIC void
263 xfs_check_block(
264         struct xfs_btree_block  *block,
265         xfs_mount_t             *mp,
266         int                     root,
267         short                   sz)
268 {
269         int                     i, j, dmxr;
270         __be64                  *pp, *thispa;   /* pointer to block address */
271         xfs_bmbt_key_t          *prevp, *keyp;
272
273         ASSERT(be16_to_cpu(block->bb_level) > 0);
274
275         prevp = NULL;
276         for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
277                 dmxr = mp->m_bmap_dmxr[0];
278                 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
279
280                 if (prevp) {
281                         ASSERT(be64_to_cpu(prevp->br_startoff) <
282                                be64_to_cpu(keyp->br_startoff));
283                 }
284                 prevp = keyp;
285
286                 /*
287                  * Compare the block numbers to see if there are dups.
288                  */
289                 if (root)
290                         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
291                 else
292                         pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
293
294                 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
295                         if (root)
296                                 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
297                         else
298                                 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
299                         if (*thispa == *pp) {
300                                 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
301                                         __func__, j, i,
302                                         (unsigned long long)be64_to_cpu(*thispa));
303                                 xfs_err(mp, "%s: ptrs are equal in node\n",
304                                         __func__);
305                                 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
306                         }
307                 }
308         }
309 }
310
311 /*
312  * Check that the extents for the inode ip are in the right order in all
313  * btree leaves. THis becomes prohibitively expensive for large extent count
314  * files, so don't bother with inodes that have more than 10,000 extents in
315  * them. The btree record ordering checks will still be done, so for such large
316  * bmapbt constructs that is going to catch most corruptions.
317  */
318 STATIC void
319 xfs_bmap_check_leaf_extents(
320         xfs_btree_cur_t         *cur,   /* btree cursor or null */
321         xfs_inode_t             *ip,            /* incore inode pointer */
322         int                     whichfork)      /* data or attr fork */
323 {
324         struct xfs_btree_block  *block; /* current btree block */
325         xfs_fsblock_t           bno;    /* block # of "block" */
326         xfs_buf_t               *bp;    /* buffer for "block" */
327         int                     error;  /* error return value */
328         xfs_extnum_t            i=0, j; /* index into the extents list */
329         struct xfs_ifork        *ifp;   /* fork structure */
330         int                     level;  /* btree level, for checking */
331         xfs_mount_t             *mp;    /* file system mount structure */
332         __be64                  *pp;    /* pointer to block address */
333         xfs_bmbt_rec_t          *ep;    /* pointer to current extent */
334         xfs_bmbt_rec_t          last = {0, 0}; /* last extent in prev block */
335         xfs_bmbt_rec_t          *nextp; /* pointer to next extent */
336         int                     bp_release = 0;
337
338         if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
339                 return;
340         }
341
342         /* skip large extent count inodes */
343         if (ip->i_d.di_nextents > 10000)
344                 return;
345
346         bno = NULLFSBLOCK;
347         mp = ip->i_mount;
348         ifp = XFS_IFORK_PTR(ip, whichfork);
349         block = ifp->if_broot;
350         /*
351          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
352          */
353         level = be16_to_cpu(block->bb_level);
354         ASSERT(level > 0);
355         xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
356         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
357         bno = be64_to_cpu(*pp);
358
359         ASSERT(bno != NULLFSBLOCK);
360         ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
361         ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
362
363         /*
364          * Go down the tree until leaf level is reached, following the first
365          * pointer (leftmost) at each level.
366          */
367         while (level-- > 0) {
368                 /* See if buf is in cur first */
369                 bp_release = 0;
370                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
371                 if (!bp) {
372                         bp_release = 1;
373                         error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
374                                                 XFS_BMAP_BTREE_REF,
375                                                 &xfs_bmbt_buf_ops);
376                         if (error)
377                                 goto error_norelse;
378                 }
379                 block = XFS_BUF_TO_BLOCK(bp);
380                 if (level == 0)
381                         break;
382
383                 /*
384                  * Check this block for basic sanity (increasing keys and
385                  * no duplicate blocks).
386                  */
387
388                 xfs_check_block(block, mp, 0, 0);
389                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
390                 bno = be64_to_cpu(*pp);
391                 XFS_WANT_CORRUPTED_GOTO(mp,
392                                         xfs_verify_fsbno(mp, bno), error0);
393                 if (bp_release) {
394                         bp_release = 0;
395                         xfs_trans_brelse(NULL, bp);
396                 }
397         }
398
399         /*
400          * Here with bp and block set to the leftmost leaf node in the tree.
401          */
402         i = 0;
403
404         /*
405          * Loop over all leaf nodes checking that all extents are in the right order.
406          */
407         for (;;) {
408                 xfs_fsblock_t   nextbno;
409                 xfs_extnum_t    num_recs;
410
411
412                 num_recs = xfs_btree_get_numrecs(block);
413
414                 /*
415                  * Read-ahead the next leaf block, if any.
416                  */
417
418                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
419
420                 /*
421                  * Check all the extents to make sure they are OK.
422                  * If we had a previous block, the last entry should
423                  * conform with the first entry in this one.
424                  */
425
426                 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
427                 if (i) {
428                         ASSERT(xfs_bmbt_disk_get_startoff(&last) +
429                                xfs_bmbt_disk_get_blockcount(&last) <=
430                                xfs_bmbt_disk_get_startoff(ep));
431                 }
432                 for (j = 1; j < num_recs; j++) {
433                         nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
434                         ASSERT(xfs_bmbt_disk_get_startoff(ep) +
435                                xfs_bmbt_disk_get_blockcount(ep) <=
436                                xfs_bmbt_disk_get_startoff(nextp));
437                         ep = nextp;
438                 }
439
440                 last = *ep;
441                 i += num_recs;
442                 if (bp_release) {
443                         bp_release = 0;
444                         xfs_trans_brelse(NULL, bp);
445                 }
446                 bno = nextbno;
447                 /*
448                  * If we've reached the end, stop.
449                  */
450                 if (bno == NULLFSBLOCK)
451                         break;
452
453                 bp_release = 0;
454                 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
455                 if (!bp) {
456                         bp_release = 1;
457                         error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
458                                                 XFS_BMAP_BTREE_REF,
459                                                 &xfs_bmbt_buf_ops);
460                         if (error)
461                                 goto error_norelse;
462                 }
463                 block = XFS_BUF_TO_BLOCK(bp);
464         }
465
466         return;
467
468 error0:
469         xfs_warn(mp, "%s: at error0", __func__);
470         if (bp_release)
471                 xfs_trans_brelse(NULL, bp);
472 error_norelse:
473         xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
474                 __func__, i);
475         xfs_err(mp, "%s: CORRUPTED BTREE OR SOMETHING", __func__);
476         xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
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_trans                *tp,
537         xfs_fsblock_t                   bno,
538         xfs_filblks_t                   len,
539         struct xfs_owner_info           *oinfo,
540         bool                            skip_discard)
541 {
542         struct xfs_extent_free_item     *new;           /* new element */
543 #ifdef DEBUG
544         struct xfs_mount                *mp = tp->t_mountp;
545         xfs_agnumber_t                  agno;
546         xfs_agblock_t                   agbno;
547
548         ASSERT(bno != NULLFSBLOCK);
549         ASSERT(len > 0);
550         ASSERT(len <= MAXEXTLEN);
551         ASSERT(!isnullstartblock(bno));
552         agno = XFS_FSB_TO_AGNO(mp, bno);
553         agbno = XFS_FSB_TO_AGBNO(mp, bno);
554         ASSERT(agno < mp->m_sb.sb_agcount);
555         ASSERT(agbno < mp->m_sb.sb_agblocks);
556         ASSERT(len < mp->m_sb.sb_agblocks);
557         ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
558 #endif
559         ASSERT(xfs_bmap_free_item_zone != NULL);
560
561         new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
562         new->xefi_startblock = bno;
563         new->xefi_blockcount = (xfs_extlen_t)len;
564         if (oinfo)
565                 new->xefi_oinfo = *oinfo;
566         else
567                 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
568         new->xefi_skip_discard = skip_discard;
569         trace_xfs_bmap_free_defer(tp->t_mountp,
570                         XFS_FSB_TO_AGNO(tp->t_mountp, bno), 0,
571                         XFS_FSB_TO_AGBNO(tp->t_mountp, bno), len);
572         xfs_defer_add(tp, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
573 }
574
575 /*
576  * Inode fork format manipulation functions
577  */
578
579 /*
580  * Transform a btree format file with only one leaf node, where the
581  * extents list will fit in the inode, into an extents format file.
582  * Since the file extents are already in-core, all we have to do is
583  * give up the space for the btree root and pitch the leaf block.
584  */
585 STATIC int                              /* error */
586 xfs_bmap_btree_to_extents(
587         xfs_trans_t             *tp,    /* transaction pointer */
588         xfs_inode_t             *ip,    /* incore inode pointer */
589         xfs_btree_cur_t         *cur,   /* btree cursor */
590         int                     *logflagsp, /* inode logging flags */
591         int                     whichfork)  /* data or attr fork */
592 {
593         /* REFERENCED */
594         struct xfs_btree_block  *cblock;/* child btree block */
595         xfs_fsblock_t           cbno;   /* child block number */
596         xfs_buf_t               *cbp;   /* child block's buffer */
597         int                     error;  /* error return value */
598         struct xfs_ifork        *ifp;   /* inode fork data */
599         xfs_mount_t             *mp;    /* mount point structure */
600         __be64                  *pp;    /* ptr to block address */
601         struct xfs_btree_block  *rblock;/* root btree block */
602         struct xfs_owner_info   oinfo;
603
604         mp = ip->i_mount;
605         ifp = XFS_IFORK_PTR(ip, whichfork);
606         ASSERT(whichfork != XFS_COW_FORK);
607         ASSERT(ifp->if_flags & XFS_IFEXTENTS);
608         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
609         rblock = ifp->if_broot;
610         ASSERT(be16_to_cpu(rblock->bb_level) == 1);
611         ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
612         ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
613         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
614         cbno = be64_to_cpu(*pp);
615         *logflagsp = 0;
616 #ifdef DEBUG
617         XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
618                         xfs_btree_check_lptr(cur, cbno, 1));
619 #endif
620         error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
621                                 &xfs_bmbt_buf_ops);
622         if (error)
623                 return error;
624         cblock = XFS_BUF_TO_BLOCK(cbp);
625         if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
626                 return error;
627         xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
628         xfs_bmap_add_free(cur->bc_tp, cbno, 1, &oinfo);
629         ip->i_d.di_nblocks--;
630         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
631         xfs_trans_binval(tp, cbp);
632         if (cur->bc_bufs[0] == cbp)
633                 cur->bc_bufs[0] = NULL;
634         xfs_iroot_realloc(ip, -1, whichfork);
635         ASSERT(ifp->if_broot == NULL);
636         ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
637         XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
638         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
639         return 0;
640 }
641
642 /*
643  * Convert an extents-format file into a btree-format file.
644  * The new file will have a root block (in the inode) and a single child block.
645  */
646 STATIC int                                      /* error */
647 xfs_bmap_extents_to_btree(
648         struct xfs_trans        *tp,            /* transaction pointer */
649         struct xfs_inode        *ip,            /* incore inode pointer */
650         struct xfs_btree_cur    **curp,         /* cursor returned to caller */
651         int                     wasdel,         /* converting a delayed alloc */
652         int                     *logflagsp,     /* inode logging flags */
653         int                     whichfork)      /* data or attr fork */
654 {
655         struct xfs_btree_block  *ablock;        /* allocated (child) bt block */
656         struct xfs_buf          *abp;           /* buffer for ablock */
657         struct xfs_alloc_arg    args;           /* allocation arguments */
658         struct xfs_bmbt_rec     *arp;           /* child record pointer */
659         struct xfs_btree_block  *block;         /* btree root block */
660         struct xfs_btree_cur    *cur;           /* bmap btree cursor */
661         int                     error;          /* error return value */
662         struct xfs_ifork        *ifp;           /* inode fork pointer */
663         struct xfs_bmbt_key     *kp;            /* root block key pointer */
664         struct xfs_mount        *mp;            /* mount structure */
665         xfs_bmbt_ptr_t          *pp;            /* root block address pointer */
666         struct xfs_iext_cursor  icur;
667         struct xfs_bmbt_irec    rec;
668         xfs_extnum_t            cnt = 0;
669
670         mp = ip->i_mount;
671         ASSERT(whichfork != XFS_COW_FORK);
672         ifp = XFS_IFORK_PTR(ip, whichfork);
673         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
674
675         /*
676          * Make space in the inode incore. This needs to be undone if we fail
677          * to expand the root.
678          */
679         xfs_iroot_realloc(ip, 1, whichfork);
680         ifp->if_flags |= XFS_IFBROOT;
681
682         /*
683          * Fill in the root.
684          */
685         block = ifp->if_broot;
686         xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
687                                  XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
688                                  XFS_BTREE_LONG_PTRS);
689         /*
690          * Need a cursor.  Can't allocate until bb_level is filled in.
691          */
692         cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
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         if (tp->t_firstblock == NULLFSBLOCK) {
703                 args.type = XFS_ALLOCTYPE_START_BNO;
704                 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
705         } else if (tp->t_flags & XFS_TRANS_LOWMODE) {
706                 args.type = XFS_ALLOCTYPE_START_BNO;
707                 args.fsbno = tp->t_firstblock;
708         } else {
709                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
710                 args.fsbno = tp->t_firstblock;
711         }
712         args.minlen = args.maxlen = args.prod = 1;
713         args.wasdel = wasdel;
714         *logflagsp = 0;
715         error = xfs_alloc_vextent(&args);
716         if (error)
717                 goto out_root_realloc;
718
719         if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
720                 error = -ENOSPC;
721                 goto out_root_realloc;
722         }
723
724         /*
725          * Allocation can't fail, the space was reserved.
726          */
727         ASSERT(tp->t_firstblock == NULLFSBLOCK ||
728                args.agno >= XFS_FSB_TO_AGNO(mp, tp->t_firstblock));
729         tp->t_firstblock = args.fsbno;
730         cur->bc_private.b.allocated++;
731         ip->i_d.di_nblocks++;
732         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
733         abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
734         if (!abp) {
735                 error = -EFSCORRUPTED;
736                 goto out_unreserve_dquot;
737         }
738
739         /*
740          * Fill in the child block.
741          */
742         abp->b_ops = &xfs_bmbt_buf_ops;
743         ablock = XFS_BUF_TO_BLOCK(abp);
744         xfs_btree_init_block_int(mp, ablock, abp->b_bn,
745                                 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
746                                 XFS_BTREE_LONG_PTRS);
747
748         for_each_xfs_iext(ifp, &icur, &rec) {
749                 if (isnullstartblock(rec.br_startblock))
750                         continue;
751                 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1 + cnt);
752                 xfs_bmbt_disk_set_all(arp, &rec);
753                 cnt++;
754         }
755         ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
756         xfs_btree_set_numrecs(ablock, cnt);
757
758         /*
759          * Fill in the root key and pointer.
760          */
761         kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
762         arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
763         kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
764         pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
765                                                 be16_to_cpu(block->bb_level)));
766         *pp = cpu_to_be64(args.fsbno);
767
768         /*
769          * Do all this logging at the end so that
770          * the root is at the right level.
771          */
772         xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
773         xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
774         ASSERT(*curp == NULL);
775         *curp = cur;
776         *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
777         return 0;
778
779 out_unreserve_dquot:
780         xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
781 out_root_realloc:
782         xfs_iroot_realloc(ip, -1, whichfork);
783         XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
784         ASSERT(ifp->if_broot == NULL);
785         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
786
787         return error;
788 }
789
790 /*
791  * Convert a local file to an extents file.
792  * This code is out of bounds for data forks of regular files,
793  * since the file data needs to get logged so things will stay consistent.
794  * (The bmap-level manipulations are ok, though).
795  */
796 void
797 xfs_bmap_local_to_extents_empty(
798         struct xfs_inode        *ip,
799         int                     whichfork)
800 {
801         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
802
803         ASSERT(whichfork != XFS_COW_FORK);
804         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
805         ASSERT(ifp->if_bytes == 0);
806         ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
807
808         xfs_bmap_forkoff_reset(ip, whichfork);
809         ifp->if_flags &= ~XFS_IFINLINE;
810         ifp->if_flags |= XFS_IFEXTENTS;
811         ifp->if_u1.if_root = NULL;
812         ifp->if_height = 0;
813         XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
814 }
815
816
817 STATIC int                              /* error */
818 xfs_bmap_local_to_extents(
819         xfs_trans_t     *tp,            /* transaction pointer */
820         xfs_inode_t     *ip,            /* incore inode pointer */
821         xfs_extlen_t    total,          /* total blocks needed by transaction */
822         int             *logflagsp,     /* inode logging flags */
823         int             whichfork,
824         void            (*init_fn)(struct xfs_trans *tp,
825                                    struct xfs_buf *bp,
826                                    struct xfs_inode *ip,
827                                    struct xfs_ifork *ifp))
828 {
829         int             error = 0;
830         int             flags;          /* logging flags returned */
831         struct xfs_ifork *ifp;          /* inode fork pointer */
832         xfs_alloc_arg_t args;           /* allocation arguments */
833         xfs_buf_t       *bp;            /* buffer for extent block */
834         struct xfs_bmbt_irec rec;
835         struct xfs_iext_cursor icur;
836
837         /*
838          * We don't want to deal with the case of keeping inode data inline yet.
839          * So sending the data fork of a regular inode is invalid.
840          */
841         ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
842         ifp = XFS_IFORK_PTR(ip, whichfork);
843         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
844
845         if (!ifp->if_bytes) {
846                 xfs_bmap_local_to_extents_empty(ip, whichfork);
847                 flags = XFS_ILOG_CORE;
848                 goto done;
849         }
850
851         flags = 0;
852         error = 0;
853         ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS)) == XFS_IFINLINE);
854         memset(&args, 0, sizeof(args));
855         args.tp = tp;
856         args.mp = ip->i_mount;
857         xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
858         /*
859          * Allocate a block.  We know we need only one, since the
860          * file currently fits in an inode.
861          */
862         if (tp->t_firstblock == NULLFSBLOCK) {
863                 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864                 args.type = XFS_ALLOCTYPE_START_BNO;
865         } else {
866                 args.fsbno = tp->t_firstblock;
867                 args.type = XFS_ALLOCTYPE_NEAR_BNO;
868         }
869         args.total = total;
870         args.minlen = args.maxlen = args.prod = 1;
871         error = xfs_alloc_vextent(&args);
872         if (error)
873                 goto done;
874
875         /* Can't fail, the space was reserved. */
876         ASSERT(args.fsbno != NULLFSBLOCK);
877         ASSERT(args.len == 1);
878         tp->t_firstblock = args.fsbno;
879         bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
880
881         /*
882          * Initialize the block, copy the data and log the remote buffer.
883          *
884          * The callout is responsible for logging because the remote format
885          * might differ from the local format and thus we don't know how much to
886          * log here. Note that init_fn must also set the buffer log item type
887          * correctly.
888          */
889         init_fn(tp, bp, ip, ifp);
890
891         /* account for the change in fork size */
892         xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
893         xfs_bmap_local_to_extents_empty(ip, whichfork);
894         flags |= XFS_ILOG_CORE;
895
896         ifp->if_u1.if_root = NULL;
897         ifp->if_height = 0;
898
899         rec.br_startoff = 0;
900         rec.br_startblock = args.fsbno;
901         rec.br_blockcount = 1;
902         rec.br_state = XFS_EXT_NORM;
903         xfs_iext_first(ifp, &icur);
904         xfs_iext_insert(ip, &icur, &rec, 0);
905
906         XFS_IFORK_NEXT_SET(ip, whichfork, 1);
907         ip->i_d.di_nblocks = 1;
908         xfs_trans_mod_dquot_byino(tp, ip,
909                 XFS_TRANS_DQ_BCOUNT, 1L);
910         flags |= xfs_ilog_fext(whichfork);
911
912 done:
913         *logflagsp = flags;
914         return error;
915 }
916
917 /*
918  * Called from xfs_bmap_add_attrfork to handle btree format files.
919  */
920 STATIC int                                      /* error */
921 xfs_bmap_add_attrfork_btree(
922         xfs_trans_t             *tp,            /* transaction pointer */
923         xfs_inode_t             *ip,            /* incore inode pointer */
924         int                     *flags)         /* inode logging flags */
925 {
926         xfs_btree_cur_t         *cur;           /* btree cursor */
927         int                     error;          /* error return value */
928         xfs_mount_t             *mp;            /* file system mount struct */
929         int                     stat;           /* newroot status */
930
931         mp = ip->i_mount;
932         if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
933                 *flags |= XFS_ILOG_DBROOT;
934         else {
935                 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
936                 error = xfs_bmbt_lookup_first(cur, &stat);
937                 if (error)
938                         goto error0;
939                 /* must be at least one entry */
940                 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
941                 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
942                         goto error0;
943                 if (stat == 0) {
944                         xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
945                         return -ENOSPC;
946                 }
947                 cur->bc_private.b.allocated = 0;
948                 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
949         }
950         return 0;
951 error0:
952         xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
953         return error;
954 }
955
956 /*
957  * Called from xfs_bmap_add_attrfork to handle extents format files.
958  */
959 STATIC int                                      /* error */
960 xfs_bmap_add_attrfork_extents(
961         struct xfs_trans        *tp,            /* transaction pointer */
962         struct xfs_inode        *ip,            /* incore inode pointer */
963         int                     *flags)         /* inode logging flags */
964 {
965         xfs_btree_cur_t         *cur;           /* bmap btree cursor */
966         int                     error;          /* error return value */
967
968         if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
969                 return 0;
970         cur = NULL;
971         error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0, flags,
972                                           XFS_DATA_FORK);
973         if (cur) {
974                 cur->bc_private.b.allocated = 0;
975                 xfs_btree_del_cursor(cur, error);
976         }
977         return error;
978 }
979
980 /*
981  * Called from xfs_bmap_add_attrfork to handle local format files. Each
982  * different data fork content type needs a different callout to do the
983  * conversion. Some are basic and only require special block initialisation
984  * callouts for the data formating, others (directories) are so specialised they
985  * handle everything themselves.
986  *
987  * XXX (dgc): investigate whether directory conversion can use the generic
988  * formatting callout. It should be possible - it's just a very complex
989  * formatter.
990  */
991 STATIC int                                      /* error */
992 xfs_bmap_add_attrfork_local(
993         struct xfs_trans        *tp,            /* transaction pointer */
994         struct xfs_inode        *ip,            /* incore inode pointer */
995         int                     *flags)         /* inode logging flags */
996 {
997         struct xfs_da_args      dargs;          /* args for dir/attr code */
998
999         if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1000                 return 0;
1001
1002         if (S_ISDIR(VFS_I(ip)->i_mode)) {
1003                 memset(&dargs, 0, sizeof(dargs));
1004                 dargs.geo = ip->i_mount->m_dir_geo;
1005                 dargs.dp = ip;
1006                 dargs.total = dargs.geo->fsbcount;
1007                 dargs.whichfork = XFS_DATA_FORK;
1008                 dargs.trans = tp;
1009                 return xfs_dir2_sf_to_block(&dargs);
1010         }
1011
1012         if (S_ISLNK(VFS_I(ip)->i_mode))
1013                 return xfs_bmap_local_to_extents(tp, ip, 1, flags,
1014                                                  XFS_DATA_FORK,
1015                                                  xfs_symlink_local_to_remote);
1016
1017         /* should only be called for types that support local format data */
1018         ASSERT(0);
1019         return -EFSCORRUPTED;
1020 }
1021
1022 /* Set an inode attr fork off based on the format */
1023 int
1024 xfs_bmap_set_attrforkoff(
1025         struct xfs_inode        *ip,
1026         int                     size,
1027         int                     *version)
1028 {
1029         switch (ip->i_d.di_format) {
1030         case XFS_DINODE_FMT_DEV:
1031                 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1032                 break;
1033         case XFS_DINODE_FMT_LOCAL:
1034         case XFS_DINODE_FMT_EXTENTS:
1035         case XFS_DINODE_FMT_BTREE:
1036                 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1037                 if (!ip->i_d.di_forkoff)
1038                         ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1039                 else if ((ip->i_mount->m_flags & XFS_MOUNT_ATTR2) && version)
1040                         *version = 2;
1041                 break;
1042         default:
1043                 ASSERT(0);
1044                 return -EINVAL;
1045         }
1046
1047         return 0;
1048 }
1049
1050 /*
1051  * Convert inode from non-attributed to attributed.
1052  * Must not be in a transaction, ip must not be locked.
1053  */
1054 int                                             /* error code */
1055 xfs_bmap_add_attrfork(
1056         xfs_inode_t             *ip,            /* incore inode pointer */
1057         int                     size,           /* space new attribute needs */
1058         int                     rsvd)           /* xact may use reserved blks */
1059 {
1060         xfs_mount_t             *mp;            /* mount structure */
1061         xfs_trans_t             *tp;            /* transaction pointer */
1062         int                     blks;           /* space reservation */
1063         int                     version = 1;    /* superblock attr version */
1064         int                     logflags;       /* logging flags */
1065         int                     error;          /* error return value */
1066
1067         ASSERT(XFS_IFORK_Q(ip) == 0);
1068
1069         mp = ip->i_mount;
1070         ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1071
1072         blks = XFS_ADDAFORK_SPACE_RES(mp);
1073
1074         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1075                         rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1076         if (error)
1077                 return error;
1078
1079         xfs_ilock(ip, XFS_ILOCK_EXCL);
1080         error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1081                         XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1082                         XFS_QMOPT_RES_REGBLKS);
1083         if (error)
1084                 goto trans_cancel;
1085         if (XFS_IFORK_Q(ip))
1086                 goto trans_cancel;
1087         if (ip->i_d.di_anextents != 0) {
1088                 error = -EFSCORRUPTED;
1089                 goto trans_cancel;
1090         }
1091         if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1092                 /*
1093                  * For inodes coming from pre-6.2 filesystems.
1094                  */
1095                 ASSERT(ip->i_d.di_aformat == 0);
1096                 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1097         }
1098
1099         xfs_trans_ijoin(tp, ip, 0);
1100         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1101         error = xfs_bmap_set_attrforkoff(ip, size, &version);
1102         if (error)
1103                 goto trans_cancel;
1104         ASSERT(ip->i_afp == NULL);
1105         ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1106         ip->i_afp->if_flags = XFS_IFEXTENTS;
1107         logflags = 0;
1108         switch (ip->i_d.di_format) {
1109         case XFS_DINODE_FMT_LOCAL:
1110                 error = xfs_bmap_add_attrfork_local(tp, ip, &logflags);
1111                 break;
1112         case XFS_DINODE_FMT_EXTENTS:
1113                 error = xfs_bmap_add_attrfork_extents(tp, ip, &logflags);
1114                 break;
1115         case XFS_DINODE_FMT_BTREE:
1116                 error = xfs_bmap_add_attrfork_btree(tp, ip, &logflags);
1117                 break;
1118         default:
1119                 error = 0;
1120                 break;
1121         }
1122         if (logflags)
1123                 xfs_trans_log_inode(tp, ip, logflags);
1124         if (error)
1125                 goto trans_cancel;
1126         if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1127            (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1128                 bool log_sb = false;
1129
1130                 spin_lock(&mp->m_sb_lock);
1131                 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1132                         xfs_sb_version_addattr(&mp->m_sb);
1133                         log_sb = true;
1134                 }
1135                 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1136                         xfs_sb_version_addattr2(&mp->m_sb);
1137                         log_sb = true;
1138                 }
1139                 spin_unlock(&mp->m_sb_lock);
1140                 if (log_sb)
1141                         xfs_log_sb(tp);
1142         }
1143
1144         error = xfs_trans_commit(tp);
1145         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1146         return error;
1147
1148 trans_cancel:
1149         xfs_trans_cancel(tp);
1150         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1151         return error;
1152 }
1153
1154 /*
1155  * Internal and external extent tree search functions.
1156  */
1157
1158 /*
1159  * Read in extents from a btree-format inode.
1160  */
1161 int
1162 xfs_iread_extents(
1163         struct xfs_trans        *tp,
1164         struct xfs_inode        *ip,
1165         int                     whichfork)
1166 {
1167         struct xfs_mount        *mp = ip->i_mount;
1168         int                     state = xfs_bmap_fork_to_state(whichfork);
1169         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
1170         xfs_extnum_t            nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1171         struct xfs_btree_block  *block = ifp->if_broot;
1172         struct xfs_iext_cursor  icur;
1173         struct xfs_bmbt_irec    new;
1174         xfs_fsblock_t           bno;
1175         struct xfs_buf          *bp;
1176         xfs_extnum_t            i, j;
1177         int                     level;
1178         __be64                  *pp;
1179         int                     error;
1180
1181         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1182
1183         if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1184                 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1185                 return -EFSCORRUPTED;
1186         }
1187
1188         /*
1189          * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1190          */
1191         level = be16_to_cpu(block->bb_level);
1192         ASSERT(level > 0);
1193         pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1194         bno = be64_to_cpu(*pp);
1195
1196         /*
1197          * Go down the tree until leaf level is reached, following the first
1198          * pointer (leftmost) at each level.
1199          */
1200         while (level-- > 0) {
1201                 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1202                                 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1203                 if (error)
1204                         goto out;
1205                 block = XFS_BUF_TO_BLOCK(bp);
1206                 if (level == 0)
1207                         break;
1208                 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1209                 bno = be64_to_cpu(*pp);
1210                 XFS_WANT_CORRUPTED_GOTO(mp,
1211                         xfs_verify_fsbno(mp, bno), out_brelse);
1212                 xfs_trans_brelse(tp, bp);
1213         }
1214
1215         /*
1216          * Here with bp and block set to the leftmost leaf node in the tree.
1217          */
1218         i = 0;
1219         xfs_iext_first(ifp, &icur);
1220
1221         /*
1222          * Loop over all leaf nodes.  Copy information to the extent records.
1223          */
1224         for (;;) {
1225                 xfs_bmbt_rec_t  *frp;
1226                 xfs_fsblock_t   nextbno;
1227                 xfs_extnum_t    num_recs;
1228
1229                 num_recs = xfs_btree_get_numrecs(block);
1230                 if (unlikely(i + num_recs > nextents)) {
1231                         xfs_warn(ip->i_mount,
1232                                 "corrupt dinode %Lu, (btree extents).",
1233                                 (unsigned long long) ip->i_ino);
1234                         xfs_inode_verifier_error(ip, -EFSCORRUPTED,
1235                                         __func__, block, sizeof(*block),
1236                                         __this_address);
1237                         error = -EFSCORRUPTED;
1238                         goto out_brelse;
1239                 }
1240                 /*
1241                  * Read-ahead the next leaf block, if any.
1242                  */
1243                 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1244                 if (nextbno != NULLFSBLOCK)
1245                         xfs_btree_reada_bufl(mp, nextbno, 1,
1246                                              &xfs_bmbt_buf_ops);
1247                 /*
1248                  * Copy records into the extent records.
1249                  */
1250                 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1251                 for (j = 0; j < num_recs; j++, frp++, i++) {
1252                         xfs_failaddr_t  fa;
1253
1254                         xfs_bmbt_disk_get_all(frp, &new);
1255                         fa = xfs_bmap_validate_extent(ip, whichfork, &new);
1256                         if (fa) {
1257                                 error = -EFSCORRUPTED;
1258                                 xfs_inode_verifier_error(ip, error,
1259                                                 "xfs_iread_extents(2)",
1260                                                 frp, sizeof(*frp), fa);
1261                                 goto out_brelse;
1262                         }
1263                         xfs_iext_insert(ip, &icur, &new, state);
1264                         trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
1265                         xfs_iext_next(ifp, &icur);
1266                 }
1267                 xfs_trans_brelse(tp, bp);
1268                 bno = nextbno;
1269                 /*
1270                  * If we've reached the end, stop.
1271                  */
1272                 if (bno == NULLFSBLOCK)
1273                         break;
1274                 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1275                                 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1276                 if (error)
1277                         goto out;
1278                 block = XFS_BUF_TO_BLOCK(bp);
1279         }
1280
1281         if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1282                 error = -EFSCORRUPTED;
1283                 goto out;
1284         }
1285         ASSERT(i == xfs_iext_count(ifp));
1286
1287         ifp->if_flags |= XFS_IFEXTENTS;
1288         return 0;
1289
1290 out_brelse:
1291         xfs_trans_brelse(tp, bp);
1292 out:
1293         xfs_iext_destroy(ifp);
1294         return error;
1295 }
1296
1297 /*
1298  * Returns the relative block number of the first unused block(s) in the given
1299  * fork with at least "len" logically contiguous blocks free.  This is the
1300  * lowest-address hole if the fork has holes, else the first block past the end
1301  * of fork.  Return 0 if the fork is currently local (in-inode).
1302  */
1303 int                                             /* error */
1304 xfs_bmap_first_unused(
1305         struct xfs_trans        *tp,            /* transaction pointer */
1306         struct xfs_inode        *ip,            /* incore inode */
1307         xfs_extlen_t            len,            /* size of hole to find */
1308         xfs_fileoff_t           *first_unused,  /* unused block */
1309         int                     whichfork)      /* data or attr fork */
1310 {
1311         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
1312         struct xfs_bmbt_irec    got;
1313         struct xfs_iext_cursor  icur;
1314         xfs_fileoff_t           lastaddr = 0;
1315         xfs_fileoff_t           lowest, max;
1316         int                     error;
1317
1318         ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1319                XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1320                XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1321
1322         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1323                 *first_unused = 0;
1324                 return 0;
1325         }
1326
1327         if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1328                 error = xfs_iread_extents(tp, ip, whichfork);
1329                 if (error)
1330                         return error;
1331         }
1332
1333         lowest = max = *first_unused;
1334         for_each_xfs_iext(ifp, &icur, &got) {
1335                 /*
1336                  * See if the hole before this extent will work.
1337                  */
1338                 if (got.br_startoff >= lowest + len &&
1339                     got.br_startoff - max >= len)
1340                         break;
1341                 lastaddr = got.br_startoff + got.br_blockcount;
1342                 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1343         }
1344
1345         *first_unused = max;
1346         return 0;
1347 }
1348
1349 /*
1350  * Returns the file-relative block number of the last block - 1 before
1351  * last_block (input value) in the file.
1352  * This is not based on i_size, it is based on the extent records.
1353  * Returns 0 for local files, as they do not have extent records.
1354  */
1355 int                                             /* error */
1356 xfs_bmap_last_before(
1357         struct xfs_trans        *tp,            /* transaction pointer */
1358         struct xfs_inode        *ip,            /* incore inode */
1359         xfs_fileoff_t           *last_block,    /* last block */
1360         int                     whichfork)      /* data or attr fork */
1361 {
1362         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
1363         struct xfs_bmbt_irec    got;
1364         struct xfs_iext_cursor  icur;
1365         int                     error;
1366
1367         switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1368         case XFS_DINODE_FMT_LOCAL:
1369                 *last_block = 0;
1370                 return 0;
1371         case XFS_DINODE_FMT_BTREE:
1372         case XFS_DINODE_FMT_EXTENTS:
1373                 break;
1374         default:
1375                 return -EIO;
1376         }
1377
1378         if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1379                 error = xfs_iread_extents(tp, ip, whichfork);
1380                 if (error)
1381                         return error;
1382         }
1383
1384         if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &icur, &got))
1385                 *last_block = 0;
1386         return 0;
1387 }
1388
1389 int
1390 xfs_bmap_last_extent(
1391         struct xfs_trans        *tp,
1392         struct xfs_inode        *ip,
1393         int                     whichfork,
1394         struct xfs_bmbt_irec    *rec,
1395         int                     *is_empty)
1396 {
1397         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
1398         struct xfs_iext_cursor  icur;
1399         int                     error;
1400
1401         if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1402                 error = xfs_iread_extents(tp, ip, whichfork);
1403                 if (error)
1404                         return error;
1405         }
1406
1407         xfs_iext_last(ifp, &icur);
1408         if (!xfs_iext_get_extent(ifp, &icur, rec))
1409                 *is_empty = 1;
1410         else
1411                 *is_empty = 0;
1412         return 0;
1413 }
1414
1415 /*
1416  * Check the last inode extent to determine whether this allocation will result
1417  * in blocks being allocated at the end of the file. When we allocate new data
1418  * blocks at the end of the file which do not start at the previous data block,
1419  * we will try to align the new blocks at stripe unit boundaries.
1420  *
1421  * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
1422  * at, or past the EOF.
1423  */
1424 STATIC int
1425 xfs_bmap_isaeof(
1426         struct xfs_bmalloca     *bma,
1427         int                     whichfork)
1428 {
1429         struct xfs_bmbt_irec    rec;
1430         int                     is_empty;
1431         int                     error;
1432
1433         bma->aeof = false;
1434         error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1435                                      &is_empty);
1436         if (error)
1437                 return error;
1438
1439         if (is_empty) {
1440                 bma->aeof = true;
1441                 return 0;
1442         }
1443
1444         /*
1445          * Check if we are allocation or past the last extent, or at least into
1446          * the last delayed allocated extent.
1447          */
1448         bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1449                 (bma->offset >= rec.br_startoff &&
1450                  isnullstartblock(rec.br_startblock));
1451         return 0;
1452 }
1453
1454 /*
1455  * Returns the file-relative block number of the first block past eof in
1456  * the file.  This is not based on i_size, it is based on the extent records.
1457  * Returns 0 for local files, as they do not have extent records.
1458  */
1459 int
1460 xfs_bmap_last_offset(
1461         struct xfs_inode        *ip,
1462         xfs_fileoff_t           *last_block,
1463         int                     whichfork)
1464 {
1465         struct xfs_bmbt_irec    rec;
1466         int                     is_empty;
1467         int                     error;
1468
1469         *last_block = 0;
1470
1471         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1472                 return 0;
1473
1474         if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1475             XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1476                return -EIO;
1477
1478         error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1479         if (error || is_empty)
1480                 return error;
1481
1482         *last_block = rec.br_startoff + rec.br_blockcount;
1483         return 0;
1484 }
1485
1486 /*
1487  * Returns whether the selected fork of the inode has exactly one
1488  * block or not.  For the data fork we check this matches di_size,
1489  * implying the file's range is 0..bsize-1.
1490  */
1491 int                                     /* 1=>1 block, 0=>otherwise */
1492 xfs_bmap_one_block(
1493         xfs_inode_t     *ip,            /* incore inode */
1494         int             whichfork)      /* data or attr fork */
1495 {
1496         struct xfs_ifork *ifp;          /* inode fork pointer */
1497         int             rval;           /* return value */
1498         xfs_bmbt_irec_t s;              /* internal version of extent */
1499         struct xfs_iext_cursor icur;
1500
1501 #ifndef DEBUG
1502         if (whichfork == XFS_DATA_FORK)
1503                 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1504 #endif  /* !DEBUG */
1505         if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1506                 return 0;
1507         if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1508                 return 0;
1509         ifp = XFS_IFORK_PTR(ip, whichfork);
1510         ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1511         xfs_iext_first(ifp, &icur);
1512         xfs_iext_get_extent(ifp, &icur, &s);
1513         rval = s.br_startoff == 0 && s.br_blockcount == 1;
1514         if (rval && whichfork == XFS_DATA_FORK)
1515                 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1516         return rval;
1517 }
1518
1519 /*
1520  * Extent tree manipulation functions used during allocation.
1521  */
1522
1523 /*
1524  * Convert a delayed allocation to a real allocation.
1525  */
1526 STATIC int                              /* error */
1527 xfs_bmap_add_extent_delay_real(
1528         struct xfs_bmalloca     *bma,
1529         int                     whichfork)
1530 {
1531         struct xfs_bmbt_irec    *new = &bma->got;
1532         int                     error;  /* error return value */
1533         int                     i;      /* temp state */
1534         struct xfs_ifork        *ifp;   /* inode fork pointer */
1535         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
1536         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
1537                                         /* left is 0, right is 1, prev is 2 */
1538         int                     rval=0; /* return value (logging flags) */
1539         int                     state = xfs_bmap_fork_to_state(whichfork);
1540         xfs_filblks_t           da_new; /* new count del alloc blocks used */
1541         xfs_filblks_t           da_old; /* old count del alloc blocks used */
1542         xfs_filblks_t           temp=0; /* value for da_new calculations */
1543         int                     tmp_rval;       /* partial logging flags */
1544         struct xfs_mount        *mp;
1545         xfs_extnum_t            *nextents;
1546         struct xfs_bmbt_irec    old;
1547
1548         mp = bma->ip->i_mount;
1549         ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1550         ASSERT(whichfork != XFS_ATTR_FORK);
1551         nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1552                                                 &bma->ip->i_d.di_nextents);
1553
1554         ASSERT(!isnullstartblock(new->br_startblock));
1555         ASSERT(!bma->cur ||
1556                (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1557
1558         XFS_STATS_INC(mp, xs_add_exlist);
1559
1560 #define LEFT            r[0]
1561 #define RIGHT           r[1]
1562 #define PREV            r[2]
1563
1564         /*
1565          * Set up a bunch of variables to make the tests simpler.
1566          */
1567         xfs_iext_get_extent(ifp, &bma->icur, &PREV);
1568         new_endoff = new->br_startoff + new->br_blockcount;
1569         ASSERT(isnullstartblock(PREV.br_startblock));
1570         ASSERT(PREV.br_startoff <= new->br_startoff);
1571         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1572
1573         da_old = startblockval(PREV.br_startblock);
1574         da_new = 0;
1575
1576         /*
1577          * Set flags determining what part of the previous delayed allocation
1578          * extent is being replaced by a real allocation.
1579          */
1580         if (PREV.br_startoff == new->br_startoff)
1581                 state |= BMAP_LEFT_FILLING;
1582         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1583                 state |= BMAP_RIGHT_FILLING;
1584
1585         /*
1586          * Check and set flags if this segment has a left neighbor.
1587          * Don't set contiguous if the combined extent would be too large.
1588          */
1589         if (xfs_iext_peek_prev_extent(ifp, &bma->icur, &LEFT)) {
1590                 state |= BMAP_LEFT_VALID;
1591                 if (isnullstartblock(LEFT.br_startblock))
1592                         state |= BMAP_LEFT_DELAY;
1593         }
1594
1595         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1596             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1597             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1598             LEFT.br_state == new->br_state &&
1599             LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1600                 state |= BMAP_LEFT_CONTIG;
1601
1602         /*
1603          * Check and set flags if this segment has a right neighbor.
1604          * Don't set contiguous if the combined extent would be too large.
1605          * Also check for all-three-contiguous being too large.
1606          */
1607         if (xfs_iext_peek_next_extent(ifp, &bma->icur, &RIGHT)) {
1608                 state |= BMAP_RIGHT_VALID;
1609                 if (isnullstartblock(RIGHT.br_startblock))
1610                         state |= BMAP_RIGHT_DELAY;
1611         }
1612
1613         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1614             new_endoff == RIGHT.br_startoff &&
1615             new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1616             new->br_state == RIGHT.br_state &&
1617             new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1618             ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1619                        BMAP_RIGHT_FILLING)) !=
1620                       (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1621                        BMAP_RIGHT_FILLING) ||
1622              LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1623                         <= MAXEXTLEN))
1624                 state |= BMAP_RIGHT_CONTIG;
1625
1626         error = 0;
1627         /*
1628          * Switch out based on the FILLING and CONTIG state bits.
1629          */
1630         switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1631                          BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1632         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1633              BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1634                 /*
1635                  * Filling in all of a previously delayed allocation extent.
1636                  * The left and right neighbors are both contiguous with new.
1637                  */
1638                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1639
1640                 xfs_iext_remove(bma->ip, &bma->icur, state);
1641                 xfs_iext_remove(bma->ip, &bma->icur, state);
1642                 xfs_iext_prev(ifp, &bma->icur);
1643                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1644                 (*nextents)--;
1645
1646                 if (bma->cur == NULL)
1647                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1648                 else {
1649                         rval = XFS_ILOG_CORE;
1650                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1651                         if (error)
1652                                 goto done;
1653                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1654                         error = xfs_btree_delete(bma->cur, &i);
1655                         if (error)
1656                                 goto done;
1657                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1658                         error = xfs_btree_decrement(bma->cur, 0, &i);
1659                         if (error)
1660                                 goto done;
1661                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1662                         error = xfs_bmbt_update(bma->cur, &LEFT);
1663                         if (error)
1664                                 goto done;
1665                 }
1666                 break;
1667
1668         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1669                 /*
1670                  * Filling in all of a previously delayed allocation extent.
1671                  * The left neighbor is contiguous, the right is not.
1672                  */
1673                 old = LEFT;
1674                 LEFT.br_blockcount += PREV.br_blockcount;
1675
1676                 xfs_iext_remove(bma->ip, &bma->icur, state);
1677                 xfs_iext_prev(ifp, &bma->icur);
1678                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1679
1680                 if (bma->cur == NULL)
1681                         rval = XFS_ILOG_DEXT;
1682                 else {
1683                         rval = 0;
1684                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1685                         if (error)
1686                                 goto done;
1687                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1688                         error = xfs_bmbt_update(bma->cur, &LEFT);
1689                         if (error)
1690                                 goto done;
1691                 }
1692                 break;
1693
1694         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1695                 /*
1696                  * Filling in all of a previously delayed allocation extent.
1697                  * The right neighbor is contiguous, the left is not. Take care
1698                  * with delay -> unwritten extent allocation here because the
1699                  * delalloc record we are overwriting is always written.
1700                  */
1701                 PREV.br_startblock = new->br_startblock;
1702                 PREV.br_blockcount += RIGHT.br_blockcount;
1703                 PREV.br_state = new->br_state;
1704
1705                 xfs_iext_next(ifp, &bma->icur);
1706                 xfs_iext_remove(bma->ip, &bma->icur, state);
1707                 xfs_iext_prev(ifp, &bma->icur);
1708                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1709
1710                 if (bma->cur == NULL)
1711                         rval = XFS_ILOG_DEXT;
1712                 else {
1713                         rval = 0;
1714                         error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
1715                         if (error)
1716                                 goto done;
1717                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1718                         error = xfs_bmbt_update(bma->cur, &PREV);
1719                         if (error)
1720                                 goto done;
1721                 }
1722                 break;
1723
1724         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1725                 /*
1726                  * Filling in all of a previously delayed allocation extent.
1727                  * Neither the left nor right neighbors are contiguous with
1728                  * the new one.
1729                  */
1730                 PREV.br_startblock = new->br_startblock;
1731                 PREV.br_state = new->br_state;
1732                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1733
1734                 (*nextents)++;
1735                 if (bma->cur == NULL)
1736                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1737                 else {
1738                         rval = XFS_ILOG_CORE;
1739                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1740                         if (error)
1741                                 goto done;
1742                         XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1743                         error = xfs_btree_insert(bma->cur, &i);
1744                         if (error)
1745                                 goto done;
1746                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1747                 }
1748                 break;
1749
1750         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1751                 /*
1752                  * Filling in the first part of a previous delayed allocation.
1753                  * The left neighbor is contiguous.
1754                  */
1755                 old = LEFT;
1756                 temp = PREV.br_blockcount - new->br_blockcount;
1757                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1758                                 startblockval(PREV.br_startblock));
1759
1760                 LEFT.br_blockcount += new->br_blockcount;
1761
1762                 PREV.br_blockcount = temp;
1763                 PREV.br_startoff += new->br_blockcount;
1764                 PREV.br_startblock = nullstartblock(da_new);
1765
1766                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1767                 xfs_iext_prev(ifp, &bma->icur);
1768                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &LEFT);
1769
1770                 if (bma->cur == NULL)
1771                         rval = XFS_ILOG_DEXT;
1772                 else {
1773                         rval = 0;
1774                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1775                         if (error)
1776                                 goto done;
1777                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1778                         error = xfs_bmbt_update(bma->cur, &LEFT);
1779                         if (error)
1780                                 goto done;
1781                 }
1782                 break;
1783
1784         case BMAP_LEFT_FILLING:
1785                 /*
1786                  * Filling in the first part of a previous delayed allocation.
1787                  * The left neighbor is not contiguous.
1788                  */
1789                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1790                 (*nextents)++;
1791                 if (bma->cur == NULL)
1792                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1793                 else {
1794                         rval = XFS_ILOG_CORE;
1795                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1796                         if (error)
1797                                 goto done;
1798                         XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1799                         error = xfs_btree_insert(bma->cur, &i);
1800                         if (error)
1801                                 goto done;
1802                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1803                 }
1804
1805                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1806                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1807                                         &bma->cur, 1, &tmp_rval, whichfork);
1808                         rval |= tmp_rval;
1809                         if (error)
1810                                 goto done;
1811                 }
1812
1813                 temp = PREV.br_blockcount - new->br_blockcount;
1814                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1815                         startblockval(PREV.br_startblock) -
1816                         (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1817
1818                 PREV.br_startoff = new_endoff;
1819                 PREV.br_blockcount = temp;
1820                 PREV.br_startblock = nullstartblock(da_new);
1821                 xfs_iext_next(ifp, &bma->icur);
1822                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1823                 xfs_iext_prev(ifp, &bma->icur);
1824                 break;
1825
1826         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1827                 /*
1828                  * Filling in the last part of a previous delayed allocation.
1829                  * The right neighbor is contiguous with the new allocation.
1830                  */
1831                 old = RIGHT;
1832                 RIGHT.br_startoff = new->br_startoff;
1833                 RIGHT.br_startblock = new->br_startblock;
1834                 RIGHT.br_blockcount += new->br_blockcount;
1835
1836                 if (bma->cur == NULL)
1837                         rval = XFS_ILOG_DEXT;
1838                 else {
1839                         rval = 0;
1840                         error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
1841                         if (error)
1842                                 goto done;
1843                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1844                         error = xfs_bmbt_update(bma->cur, &RIGHT);
1845                         if (error)
1846                                 goto done;
1847                 }
1848
1849                 temp = PREV.br_blockcount - new->br_blockcount;
1850                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1851                         startblockval(PREV.br_startblock));
1852
1853                 PREV.br_blockcount = temp;
1854                 PREV.br_startblock = nullstartblock(da_new);
1855
1856                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1857                 xfs_iext_next(ifp, &bma->icur);
1858                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &RIGHT);
1859                 break;
1860
1861         case BMAP_RIGHT_FILLING:
1862                 /*
1863                  * Filling in the last part of a previous delayed allocation.
1864                  * The right neighbor is not contiguous.
1865                  */
1866                 xfs_iext_update_extent(bma->ip, state, &bma->icur, new);
1867                 (*nextents)++;
1868                 if (bma->cur == NULL)
1869                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1870                 else {
1871                         rval = XFS_ILOG_CORE;
1872                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1873                         if (error)
1874                                 goto done;
1875                         XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1876                         error = xfs_btree_insert(bma->cur, &i);
1877                         if (error)
1878                                 goto done;
1879                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1880                 }
1881
1882                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1883                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1884                                 &bma->cur, 1, &tmp_rval, whichfork);
1885                         rval |= tmp_rval;
1886                         if (error)
1887                                 goto done;
1888                 }
1889
1890                 temp = PREV.br_blockcount - new->br_blockcount;
1891                 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1892                         startblockval(PREV.br_startblock) -
1893                         (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1894
1895                 PREV.br_startblock = nullstartblock(da_new);
1896                 PREV.br_blockcount = temp;
1897                 xfs_iext_insert(bma->ip, &bma->icur, &PREV, state);
1898                 xfs_iext_next(ifp, &bma->icur);
1899                 break;
1900
1901         case 0:
1902                 /*
1903                  * Filling in the middle part of a previous delayed allocation.
1904                  * Contiguity is impossible here.
1905                  * This case is avoided almost all the time.
1906                  *
1907                  * We start with a delayed allocation:
1908                  *
1909                  * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1910                  *  PREV @ idx
1911                  *
1912                  * and we are allocating:
1913                  *                     +rrrrrrrrrrrrrrrrr+
1914                  *                            new
1915                  *
1916                  * and we set it up for insertion as:
1917                  * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1918                  *                            new
1919                  *  PREV @ idx          LEFT              RIGHT
1920                  *                      inserted at idx + 1
1921                  */
1922                 old = PREV;
1923
1924                 /* LEFT is the new middle */
1925                 LEFT = *new;
1926
1927                 /* RIGHT is the new right */
1928                 RIGHT.br_state = PREV.br_state;
1929                 RIGHT.br_startoff = new_endoff;
1930                 RIGHT.br_blockcount =
1931                         PREV.br_startoff + PREV.br_blockcount - new_endoff;
1932                 RIGHT.br_startblock =
1933                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1934                                         RIGHT.br_blockcount));
1935
1936                 /* truncate PREV */
1937                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1938                 PREV.br_startblock =
1939                         nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1940                                         PREV.br_blockcount));
1941                 xfs_iext_update_extent(bma->ip, state, &bma->icur, &PREV);
1942
1943                 xfs_iext_next(ifp, &bma->icur);
1944                 xfs_iext_insert(bma->ip, &bma->icur, &RIGHT, state);
1945                 xfs_iext_insert(bma->ip, &bma->icur, &LEFT, state);
1946                 (*nextents)++;
1947
1948                 if (bma->cur == NULL)
1949                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1950                 else {
1951                         rval = XFS_ILOG_CORE;
1952                         error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
1953                         if (error)
1954                                 goto done;
1955                         XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
1956                         error = xfs_btree_insert(bma->cur, &i);
1957                         if (error)
1958                                 goto done;
1959                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1960                 }
1961
1962                 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1963                         error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1964                                         &bma->cur, 1, &tmp_rval, whichfork);
1965                         rval |= tmp_rval;
1966                         if (error)
1967                                 goto done;
1968                 }
1969
1970                 da_new = startblockval(PREV.br_startblock) +
1971                          startblockval(RIGHT.br_startblock);
1972                 break;
1973
1974         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1975         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1976         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1977         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1978         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979         case BMAP_LEFT_CONTIG:
1980         case BMAP_RIGHT_CONTIG:
1981                 /*
1982                  * These cases are all impossible.
1983                  */
1984                 ASSERT(0);
1985         }
1986
1987         /* add reverse mapping unless caller opted out */
1988         if (!(bma->flags & XFS_BMAPI_NORMAP)) {
1989                 error = xfs_rmap_map_extent(bma->tp, bma->ip, whichfork, new);
1990                 if (error)
1991                         goto done;
1992         }
1993
1994         /* convert to a btree if necessary */
1995         if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1996                 int     tmp_logflags;   /* partial log flag return val */
1997
1998                 ASSERT(bma->cur == NULL);
1999                 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2000                                 &bma->cur, da_old > 0, &tmp_logflags,
2001                                 whichfork);
2002                 bma->logflags |= tmp_logflags;
2003                 if (error)
2004                         goto done;
2005         }
2006
2007         if (bma->cur) {
2008                 da_new += bma->cur->bc_private.b.allocated;
2009                 bma->cur->bc_private.b.allocated = 0;
2010         }
2011
2012         /* adjust for changes in reserved delayed indirect blocks */
2013         if (da_new != da_old) {
2014                 ASSERT(state == 0 || da_new < da_old);
2015                 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2016                                 false);
2017         }
2018
2019         xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
2020 done:
2021         if (whichfork != XFS_COW_FORK)
2022                 bma->logflags |= rval;
2023         return error;
2024 #undef  LEFT
2025 #undef  RIGHT
2026 #undef  PREV
2027 }
2028
2029 /*
2030  * Convert an unwritten allocation to a real allocation or vice versa.
2031  */
2032 STATIC int                              /* error */
2033 xfs_bmap_add_extent_unwritten_real(
2034         struct xfs_trans        *tp,
2035         xfs_inode_t             *ip,    /* incore inode pointer */
2036         int                     whichfork,
2037         struct xfs_iext_cursor  *icur,
2038         xfs_btree_cur_t         **curp, /* if *curp is null, not a btree */
2039         xfs_bmbt_irec_t         *new,   /* new data to add to file extents */
2040         int                     *logflagsp) /* inode logging flags */
2041 {
2042         xfs_btree_cur_t         *cur;   /* btree cursor */
2043         int                     error;  /* error return value */
2044         int                     i;      /* temp state */
2045         struct xfs_ifork        *ifp;   /* inode fork pointer */
2046         xfs_fileoff_t           new_endoff;     /* end offset of new entry */
2047         xfs_bmbt_irec_t         r[3];   /* neighbor extent entries */
2048                                         /* left is 0, right is 1, prev is 2 */
2049         int                     rval=0; /* return value (logging flags) */
2050         int                     state = xfs_bmap_fork_to_state(whichfork);
2051         struct xfs_mount        *mp = ip->i_mount;
2052         struct xfs_bmbt_irec    old;
2053
2054         *logflagsp = 0;
2055
2056         cur = *curp;
2057         ifp = XFS_IFORK_PTR(ip, whichfork);
2058
2059         ASSERT(!isnullstartblock(new->br_startblock));
2060
2061         XFS_STATS_INC(mp, xs_add_exlist);
2062
2063 #define LEFT            r[0]
2064 #define RIGHT           r[1]
2065 #define PREV            r[2]
2066
2067         /*
2068          * Set up a bunch of variables to make the tests simpler.
2069          */
2070         error = 0;
2071         xfs_iext_get_extent(ifp, icur, &PREV);
2072         ASSERT(new->br_state != PREV.br_state);
2073         new_endoff = new->br_startoff + new->br_blockcount;
2074         ASSERT(PREV.br_startoff <= new->br_startoff);
2075         ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
2076
2077         /*
2078          * Set flags determining what part of the previous oldext allocation
2079          * extent is being replaced by a newext allocation.
2080          */
2081         if (PREV.br_startoff == new->br_startoff)
2082                 state |= BMAP_LEFT_FILLING;
2083         if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2084                 state |= BMAP_RIGHT_FILLING;
2085
2086         /*
2087          * Check and set flags if this segment has a left neighbor.
2088          * Don't set contiguous if the combined extent would be too large.
2089          */
2090         if (xfs_iext_peek_prev_extent(ifp, icur, &LEFT)) {
2091                 state |= BMAP_LEFT_VALID;
2092                 if (isnullstartblock(LEFT.br_startblock))
2093                         state |= BMAP_LEFT_DELAY;
2094         }
2095
2096         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2097             LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2098             LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2099             LEFT.br_state == new->br_state &&
2100             LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2101                 state |= BMAP_LEFT_CONTIG;
2102
2103         /*
2104          * Check and set flags if this segment has a right neighbor.
2105          * Don't set contiguous if the combined extent would be too large.
2106          * Also check for all-three-contiguous being too large.
2107          */
2108         if (xfs_iext_peek_next_extent(ifp, icur, &RIGHT)) {
2109                 state |= BMAP_RIGHT_VALID;
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                 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2139
2140                 xfs_iext_remove(ip, icur, state);
2141                 xfs_iext_remove(ip, icur, state);
2142                 xfs_iext_prev(ifp, icur);
2143                 xfs_iext_update_extent(ip, state, icur, &LEFT);
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                 LEFT.br_blockcount += PREV.br_blockcount;
2178
2179                 xfs_iext_remove(ip, icur, state);
2180                 xfs_iext_prev(ifp, icur);
2181                 xfs_iext_update_extent(ip, state, icur, &LEFT);
2182                 XFS_IFORK_NEXT_SET(ip, whichfork,
2183                                 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2184                 if (cur == NULL)
2185                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2186                 else {
2187                         rval = XFS_ILOG_CORE;
2188                         error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2189                         if (error)
2190                                 goto done;
2191                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2192                         if ((error = xfs_btree_delete(cur, &i)))
2193                                 goto done;
2194                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2195                         if ((error = xfs_btree_decrement(cur, 0, &i)))
2196                                 goto done;
2197                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2198                         error = xfs_bmbt_update(cur, &LEFT);
2199                         if (error)
2200                                 goto done;
2201                 }
2202                 break;
2203
2204         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2205                 /*
2206                  * Setting all of a previous oldext extent to newext.
2207                  * The right neighbor is contiguous, the left is not.
2208                  */
2209                 PREV.br_blockcount += RIGHT.br_blockcount;
2210                 PREV.br_state = new->br_state;
2211
2212                 xfs_iext_next(ifp, icur);
2213                 xfs_iext_remove(ip, icur, state);
2214                 xfs_iext_prev(ifp, icur);
2215                 xfs_iext_update_extent(ip, state, icur, &PREV);
2216
2217                 XFS_IFORK_NEXT_SET(ip, whichfork,
2218                                 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2219                 if (cur == NULL)
2220                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2221                 else {
2222                         rval = XFS_ILOG_CORE;
2223                         error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2224                         if (error)
2225                                 goto done;
2226                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2227                         if ((error = xfs_btree_delete(cur, &i)))
2228                                 goto done;
2229                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2230                         if ((error = xfs_btree_decrement(cur, 0, &i)))
2231                                 goto done;
2232                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2233                         error = xfs_bmbt_update(cur, &PREV);
2234                         if (error)
2235                                 goto done;
2236                 }
2237                 break;
2238
2239         case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2240                 /*
2241                  * Setting all of a previous oldext extent to newext.
2242                  * Neither the left nor right neighbors are contiguous with
2243                  * the new one.
2244                  */
2245                 PREV.br_state = new->br_state;
2246                 xfs_iext_update_extent(ip, state, icur, &PREV);
2247
2248                 if (cur == NULL)
2249                         rval = XFS_ILOG_DEXT;
2250                 else {
2251                         rval = 0;
2252                         error = xfs_bmbt_lookup_eq(cur, new, &i);
2253                         if (error)
2254                                 goto done;
2255                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2256                         error = xfs_bmbt_update(cur, &PREV);
2257                         if (error)
2258                                 goto done;
2259                 }
2260                 break;
2261
2262         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2263                 /*
2264                  * Setting the first part of a previous oldext extent to newext.
2265                  * The left neighbor is contiguous.
2266                  */
2267                 LEFT.br_blockcount += new->br_blockcount;
2268
2269                 old = PREV;
2270                 PREV.br_startoff += new->br_blockcount;
2271                 PREV.br_startblock += new->br_blockcount;
2272                 PREV.br_blockcount -= new->br_blockcount;
2273
2274                 xfs_iext_update_extent(ip, state, icur, &PREV);
2275                 xfs_iext_prev(ifp, icur);
2276                 xfs_iext_update_extent(ip, state, icur, &LEFT);
2277
2278                 if (cur == NULL)
2279                         rval = XFS_ILOG_DEXT;
2280                 else {
2281                         rval = 0;
2282                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
2283                         if (error)
2284                                 goto done;
2285                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2286                         error = xfs_bmbt_update(cur, &PREV);
2287                         if (error)
2288                                 goto done;
2289                         error = xfs_btree_decrement(cur, 0, &i);
2290                         if (error)
2291                                 goto done;
2292                         error = xfs_bmbt_update(cur, &LEFT);
2293                         if (error)
2294                                 goto done;
2295                 }
2296                 break;
2297
2298         case BMAP_LEFT_FILLING:
2299                 /*
2300                  * Setting the first part of a previous oldext extent to newext.
2301                  * The left neighbor is not contiguous.
2302                  */
2303                 old = PREV;
2304                 PREV.br_startoff += new->br_blockcount;
2305                 PREV.br_startblock += new->br_blockcount;
2306                 PREV.br_blockcount -= new->br_blockcount;
2307
2308                 xfs_iext_update_extent(ip, state, icur, &PREV);
2309                 xfs_iext_insert(ip, icur, new, state);
2310                 XFS_IFORK_NEXT_SET(ip, whichfork,
2311                                 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2312                 if (cur == NULL)
2313                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2314                 else {
2315                         rval = XFS_ILOG_CORE;
2316                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
2317                         if (error)
2318                                 goto done;
2319                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2320                         error = xfs_bmbt_update(cur, &PREV);
2321                         if (error)
2322                                 goto done;
2323                         cur->bc_rec.b = *new;
2324                         if ((error = xfs_btree_insert(cur, &i)))
2325                                 goto done;
2326                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2327                 }
2328                 break;
2329
2330         case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2331                 /*
2332                  * Setting the last part of a previous oldext extent to newext.
2333                  * The right neighbor is contiguous with the new allocation.
2334                  */
2335                 old = PREV;
2336                 PREV.br_blockcount -= new->br_blockcount;
2337
2338                 RIGHT.br_startoff = new->br_startoff;
2339                 RIGHT.br_startblock = new->br_startblock;
2340                 RIGHT.br_blockcount += new->br_blockcount;
2341
2342                 xfs_iext_update_extent(ip, state, icur, &PREV);
2343                 xfs_iext_next(ifp, icur);
2344                 xfs_iext_update_extent(ip, state, icur, &RIGHT);
2345
2346                 if (cur == NULL)
2347                         rval = XFS_ILOG_DEXT;
2348                 else {
2349                         rval = 0;
2350                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
2351                         if (error)
2352                                 goto done;
2353                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2354                         error = xfs_bmbt_update(cur, &PREV);
2355                         if (error)
2356                                 goto done;
2357                         error = xfs_btree_increment(cur, 0, &i);
2358                         if (error)
2359                                 goto done;
2360                         error = xfs_bmbt_update(cur, &RIGHT);
2361                         if (error)
2362                                 goto done;
2363                 }
2364                 break;
2365
2366         case BMAP_RIGHT_FILLING:
2367                 /*
2368                  * Setting the last part of a previous oldext extent to newext.
2369                  * The right neighbor is not contiguous.
2370                  */
2371                 old = PREV;
2372                 PREV.br_blockcount -= new->br_blockcount;
2373
2374                 xfs_iext_update_extent(ip, state, icur, &PREV);
2375                 xfs_iext_next(ifp, icur);
2376                 xfs_iext_insert(ip, icur, new, state);
2377
2378                 XFS_IFORK_NEXT_SET(ip, whichfork,
2379                                 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2380                 if (cur == NULL)
2381                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2382                 else {
2383                         rval = XFS_ILOG_CORE;
2384                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
2385                         if (error)
2386                                 goto done;
2387                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2388                         error = xfs_bmbt_update(cur, &PREV);
2389                         if (error)
2390                                 goto done;
2391                         error = xfs_bmbt_lookup_eq(cur, new, &i);
2392                         if (error)
2393                                 goto done;
2394                         XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2395                         if ((error = xfs_btree_insert(cur, &i)))
2396                                 goto done;
2397                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2398                 }
2399                 break;
2400
2401         case 0:
2402                 /*
2403                  * Setting the middle part of a previous oldext extent to
2404                  * newext.  Contiguity is impossible here.
2405                  * One extent becomes three extents.
2406                  */
2407                 old = PREV;
2408                 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2409
2410                 r[0] = *new;
2411                 r[1].br_startoff = new_endoff;
2412                 r[1].br_blockcount =
2413                         old.br_startoff + old.br_blockcount - new_endoff;
2414                 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2415                 r[1].br_state = PREV.br_state;
2416
2417                 xfs_iext_update_extent(ip, state, icur, &PREV);
2418                 xfs_iext_next(ifp, icur);
2419                 xfs_iext_insert(ip, icur, &r[1], state);
2420                 xfs_iext_insert(ip, icur, &r[0], state);
2421
2422                 XFS_IFORK_NEXT_SET(ip, whichfork,
2423                                 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
2424                 if (cur == NULL)
2425                         rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2426                 else {
2427                         rval = XFS_ILOG_CORE;
2428                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
2429                         if (error)
2430                                 goto done;
2431                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2432                         /* new right extent - oldext */
2433                         error = xfs_bmbt_update(cur, &r[1]);
2434                         if (error)
2435                                 goto done;
2436                         /* new left extent - oldext */
2437                         cur->bc_rec.b = PREV;
2438                         if ((error = xfs_btree_insert(cur, &i)))
2439                                 goto done;
2440                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2441                         /*
2442                          * Reset the cursor to the position of the new extent
2443                          * we are about to insert as we can't trust it after
2444                          * the previous insert.
2445                          */
2446                         error = xfs_bmbt_lookup_eq(cur, new, &i);
2447                         if (error)
2448                                 goto done;
2449                         XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2450                         /* new middle extent - newext */
2451                         if ((error = xfs_btree_insert(cur, &i)))
2452                                 goto done;
2453                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2454                 }
2455                 break;
2456
2457         case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2458         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2459         case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2460         case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2461         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2462         case BMAP_LEFT_CONTIG:
2463         case BMAP_RIGHT_CONTIG:
2464                 /*
2465                  * These cases are all impossible.
2466                  */
2467                 ASSERT(0);
2468         }
2469
2470         /* update reverse mappings */
2471         error = xfs_rmap_convert_extent(mp, tp, ip, whichfork, new);
2472         if (error)
2473                 goto done;
2474
2475         /* convert to a btree if necessary */
2476         if (xfs_bmap_needs_btree(ip, whichfork)) {
2477                 int     tmp_logflags;   /* partial log flag return val */
2478
2479                 ASSERT(cur == NULL);
2480                 error = xfs_bmap_extents_to_btree(tp, ip, &cur, 0,
2481                                 &tmp_logflags, whichfork);
2482                 *logflagsp |= tmp_logflags;
2483                 if (error)
2484                         goto done;
2485         }
2486
2487         /* clear out the allocated field, done with it now in any case. */
2488         if (cur) {
2489                 cur->bc_private.b.allocated = 0;
2490                 *curp = cur;
2491         }
2492
2493         xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
2494 done:
2495         *logflagsp |= rval;
2496         return error;
2497 #undef  LEFT
2498 #undef  RIGHT
2499 #undef  PREV
2500 }
2501
2502 /*
2503  * Convert a hole to a delayed allocation.
2504  */
2505 STATIC void
2506 xfs_bmap_add_extent_hole_delay(
2507         xfs_inode_t             *ip,    /* incore inode pointer */
2508         int                     whichfork,
2509         struct xfs_iext_cursor  *icur,
2510         xfs_bmbt_irec_t         *new)   /* new data to add to file extents */
2511 {
2512         struct xfs_ifork        *ifp;   /* inode fork pointer */
2513         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
2514         xfs_filblks_t           newlen=0;       /* new indirect size */
2515         xfs_filblks_t           oldlen=0;       /* old indirect size */
2516         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
2517         int                     state = xfs_bmap_fork_to_state(whichfork);
2518         xfs_filblks_t           temp;    /* temp for indirect calculations */
2519
2520         ifp = XFS_IFORK_PTR(ip, whichfork);
2521         ASSERT(isnullstartblock(new->br_startblock));
2522
2523         /*
2524          * Check and set flags if this segment has a left neighbor
2525          */
2526         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2527                 state |= BMAP_LEFT_VALID;
2528                 if (isnullstartblock(left.br_startblock))
2529                         state |= BMAP_LEFT_DELAY;
2530         }
2531
2532         /*
2533          * Check and set flags if the current (right) segment exists.
2534          * If it doesn't exist, we're converting the hole at end-of-file.
2535          */
2536         if (xfs_iext_get_extent(ifp, icur, &right)) {
2537                 state |= BMAP_RIGHT_VALID;
2538                 if (isnullstartblock(right.br_startblock))
2539                         state |= BMAP_RIGHT_DELAY;
2540         }
2541
2542         /*
2543          * Set contiguity flags on the left and right neighbors.
2544          * Don't let extents get too large, even if the pieces are contiguous.
2545          */
2546         if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2547             left.br_startoff + left.br_blockcount == new->br_startoff &&
2548             left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2549                 state |= BMAP_LEFT_CONTIG;
2550
2551         if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2552             new->br_startoff + new->br_blockcount == right.br_startoff &&
2553             new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2554             (!(state & BMAP_LEFT_CONTIG) ||
2555              (left.br_blockcount + new->br_blockcount +
2556               right.br_blockcount <= MAXEXTLEN)))
2557                 state |= BMAP_RIGHT_CONTIG;
2558
2559         /*
2560          * Switch out based on the contiguity flags.
2561          */
2562         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2563         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2564                 /*
2565                  * New allocation is contiguous with delayed allocations
2566                  * on the left and on the right.
2567                  * Merge all three into a single extent record.
2568                  */
2569                 temp = left.br_blockcount + new->br_blockcount +
2570                         right.br_blockcount;
2571
2572                 oldlen = startblockval(left.br_startblock) +
2573                         startblockval(new->br_startblock) +
2574                         startblockval(right.br_startblock);
2575                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2576                                          oldlen);
2577                 left.br_startblock = nullstartblock(newlen);
2578                 left.br_blockcount = temp;
2579
2580                 xfs_iext_remove(ip, icur, state);
2581                 xfs_iext_prev(ifp, icur);
2582                 xfs_iext_update_extent(ip, state, icur, &left);
2583                 break;
2584
2585         case BMAP_LEFT_CONTIG:
2586                 /*
2587                  * New allocation is contiguous with a delayed allocation
2588                  * on the left.
2589                  * Merge the new allocation with the left neighbor.
2590                  */
2591                 temp = left.br_blockcount + new->br_blockcount;
2592
2593                 oldlen = startblockval(left.br_startblock) +
2594                         startblockval(new->br_startblock);
2595                 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2596                                          oldlen);
2597                 left.br_blockcount = temp;
2598                 left.br_startblock = nullstartblock(newlen);
2599
2600                 xfs_iext_prev(ifp, icur);
2601                 xfs_iext_update_extent(ip, state, icur, &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, icur, &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, icur, 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         struct xfs_iext_cursor  *icur,
2650         struct xfs_btree_cur    **curp,
2651         struct xfs_bmbt_irec    *new,
2652         int                     *logflagsp,
2653         int                     flags)
2654 {
2655         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
2656         struct xfs_mount        *mp = ip->i_mount;
2657         struct xfs_btree_cur    *cur = *curp;
2658         int                     error;  /* error return value */
2659         int                     i;      /* temp state */
2660         xfs_bmbt_irec_t         left;   /* left neighbor extent entry */
2661         xfs_bmbt_irec_t         right;  /* right neighbor extent entry */
2662         int                     rval=0; /* return value (logging flags) */
2663         int                     state = xfs_bmap_fork_to_state(whichfork);
2664         struct xfs_bmbt_irec    old;
2665
2666         ASSERT(!isnullstartblock(new->br_startblock));
2667         ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
2668
2669         XFS_STATS_INC(mp, xs_add_exlist);
2670
2671         /*
2672          * Check and set flags if this segment has a left neighbor.
2673          */
2674         if (xfs_iext_peek_prev_extent(ifp, icur, &left)) {
2675                 state |= BMAP_LEFT_VALID;
2676                 if (isnullstartblock(left.br_startblock))
2677                         state |= BMAP_LEFT_DELAY;
2678         }
2679
2680         /*
2681          * Check and set flags if this segment has a current value.
2682          * Not true if we're inserting into the "hole" at eof.
2683          */
2684         if (xfs_iext_get_extent(ifp, icur, &right)) {
2685                 state |= BMAP_RIGHT_VALID;
2686                 if (isnullstartblock(right.br_startblock))
2687                         state |= BMAP_RIGHT_DELAY;
2688         }
2689
2690         /*
2691          * We're inserting a real allocation between "left" and "right".
2692          * Set the contiguity flags.  Don't let extents get too large.
2693          */
2694         if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2695             left.br_startoff + left.br_blockcount == new->br_startoff &&
2696             left.br_startblock + left.br_blockcount == new->br_startblock &&
2697             left.br_state == new->br_state &&
2698             left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2699                 state |= BMAP_LEFT_CONTIG;
2700
2701         if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2702             new->br_startoff + new->br_blockcount == right.br_startoff &&
2703             new->br_startblock + new->br_blockcount == right.br_startblock &&
2704             new->br_state == right.br_state &&
2705             new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2706             (!(state & BMAP_LEFT_CONTIG) ||
2707              left.br_blockcount + new->br_blockcount +
2708              right.br_blockcount <= MAXEXTLEN))
2709                 state |= BMAP_RIGHT_CONTIG;
2710
2711         error = 0;
2712         /*
2713          * Select which case we're in here, and implement it.
2714          */
2715         switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2716         case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2717                 /*
2718                  * New allocation is contiguous with real allocations on the
2719                  * left and on the right.
2720                  * Merge all three into a single extent record.
2721                  */
2722                 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2723
2724                 xfs_iext_remove(ip, icur, state);
2725                 xfs_iext_prev(ifp, icur);
2726                 xfs_iext_update_extent(ip, state, icur, &left);
2727
2728                 XFS_IFORK_NEXT_SET(ip, whichfork,
2729                         XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2730                 if (cur == NULL) {
2731                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2732                 } else {
2733                         rval = XFS_ILOG_CORE;
2734                         error = xfs_bmbt_lookup_eq(cur, &right, &i);
2735                         if (error)
2736                                 goto done;
2737                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2738                         error = xfs_btree_delete(cur, &i);
2739                         if (error)
2740                                 goto done;
2741                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2742                         error = xfs_btree_decrement(cur, 0, &i);
2743                         if (error)
2744                                 goto done;
2745                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2746                         error = xfs_bmbt_update(cur, &left);
2747                         if (error)
2748                                 goto done;
2749                 }
2750                 break;
2751
2752         case BMAP_LEFT_CONTIG:
2753                 /*
2754                  * New allocation is contiguous with a real allocation
2755                  * on the left.
2756                  * Merge the new allocation with the left neighbor.
2757                  */
2758                 old = left;
2759                 left.br_blockcount += new->br_blockcount;
2760
2761                 xfs_iext_prev(ifp, icur);
2762                 xfs_iext_update_extent(ip, state, icur, &left);
2763
2764                 if (cur == NULL) {
2765                         rval = xfs_ilog_fext(whichfork);
2766                 } else {
2767                         rval = 0;
2768                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
2769                         if (error)
2770                                 goto done;
2771                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2772                         error = xfs_bmbt_update(cur, &left);
2773                         if (error)
2774                                 goto done;
2775                 }
2776                 break;
2777
2778         case BMAP_RIGHT_CONTIG:
2779                 /*
2780                  * New allocation is contiguous with a real allocation
2781                  * on the right.
2782                  * Merge the new allocation with the right neighbor.
2783                  */
2784                 old = right;
2785
2786                 right.br_startoff = new->br_startoff;
2787                 right.br_startblock = new->br_startblock;
2788                 right.br_blockcount += new->br_blockcount;
2789                 xfs_iext_update_extent(ip, state, icur, &right);
2790
2791                 if (cur == NULL) {
2792                         rval = xfs_ilog_fext(whichfork);
2793                 } else {
2794                         rval = 0;
2795                         error = xfs_bmbt_lookup_eq(cur, &old, &i);
2796                         if (error)
2797                                 goto done;
2798                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2799                         error = xfs_bmbt_update(cur, &right);
2800                         if (error)
2801                                 goto done;
2802                 }
2803                 break;
2804
2805         case 0:
2806                 /*
2807                  * New allocation is not contiguous with another
2808                  * real allocation.
2809                  * Insert a new entry.
2810                  */
2811                 xfs_iext_insert(ip, icur, new, state);
2812                 XFS_IFORK_NEXT_SET(ip, whichfork,
2813                         XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2814                 if (cur == NULL) {
2815                         rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2816                 } else {
2817                         rval = XFS_ILOG_CORE;
2818                         error = xfs_bmbt_lookup_eq(cur, new, &i);
2819                         if (error)
2820                                 goto done;
2821                         XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
2822                         error = xfs_btree_insert(cur, &i);
2823                         if (error)
2824                                 goto done;
2825                         XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2826                 }
2827                 break;
2828         }
2829
2830         /* add reverse mapping unless caller opted out */
2831         if (!(flags & XFS_BMAPI_NORMAP)) {
2832                 error = xfs_rmap_map_extent(tp, ip, whichfork, new);
2833                 if (error)
2834                         goto done;
2835         }
2836
2837         /* convert to a btree if necessary */
2838         if (xfs_bmap_needs_btree(ip, whichfork)) {
2839                 int     tmp_logflags;   /* partial log flag return val */
2840
2841                 ASSERT(cur == NULL);
2842                 error = xfs_bmap_extents_to_btree(tp, ip, curp, 0,
2843                                 &tmp_logflags, whichfork);
2844                 *logflagsp |= tmp_logflags;
2845                 cur = *curp;
2846                 if (error)
2847                         goto done;
2848         }
2849
2850         /* clear out the allocated field, done with it now in any case. */
2851         if (cur)
2852                 cur->bc_private.b.allocated = 0;
2853
2854         xfs_bmap_check_leaf_extents(cur, ip, whichfork);
2855 done:
2856         *logflagsp |= rval;
2857         return error;
2858 }
2859
2860 /*
2861  * Functions used in the extent read, allocate and remove paths
2862  */
2863
2864 /*
2865  * Adjust the size of the new extent based on di_extsize and rt extsize.
2866  */
2867 int
2868 xfs_bmap_extsize_align(
2869         xfs_mount_t     *mp,
2870         xfs_bmbt_irec_t *gotp,          /* next extent pointer */
2871         xfs_bmbt_irec_t *prevp,         /* previous extent pointer */
2872         xfs_extlen_t    extsz,          /* align to this extent size */
2873         int             rt,             /* is this a realtime inode? */
2874         int             eof,            /* is extent at end-of-file? */
2875         int             delay,          /* creating delalloc extent? */
2876         int             convert,        /* overwriting unwritten extent? */
2877         xfs_fileoff_t   *offp,          /* in/out: aligned offset */
2878         xfs_extlen_t    *lenp)          /* in/out: aligned length */
2879 {
2880         xfs_fileoff_t   orig_off;       /* original offset */
2881         xfs_extlen_t    orig_alen;      /* original length */
2882         xfs_fileoff_t   orig_end;       /* original off+len */
2883         xfs_fileoff_t   nexto;          /* next file offset */
2884         xfs_fileoff_t   prevo;          /* previous file offset */
2885         xfs_fileoff_t   align_off;      /* temp for offset */
2886         xfs_extlen_t    align_alen;     /* temp for length */
2887         xfs_extlen_t    temp;           /* temp for calculations */
2888
2889         if (convert)
2890                 return 0;
2891
2892         orig_off = align_off = *offp;
2893         orig_alen = align_alen = *lenp;
2894         orig_end = orig_off + orig_alen;
2895
2896         /*
2897          * If this request overlaps an existing extent, then don't
2898          * attempt to perform any additional alignment.
2899          */
2900         if (!delay && !eof &&
2901             (orig_off >= gotp->br_startoff) &&
2902             (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2903                 return 0;
2904         }
2905
2906         /*
2907          * If the file offset is unaligned vs. the extent size
2908          * we need to align it.  This will be possible unless
2909          * the file was previously written with a kernel that didn't
2910          * perform this alignment, or if a truncate shot us in the
2911          * foot.
2912          */
2913         div_u64_rem(orig_off, extsz, &temp);
2914         if (temp) {
2915                 align_alen += temp;
2916                 align_off -= temp;
2917         }
2918
2919         /* Same adjustment for the end of the requested area. */
2920         temp = (align_alen % extsz);
2921         if (temp)
2922                 align_alen += extsz - temp;
2923
2924         /*
2925          * For large extent hint sizes, the aligned extent might be larger than
2926          * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2927          * the length back under MAXEXTLEN. The outer allocation loops handle
2928          * short allocation just fine, so it is safe to do this. We only want to
2929          * do it when we are forced to, though, because it means more allocation
2930          * operations are required.
2931          */
2932         while (align_alen > MAXEXTLEN)
2933                 align_alen -= extsz;
2934         ASSERT(align_alen <= MAXEXTLEN);
2935
2936         /*
2937          * If the previous block overlaps with this proposed allocation
2938          * then move the start forward without adjusting the length.
2939          */
2940         if (prevp->br_startoff != NULLFILEOFF) {
2941                 if (prevp->br_startblock == HOLESTARTBLOCK)
2942                         prevo = prevp->br_startoff;
2943                 else
2944                         prevo = prevp->br_startoff + prevp->br_blockcount;
2945         } else
2946                 prevo = 0;
2947         if (align_off != orig_off && align_off < prevo)
2948                 align_off = prevo;
2949         /*
2950          * If the next block overlaps with this proposed allocation
2951          * then move the start back without adjusting the length,
2952          * but not before offset 0.
2953          * This may of course make the start overlap previous block,
2954          * and if we hit the offset 0 limit then the next block
2955          * can still overlap too.
2956          */
2957         if (!eof && gotp->br_startoff != NULLFILEOFF) {
2958                 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2959                     (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2960                         nexto = gotp->br_startoff + gotp->br_blockcount;
2961                 else
2962                         nexto = gotp->br_startoff;
2963         } else
2964                 nexto = NULLFILEOFF;
2965         if (!eof &&
2966             align_off + align_alen != orig_end &&
2967             align_off + align_alen > nexto)
2968                 align_off = nexto > align_alen ? nexto - align_alen : 0;
2969         /*
2970          * If we're now overlapping the next or previous extent that
2971          * means we can't fit an extsz piece in this hole.  Just move
2972          * the start forward to the first valid spot and set
2973          * the length so we hit the end.
2974          */
2975         if (align_off != orig_off && align_off < prevo)
2976                 align_off = prevo;
2977         if (align_off + align_alen != orig_end &&
2978             align_off + align_alen > nexto &&
2979             nexto != NULLFILEOFF) {
2980                 ASSERT(nexto > prevo);
2981                 align_alen = nexto - align_off;
2982         }
2983
2984         /*
2985          * If realtime, and the result isn't a multiple of the realtime
2986          * extent size we need to remove blocks until it is.
2987          */