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