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