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