xfs: move acl structures to xfs_format.h
[firefly-linux-kernel-4.4.55.git] / fs / xfs / libxfs / xfs_dir2_sf.c
1 /*
2  * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_sb.h"
24 #include "xfs_ag.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_format.h"
27 #include "xfs_da_btree.h"
28 #include "xfs_inode.h"
29 #include "xfs_trans.h"
30 #include "xfs_inode_item.h"
31 #include "xfs_error.h"
32 #include "xfs_dir2.h"
33 #include "xfs_dir2_priv.h"
34 #include "xfs_trace.h"
35
36 /*
37  * Prototypes for internal functions.
38  */
39 static void xfs_dir2_sf_addname_easy(xfs_da_args_t *args,
40                                      xfs_dir2_sf_entry_t *sfep,
41                                      xfs_dir2_data_aoff_t offset,
42                                      int new_isize);
43 static void xfs_dir2_sf_addname_hard(xfs_da_args_t *args, int objchange,
44                                      int new_isize);
45 static int xfs_dir2_sf_addname_pick(xfs_da_args_t *args, int objchange,
46                                     xfs_dir2_sf_entry_t **sfepp,
47                                     xfs_dir2_data_aoff_t *offsetp);
48 #ifdef DEBUG
49 static void xfs_dir2_sf_check(xfs_da_args_t *args);
50 #else
51 #define xfs_dir2_sf_check(args)
52 #endif /* DEBUG */
53
54 static void xfs_dir2_sf_toino4(xfs_da_args_t *args);
55 static void xfs_dir2_sf_toino8(xfs_da_args_t *args);
56
57 /*
58  * Given a block directory (dp/block), calculate its size as a shortform (sf)
59  * directory and a header for the sf directory, if it will fit it the
60  * space currently present in the inode.  If it won't fit, the output
61  * size is too big (but not accurate).
62  */
63 int                                             /* size for sf form */
64 xfs_dir2_block_sfsize(
65         xfs_inode_t             *dp,            /* incore inode pointer */
66         xfs_dir2_data_hdr_t     *hdr,           /* block directory data */
67         xfs_dir2_sf_hdr_t       *sfhp)          /* output: header for sf form */
68 {
69         xfs_dir2_dataptr_t      addr;           /* data entry address */
70         xfs_dir2_leaf_entry_t   *blp;           /* leaf area of the block */
71         xfs_dir2_block_tail_t   *btp;           /* tail area of the block */
72         int                     count;          /* shortform entry count */
73         xfs_dir2_data_entry_t   *dep;           /* data entry in the block */
74         int                     i;              /* block entry index */
75         int                     i8count;        /* count of big-inode entries */
76         int                     isdot;          /* entry is "." */
77         int                     isdotdot;       /* entry is ".." */
78         xfs_mount_t             *mp;            /* mount structure pointer */
79         int                     namelen;        /* total name bytes */
80         xfs_ino_t               parent = 0;     /* parent inode number */
81         int                     size=0;         /* total computed size */
82         int                     has_ftype;
83         struct xfs_da_geometry  *geo;
84
85         mp = dp->i_mount;
86         geo = mp->m_dir_geo;
87
88         /*
89          * if there is a filetype field, add the extra byte to the namelen
90          * for each entry that we see.
91          */
92         has_ftype = xfs_sb_version_hasftype(&mp->m_sb) ? 1 : 0;
93
94         count = i8count = namelen = 0;
95         btp = xfs_dir2_block_tail_p(geo, hdr);
96         blp = xfs_dir2_block_leaf_p(btp);
97
98         /*
99          * Iterate over the block's data entries by using the leaf pointers.
100          */
101         for (i = 0; i < be32_to_cpu(btp->count); i++) {
102                 if ((addr = be32_to_cpu(blp[i].address)) == XFS_DIR2_NULL_DATAPTR)
103                         continue;
104                 /*
105                  * Calculate the pointer to the entry at hand.
106                  */
107                 dep = (xfs_dir2_data_entry_t *)((char *)hdr +
108                                 xfs_dir2_dataptr_to_off(geo, addr));
109                 /*
110                  * Detect . and .., so we can special-case them.
111                  * . is not included in sf directories.
112                  * .. is included by just the parent inode number.
113                  */
114                 isdot = dep->namelen == 1 && dep->name[0] == '.';
115                 isdotdot =
116                         dep->namelen == 2 &&
117                         dep->name[0] == '.' && dep->name[1] == '.';
118
119                 if (!isdot)
120                         i8count += be64_to_cpu(dep->inumber) > XFS_DIR2_MAX_SHORT_INUM;
121
122                 /* take into account the file type field */
123                 if (!isdot && !isdotdot) {
124                         count++;
125                         namelen += dep->namelen + has_ftype;
126                 } else if (isdotdot)
127                         parent = be64_to_cpu(dep->inumber);
128                 /*
129                  * Calculate the new size, see if we should give up yet.
130                  */
131                 size = xfs_dir2_sf_hdr_size(i8count) +          /* header */
132                        count +                                  /* namelen */
133                        count * (uint)sizeof(xfs_dir2_sf_off_t) + /* offset */
134                        namelen +                                /* name */
135                        (i8count ?                               /* inumber */
136                                 (uint)sizeof(xfs_dir2_ino8_t) * count :
137                                 (uint)sizeof(xfs_dir2_ino4_t) * count);
138                 if (size > XFS_IFORK_DSIZE(dp))
139                         return size;            /* size value is a failure */
140         }
141         /*
142          * Create the output header, if it worked.
143          */
144         sfhp->count = count;
145         sfhp->i8count = i8count;
146         dp->d_ops->sf_put_parent_ino(sfhp, parent);
147         return size;
148 }
149
150 /*
151  * Convert a block format directory to shortform.
152  * Caller has already checked that it will fit, and built us a header.
153  */
154 int                                             /* error */
155 xfs_dir2_block_to_sf(
156         xfs_da_args_t           *args,          /* operation arguments */
157         struct xfs_buf          *bp,
158         int                     size,           /* shortform directory size */
159         xfs_dir2_sf_hdr_t       *sfhp)          /* shortform directory hdr */
160 {
161         xfs_dir2_data_hdr_t     *hdr;           /* block header */
162         xfs_dir2_block_tail_t   *btp;           /* block tail pointer */
163         xfs_dir2_data_entry_t   *dep;           /* data entry pointer */
164         xfs_inode_t             *dp;            /* incore directory inode */
165         xfs_dir2_data_unused_t  *dup;           /* unused data pointer */
166         char                    *endptr;        /* end of data entries */
167         int                     error;          /* error return value */
168         int                     logflags;       /* inode logging flags */
169         xfs_mount_t             *mp;            /* filesystem mount point */
170         char                    *ptr;           /* current data pointer */
171         xfs_dir2_sf_entry_t     *sfep;          /* shortform entry */
172         xfs_dir2_sf_hdr_t       *sfp;           /* shortform directory header */
173         xfs_dir2_sf_hdr_t       *dst;           /* temporary data buffer */
174
175         trace_xfs_dir2_block_to_sf(args);
176
177         dp = args->dp;
178         mp = dp->i_mount;
179
180         /*
181          * allocate a temporary destination buffer the size of the inode
182          * to format the data into. Once we have formatted the data, we
183          * can free the block and copy the formatted data into the inode literal
184          * area.
185          */
186         dst = kmem_alloc(mp->m_sb.sb_inodesize, KM_SLEEP);
187         hdr = bp->b_addr;
188
189         /*
190          * Copy the header into the newly allocate local space.
191          */
192         sfp = (xfs_dir2_sf_hdr_t *)dst;
193         memcpy(sfp, sfhp, xfs_dir2_sf_hdr_size(sfhp->i8count));
194
195         /*
196          * Set up to loop over the block's entries.
197          */
198         btp = xfs_dir2_block_tail_p(args->geo, hdr);
199         ptr = (char *)dp->d_ops->data_entry_p(hdr);
200         endptr = (char *)xfs_dir2_block_leaf_p(btp);
201         sfep = xfs_dir2_sf_firstentry(sfp);
202         /*
203          * Loop over the active and unused entries.
204          * Stop when we reach the leaf/tail portion of the block.
205          */
206         while (ptr < endptr) {
207                 /*
208                  * If it's unused, just skip over it.
209                  */
210                 dup = (xfs_dir2_data_unused_t *)ptr;
211                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
212                         ptr += be16_to_cpu(dup->length);
213                         continue;
214                 }
215                 dep = (xfs_dir2_data_entry_t *)ptr;
216                 /*
217                  * Skip .
218                  */
219                 if (dep->namelen == 1 && dep->name[0] == '.')
220                         ASSERT(be64_to_cpu(dep->inumber) == dp->i_ino);
221                 /*
222                  * Skip .., but make sure the inode number is right.
223                  */
224                 else if (dep->namelen == 2 &&
225                          dep->name[0] == '.' && dep->name[1] == '.')
226                         ASSERT(be64_to_cpu(dep->inumber) ==
227                                dp->d_ops->sf_get_parent_ino(sfp));
228                 /*
229                  * Normal entry, copy it into shortform.
230                  */
231                 else {
232                         sfep->namelen = dep->namelen;
233                         xfs_dir2_sf_put_offset(sfep,
234                                 (xfs_dir2_data_aoff_t)
235                                 ((char *)dep - (char *)hdr));
236                         memcpy(sfep->name, dep->name, dep->namelen);
237                         dp->d_ops->sf_put_ino(sfp, sfep,
238                                               be64_to_cpu(dep->inumber));
239                         dp->d_ops->sf_put_ftype(sfep,
240                                         dp->d_ops->data_get_ftype(dep));
241
242                         sfep = dp->d_ops->sf_nextentry(sfp, sfep);
243                 }
244                 ptr += dp->d_ops->data_entsize(dep->namelen);
245         }
246         ASSERT((char *)sfep - (char *)sfp == size);
247
248         /* now we are done with the block, we can shrink the inode */
249         logflags = XFS_ILOG_CORE;
250         error = xfs_dir2_shrink_inode(args, args->geo->datablk, bp);
251         if (error) {
252                 ASSERT(error != -ENOSPC);
253                 goto out;
254         }
255
256         /*
257          * The buffer is now unconditionally gone, whether
258          * xfs_dir2_shrink_inode worked or not.
259          *
260          * Convert the inode to local format and copy the data in.
261          */
262         dp->i_df.if_flags &= ~XFS_IFEXTENTS;
263         dp->i_df.if_flags |= XFS_IFINLINE;
264         dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
265         ASSERT(dp->i_df.if_bytes == 0);
266         xfs_idata_realloc(dp, size, XFS_DATA_FORK);
267
268         logflags |= XFS_ILOG_DDATA;
269         memcpy(dp->i_df.if_u1.if_data, dst, size);
270         dp->i_d.di_size = size;
271         xfs_dir2_sf_check(args);
272 out:
273         xfs_trans_log_inode(args->trans, dp, logflags);
274         kmem_free(dst);
275         return error;
276 }
277
278 /*
279  * Add a name to a shortform directory.
280  * There are two algorithms, "easy" and "hard" which we decide on
281  * before changing anything.
282  * Convert to block form if necessary, if the new entry won't fit.
283  */
284 int                                             /* error */
285 xfs_dir2_sf_addname(
286         xfs_da_args_t           *args)          /* operation arguments */
287 {
288         xfs_inode_t             *dp;            /* incore directory inode */
289         int                     error;          /* error return value */
290         int                     incr_isize;     /* total change in size */
291         int                     new_isize;      /* di_size after adding name */
292         int                     objchange;      /* changing to 8-byte inodes */
293         xfs_dir2_data_aoff_t    offset = 0;     /* offset for new entry */
294         int                     pick;           /* which algorithm to use */
295         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
296         xfs_dir2_sf_entry_t     *sfep = NULL;   /* shortform entry */
297
298         trace_xfs_dir2_sf_addname(args);
299
300         ASSERT(xfs_dir2_sf_lookup(args) == -ENOENT);
301         dp = args->dp;
302         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
303         /*
304          * Make sure the shortform value has some of its header.
305          */
306         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
307                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
308                 return -EIO;
309         }
310         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
311         ASSERT(dp->i_df.if_u1.if_data != NULL);
312         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
313         ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
314         /*
315          * Compute entry (and change in) size.
316          */
317         incr_isize = dp->d_ops->sf_entsize(sfp, args->namelen);
318         objchange = 0;
319
320         /*
321          * Do we have to change to 8 byte inodes?
322          */
323         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
324                 /*
325                  * Yes, adjust the inode size.  old count + (parent + new)
326                  */
327                 incr_isize +=
328                         (sfp->count + 2) *
329                         ((uint)sizeof(xfs_dir2_ino8_t) -
330                          (uint)sizeof(xfs_dir2_ino4_t));
331                 objchange = 1;
332         }
333
334         new_isize = (int)dp->i_d.di_size + incr_isize;
335         /*
336          * Won't fit as shortform any more (due to size),
337          * or the pick routine says it won't (due to offset values).
338          */
339         if (new_isize > XFS_IFORK_DSIZE(dp) ||
340             (pick =
341              xfs_dir2_sf_addname_pick(args, objchange, &sfep, &offset)) == 0) {
342                 /*
343                  * Just checking or no space reservation, it doesn't fit.
344                  */
345                 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
346                         return -ENOSPC;
347                 /*
348                  * Convert to block form then add the name.
349                  */
350                 error = xfs_dir2_sf_to_block(args);
351                 if (error)
352                         return error;
353                 return xfs_dir2_block_addname(args);
354         }
355         /*
356          * Just checking, it fits.
357          */
358         if (args->op_flags & XFS_DA_OP_JUSTCHECK)
359                 return 0;
360         /*
361          * Do it the easy way - just add it at the end.
362          */
363         if (pick == 1)
364                 xfs_dir2_sf_addname_easy(args, sfep, offset, new_isize);
365         /*
366          * Do it the hard way - look for a place to insert the new entry.
367          * Convert to 8 byte inode numbers first if necessary.
368          */
369         else {
370                 ASSERT(pick == 2);
371                 if (objchange)
372                         xfs_dir2_sf_toino8(args);
373                 xfs_dir2_sf_addname_hard(args, objchange, new_isize);
374         }
375         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
376         return 0;
377 }
378
379 /*
380  * Add the new entry the "easy" way.
381  * This is copying the old directory and adding the new entry at the end.
382  * Since it's sorted by "offset" we need room after the last offset
383  * that's already there, and then room to convert to a block directory.
384  * This is already checked by the pick routine.
385  */
386 static void
387 xfs_dir2_sf_addname_easy(
388         xfs_da_args_t           *args,          /* operation arguments */
389         xfs_dir2_sf_entry_t     *sfep,          /* pointer to new entry */
390         xfs_dir2_data_aoff_t    offset,         /* offset to use for new ent */
391         int                     new_isize)      /* new directory size */
392 {
393         int                     byteoff;        /* byte offset in sf dir */
394         xfs_inode_t             *dp;            /* incore directory inode */
395         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
396
397         dp = args->dp;
398
399         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
400         byteoff = (int)((char *)sfep - (char *)sfp);
401         /*
402          * Grow the in-inode space.
403          */
404         xfs_idata_realloc(dp, dp->d_ops->sf_entsize(sfp, args->namelen),
405                           XFS_DATA_FORK);
406         /*
407          * Need to set up again due to realloc of the inode data.
408          */
409         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
410         sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + byteoff);
411         /*
412          * Fill in the new entry.
413          */
414         sfep->namelen = args->namelen;
415         xfs_dir2_sf_put_offset(sfep, offset);
416         memcpy(sfep->name, args->name, sfep->namelen);
417         dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
418         dp->d_ops->sf_put_ftype(sfep, args->filetype);
419
420         /*
421          * Update the header and inode.
422          */
423         sfp->count++;
424         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM)
425                 sfp->i8count++;
426         dp->i_d.di_size = new_isize;
427         xfs_dir2_sf_check(args);
428 }
429
430 /*
431  * Add the new entry the "hard" way.
432  * The caller has already converted to 8 byte inode numbers if necessary,
433  * in which case we need to leave the i8count at 1.
434  * Find a hole that the new entry will fit into, and copy
435  * the first part of the entries, the new entry, and the last part of
436  * the entries.
437  */
438 /* ARGSUSED */
439 static void
440 xfs_dir2_sf_addname_hard(
441         xfs_da_args_t           *args,          /* operation arguments */
442         int                     objchange,      /* changing inode number size */
443         int                     new_isize)      /* new directory size */
444 {
445         int                     add_datasize;   /* data size need for new ent */
446         char                    *buf;           /* buffer for old */
447         xfs_inode_t             *dp;            /* incore directory inode */
448         int                     eof;            /* reached end of old dir */
449         int                     nbytes;         /* temp for byte copies */
450         xfs_dir2_data_aoff_t    new_offset;     /* next offset value */
451         xfs_dir2_data_aoff_t    offset;         /* current offset value */
452         int                     old_isize;      /* previous di_size */
453         xfs_dir2_sf_entry_t     *oldsfep;       /* entry in original dir */
454         xfs_dir2_sf_hdr_t       *oldsfp;        /* original shortform dir */
455         xfs_dir2_sf_entry_t     *sfep;          /* entry in new dir */
456         xfs_dir2_sf_hdr_t       *sfp;           /* new shortform dir */
457         struct xfs_mount        *mp;
458
459         /*
460          * Copy the old directory to the stack buffer.
461          */
462         dp = args->dp;
463         mp = dp->i_mount;
464
465         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
466         old_isize = (int)dp->i_d.di_size;
467         buf = kmem_alloc(old_isize, KM_SLEEP);
468         oldsfp = (xfs_dir2_sf_hdr_t *)buf;
469         memcpy(oldsfp, sfp, old_isize);
470         /*
471          * Loop over the old directory finding the place we're going
472          * to insert the new entry.
473          * If it's going to end up at the end then oldsfep will point there.
474          */
475         for (offset = dp->d_ops->data_first_offset,
476               oldsfep = xfs_dir2_sf_firstentry(oldsfp),
477               add_datasize = dp->d_ops->data_entsize(args->namelen),
478               eof = (char *)oldsfep == &buf[old_isize];
479              !eof;
480              offset = new_offset + dp->d_ops->data_entsize(oldsfep->namelen),
481               oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep),
482               eof = (char *)oldsfep == &buf[old_isize]) {
483                 new_offset = xfs_dir2_sf_get_offset(oldsfep);
484                 if (offset + add_datasize <= new_offset)
485                         break;
486         }
487         /*
488          * Get rid of the old directory, then allocate space for
489          * the new one.  We do this so xfs_idata_realloc won't copy
490          * the data.
491          */
492         xfs_idata_realloc(dp, -old_isize, XFS_DATA_FORK);
493         xfs_idata_realloc(dp, new_isize, XFS_DATA_FORK);
494         /*
495          * Reset the pointer since the buffer was reallocated.
496          */
497         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
498         /*
499          * Copy the first part of the directory, including the header.
500          */
501         nbytes = (int)((char *)oldsfep - (char *)oldsfp);
502         memcpy(sfp, oldsfp, nbytes);
503         sfep = (xfs_dir2_sf_entry_t *)((char *)sfp + nbytes);
504         /*
505          * Fill in the new entry, and update the header counts.
506          */
507         sfep->namelen = args->namelen;
508         xfs_dir2_sf_put_offset(sfep, offset);
509         memcpy(sfep->name, args->name, sfep->namelen);
510         dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
511         dp->d_ops->sf_put_ftype(sfep, args->filetype);
512         sfp->count++;
513         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && !objchange)
514                 sfp->i8count++;
515         /*
516          * If there's more left to copy, do that.
517          */
518         if (!eof) {
519                 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
520                 memcpy(sfep, oldsfep, old_isize - nbytes);
521         }
522         kmem_free(buf);
523         dp->i_d.di_size = new_isize;
524         xfs_dir2_sf_check(args);
525 }
526
527 /*
528  * Decide if the new entry will fit at all.
529  * If it will fit, pick between adding the new entry to the end (easy)
530  * or somewhere else (hard).
531  * Return 0 (won't fit), 1 (easy), 2 (hard).
532  */
533 /*ARGSUSED*/
534 static int                                      /* pick result */
535 xfs_dir2_sf_addname_pick(
536         xfs_da_args_t           *args,          /* operation arguments */
537         int                     objchange,      /* inode # size changes */
538         xfs_dir2_sf_entry_t     **sfepp,        /* out(1): new entry ptr */
539         xfs_dir2_data_aoff_t    *offsetp)       /* out(1): new offset */
540 {
541         xfs_inode_t             *dp;            /* incore directory inode */
542         int                     holefit;        /* found hole it will fit in */
543         int                     i;              /* entry number */
544         xfs_mount_t             *mp;            /* filesystem mount point */
545         xfs_dir2_data_aoff_t    offset;         /* data block offset */
546         xfs_dir2_sf_entry_t     *sfep;          /* shortform entry */
547         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
548         int                     size;           /* entry's data size */
549         int                     used;           /* data bytes used */
550
551         dp = args->dp;
552         mp = dp->i_mount;
553
554         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
555         size = dp->d_ops->data_entsize(args->namelen);
556         offset = dp->d_ops->data_first_offset;
557         sfep = xfs_dir2_sf_firstentry(sfp);
558         holefit = 0;
559         /*
560          * Loop over sf entries.
561          * Keep track of data offset and whether we've seen a place
562          * to insert the new entry.
563          */
564         for (i = 0; i < sfp->count; i++) {
565                 if (!holefit)
566                         holefit = offset + size <= xfs_dir2_sf_get_offset(sfep);
567                 offset = xfs_dir2_sf_get_offset(sfep) +
568                          dp->d_ops->data_entsize(sfep->namelen);
569                 sfep = dp->d_ops->sf_nextentry(sfp, sfep);
570         }
571         /*
572          * Calculate data bytes used excluding the new entry, if this
573          * was a data block (block form directory).
574          */
575         used = offset +
576                (sfp->count + 3) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
577                (uint)sizeof(xfs_dir2_block_tail_t);
578         /*
579          * If it won't fit in a block form then we can't insert it,
580          * we'll go back, convert to block, then try the insert and convert
581          * to leaf.
582          */
583         if (used + (holefit ? 0 : size) > args->geo->blksize)
584                 return 0;
585         /*
586          * If changing the inode number size, do it the hard way.
587          */
588         if (objchange)
589                 return 2;
590         /*
591          * If it won't fit at the end then do it the hard way (use the hole).
592          */
593         if (used + size > args->geo->blksize)
594                 return 2;
595         /*
596          * Do it the easy way.
597          */
598         *sfepp = sfep;
599         *offsetp = offset;
600         return 1;
601 }
602
603 #ifdef DEBUG
604 /*
605  * Check consistency of shortform directory, assert if bad.
606  */
607 static void
608 xfs_dir2_sf_check(
609         xfs_da_args_t           *args)          /* operation arguments */
610 {
611         xfs_inode_t             *dp;            /* incore directory inode */
612         int                     i;              /* entry number */
613         int                     i8count;        /* number of big inode#s */
614         xfs_ino_t               ino;            /* entry inode number */
615         int                     offset;         /* data offset */
616         xfs_dir2_sf_entry_t     *sfep;          /* shortform dir entry */
617         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
618         struct xfs_mount        *mp;
619
620         dp = args->dp;
621         mp = dp->i_mount;
622
623         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
624         offset = dp->d_ops->data_first_offset;
625         ino = dp->d_ops->sf_get_parent_ino(sfp);
626         i8count = ino > XFS_DIR2_MAX_SHORT_INUM;
627
628         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp);
629              i < sfp->count;
630              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
631                 ASSERT(xfs_dir2_sf_get_offset(sfep) >= offset);
632                 ino = dp->d_ops->sf_get_ino(sfp, sfep);
633                 i8count += ino > XFS_DIR2_MAX_SHORT_INUM;
634                 offset =
635                         xfs_dir2_sf_get_offset(sfep) +
636                         dp->d_ops->data_entsize(sfep->namelen);
637                 ASSERT(dp->d_ops->sf_get_ftype(sfep) < XFS_DIR3_FT_MAX);
638         }
639         ASSERT(i8count == sfp->i8count);
640         ASSERT((char *)sfep - (char *)sfp == dp->i_d.di_size);
641         ASSERT(offset +
642                (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t) +
643                (uint)sizeof(xfs_dir2_block_tail_t) <= args->geo->blksize);
644 }
645 #endif  /* DEBUG */
646
647 /*
648  * Create a new (shortform) directory.
649  */
650 int                                     /* error, always 0 */
651 xfs_dir2_sf_create(
652         xfs_da_args_t   *args,          /* operation arguments */
653         xfs_ino_t       pino)           /* parent inode number */
654 {
655         xfs_inode_t     *dp;            /* incore directory inode */
656         int             i8count;        /* parent inode is an 8-byte number */
657         xfs_dir2_sf_hdr_t *sfp;         /* shortform structure */
658         int             size;           /* directory size */
659
660         trace_xfs_dir2_sf_create(args);
661
662         dp = args->dp;
663
664         ASSERT(dp != NULL);
665         ASSERT(dp->i_d.di_size == 0);
666         /*
667          * If it's currently a zero-length extent file,
668          * convert it to local format.
669          */
670         if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
671                 dp->i_df.if_flags &= ~XFS_IFEXTENTS;    /* just in case */
672                 dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
673                 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
674                 dp->i_df.if_flags |= XFS_IFINLINE;
675         }
676         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
677         ASSERT(dp->i_df.if_bytes == 0);
678         i8count = pino > XFS_DIR2_MAX_SHORT_INUM;
679         size = xfs_dir2_sf_hdr_size(i8count);
680         /*
681          * Make a buffer for the data.
682          */
683         xfs_idata_realloc(dp, size, XFS_DATA_FORK);
684         /*
685          * Fill in the header,
686          */
687         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
688         sfp->i8count = i8count;
689         /*
690          * Now can put in the inode number, since i8count is set.
691          */
692         dp->d_ops->sf_put_parent_ino(sfp, pino);
693         sfp->count = 0;
694         dp->i_d.di_size = size;
695         xfs_dir2_sf_check(args);
696         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
697         return 0;
698 }
699
700 /*
701  * Lookup an entry in a shortform directory.
702  * Returns EEXIST if found, ENOENT if not found.
703  */
704 int                                             /* error */
705 xfs_dir2_sf_lookup(
706         xfs_da_args_t           *args)          /* operation arguments */
707 {
708         xfs_inode_t             *dp;            /* incore directory inode */
709         int                     i;              /* entry index */
710         int                     error;
711         xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
712         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
713         enum xfs_dacmp          cmp;            /* comparison result */
714         xfs_dir2_sf_entry_t     *ci_sfep;       /* case-insens. entry */
715
716         trace_xfs_dir2_sf_lookup(args);
717
718         xfs_dir2_sf_check(args);
719         dp = args->dp;
720
721         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
722         /*
723          * Bail out if the directory is way too short.
724          */
725         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
726                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
727                 return -EIO;
728         }
729         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
730         ASSERT(dp->i_df.if_u1.if_data != NULL);
731         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
732         ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
733         /*
734          * Special case for .
735          */
736         if (args->namelen == 1 && args->name[0] == '.') {
737                 args->inumber = dp->i_ino;
738                 args->cmpresult = XFS_CMP_EXACT;
739                 args->filetype = XFS_DIR3_FT_DIR;
740                 return -EEXIST;
741         }
742         /*
743          * Special case for ..
744          */
745         if (args->namelen == 2 &&
746             args->name[0] == '.' && args->name[1] == '.') {
747                 args->inumber = dp->d_ops->sf_get_parent_ino(sfp);
748                 args->cmpresult = XFS_CMP_EXACT;
749                 args->filetype = XFS_DIR3_FT_DIR;
750                 return -EEXIST;
751         }
752         /*
753          * Loop over all the entries trying to match ours.
754          */
755         ci_sfep = NULL;
756         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
757              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
758                 /*
759                  * Compare name and if it's an exact match, return the inode
760                  * number. If it's the first case-insensitive match, store the
761                  * inode number and continue looking for an exact match.
762                  */
763                 cmp = dp->i_mount->m_dirnameops->compname(args, sfep->name,
764                                                                 sfep->namelen);
765                 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
766                         args->cmpresult = cmp;
767                         args->inumber = dp->d_ops->sf_get_ino(sfp, sfep);
768                         args->filetype = dp->d_ops->sf_get_ftype(sfep);
769                         if (cmp == XFS_CMP_EXACT)
770                                 return -EEXIST;
771                         ci_sfep = sfep;
772                 }
773         }
774         ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
775         /*
776          * Here, we can only be doing a lookup (not a rename or replace).
777          * If a case-insensitive match was not found, return -ENOENT.
778          */
779         if (!ci_sfep)
780                 return -ENOENT;
781         /* otherwise process the CI match as required by the caller */
782         error = xfs_dir_cilookup_result(args, ci_sfep->name, ci_sfep->namelen);
783         return error;
784 }
785
786 /*
787  * Remove an entry from a shortform directory.
788  */
789 int                                             /* error */
790 xfs_dir2_sf_removename(
791         xfs_da_args_t           *args)
792 {
793         int                     byteoff;        /* offset of removed entry */
794         xfs_inode_t             *dp;            /* incore directory inode */
795         int                     entsize;        /* this entry's size */
796         int                     i;              /* shortform entry index */
797         int                     newsize;        /* new inode size */
798         int                     oldsize;        /* old inode size */
799         xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
800         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
801
802         trace_xfs_dir2_sf_removename(args);
803
804         dp = args->dp;
805
806         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
807         oldsize = (int)dp->i_d.di_size;
808         /*
809          * Bail out if the directory is way too short.
810          */
811         if (oldsize < offsetof(xfs_dir2_sf_hdr_t, parent)) {
812                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
813                 return -EIO;
814         }
815         ASSERT(dp->i_df.if_bytes == oldsize);
816         ASSERT(dp->i_df.if_u1.if_data != NULL);
817         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
818         ASSERT(oldsize >= xfs_dir2_sf_hdr_size(sfp->i8count));
819         /*
820          * Loop over the old directory entries.
821          * Find the one we're deleting.
822          */
823         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
824              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
825                 if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
826                                                                 XFS_CMP_EXACT) {
827                         ASSERT(dp->d_ops->sf_get_ino(sfp, sfep) ==
828                                args->inumber);
829                         break;
830                 }
831         }
832         /*
833          * Didn't find it.
834          */
835         if (i == sfp->count)
836                 return -ENOENT;
837         /*
838          * Calculate sizes.
839          */
840         byteoff = (int)((char *)sfep - (char *)sfp);
841         entsize = dp->d_ops->sf_entsize(sfp, args->namelen);
842         newsize = oldsize - entsize;
843         /*
844          * Copy the part if any after the removed entry, sliding it down.
845          */
846         if (byteoff + entsize < oldsize)
847                 memmove((char *)sfp + byteoff, (char *)sfp + byteoff + entsize,
848                         oldsize - (byteoff + entsize));
849         /*
850          * Fix up the header and file size.
851          */
852         sfp->count--;
853         dp->i_d.di_size = newsize;
854         /*
855          * Reallocate, making it smaller.
856          */
857         xfs_idata_realloc(dp, newsize - oldsize, XFS_DATA_FORK);
858         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
859         /*
860          * Are we changing inode number size?
861          */
862         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
863                 if (sfp->i8count == 1)
864                         xfs_dir2_sf_toino4(args);
865                 else
866                         sfp->i8count--;
867         }
868         xfs_dir2_sf_check(args);
869         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
870         return 0;
871 }
872
873 /*
874  * Replace the inode number of an entry in a shortform directory.
875  */
876 int                                             /* error */
877 xfs_dir2_sf_replace(
878         xfs_da_args_t           *args)          /* operation arguments */
879 {
880         xfs_inode_t             *dp;            /* incore directory inode */
881         int                     i;              /* entry index */
882         xfs_ino_t               ino=0;          /* entry old inode number */
883         int                     i8elevated;     /* sf_toino8 set i8count=1 */
884         xfs_dir2_sf_entry_t     *sfep;          /* shortform directory entry */
885         xfs_dir2_sf_hdr_t       *sfp;           /* shortform structure */
886
887         trace_xfs_dir2_sf_replace(args);
888
889         dp = args->dp;
890
891         ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
892         /*
893          * Bail out if the shortform directory is way too small.
894          */
895         if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) {
896                 ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
897                 return -EIO;
898         }
899         ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
900         ASSERT(dp->i_df.if_u1.if_data != NULL);
901         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
902         ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(sfp->i8count));
903
904         /*
905          * New inode number is large, and need to convert to 8-byte inodes.
906          */
907         if (args->inumber > XFS_DIR2_MAX_SHORT_INUM && sfp->i8count == 0) {
908                 int     error;                  /* error return value */
909                 int     newsize;                /* new inode size */
910
911                 newsize =
912                         dp->i_df.if_bytes +
913                         (sfp->count + 1) *
914                         ((uint)sizeof(xfs_dir2_ino8_t) -
915                          (uint)sizeof(xfs_dir2_ino4_t));
916                 /*
917                  * Won't fit as shortform, convert to block then do replace.
918                  */
919                 if (newsize > XFS_IFORK_DSIZE(dp)) {
920                         error = xfs_dir2_sf_to_block(args);
921                         if (error) {
922                                 return error;
923                         }
924                         return xfs_dir2_block_replace(args);
925                 }
926                 /*
927                  * Still fits, convert to 8-byte now.
928                  */
929                 xfs_dir2_sf_toino8(args);
930                 i8elevated = 1;
931                 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
932         } else
933                 i8elevated = 0;
934
935         ASSERT(args->namelen != 1 || args->name[0] != '.');
936         /*
937          * Replace ..'s entry.
938          */
939         if (args->namelen == 2 &&
940             args->name[0] == '.' && args->name[1] == '.') {
941                 ino = dp->d_ops->sf_get_parent_ino(sfp);
942                 ASSERT(args->inumber != ino);
943                 dp->d_ops->sf_put_parent_ino(sfp, args->inumber);
944         }
945         /*
946          * Normal entry, look for the name.
947          */
948         else {
949                 for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp); i < sfp->count;
950                      i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep)) {
951                         if (xfs_da_compname(args, sfep->name, sfep->namelen) ==
952                                                                 XFS_CMP_EXACT) {
953                                 ino = dp->d_ops->sf_get_ino(sfp, sfep);
954                                 ASSERT(args->inumber != ino);
955                                 dp->d_ops->sf_put_ino(sfp, sfep, args->inumber);
956                                 dp->d_ops->sf_put_ftype(sfep, args->filetype);
957                                 break;
958                         }
959                 }
960                 /*
961                  * Didn't find it.
962                  */
963                 if (i == sfp->count) {
964                         ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
965                         if (i8elevated)
966                                 xfs_dir2_sf_toino4(args);
967                         return -ENOENT;
968                 }
969         }
970         /*
971          * See if the old number was large, the new number is small.
972          */
973         if (ino > XFS_DIR2_MAX_SHORT_INUM &&
974             args->inumber <= XFS_DIR2_MAX_SHORT_INUM) {
975                 /*
976                  * And the old count was one, so need to convert to small.
977                  */
978                 if (sfp->i8count == 1)
979                         xfs_dir2_sf_toino4(args);
980                 else
981                         sfp->i8count--;
982         }
983         /*
984          * See if the old number was small, the new number is large.
985          */
986         if (ino <= XFS_DIR2_MAX_SHORT_INUM &&
987             args->inumber > XFS_DIR2_MAX_SHORT_INUM) {
988                 /*
989                  * add to the i8count unless we just converted to 8-byte
990                  * inodes (which does an implied i8count = 1)
991                  */
992                 ASSERT(sfp->i8count != 0);
993                 if (!i8elevated)
994                         sfp->i8count++;
995         }
996         xfs_dir2_sf_check(args);
997         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
998         return 0;
999 }
1000
1001 /*
1002  * Convert from 8-byte inode numbers to 4-byte inode numbers.
1003  * The last 8-byte inode number is gone, but the count is still 1.
1004  */
1005 static void
1006 xfs_dir2_sf_toino4(
1007         xfs_da_args_t           *args)          /* operation arguments */
1008 {
1009         char                    *buf;           /* old dir's buffer */
1010         xfs_inode_t             *dp;            /* incore directory inode */
1011         int                     i;              /* entry index */
1012         int                     newsize;        /* new inode size */
1013         xfs_dir2_sf_entry_t     *oldsfep;       /* old sf entry */
1014         xfs_dir2_sf_hdr_t       *oldsfp;        /* old sf directory */
1015         int                     oldsize;        /* old inode size */
1016         xfs_dir2_sf_entry_t     *sfep;          /* new sf entry */
1017         xfs_dir2_sf_hdr_t       *sfp;           /* new sf directory */
1018         struct xfs_mount        *mp;
1019
1020         trace_xfs_dir2_sf_toino4(args);
1021
1022         dp = args->dp;
1023         mp = dp->i_mount;
1024
1025         /*
1026          * Copy the old directory to the buffer.
1027          * Then nuke it from the inode, and add the new buffer to the inode.
1028          * Don't want xfs_idata_realloc copying the data here.
1029          */
1030         oldsize = dp->i_df.if_bytes;
1031         buf = kmem_alloc(oldsize, KM_SLEEP);
1032         oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1033         ASSERT(oldsfp->i8count == 1);
1034         memcpy(buf, oldsfp, oldsize);
1035         /*
1036          * Compute the new inode size.
1037          */
1038         newsize =
1039                 oldsize -
1040                 (oldsfp->count + 1) *
1041                 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1042         xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1043         xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1044         /*
1045          * Reset our pointers, the data has moved.
1046          */
1047         oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1048         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1049         /*
1050          * Fill in the new header.
1051          */
1052         sfp->count = oldsfp->count;
1053         sfp->i8count = 0;
1054         dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1055         /*
1056          * Copy the entries field by field.
1057          */
1058         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1059                     oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1060              i < sfp->count;
1061              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1062                   oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1063                 sfep->namelen = oldsfep->namelen;
1064                 sfep->offset = oldsfep->offset;
1065                 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1066                 dp->d_ops->sf_put_ino(sfp, sfep,
1067                                       dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1068                 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1069         }
1070         /*
1071          * Clean up the inode.
1072          */
1073         kmem_free(buf);
1074         dp->i_d.di_size = newsize;
1075         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1076 }
1077
1078 /*
1079  * Convert existing entries from 4-byte inode numbers to 8-byte inode numbers.
1080  * The new entry w/ an 8-byte inode number is not there yet; we leave with
1081  * i8count set to 1, but no corresponding 8-byte entry.
1082  */
1083 static void
1084 xfs_dir2_sf_toino8(
1085         xfs_da_args_t           *args)          /* operation arguments */
1086 {
1087         char                    *buf;           /* old dir's buffer */
1088         xfs_inode_t             *dp;            /* incore directory inode */
1089         int                     i;              /* entry index */
1090         int                     newsize;        /* new inode size */
1091         xfs_dir2_sf_entry_t     *oldsfep;       /* old sf entry */
1092         xfs_dir2_sf_hdr_t       *oldsfp;        /* old sf directory */
1093         int                     oldsize;        /* old inode size */
1094         xfs_dir2_sf_entry_t     *sfep;          /* new sf entry */
1095         xfs_dir2_sf_hdr_t       *sfp;           /* new sf directory */
1096         struct xfs_mount        *mp;
1097
1098         trace_xfs_dir2_sf_toino8(args);
1099
1100         dp = args->dp;
1101         mp = dp->i_mount;
1102
1103         /*
1104          * Copy the old directory to the buffer.
1105          * Then nuke it from the inode, and add the new buffer to the inode.
1106          * Don't want xfs_idata_realloc copying the data here.
1107          */
1108         oldsize = dp->i_df.if_bytes;
1109         buf = kmem_alloc(oldsize, KM_SLEEP);
1110         oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1111         ASSERT(oldsfp->i8count == 0);
1112         memcpy(buf, oldsfp, oldsize);
1113         /*
1114          * Compute the new inode size (nb: entry count + 1 for parent)
1115          */
1116         newsize =
1117                 oldsize +
1118                 (oldsfp->count + 1) *
1119                 ((uint)sizeof(xfs_dir2_ino8_t) - (uint)sizeof(xfs_dir2_ino4_t));
1120         xfs_idata_realloc(dp, -oldsize, XFS_DATA_FORK);
1121         xfs_idata_realloc(dp, newsize, XFS_DATA_FORK);
1122         /*
1123          * Reset our pointers, the data has moved.
1124          */
1125         oldsfp = (xfs_dir2_sf_hdr_t *)buf;
1126         sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
1127         /*
1128          * Fill in the new header.
1129          */
1130         sfp->count = oldsfp->count;
1131         sfp->i8count = 1;
1132         dp->d_ops->sf_put_parent_ino(sfp, dp->d_ops->sf_get_parent_ino(oldsfp));
1133         /*
1134          * Copy the entries field by field.
1135          */
1136         for (i = 0, sfep = xfs_dir2_sf_firstentry(sfp),
1137                     oldsfep = xfs_dir2_sf_firstentry(oldsfp);
1138              i < sfp->count;
1139              i++, sfep = dp->d_ops->sf_nextentry(sfp, sfep),
1140                   oldsfep = dp->d_ops->sf_nextentry(oldsfp, oldsfep)) {
1141                 sfep->namelen = oldsfep->namelen;
1142                 sfep->offset = oldsfep->offset;
1143                 memcpy(sfep->name, oldsfep->name, sfep->namelen);
1144                 dp->d_ops->sf_put_ino(sfp, sfep,
1145                                       dp->d_ops->sf_get_ino(oldsfp, oldsfep));
1146                 dp->d_ops->sf_put_ftype(sfep, dp->d_ops->sf_get_ftype(oldsfep));
1147         }
1148         /*
1149          * Clean up the inode.
1150          */
1151         kmem_free(buf);
1152         dp->i_d.di_size = newsize;
1153         xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
1154 }