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