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