xfs: create a hashname function for parent pointers
authorDarrick J. Wong <djwong@kernel.org>
Mon, 22 Apr 2024 16:47:46 +0000 (09:47 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 23 Apr 2024 14:46:58 +0000 (07:46 -0700)
Although directory entry and parent pointer recordsets look very similar
(name -> ino), there's one major difference between them: a file can be
hardlinked from multiple parent directories with the same filename.
This is common in shared container environments where a base directory
tree might be hardlink-copied multiple times.  IOWs the same 'ls'
program might be hardlinked to multiple /srv/*/bin/ls paths.

We don't want parent pointer operations to bog down on hash collisions
between the same dirent name, so create a special hash function that
mixes in the parent directory inode number.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
fs/xfs/libxfs/xfs_attr.c
fs/xfs/libxfs/xfs_parent.c
fs/xfs/libxfs/xfs_parent.h
fs/xfs/scrub/attr.c

index 93524efa6e56c1fc083438069d57672f7b623920..8c283e5c24702db4b8ac78f0bca5741a54023d6c 100644 (file)
@@ -437,6 +437,9 @@ xfs_attr_hashval(
 {
        ASSERT(xfs_attr_check_namespace(attr_flags));
 
+       if (attr_flags & XFS_ATTR_PARENT)
+               return xfs_parent_hashattr(mp, name, namelen, value, valuelen);
+
        return xfs_attr_hashname(name, namelen);
 }
 
index 5961fa8c85615dc393ef2faaa66879a33d09c329..d564baf2549c5c17a46d81a9c3ecd655e165ca92 100644 (file)
@@ -90,3 +90,50 @@ xfs_parent_valuecheck(
 
        return true;
 }
+
+/* Compute the attribute name hash for a parent pointer. */
+xfs_dahash_t
+xfs_parent_hashval(
+       struct xfs_mount                *mp,
+       const uint8_t                   *name,
+       int                             namelen,
+       xfs_ino_t                       parent_ino)
+{
+       struct xfs_name                 xname = {
+               .name                   = name,
+               .len                    = namelen,
+       };
+
+       /*
+        * Use the same dirent name hash as would be used on the directory, but
+        * mix in the parent inode number to avoid collisions on hardlinked
+        * files with identical names but different parents.
+        */
+       return xfs_dir2_hashname(mp, &xname) ^
+               upper_32_bits(parent_ino) ^ lower_32_bits(parent_ino);
+}
+
+/* Compute the attribute name hash from the xattr components. */
+xfs_dahash_t
+xfs_parent_hashattr(
+       struct xfs_mount                *mp,
+       const uint8_t                   *name,
+       int                             namelen,
+       const void                      *value,
+       int                             valuelen)
+{
+       const struct xfs_parent_rec     *rec = value;
+
+       /* Requires a local attr value in xfs_parent_rec format */
+       if (valuelen != sizeof(struct xfs_parent_rec)) {
+               ASSERT(valuelen == sizeof(struct xfs_parent_rec));
+               return 0;
+       }
+
+       if (!value) {
+               ASSERT(value != NULL);
+               return 0;
+       }
+
+       return xfs_parent_hashval(mp, name, namelen, be64_to_cpu(rec->p_ino));
+}
index ef8aff86078016019cbf8c7166fb370561bf710d..6a4028871b72a118dad5b9b87501219d8653e5af 100644 (file)
@@ -12,4 +12,9 @@ bool xfs_parent_namecheck(unsigned int attr_flags, const void *name,
 bool xfs_parent_valuecheck(struct xfs_mount *mp, const void *value,
                size_t valuelen);
 
+xfs_dahash_t xfs_parent_hashval(struct xfs_mount *mp, const uint8_t *name,
+               int namelen, xfs_ino_t parent_ino);
+xfs_dahash_t xfs_parent_hashattr(struct xfs_mount *mp, const uint8_t *name,
+               int namelen, const void *value, int valuelen);
+
 #endif /* __XFS_PARENT_H__ */
index 22d7ef4df1699d29387f713b92ac0889f0aa5a4e..c07d050b39b2eb04d09d1cab11c286622945c3ec 100644 (file)
@@ -536,6 +536,10 @@ xchk_xattr_rec(
                        xchk_da_set_corrupt(ds, level);
                        goto out;
                }
+               if (ent->flags & XFS_ATTR_PARENT) {
+                       xchk_da_set_corrupt(ds, level);
+                       goto out;
+               }
                calc_hash = xfs_attr_hashval(mp, ent->flags, rentry->name,
                                             rentry->namelen, NULL,
                                             be32_to_cpu(rentry->valuelen));