]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/libxfs/xfs_inode_fork.c
Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[karo-tx-linux.git] / fs / xfs / libxfs / xfs_inode_fork.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 <linux/log2.h>
19
20 #include "xfs.h"
21 #include "xfs_fs.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_inode_item.h"
29 #include "xfs_bmap_btree.h"
30 #include "xfs_bmap.h"
31 #include "xfs_error.h"
32 #include "xfs_trace.h"
33 #include "xfs_attr_sf.h"
34 #include "xfs_da_format.h"
35
36 kmem_zone_t *xfs_ifork_zone;
37
38 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
39 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
40 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
41
42 #ifdef DEBUG
43 /*
44  * Make sure that the extents in the given memory buffer
45  * are valid.
46  */
47 void
48 xfs_validate_extents(
49         xfs_ifork_t             *ifp,
50         int                     nrecs,
51         xfs_exntfmt_t           fmt)
52 {
53         xfs_bmbt_irec_t         irec;
54         xfs_bmbt_rec_host_t     rec;
55         int                     i;
56
57         for (i = 0; i < nrecs; i++) {
58                 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
59                 rec.l0 = get_unaligned(&ep->l0);
60                 rec.l1 = get_unaligned(&ep->l1);
61                 xfs_bmbt_get_all(&rec, &irec);
62                 if (fmt == XFS_EXTFMT_NOSTATE)
63                         ASSERT(irec.br_state == XFS_EXT_NORM);
64         }
65 }
66 #else /* DEBUG */
67 #define xfs_validate_extents(ifp, nrecs, fmt)
68 #endif /* DEBUG */
69
70
71 /*
72  * Move inode type and inode format specific information from the
73  * on-disk inode to the in-core inode.  For fifos, devs, and sockets
74  * this means set if_rdev to the proper value.  For files, directories,
75  * and symlinks this means to bring in the in-line data or extent
76  * pointers.  For a file in B-tree format, only the root is immediately
77  * brought in-core.  The rest will be in-lined in if_extents when it
78  * is first referenced (see xfs_iread_extents()).
79  */
80 int
81 xfs_iformat_fork(
82         xfs_inode_t             *ip,
83         xfs_dinode_t            *dip)
84 {
85         xfs_attr_shortform_t    *atp;
86         int                     size;
87         int                     error = 0;
88         xfs_fsize_t             di_size;
89
90         if (unlikely(be32_to_cpu(dip->di_nextents) +
91                      be16_to_cpu(dip->di_anextents) >
92                      be64_to_cpu(dip->di_nblocks))) {
93                 xfs_warn(ip->i_mount,
94                         "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
95                         (unsigned long long)ip->i_ino,
96                         (int)(be32_to_cpu(dip->di_nextents) +
97                               be16_to_cpu(dip->di_anextents)),
98                         (unsigned long long)
99                                 be64_to_cpu(dip->di_nblocks));
100                 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
101                                      ip->i_mount, dip);
102                 return -EFSCORRUPTED;
103         }
104
105         if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
106                 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
107                         (unsigned long long)ip->i_ino,
108                         dip->di_forkoff);
109                 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
110                                      ip->i_mount, dip);
111                 return -EFSCORRUPTED;
112         }
113
114         if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
115                      !ip->i_mount->m_rtdev_targp)) {
116                 xfs_warn(ip->i_mount,
117                         "corrupt dinode %Lu, has realtime flag set.",
118                         ip->i_ino);
119                 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
120                                      XFS_ERRLEVEL_LOW, ip->i_mount, dip);
121                 return -EFSCORRUPTED;
122         }
123
124         if (unlikely(xfs_is_reflink_inode(ip) &&
125             (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) {
126                 xfs_warn(ip->i_mount,
127                         "corrupt dinode %llu, wrong file type for reflink.",
128                         ip->i_ino);
129                 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
130                                      XFS_ERRLEVEL_LOW, ip->i_mount, dip);
131                 return -EFSCORRUPTED;
132         }
133
134         if (unlikely(xfs_is_reflink_inode(ip) &&
135             (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
136                 xfs_warn(ip->i_mount,
137                         "corrupt dinode %llu, has reflink+realtime flag set.",
138                         ip->i_ino);
139                 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
140                                      XFS_ERRLEVEL_LOW, ip->i_mount, dip);
141                 return -EFSCORRUPTED;
142         }
143
144         switch (VFS_I(ip)->i_mode & S_IFMT) {
145         case S_IFIFO:
146         case S_IFCHR:
147         case S_IFBLK:
148         case S_IFSOCK:
149                 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
150                         XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
151                                               ip->i_mount, dip);
152                         return -EFSCORRUPTED;
153                 }
154                 ip->i_d.di_size = 0;
155                 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
156                 break;
157
158         case S_IFREG:
159         case S_IFLNK:
160         case S_IFDIR:
161                 switch (dip->di_format) {
162                 case XFS_DINODE_FMT_LOCAL:
163                         /*
164                          * no local regular files yet
165                          */
166                         if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
167                                 xfs_warn(ip->i_mount,
168                         "corrupt inode %Lu (local format for regular file).",
169                                         (unsigned long long) ip->i_ino);
170                                 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
171                                                      XFS_ERRLEVEL_LOW,
172                                                      ip->i_mount, dip);
173                                 return -EFSCORRUPTED;
174                         }
175
176                         di_size = be64_to_cpu(dip->di_size);
177                         if (unlikely(di_size < 0 ||
178                                      di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
179                                 xfs_warn(ip->i_mount,
180                         "corrupt inode %Lu (bad size %Ld for local inode).",
181                                         (unsigned long long) ip->i_ino,
182                                         (long long) di_size);
183                                 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
184                                                      XFS_ERRLEVEL_LOW,
185                                                      ip->i_mount, dip);
186                                 return -EFSCORRUPTED;
187                         }
188
189                         size = (int)di_size;
190                         error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
191                         break;
192                 case XFS_DINODE_FMT_EXTENTS:
193                         error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
194                         break;
195                 case XFS_DINODE_FMT_BTREE:
196                         error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
197                         break;
198                 default:
199                         XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
200                                          ip->i_mount);
201                         return -EFSCORRUPTED;
202                 }
203                 break;
204
205         default:
206                 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
207                 return -EFSCORRUPTED;
208         }
209         if (error)
210                 return error;
211
212         if (xfs_is_reflink_inode(ip)) {
213                 ASSERT(ip->i_cowfp == NULL);
214                 xfs_ifork_init_cow(ip);
215         }
216
217         if (!XFS_DFORK_Q(dip))
218                 return 0;
219
220         ASSERT(ip->i_afp == NULL);
221         ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
222
223         switch (dip->di_aformat) {
224         case XFS_DINODE_FMT_LOCAL:
225                 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
226                 size = be16_to_cpu(atp->hdr.totsize);
227
228                 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
229                         xfs_warn(ip->i_mount,
230                                 "corrupt inode %Lu (bad attr fork size %Ld).",
231                                 (unsigned long long) ip->i_ino,
232                                 (long long) size);
233                         XFS_CORRUPTION_ERROR("xfs_iformat(8)",
234                                              XFS_ERRLEVEL_LOW,
235                                              ip->i_mount, dip);
236                         error = -EFSCORRUPTED;
237                         break;
238                 }
239
240                 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
241                 break;
242         case XFS_DINODE_FMT_EXTENTS:
243                 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
244                 break;
245         case XFS_DINODE_FMT_BTREE:
246                 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
247                 break;
248         default:
249                 error = -EFSCORRUPTED;
250                 break;
251         }
252         if (error) {
253                 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
254                 ip->i_afp = NULL;
255                 if (ip->i_cowfp)
256                         kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
257                 ip->i_cowfp = NULL;
258                 xfs_idestroy_fork(ip, XFS_DATA_FORK);
259         }
260         return error;
261 }
262
263 void
264 xfs_init_local_fork(
265         struct xfs_inode        *ip,
266         int                     whichfork,
267         const void              *data,
268         int                     size)
269 {
270         struct xfs_ifork        *ifp = XFS_IFORK_PTR(ip, whichfork);
271         int                     mem_size = size, real_size = 0;
272         bool                    zero_terminate;
273
274         /*
275          * If we are using the local fork to store a symlink body we need to
276          * zero-terminate it so that we can pass it back to the VFS directly.
277          * Overallocate the in-memory fork by one for that and add a zero
278          * to terminate it below.
279          */
280         zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
281         if (zero_terminate)
282                 mem_size++;
283
284         if (size == 0)
285                 ifp->if_u1.if_data = NULL;
286         else if (mem_size <= sizeof(ifp->if_u2.if_inline_data))
287                 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
288         else {
289                 real_size = roundup(mem_size, 4);
290                 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
291         }
292
293         if (size) {
294                 memcpy(ifp->if_u1.if_data, data, size);
295                 if (zero_terminate)
296                         ifp->if_u1.if_data[size] = '\0';
297         }
298
299         ifp->if_bytes = size;
300         ifp->if_real_bytes = real_size;
301         ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
302         ifp->if_flags |= XFS_IFINLINE;
303 }
304
305 /*
306  * The file is in-lined in the on-disk inode.
307  * If it fits into if_inline_data, then copy
308  * it there, otherwise allocate a buffer for it
309  * and copy the data there.  Either way, set
310  * if_data to point at the data.
311  * If we allocate a buffer for the data, make
312  * sure that its size is a multiple of 4 and
313  * record the real size in i_real_bytes.
314  */
315 STATIC int
316 xfs_iformat_local(
317         xfs_inode_t     *ip,
318         xfs_dinode_t    *dip,
319         int             whichfork,
320         int             size)
321 {
322
323         /*
324          * If the size is unreasonable, then something
325          * is wrong and we just bail out rather than crash in
326          * kmem_alloc() or memcpy() below.
327          */
328         if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
329                 xfs_warn(ip->i_mount,
330         "corrupt inode %Lu (bad size %d for local fork, size = %d).",
331                         (unsigned long long) ip->i_ino, size,
332                         XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
333                 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
334                                      ip->i_mount, dip);
335                 return -EFSCORRUPTED;
336         }
337
338         xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
339         return 0;
340 }
341
342 /*
343  * The file consists of a set of extents all
344  * of which fit into the on-disk inode.
345  * If there are few enough extents to fit into
346  * the if_inline_ext, then copy them there.
347  * Otherwise allocate a buffer for them and copy
348  * them into it.  Either way, set if_extents
349  * to point at the extents.
350  */
351 STATIC int
352 xfs_iformat_extents(
353         xfs_inode_t     *ip,
354         xfs_dinode_t    *dip,
355         int             whichfork)
356 {
357         xfs_bmbt_rec_t  *dp;
358         xfs_ifork_t     *ifp;
359         int             nex;
360         int             size;
361         int             i;
362
363         ifp = XFS_IFORK_PTR(ip, whichfork);
364         nex = XFS_DFORK_NEXTENTS(dip, whichfork);
365         size = nex * (uint)sizeof(xfs_bmbt_rec_t);
366
367         /*
368          * If the number of extents is unreasonable, then something
369          * is wrong and we just bail out rather than crash in
370          * kmem_alloc() or memcpy() below.
371          */
372         if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
373                 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
374                         (unsigned long long) ip->i_ino, nex);
375                 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
376                                      ip->i_mount, dip);
377                 return -EFSCORRUPTED;
378         }
379
380         ifp->if_real_bytes = 0;
381         if (nex == 0)
382                 ifp->if_u1.if_extents = NULL;
383         else if (nex <= XFS_INLINE_EXTS)
384                 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
385         else
386                 xfs_iext_add(ifp, 0, nex);
387
388         ifp->if_bytes = size;
389         if (size) {
390                 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
391                 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
392                 for (i = 0; i < nex; i++, dp++) {
393                         xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
394                         ep->l0 = get_unaligned_be64(&dp->l0);
395                         ep->l1 = get_unaligned_be64(&dp->l1);
396                 }
397                 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
398                 if (whichfork != XFS_DATA_FORK ||
399                         XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
400                                 if (unlikely(xfs_check_nostate_extents(
401                                     ifp, 0, nex))) {
402                                         XFS_ERROR_REPORT("xfs_iformat_extents(2)",
403                                                          XFS_ERRLEVEL_LOW,
404                                                          ip->i_mount);
405                                         return -EFSCORRUPTED;
406                                 }
407         }
408         ifp->if_flags |= XFS_IFEXTENTS;
409         return 0;
410 }
411
412 /*
413  * The file has too many extents to fit into
414  * the inode, so they are in B-tree format.
415  * Allocate a buffer for the root of the B-tree
416  * and copy the root into it.  The i_extents
417  * field will remain NULL until all of the
418  * extents are read in (when they are needed).
419  */
420 STATIC int
421 xfs_iformat_btree(
422         xfs_inode_t             *ip,
423         xfs_dinode_t            *dip,
424         int                     whichfork)
425 {
426         struct xfs_mount        *mp = ip->i_mount;
427         xfs_bmdr_block_t        *dfp;
428         xfs_ifork_t             *ifp;
429         /* REFERENCED */
430         int                     nrecs;
431         int                     size;
432
433         ifp = XFS_IFORK_PTR(ip, whichfork);
434         dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
435         size = XFS_BMAP_BROOT_SPACE(mp, dfp);
436         nrecs = be16_to_cpu(dfp->bb_numrecs);
437
438         /*
439          * blow out if -- fork has less extents than can fit in
440          * fork (fork shouldn't be a btree format), root btree
441          * block has more records than can fit into the fork,
442          * or the number of extents is greater than the number of
443          * blocks.
444          */
445         if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
446                                         XFS_IFORK_MAXEXT(ip, whichfork) ||
447                      XFS_BMDR_SPACE_CALC(nrecs) >
448                                         XFS_DFORK_SIZE(dip, mp, whichfork) ||
449                      XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
450                 xfs_warn(mp, "corrupt inode %Lu (btree).",
451                                         (unsigned long long) ip->i_ino);
452                 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
453                                          mp, dip);
454                 return -EFSCORRUPTED;
455         }
456
457         ifp->if_broot_bytes = size;
458         ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
459         ASSERT(ifp->if_broot != NULL);
460         /*
461          * Copy and convert from the on-disk structure
462          * to the in-memory structure.
463          */
464         xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
465                          ifp->if_broot, size);
466         ifp->if_flags &= ~XFS_IFEXTENTS;
467         ifp->if_flags |= XFS_IFBROOT;
468
469         return 0;
470 }
471
472 /*
473  * Read in extents from a btree-format inode.
474  * Allocate and fill in if_extents.  Real work is done in xfs_bmap.c.
475  */
476 int
477 xfs_iread_extents(
478         xfs_trans_t     *tp,
479         xfs_inode_t     *ip,
480         int             whichfork)
481 {
482         int             error;
483         xfs_ifork_t     *ifp;
484         xfs_extnum_t    nextents;
485
486         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
487
488         if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
489                 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
490                                  ip->i_mount);
491                 return -EFSCORRUPTED;
492         }
493         nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
494         ifp = XFS_IFORK_PTR(ip, whichfork);
495
496         /*
497          * We know that the size is valid (it's checked in iformat_btree)
498          */
499         ifp->if_bytes = ifp->if_real_bytes = 0;
500         ifp->if_flags |= XFS_IFEXTENTS;
501         xfs_iext_add(ifp, 0, nextents);
502         error = xfs_bmap_read_extents(tp, ip, whichfork);
503         if (error) {
504                 xfs_iext_destroy(ifp);
505                 ifp->if_flags &= ~XFS_IFEXTENTS;
506                 return error;
507         }
508         xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
509         return 0;
510 }
511 /*
512  * Reallocate the space for if_broot based on the number of records
513  * being added or deleted as indicated in rec_diff.  Move the records
514  * and pointers in if_broot to fit the new size.  When shrinking this
515  * will eliminate holes between the records and pointers created by
516  * the caller.  When growing this will create holes to be filled in
517  * by the caller.
518  *
519  * The caller must not request to add more records than would fit in
520  * the on-disk inode root.  If the if_broot is currently NULL, then
521  * if we are adding records, one will be allocated.  The caller must also
522  * not request that the number of records go below zero, although
523  * it can go to zero.
524  *
525  * ip -- the inode whose if_broot area is changing
526  * ext_diff -- the change in the number of records, positive or negative,
527  *       requested for the if_broot array.
528  */
529 void
530 xfs_iroot_realloc(
531         xfs_inode_t             *ip,
532         int                     rec_diff,
533         int                     whichfork)
534 {
535         struct xfs_mount        *mp = ip->i_mount;
536         int                     cur_max;
537         xfs_ifork_t             *ifp;
538         struct xfs_btree_block  *new_broot;
539         int                     new_max;
540         size_t                  new_size;
541         char                    *np;
542         char                    *op;
543
544         /*
545          * Handle the degenerate case quietly.
546          */
547         if (rec_diff == 0) {
548                 return;
549         }
550
551         ifp = XFS_IFORK_PTR(ip, whichfork);
552         if (rec_diff > 0) {
553                 /*
554                  * If there wasn't any memory allocated before, just
555                  * allocate it now and get out.
556                  */
557                 if (ifp->if_broot_bytes == 0) {
558                         new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
559                         ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
560                         ifp->if_broot_bytes = (int)new_size;
561                         return;
562                 }
563
564                 /*
565                  * If there is already an existing if_broot, then we need
566                  * to realloc() it and shift the pointers to their new
567                  * location.  The records don't change location because
568                  * they are kept butted up against the btree block header.
569                  */
570                 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
571                 new_max = cur_max + rec_diff;
572                 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
573                 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
574                                 KM_SLEEP | KM_NOFS);
575                 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
576                                                      ifp->if_broot_bytes);
577                 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
578                                                      (int)new_size);
579                 ifp->if_broot_bytes = (int)new_size;
580                 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
581                         XFS_IFORK_SIZE(ip, whichfork));
582                 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
583                 return;
584         }
585
586         /*
587          * rec_diff is less than 0.  In this case, we are shrinking the
588          * if_broot buffer.  It must already exist.  If we go to zero
589          * records, just get rid of the root and clear the status bit.
590          */
591         ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
592         cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
593         new_max = cur_max + rec_diff;
594         ASSERT(new_max >= 0);
595         if (new_max > 0)
596                 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
597         else
598                 new_size = 0;
599         if (new_size > 0) {
600                 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
601                 /*
602                  * First copy over the btree block header.
603                  */
604                 memcpy(new_broot, ifp->if_broot,
605                         XFS_BMBT_BLOCK_LEN(ip->i_mount));
606         } else {
607                 new_broot = NULL;
608                 ifp->if_flags &= ~XFS_IFBROOT;
609         }
610
611         /*
612          * Only copy the records and pointers if there are any.
613          */
614         if (new_max > 0) {
615                 /*
616                  * First copy the records.
617                  */
618                 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
619                 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
620                 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
621
622                 /*
623                  * Then copy the pointers.
624                  */
625                 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
626                                                      ifp->if_broot_bytes);
627                 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
628                                                      (int)new_size);
629                 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
630         }
631         kmem_free(ifp->if_broot);
632         ifp->if_broot = new_broot;
633         ifp->if_broot_bytes = (int)new_size;
634         if (ifp->if_broot)
635                 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
636                         XFS_IFORK_SIZE(ip, whichfork));
637         return;
638 }
639
640
641 /*
642  * This is called when the amount of space needed for if_data
643  * is increased or decreased.  The change in size is indicated by
644  * the number of bytes that need to be added or deleted in the
645  * byte_diff parameter.
646  *
647  * If the amount of space needed has decreased below the size of the
648  * inline buffer, then switch to using the inline buffer.  Otherwise,
649  * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
650  * to what is needed.
651  *
652  * ip -- the inode whose if_data area is changing
653  * byte_diff -- the change in the number of bytes, positive or negative,
654  *       requested for the if_data array.
655  */
656 void
657 xfs_idata_realloc(
658         xfs_inode_t     *ip,
659         int             byte_diff,
660         int             whichfork)
661 {
662         xfs_ifork_t     *ifp;
663         int             new_size;
664         int             real_size;
665
666         if (byte_diff == 0) {
667                 return;
668         }
669
670         ifp = XFS_IFORK_PTR(ip, whichfork);
671         new_size = (int)ifp->if_bytes + byte_diff;
672         ASSERT(new_size >= 0);
673
674         if (new_size == 0) {
675                 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
676                         kmem_free(ifp->if_u1.if_data);
677                 }
678                 ifp->if_u1.if_data = NULL;
679                 real_size = 0;
680         } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
681                 /*
682                  * If the valid extents/data can fit in if_inline_ext/data,
683                  * copy them from the malloc'd vector and free it.
684                  */
685                 if (ifp->if_u1.if_data == NULL) {
686                         ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
687                 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
688                         ASSERT(ifp->if_real_bytes != 0);
689                         memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
690                               new_size);
691                         kmem_free(ifp->if_u1.if_data);
692                         ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
693                 }
694                 real_size = 0;
695         } else {
696                 /*
697                  * Stuck with malloc/realloc.
698                  * For inline data, the underlying buffer must be
699                  * a multiple of 4 bytes in size so that it can be
700                  * logged and stay on word boundaries.  We enforce
701                  * that here.
702                  */
703                 real_size = roundup(new_size, 4);
704                 if (ifp->if_u1.if_data == NULL) {
705                         ASSERT(ifp->if_real_bytes == 0);
706                         ifp->if_u1.if_data = kmem_alloc(real_size,
707                                                         KM_SLEEP | KM_NOFS);
708                 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
709                         /*
710                          * Only do the realloc if the underlying size
711                          * is really changing.
712                          */
713                         if (ifp->if_real_bytes != real_size) {
714                                 ifp->if_u1.if_data =
715                                         kmem_realloc(ifp->if_u1.if_data,
716                                                         real_size,
717                                                         KM_SLEEP | KM_NOFS);
718                         }
719                 } else {
720                         ASSERT(ifp->if_real_bytes == 0);
721                         ifp->if_u1.if_data = kmem_alloc(real_size,
722                                                         KM_SLEEP | KM_NOFS);
723                         memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
724                                 ifp->if_bytes);
725                 }
726         }
727         ifp->if_real_bytes = real_size;
728         ifp->if_bytes = new_size;
729         ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
730 }
731
732 void
733 xfs_idestroy_fork(
734         xfs_inode_t     *ip,
735         int             whichfork)
736 {
737         xfs_ifork_t     *ifp;
738
739         ifp = XFS_IFORK_PTR(ip, whichfork);
740         if (ifp->if_broot != NULL) {
741                 kmem_free(ifp->if_broot);
742                 ifp->if_broot = NULL;
743         }
744
745         /*
746          * If the format is local, then we can't have an extents
747          * array so just look for an inline data array.  If we're
748          * not local then we may or may not have an extents list,
749          * so check and free it up if we do.
750          */
751         if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
752                 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
753                     (ifp->if_u1.if_data != NULL)) {
754                         ASSERT(ifp->if_real_bytes != 0);
755                         kmem_free(ifp->if_u1.if_data);
756                         ifp->if_u1.if_data = NULL;
757                         ifp->if_real_bytes = 0;
758                 }
759         } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
760                    ((ifp->if_flags & XFS_IFEXTIREC) ||
761                     ((ifp->if_u1.if_extents != NULL) &&
762                      (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
763                 ASSERT(ifp->if_real_bytes != 0);
764                 xfs_iext_destroy(ifp);
765         }
766         ASSERT(ifp->if_u1.if_extents == NULL ||
767                ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
768         ASSERT(ifp->if_real_bytes == 0);
769         if (whichfork == XFS_ATTR_FORK) {
770                 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
771                 ip->i_afp = NULL;
772         } else if (whichfork == XFS_COW_FORK) {
773                 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
774                 ip->i_cowfp = NULL;
775         }
776 }
777
778 /* Count number of incore extents based on if_bytes */
779 xfs_extnum_t
780 xfs_iext_count(struct xfs_ifork *ifp)
781 {
782         return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
783 }
784
785 /*
786  * Convert in-core extents to on-disk form
787  *
788  * For either the data or attr fork in extent format, we need to endian convert
789  * the in-core extent as we place them into the on-disk inode.
790  *
791  * In the case of the data fork, the in-core and on-disk fork sizes can be
792  * different due to delayed allocation extents. We only copy on-disk extents
793  * here, so callers must always use the physical fork size to determine the
794  * size of the buffer passed to this routine.  We will return the size actually
795  * used.
796  */
797 int
798 xfs_iextents_copy(
799         xfs_inode_t             *ip,
800         xfs_bmbt_rec_t          *dp,
801         int                     whichfork)
802 {
803         int                     copied;
804         int                     i;
805         xfs_ifork_t             *ifp;
806         int                     nrecs;
807         xfs_fsblock_t           start_block;
808
809         ifp = XFS_IFORK_PTR(ip, whichfork);
810         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
811         ASSERT(ifp->if_bytes > 0);
812
813         nrecs = xfs_iext_count(ifp);
814         XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
815         ASSERT(nrecs > 0);
816
817         /*
818          * There are some delayed allocation extents in the
819          * inode, so copy the extents one at a time and skip
820          * the delayed ones.  There must be at least one
821          * non-delayed extent.
822          */
823         copied = 0;
824         for (i = 0; i < nrecs; i++) {
825                 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
826                 start_block = xfs_bmbt_get_startblock(ep);
827                 if (isnullstartblock(start_block)) {
828                         /*
829                          * It's a delayed allocation extent, so skip it.
830                          */
831                         continue;
832                 }
833
834                 /* Translate to on disk format */
835                 put_unaligned_be64(ep->l0, &dp->l0);
836                 put_unaligned_be64(ep->l1, &dp->l1);
837                 dp++;
838                 copied++;
839         }
840         ASSERT(copied != 0);
841         xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
842
843         return (copied * (uint)sizeof(xfs_bmbt_rec_t));
844 }
845
846 /*
847  * Each of the following cases stores data into the same region
848  * of the on-disk inode, so only one of them can be valid at
849  * any given time. While it is possible to have conflicting formats
850  * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
851  * in EXTENTS format, this can only happen when the fork has
852  * changed formats after being modified but before being flushed.
853  * In these cases, the format always takes precedence, because the
854  * format indicates the current state of the fork.
855  */
856 void
857 xfs_iflush_fork(
858         xfs_inode_t             *ip,
859         xfs_dinode_t            *dip,
860         xfs_inode_log_item_t    *iip,
861         int                     whichfork)
862 {
863         char                    *cp;
864         xfs_ifork_t             *ifp;
865         xfs_mount_t             *mp;
866         static const short      brootflag[2] =
867                 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
868         static const short      dataflag[2] =
869                 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
870         static const short      extflag[2] =
871                 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
872
873         if (!iip)
874                 return;
875         ifp = XFS_IFORK_PTR(ip, whichfork);
876         /*
877          * This can happen if we gave up in iformat in an error path,
878          * for the attribute fork.
879          */
880         if (!ifp) {
881                 ASSERT(whichfork == XFS_ATTR_FORK);
882                 return;
883         }
884         cp = XFS_DFORK_PTR(dip, whichfork);
885         mp = ip->i_mount;
886         switch (XFS_IFORK_FORMAT(ip, whichfork)) {
887         case XFS_DINODE_FMT_LOCAL:
888                 if ((iip->ili_fields & dataflag[whichfork]) &&
889                     (ifp->if_bytes > 0)) {
890                         ASSERT(ifp->if_u1.if_data != NULL);
891                         ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
892                         memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
893                 }
894                 break;
895
896         case XFS_DINODE_FMT_EXTENTS:
897                 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
898                        !(iip->ili_fields & extflag[whichfork]));
899                 if ((iip->ili_fields & extflag[whichfork]) &&
900                     (ifp->if_bytes > 0)) {
901                         ASSERT(xfs_iext_get_ext(ifp, 0));
902                         ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
903                         (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
904                                 whichfork);
905                 }
906                 break;
907
908         case XFS_DINODE_FMT_BTREE:
909                 if ((iip->ili_fields & brootflag[whichfork]) &&
910                     (ifp->if_broot_bytes > 0)) {
911                         ASSERT(ifp->if_broot != NULL);
912                         ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
913                                 XFS_IFORK_SIZE(ip, whichfork));
914                         xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
915                                 (xfs_bmdr_block_t *)cp,
916                                 XFS_DFORK_SIZE(dip, mp, whichfork));
917                 }
918                 break;
919
920         case XFS_DINODE_FMT_DEV:
921                 if (iip->ili_fields & XFS_ILOG_DEV) {
922                         ASSERT(whichfork == XFS_DATA_FORK);
923                         xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
924                 }
925                 break;
926
927         case XFS_DINODE_FMT_UUID:
928                 if (iip->ili_fields & XFS_ILOG_UUID) {
929                         ASSERT(whichfork == XFS_DATA_FORK);
930                         memcpy(XFS_DFORK_DPTR(dip),
931                                &ip->i_df.if_u2.if_uuid,
932                                sizeof(uuid_t));
933                 }
934                 break;
935
936         default:
937                 ASSERT(0);
938                 break;
939         }
940 }
941
942 /*
943  * Return a pointer to the extent record at file index idx.
944  */
945 xfs_bmbt_rec_host_t *
946 xfs_iext_get_ext(
947         xfs_ifork_t     *ifp,           /* inode fork pointer */
948         xfs_extnum_t    idx)            /* index of target extent */
949 {
950         ASSERT(idx >= 0);
951         ASSERT(idx < xfs_iext_count(ifp));
952
953         if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
954                 return ifp->if_u1.if_ext_irec->er_extbuf;
955         } else if (ifp->if_flags & XFS_IFEXTIREC) {
956                 xfs_ext_irec_t  *erp;           /* irec pointer */
957                 int             erp_idx = 0;    /* irec index */
958                 xfs_extnum_t    page_idx = idx; /* ext index in target list */
959
960                 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
961                 return &erp->er_extbuf[page_idx];
962         } else if (ifp->if_bytes) {
963                 return &ifp->if_u1.if_extents[idx];
964         } else {
965                 return NULL;
966         }
967 }
968
969 /* Convert bmap state flags to an inode fork. */
970 struct xfs_ifork *
971 xfs_iext_state_to_fork(
972         struct xfs_inode        *ip,
973         int                     state)
974 {
975         if (state & BMAP_COWFORK)
976                 return ip->i_cowfp;
977         else if (state & BMAP_ATTRFORK)
978                 return ip->i_afp;
979         return &ip->i_df;
980 }
981
982 /*
983  * Insert new item(s) into the extent records for incore inode
984  * fork 'ifp'.  'count' new items are inserted at index 'idx'.
985  */
986 void
987 xfs_iext_insert(
988         xfs_inode_t     *ip,            /* incore inode pointer */
989         xfs_extnum_t    idx,            /* starting index of new items */
990         xfs_extnum_t    count,          /* number of inserted items */
991         xfs_bmbt_irec_t *new,           /* items to insert */
992         int             state)          /* type of extent conversion */
993 {
994         xfs_ifork_t     *ifp = xfs_iext_state_to_fork(ip, state);
995         xfs_extnum_t    i;              /* extent record index */
996
997         trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
998
999         ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1000         xfs_iext_add(ifp, idx, count);
1001         for (i = idx; i < idx + count; i++, new++)
1002                 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
1003 }
1004
1005 /*
1006  * This is called when the amount of space required for incore file
1007  * extents needs to be increased. The ext_diff parameter stores the
1008  * number of new extents being added and the idx parameter contains
1009  * the extent index where the new extents will be added. If the new
1010  * extents are being appended, then we just need to (re)allocate and
1011  * initialize the space. Otherwise, if the new extents are being
1012  * inserted into the middle of the existing entries, a bit more work
1013  * is required to make room for the new extents to be inserted. The
1014  * caller is responsible for filling in the new extent entries upon
1015  * return.
1016  */
1017 void
1018 xfs_iext_add(
1019         xfs_ifork_t     *ifp,           /* inode fork pointer */
1020         xfs_extnum_t    idx,            /* index to begin adding exts */
1021         int             ext_diff)       /* number of extents to add */
1022 {
1023         int             byte_diff;      /* new bytes being added */
1024         int             new_size;       /* size of extents after adding */
1025         xfs_extnum_t    nextents;       /* number of extents in file */
1026
1027         nextents = xfs_iext_count(ifp);
1028         ASSERT((idx >= 0) && (idx <= nextents));
1029         byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
1030         new_size = ifp->if_bytes + byte_diff;
1031         /*
1032          * If the new number of extents (nextents + ext_diff)
1033          * fits inside the inode, then continue to use the inline
1034          * extent buffer.
1035          */
1036         if (nextents + ext_diff <= XFS_INLINE_EXTS) {
1037                 if (idx < nextents) {
1038                         memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
1039                                 &ifp->if_u2.if_inline_ext[idx],
1040                                 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1041                         memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
1042                 }
1043                 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1044                 ifp->if_real_bytes = 0;
1045         }
1046         /*
1047          * Otherwise use a linear (direct) extent list.
1048          * If the extents are currently inside the inode,
1049          * xfs_iext_realloc_direct will switch us from
1050          * inline to direct extent allocation mode.
1051          */
1052         else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
1053                 xfs_iext_realloc_direct(ifp, new_size);
1054                 if (idx < nextents) {
1055                         memmove(&ifp->if_u1.if_extents[idx + ext_diff],
1056                                 &ifp->if_u1.if_extents[idx],
1057                                 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1058                         memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
1059                 }
1060         }
1061         /* Indirection array */
1062         else {
1063                 xfs_ext_irec_t  *erp;
1064                 int             erp_idx = 0;
1065                 int             page_idx = idx;
1066
1067                 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
1068                 if (ifp->if_flags & XFS_IFEXTIREC) {
1069                         erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
1070                 } else {
1071                         xfs_iext_irec_init(ifp);
1072                         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1073                         erp = ifp->if_u1.if_ext_irec;
1074                 }
1075                 /* Extents fit in target extent page */
1076                 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1077                         if (page_idx < erp->er_extcount) {
1078                                 memmove(&erp->er_extbuf[page_idx + ext_diff],
1079                                         &erp->er_extbuf[page_idx],
1080                                         (erp->er_extcount - page_idx) *
1081                                         sizeof(xfs_bmbt_rec_t));
1082                                 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1083                         }
1084                         erp->er_extcount += ext_diff;
1085                         xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1086                 }
1087                 /* Insert a new extent page */
1088                 else if (erp) {
1089                         xfs_iext_add_indirect_multi(ifp,
1090                                 erp_idx, page_idx, ext_diff);
1091                 }
1092                 /*
1093                  * If extent(s) are being appended to the last page in
1094                  * the indirection array and the new extent(s) don't fit
1095                  * in the page, then erp is NULL and erp_idx is set to
1096                  * the next index needed in the indirection array.
1097                  */
1098                 else {
1099                         uint    count = ext_diff;
1100
1101                         while (count) {
1102                                 erp = xfs_iext_irec_new(ifp, erp_idx);
1103                                 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1104                                 count -= erp->er_extcount;
1105                                 if (count)
1106                                         erp_idx++;
1107                         }
1108                 }
1109         }
1110         ifp->if_bytes = new_size;
1111 }
1112
1113 /*
1114  * This is called when incore extents are being added to the indirection
1115  * array and the new extents do not fit in the target extent list. The
1116  * erp_idx parameter contains the irec index for the target extent list
1117  * in the indirection array, and the idx parameter contains the extent
1118  * index within the list. The number of extents being added is stored
1119  * in the count parameter.
1120  *
1121  *    |-------|   |-------|
1122  *    |       |   |       |    idx - number of extents before idx
1123  *    |  idx  |   | count |
1124  *    |       |   |       |    count - number of extents being inserted at idx
1125  *    |-------|   |-------|
1126  *    | count |   | nex2  |    nex2 - number of extents after idx + count
1127  *    |-------|   |-------|
1128  */
1129 void
1130 xfs_iext_add_indirect_multi(
1131         xfs_ifork_t     *ifp,                   /* inode fork pointer */
1132         int             erp_idx,                /* target extent irec index */
1133         xfs_extnum_t    idx,                    /* index within target list */
1134         int             count)                  /* new extents being added */
1135 {
1136         int             byte_diff;              /* new bytes being added */
1137         xfs_ext_irec_t  *erp;                   /* pointer to irec entry */
1138         xfs_extnum_t    ext_diff;               /* number of extents to add */
1139         xfs_extnum_t    ext_cnt;                /* new extents still needed */
1140         xfs_extnum_t    nex2;                   /* extents after idx + count */
1141         xfs_bmbt_rec_t  *nex2_ep = NULL;        /* temp list for nex2 extents */
1142         int             nlists;                 /* number of irec's (lists) */
1143
1144         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1145         erp = &ifp->if_u1.if_ext_irec[erp_idx];
1146         nex2 = erp->er_extcount - idx;
1147         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1148
1149         /*
1150          * Save second part of target extent list
1151          * (all extents past */
1152         if (nex2) {
1153                 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1154                 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1155                 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1156                 erp->er_extcount -= nex2;
1157                 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1158                 memset(&erp->er_extbuf[idx], 0, byte_diff);
1159         }
1160
1161         /*
1162          * Add the new extents to the end of the target
1163          * list, then allocate new irec record(s) and
1164          * extent buffer(s) as needed to store the rest
1165          * of the new extents.
1166          */
1167         ext_cnt = count;
1168         ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1169         if (ext_diff) {
1170                 erp->er_extcount += ext_diff;
1171                 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1172                 ext_cnt -= ext_diff;
1173         }
1174         while (ext_cnt) {
1175                 erp_idx++;
1176                 erp = xfs_iext_irec_new(ifp, erp_idx);
1177                 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1178                 erp->er_extcount = ext_diff;
1179                 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1180                 ext_cnt -= ext_diff;
1181         }
1182
1183         /* Add nex2 extents back to indirection array */
1184         if (nex2) {
1185                 xfs_extnum_t    ext_avail;
1186                 int             i;
1187
1188                 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1189                 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1190                 i = 0;
1191                 /*
1192                  * If nex2 extents fit in the current page, append
1193                  * nex2_ep after the new extents.
1194                  */
1195                 if (nex2 <= ext_avail) {
1196                         i = erp->er_extcount;
1197                 }
1198                 /*
1199                  * Otherwise, check if space is available in the
1200                  * next page.
1201                  */
1202                 else if ((erp_idx < nlists - 1) &&
1203                          (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1204                           ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1205                         erp_idx++;
1206                         erp++;
1207                         /* Create a hole for nex2 extents */
1208                         memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1209                                 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1210                 }
1211                 /*
1212                  * Final choice, create a new extent page for
1213                  * nex2 extents.
1214                  */
1215                 else {
1216                         erp_idx++;
1217                         erp = xfs_iext_irec_new(ifp, erp_idx);
1218                 }
1219                 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1220                 kmem_free(nex2_ep);
1221                 erp->er_extcount += nex2;
1222                 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1223         }
1224 }
1225
1226 /*
1227  * This is called when the amount of space required for incore file
1228  * extents needs to be decreased. The ext_diff parameter stores the
1229  * number of extents to be removed and the idx parameter contains
1230  * the extent index where the extents will be removed from.
1231  *
1232  * If the amount of space needed has decreased below the linear
1233  * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1234  * extent array.  Otherwise, use kmem_realloc() to adjust the
1235  * size to what is needed.
1236  */
1237 void
1238 xfs_iext_remove(
1239         xfs_inode_t     *ip,            /* incore inode pointer */
1240         xfs_extnum_t    idx,            /* index to begin removing exts */
1241         int             ext_diff,       /* number of extents to remove */
1242         int             state)          /* type of extent conversion */
1243 {
1244         xfs_ifork_t     *ifp = xfs_iext_state_to_fork(ip, state);
1245         xfs_extnum_t    nextents;       /* number of extents in file */
1246         int             new_size;       /* size of extents after removal */
1247
1248         trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1249
1250         ASSERT(ext_diff > 0);
1251         nextents = xfs_iext_count(ifp);
1252         new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1253
1254         if (new_size == 0) {
1255                 xfs_iext_destroy(ifp);
1256         } else if (ifp->if_flags & XFS_IFEXTIREC) {
1257                 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1258         } else if (ifp->if_real_bytes) {
1259                 xfs_iext_remove_direct(ifp, idx, ext_diff);
1260         } else {
1261                 xfs_iext_remove_inline(ifp, idx, ext_diff);
1262         }
1263         ifp->if_bytes = new_size;
1264 }
1265
1266 /*
1267  * This removes ext_diff extents from the inline buffer, beginning
1268  * at extent index idx.
1269  */
1270 void
1271 xfs_iext_remove_inline(
1272         xfs_ifork_t     *ifp,           /* inode fork pointer */
1273         xfs_extnum_t    idx,            /* index to begin removing exts */
1274         int             ext_diff)       /* number of extents to remove */
1275 {
1276         int             nextents;       /* number of extents in file */
1277
1278         ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1279         ASSERT(idx < XFS_INLINE_EXTS);
1280         nextents = xfs_iext_count(ifp);
1281         ASSERT(((nextents - ext_diff) > 0) &&
1282                 (nextents - ext_diff) < XFS_INLINE_EXTS);
1283
1284         if (idx + ext_diff < nextents) {
1285                 memmove(&ifp->if_u2.if_inline_ext[idx],
1286                         &ifp->if_u2.if_inline_ext[idx + ext_diff],
1287                         (nextents - (idx + ext_diff)) *
1288                          sizeof(xfs_bmbt_rec_t));
1289                 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1290                         0, ext_diff * sizeof(xfs_bmbt_rec_t));
1291         } else {
1292                 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1293                         ext_diff * sizeof(xfs_bmbt_rec_t));
1294         }
1295 }
1296
1297 /*
1298  * This removes ext_diff extents from a linear (direct) extent list,
1299  * beginning at extent index idx. If the extents are being removed
1300  * from the end of the list (ie. truncate) then we just need to re-
1301  * allocate the list to remove the extra space. Otherwise, if the
1302  * extents are being removed from the middle of the existing extent
1303  * entries, then we first need to move the extent records beginning
1304  * at idx + ext_diff up in the list to overwrite the records being
1305  * removed, then remove the extra space via kmem_realloc.
1306  */
1307 void
1308 xfs_iext_remove_direct(
1309         xfs_ifork_t     *ifp,           /* inode fork pointer */
1310         xfs_extnum_t    idx,            /* index to begin removing exts */
1311         int             ext_diff)       /* number of extents to remove */
1312 {
1313         xfs_extnum_t    nextents;       /* number of extents in file */
1314         int             new_size;       /* size of extents after removal */
1315
1316         ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1317         new_size = ifp->if_bytes -
1318                 (ext_diff * sizeof(xfs_bmbt_rec_t));
1319         nextents = xfs_iext_count(ifp);
1320
1321         if (new_size == 0) {
1322                 xfs_iext_destroy(ifp);
1323                 return;
1324         }
1325         /* Move extents up in the list (if needed) */
1326         if (idx + ext_diff < nextents) {
1327                 memmove(&ifp->if_u1.if_extents[idx],
1328                         &ifp->if_u1.if_extents[idx + ext_diff],
1329                         (nextents - (idx + ext_diff)) *
1330                          sizeof(xfs_bmbt_rec_t));
1331         }
1332         memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1333                 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1334         /*
1335          * Reallocate the direct extent list. If the extents
1336          * will fit inside the inode then xfs_iext_realloc_direct
1337          * will switch from direct to inline extent allocation
1338          * mode for us.
1339          */
1340         xfs_iext_realloc_direct(ifp, new_size);
1341         ifp->if_bytes = new_size;
1342 }
1343
1344 /*
1345  * This is called when incore extents are being removed from the
1346  * indirection array and the extents being removed span multiple extent
1347  * buffers. The idx parameter contains the file extent index where we
1348  * want to begin removing extents, and the count parameter contains
1349  * how many extents need to be removed.
1350  *
1351  *    |-------|   |-------|
1352  *    | nex1  |   |       |    nex1 - number of extents before idx
1353  *    |-------|   | count |
1354  *    |       |   |       |    count - number of extents being removed at idx
1355  *    | count |   |-------|
1356  *    |       |   | nex2  |    nex2 - number of extents after idx + count
1357  *    |-------|   |-------|
1358  */
1359 void
1360 xfs_iext_remove_indirect(
1361         xfs_ifork_t     *ifp,           /* inode fork pointer */
1362         xfs_extnum_t    idx,            /* index to begin removing extents */
1363         int             count)          /* number of extents to remove */
1364 {
1365         xfs_ext_irec_t  *erp;           /* indirection array pointer */
1366         int             erp_idx = 0;    /* indirection array index */
1367         xfs_extnum_t    ext_cnt;        /* extents left to remove */
1368         xfs_extnum_t    ext_diff;       /* extents to remove in current list */
1369         xfs_extnum_t    nex1;           /* number of extents before idx */
1370         xfs_extnum_t    nex2;           /* extents after idx + count */
1371         int             page_idx = idx; /* index in target extent list */
1372
1373         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1374         erp = xfs_iext_idx_to_irec(ifp,  &page_idx, &erp_idx, 0);
1375         ASSERT(erp != NULL);
1376         nex1 = page_idx;
1377         ext_cnt = count;
1378         while (ext_cnt) {
1379                 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1380                 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1381                 /*
1382                  * Check for deletion of entire list;
1383                  * xfs_iext_irec_remove() updates extent offsets.
1384                  */
1385                 if (ext_diff == erp->er_extcount) {
1386                         xfs_iext_irec_remove(ifp, erp_idx);
1387                         ext_cnt -= ext_diff;
1388                         nex1 = 0;
1389                         if (ext_cnt) {
1390                                 ASSERT(erp_idx < ifp->if_real_bytes /
1391                                         XFS_IEXT_BUFSZ);
1392                                 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1393                                 nex1 = 0;
1394                                 continue;
1395                         } else {
1396                                 break;
1397                         }
1398                 }
1399                 /* Move extents up (if needed) */
1400                 if (nex2) {
1401                         memmove(&erp->er_extbuf[nex1],
1402                                 &erp->er_extbuf[nex1 + ext_diff],
1403                                 nex2 * sizeof(xfs_bmbt_rec_t));
1404                 }
1405                 /* Zero out rest of page */
1406                 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1407                         ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1408                 /* Update remaining counters */
1409                 erp->er_extcount -= ext_diff;
1410                 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1411                 ext_cnt -= ext_diff;
1412                 nex1 = 0;
1413                 erp_idx++;
1414                 erp++;
1415         }
1416         ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1417         xfs_iext_irec_compact(ifp);
1418 }
1419
1420 /*
1421  * Create, destroy, or resize a linear (direct) block of extents.
1422  */
1423 void
1424 xfs_iext_realloc_direct(
1425         xfs_ifork_t     *ifp,           /* inode fork pointer */
1426         int             new_size)       /* new size of extents after adding */
1427 {
1428         int             rnew_size;      /* real new size of extents */
1429
1430         rnew_size = new_size;
1431
1432         ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1433                 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1434                  (new_size != ifp->if_real_bytes)));
1435
1436         /* Free extent records */
1437         if (new_size == 0) {
1438                 xfs_iext_destroy(ifp);
1439         }
1440         /* Resize direct extent list and zero any new bytes */
1441         else if (ifp->if_real_bytes) {
1442                 /* Check if extents will fit inside the inode */
1443                 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1444                         xfs_iext_direct_to_inline(ifp, new_size /
1445                                 (uint)sizeof(xfs_bmbt_rec_t));
1446                         ifp->if_bytes = new_size;
1447                         return;
1448                 }
1449                 if (!is_power_of_2(new_size)){
1450                         rnew_size = roundup_pow_of_two(new_size);
1451                 }
1452                 if (rnew_size != ifp->if_real_bytes) {
1453                         ifp->if_u1.if_extents =
1454                                 kmem_realloc(ifp->if_u1.if_extents,
1455                                                 rnew_size, KM_NOFS);
1456                 }
1457                 if (rnew_size > ifp->if_real_bytes) {
1458                         memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1459                                 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1460                                 rnew_size - ifp->if_real_bytes);
1461                 }
1462         }
1463         /* Switch from the inline extent buffer to a direct extent list */
1464         else {
1465                 if (!is_power_of_2(new_size)) {
1466                         rnew_size = roundup_pow_of_two(new_size);
1467                 }
1468                 xfs_iext_inline_to_direct(ifp, rnew_size);
1469         }
1470         ifp->if_real_bytes = rnew_size;
1471         ifp->if_bytes = new_size;
1472 }
1473
1474 /*
1475  * Switch from linear (direct) extent records to inline buffer.
1476  */
1477 void
1478 xfs_iext_direct_to_inline(
1479         xfs_ifork_t     *ifp,           /* inode fork pointer */
1480         xfs_extnum_t    nextents)       /* number of extents in file */
1481 {
1482         ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1483         ASSERT(nextents <= XFS_INLINE_EXTS);
1484         /*
1485          * The inline buffer was zeroed when we switched
1486          * from inline to direct extent allocation mode,
1487          * so we don't need to clear it here.
1488          */
1489         memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1490                 nextents * sizeof(xfs_bmbt_rec_t));
1491         kmem_free(ifp->if_u1.if_extents);
1492         ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1493         ifp->if_real_bytes = 0;
1494 }
1495
1496 /*
1497  * Switch from inline buffer to linear (direct) extent records.
1498  * new_size should already be rounded up to the next power of 2
1499  * by the caller (when appropriate), so use new_size as it is.
1500  * However, since new_size may be rounded up, we can't update
1501  * if_bytes here. It is the caller's responsibility to update
1502  * if_bytes upon return.
1503  */
1504 void
1505 xfs_iext_inline_to_direct(
1506         xfs_ifork_t     *ifp,           /* inode fork pointer */
1507         int             new_size)       /* number of extents in file */
1508 {
1509         ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1510         memset(ifp->if_u1.if_extents, 0, new_size);
1511         if (ifp->if_bytes) {
1512                 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1513                         ifp->if_bytes);
1514                 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1515                         sizeof(xfs_bmbt_rec_t));
1516         }
1517         ifp->if_real_bytes = new_size;
1518 }
1519
1520 /*
1521  * Resize an extent indirection array to new_size bytes.
1522  */
1523 STATIC void
1524 xfs_iext_realloc_indirect(
1525         xfs_ifork_t     *ifp,           /* inode fork pointer */
1526         int             new_size)       /* new indirection array size */
1527 {
1528         int             nlists;         /* number of irec's (ex lists) */
1529         int             size;           /* current indirection array size */
1530
1531         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1532         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1533         size = nlists * sizeof(xfs_ext_irec_t);
1534         ASSERT(ifp->if_real_bytes);
1535         ASSERT((new_size >= 0) && (new_size != size));
1536         if (new_size == 0) {
1537                 xfs_iext_destroy(ifp);
1538         } else {
1539                 ifp->if_u1.if_ext_irec =
1540                         kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
1541         }
1542 }
1543
1544 /*
1545  * Switch from indirection array to linear (direct) extent allocations.
1546  */
1547 STATIC void
1548 xfs_iext_indirect_to_direct(
1549          xfs_ifork_t    *ifp)           /* inode fork pointer */
1550 {
1551         xfs_bmbt_rec_host_t *ep;        /* extent record pointer */
1552         xfs_extnum_t    nextents;       /* number of extents in file */
1553         int             size;           /* size of file extents */
1554
1555         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1556         nextents = xfs_iext_count(ifp);
1557         ASSERT(nextents <= XFS_LINEAR_EXTS);
1558         size = nextents * sizeof(xfs_bmbt_rec_t);
1559
1560         xfs_iext_irec_compact_pages(ifp);
1561         ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1562
1563         ep = ifp->if_u1.if_ext_irec->er_extbuf;
1564         kmem_free(ifp->if_u1.if_ext_irec);
1565         ifp->if_flags &= ~XFS_IFEXTIREC;
1566         ifp->if_u1.if_extents = ep;
1567         ifp->if_bytes = size;
1568         if (nextents < XFS_LINEAR_EXTS) {
1569                 xfs_iext_realloc_direct(ifp, size);
1570         }
1571 }
1572
1573 /*
1574  * Remove all records from the indirection array.
1575  */
1576 STATIC void
1577 xfs_iext_irec_remove_all(
1578         struct xfs_ifork *ifp)
1579 {
1580         int             nlists;
1581         int             i;
1582
1583         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1584         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1585         for (i = 0; i < nlists; i++)
1586                 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1587         kmem_free(ifp->if_u1.if_ext_irec);
1588         ifp->if_flags &= ~XFS_IFEXTIREC;
1589 }
1590
1591 /*
1592  * Free incore file extents.
1593  */
1594 void
1595 xfs_iext_destroy(
1596         xfs_ifork_t     *ifp)           /* inode fork pointer */
1597 {
1598         if (ifp->if_flags & XFS_IFEXTIREC) {
1599                 xfs_iext_irec_remove_all(ifp);
1600         } else if (ifp->if_real_bytes) {
1601                 kmem_free(ifp->if_u1.if_extents);
1602         } else if (ifp->if_bytes) {
1603                 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1604                         sizeof(xfs_bmbt_rec_t));
1605         }
1606         ifp->if_u1.if_extents = NULL;
1607         ifp->if_real_bytes = 0;
1608         ifp->if_bytes = 0;
1609 }
1610
1611 /*
1612  * Return a pointer to the extent record for file system block bno.
1613  */
1614 xfs_bmbt_rec_host_t *                   /* pointer to found extent record */
1615 xfs_iext_bno_to_ext(
1616         xfs_ifork_t     *ifp,           /* inode fork pointer */
1617         xfs_fileoff_t   bno,            /* block number to search for */
1618         xfs_extnum_t    *idxp)          /* index of target extent */
1619 {
1620         xfs_bmbt_rec_host_t *base;      /* pointer to first extent */
1621         xfs_filblks_t   blockcount = 0; /* number of blocks in extent */
1622         xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1623         xfs_ext_irec_t  *erp = NULL;    /* indirection array pointer */
1624         int             high;           /* upper boundary in search */
1625         xfs_extnum_t    idx = 0;        /* index of target extent */
1626         int             low;            /* lower boundary in search */
1627         xfs_extnum_t    nextents;       /* number of file extents */
1628         xfs_fileoff_t   startoff = 0;   /* start offset of extent */
1629
1630         nextents = xfs_iext_count(ifp);
1631         if (nextents == 0) {
1632                 *idxp = 0;
1633                 return NULL;
1634         }
1635         low = 0;
1636         if (ifp->if_flags & XFS_IFEXTIREC) {
1637                 /* Find target extent list */
1638                 int     erp_idx = 0;
1639                 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1640                 base = erp->er_extbuf;
1641                 high = erp->er_extcount - 1;
1642         } else {
1643                 base = ifp->if_u1.if_extents;
1644                 high = nextents - 1;
1645         }
1646         /* Binary search extent records */
1647         while (low <= high) {
1648                 idx = (low + high) >> 1;
1649                 ep = base + idx;
1650                 startoff = xfs_bmbt_get_startoff(ep);
1651                 blockcount = xfs_bmbt_get_blockcount(ep);
1652                 if (bno < startoff) {
1653                         high = idx - 1;
1654                 } else if (bno >= startoff + blockcount) {
1655                         low = idx + 1;
1656                 } else {
1657                         /* Convert back to file-based extent index */
1658                         if (ifp->if_flags & XFS_IFEXTIREC) {
1659                                 idx += erp->er_extoff;
1660                         }
1661                         *idxp = idx;
1662                         return ep;
1663                 }
1664         }
1665         /* Convert back to file-based extent index */
1666         if (ifp->if_flags & XFS_IFEXTIREC) {
1667                 idx += erp->er_extoff;
1668         }
1669         if (bno >= startoff + blockcount) {
1670                 if (++idx == nextents) {
1671                         ep = NULL;
1672                 } else {
1673                         ep = xfs_iext_get_ext(ifp, idx);
1674                 }
1675         }
1676         *idxp = idx;
1677         return ep;
1678 }
1679
1680 /*
1681  * Return a pointer to the indirection array entry containing the
1682  * extent record for filesystem block bno. Store the index of the
1683  * target irec in *erp_idxp.
1684  */
1685 xfs_ext_irec_t *                        /* pointer to found extent record */
1686 xfs_iext_bno_to_irec(
1687         xfs_ifork_t     *ifp,           /* inode fork pointer */
1688         xfs_fileoff_t   bno,            /* block number to search for */
1689         int             *erp_idxp)      /* irec index of target ext list */
1690 {
1691         xfs_ext_irec_t  *erp = NULL;    /* indirection array pointer */
1692         xfs_ext_irec_t  *erp_next;      /* next indirection array entry */
1693         int             erp_idx;        /* indirection array index */
1694         int             nlists;         /* number of extent irec's (lists) */
1695         int             high;           /* binary search upper limit */
1696         int             low;            /* binary search lower limit */
1697
1698         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1699         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1700         erp_idx = 0;
1701         low = 0;
1702         high = nlists - 1;
1703         while (low <= high) {
1704                 erp_idx = (low + high) >> 1;
1705                 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1706                 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1707                 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1708                         high = erp_idx - 1;
1709                 } else if (erp_next && bno >=
1710                            xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1711                         low = erp_idx + 1;
1712                 } else {
1713                         break;
1714                 }
1715         }
1716         *erp_idxp = erp_idx;
1717         return erp;
1718 }
1719
1720 /*
1721  * Return a pointer to the indirection array entry containing the
1722  * extent record at file extent index *idxp. Store the index of the
1723  * target irec in *erp_idxp and store the page index of the target
1724  * extent record in *idxp.
1725  */
1726 xfs_ext_irec_t *
1727 xfs_iext_idx_to_irec(
1728         xfs_ifork_t     *ifp,           /* inode fork pointer */
1729         xfs_extnum_t    *idxp,          /* extent index (file -> page) */
1730         int             *erp_idxp,      /* pointer to target irec */
1731         int             realloc)        /* new bytes were just added */
1732 {
1733         xfs_ext_irec_t  *prev;          /* pointer to previous irec */
1734         xfs_ext_irec_t  *erp = NULL;    /* pointer to current irec */
1735         int             erp_idx;        /* indirection array index */
1736         int             nlists;         /* number of irec's (ex lists) */
1737         int             high;           /* binary search upper limit */
1738         int             low;            /* binary search lower limit */
1739         xfs_extnum_t    page_idx = *idxp; /* extent index in target list */
1740
1741         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1742         ASSERT(page_idx >= 0);
1743         ASSERT(page_idx <= xfs_iext_count(ifp));
1744         ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
1745
1746         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1747         erp_idx = 0;
1748         low = 0;
1749         high = nlists - 1;
1750
1751         /* Binary search extent irec's */
1752         while (low <= high) {
1753                 erp_idx = (low + high) >> 1;
1754                 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1755                 prev = erp_idx > 0 ? erp - 1 : NULL;
1756                 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1757                      realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1758                         high = erp_idx - 1;
1759                 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1760                            (page_idx == erp->er_extoff + erp->er_extcount &&
1761                             !realloc)) {
1762                         low = erp_idx + 1;
1763                 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1764                            erp->er_extcount == XFS_LINEAR_EXTS) {
1765                         ASSERT(realloc);
1766                         page_idx = 0;
1767                         erp_idx++;
1768                         erp = erp_idx < nlists ? erp + 1 : NULL;
1769                         break;
1770                 } else {
1771                         page_idx -= erp->er_extoff;
1772                         break;
1773                 }
1774         }
1775         *idxp = page_idx;
1776         *erp_idxp = erp_idx;
1777         return erp;
1778 }
1779
1780 /*
1781  * Allocate and initialize an indirection array once the space needed
1782  * for incore extents increases above XFS_IEXT_BUFSZ.
1783  */
1784 void
1785 xfs_iext_irec_init(
1786         xfs_ifork_t     *ifp)           /* inode fork pointer */
1787 {
1788         xfs_ext_irec_t  *erp;           /* indirection array pointer */
1789         xfs_extnum_t    nextents;       /* number of extents in file */
1790
1791         ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1792         nextents = xfs_iext_count(ifp);
1793         ASSERT(nextents <= XFS_LINEAR_EXTS);
1794
1795         erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1796
1797         if (nextents == 0) {
1798                 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1799         } else if (!ifp->if_real_bytes) {
1800                 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1801         } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1802                 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1803         }
1804         erp->er_extbuf = ifp->if_u1.if_extents;
1805         erp->er_extcount = nextents;
1806         erp->er_extoff = 0;
1807
1808         ifp->if_flags |= XFS_IFEXTIREC;
1809         ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1810         ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1811         ifp->if_u1.if_ext_irec = erp;
1812
1813         return;
1814 }
1815
1816 /*
1817  * Allocate and initialize a new entry in the indirection array.
1818  */
1819 xfs_ext_irec_t *
1820 xfs_iext_irec_new(
1821         xfs_ifork_t     *ifp,           /* inode fork pointer */
1822         int             erp_idx)        /* index for new irec */
1823 {
1824         xfs_ext_irec_t  *erp;           /* indirection array pointer */
1825         int             i;              /* loop counter */
1826         int             nlists;         /* number of irec's (ex lists) */
1827
1828         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1829         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1830
1831         /* Resize indirection array */
1832         xfs_iext_realloc_indirect(ifp, ++nlists *
1833                                   sizeof(xfs_ext_irec_t));
1834         /*
1835          * Move records down in the array so the
1836          * new page can use erp_idx.
1837          */
1838         erp = ifp->if_u1.if_ext_irec;
1839         for (i = nlists - 1; i > erp_idx; i--) {
1840                 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1841         }
1842         ASSERT(i == erp_idx);
1843
1844         /* Initialize new extent record */
1845         erp = ifp->if_u1.if_ext_irec;
1846         erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1847         ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1848         memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1849         erp[erp_idx].er_extcount = 0;
1850         erp[erp_idx].er_extoff = erp_idx > 0 ?
1851                 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1852         return (&erp[erp_idx]);
1853 }
1854
1855 /*
1856  * Remove a record from the indirection array.
1857  */
1858 void
1859 xfs_iext_irec_remove(
1860         xfs_ifork_t     *ifp,           /* inode fork pointer */
1861         int             erp_idx)        /* irec index to remove */
1862 {
1863         xfs_ext_irec_t  *erp;           /* indirection array pointer */
1864         int             i;              /* loop counter */
1865         int             nlists;         /* number of irec's (ex lists) */
1866
1867         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1868         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1869         erp = &ifp->if_u1.if_ext_irec[erp_idx];
1870         if (erp->er_extbuf) {
1871                 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1872                         -erp->er_extcount);
1873                 kmem_free(erp->er_extbuf);
1874         }
1875         /* Compact extent records */
1876         erp = ifp->if_u1.if_ext_irec;
1877         for (i = erp_idx; i < nlists - 1; i++) {
1878                 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1879         }
1880         /*
1881          * Manually free the last extent record from the indirection
1882          * array.  A call to xfs_iext_realloc_indirect() with a size
1883          * of zero would result in a call to xfs_iext_destroy() which
1884          * would in turn call this function again, creating a nasty
1885          * infinite loop.
1886          */
1887         if (--nlists) {
1888                 xfs_iext_realloc_indirect(ifp,
1889                         nlists * sizeof(xfs_ext_irec_t));
1890         } else {
1891                 kmem_free(ifp->if_u1.if_ext_irec);
1892         }
1893         ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1894 }
1895
1896 /*
1897  * This is called to clean up large amounts of unused memory allocated
1898  * by the indirection array.  Before compacting anything though, verify
1899  * that the indirection array is still needed and switch back to the
1900  * linear extent list (or even the inline buffer) if possible.  The
1901  * compaction policy is as follows:
1902  *
1903  *    Full Compaction: Extents fit into a single page (or inline buffer)
1904  * Partial Compaction: Extents occupy less than 50% of allocated space
1905  *      No Compaction: Extents occupy at least 50% of allocated space
1906  */
1907 void
1908 xfs_iext_irec_compact(
1909         xfs_ifork_t     *ifp)           /* inode fork pointer */
1910 {
1911         xfs_extnum_t    nextents;       /* number of extents in file */
1912         int             nlists;         /* number of irec's (ex lists) */
1913
1914         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1915         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1916         nextents = xfs_iext_count(ifp);
1917
1918         if (nextents == 0) {
1919                 xfs_iext_destroy(ifp);
1920         } else if (nextents <= XFS_INLINE_EXTS) {
1921                 xfs_iext_indirect_to_direct(ifp);
1922                 xfs_iext_direct_to_inline(ifp, nextents);
1923         } else if (nextents <= XFS_LINEAR_EXTS) {
1924                 xfs_iext_indirect_to_direct(ifp);
1925         } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1926                 xfs_iext_irec_compact_pages(ifp);
1927         }
1928 }
1929
1930 /*
1931  * Combine extents from neighboring extent pages.
1932  */
1933 void
1934 xfs_iext_irec_compact_pages(
1935         xfs_ifork_t     *ifp)           /* inode fork pointer */
1936 {
1937         xfs_ext_irec_t  *erp, *erp_next;/* pointers to irec entries */
1938         int             erp_idx = 0;    /* indirection array index */
1939         int             nlists;         /* number of irec's (ex lists) */
1940
1941         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1942         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1943         while (erp_idx < nlists - 1) {
1944                 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1945                 erp_next = erp + 1;
1946                 if (erp_next->er_extcount <=
1947                     (XFS_LINEAR_EXTS - erp->er_extcount)) {
1948                         memcpy(&erp->er_extbuf[erp->er_extcount],
1949                                 erp_next->er_extbuf, erp_next->er_extcount *
1950                                 sizeof(xfs_bmbt_rec_t));
1951                         erp->er_extcount += erp_next->er_extcount;
1952                         /*
1953                          * Free page before removing extent record
1954                          * so er_extoffs don't get modified in
1955                          * xfs_iext_irec_remove.
1956                          */
1957                         kmem_free(erp_next->er_extbuf);
1958                         erp_next->er_extbuf = NULL;
1959                         xfs_iext_irec_remove(ifp, erp_idx + 1);
1960                         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1961                 } else {
1962                         erp_idx++;
1963                 }
1964         }
1965 }
1966
1967 /*
1968  * This is called to update the er_extoff field in the indirection
1969  * array when extents have been added or removed from one of the
1970  * extent lists. erp_idx contains the irec index to begin updating
1971  * at and ext_diff contains the number of extents that were added
1972  * or removed.
1973  */
1974 void
1975 xfs_iext_irec_update_extoffs(
1976         xfs_ifork_t     *ifp,           /* inode fork pointer */
1977         int             erp_idx,        /* irec index to update */
1978         int             ext_diff)       /* number of new extents */
1979 {
1980         int             i;              /* loop counter */
1981         int             nlists;         /* number of irec's (ex lists */
1982
1983         ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1984         nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1985         for (i = erp_idx; i < nlists; i++) {
1986                 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1987         }
1988 }
1989
1990 /*
1991  * Initialize an inode's copy-on-write fork.
1992  */
1993 void
1994 xfs_ifork_init_cow(
1995         struct xfs_inode        *ip)
1996 {
1997         if (ip->i_cowfp)
1998                 return;
1999
2000         ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
2001                                        KM_SLEEP | KM_NOFS);
2002         ip->i_cowfp->if_flags = XFS_IFEXTENTS;
2003         ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
2004         ip->i_cnextents = 0;
2005 }
2006
2007 /*
2008  * Lookup the extent covering bno.
2009  *
2010  * If there is an extent covering bno return the extent index, and store the
2011  * expanded extent structure in *gotp, and the extent index in *idx.
2012  * If there is no extent covering bno, but there is an extent after it (e.g.
2013  * it lies in a hole) return that extent in *gotp and its index in *idx
2014  * instead.
2015  * If bno is beyond the last extent return false, and return the index after
2016  * the last valid index in *idxp.
2017  */
2018 bool
2019 xfs_iext_lookup_extent(
2020         struct xfs_inode        *ip,
2021         struct xfs_ifork        *ifp,
2022         xfs_fileoff_t           bno,
2023         xfs_extnum_t            *idxp,
2024         struct xfs_bmbt_irec    *gotp)
2025 {
2026         struct xfs_bmbt_rec_host *ep;
2027
2028         XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2029
2030         ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2031         if (!ep)
2032                 return false;
2033         xfs_bmbt_get_all(ep, gotp);
2034         return true;
2035 }
2036
2037 /*
2038  * Return true if there is an extent at index idx, and return the expanded
2039  * extent structure at idx in that case.  Else return false.
2040  */
2041 bool
2042 xfs_iext_get_extent(
2043         struct xfs_ifork        *ifp,
2044         xfs_extnum_t            idx,
2045         struct xfs_bmbt_irec    *gotp)
2046 {
2047         if (idx < 0 || idx >= xfs_iext_count(ifp))
2048                 return false;
2049         xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);
2050         return true;
2051 }