LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_dir2_sf.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 479 605 79.2 %
Date: 2023-07-31 20:08:34 Functions: 20 24 83.3 %

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

Generated by: LCOV version 1.14