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