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