OSDN Git Service

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