]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
xfs: create a hashname function for parent pointers
authorDarrick J. Wong <djwong@kernel.org>
Wed, 27 Mar 2024 00:20:30 +0000 (17:20 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Wed, 10 Apr 2024 00:21:29 +0000 (17:21 -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>
libxfs/libxfs_priv.h
libxfs/xfs_attr.c
libxfs/xfs_parent.c
libxfs/xfs_parent.h

index 066c80c9a1122c06540887bec81dfdb2b343f1aa..5d4d46013eb080e0525696af96cd6653624bd7e2 100644 (file)
@@ -615,4 +615,22 @@ int xfs_bmap_last_extent(struct xfs_trans *tp, struct xfs_inode *ip,
 #define xfs_exchrange_possible(mp) \
        (xfs_has_bigtime(mp) || xfs_has_large_extent_counts(mp))
 
+/* linux/wordpart.h */
+
+/**
+ * upper_32_bits - return bits 32-63 of a number
+ * @n: the number we're accessing
+ *
+ * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
+ * the "right shift count >= width of type" warning when that quantity is
+ * 32-bits.
+ */
+#define upper_32_bits(n) ((uint32_t)(((n) >> 16) >> 16))
+
+/**
+ * lower_32_bits - return bits 0-31 of a number
+ * @n: the number we're accessing
+ */
+#define lower_32_bits(n) ((uint32_t)((n) & 0xffffffff))
+
 #endif /* __LIBXFS_INTERNAL_XFS_H__ */
index 87b7c7964ecf94f359364138debf7bb583004742..97c62ef734ec8bffa20aae66ab9c0ddcd678f501 100644 (file)
@@ -438,6 +438,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 c0ed7c52335c0b15f22425af0f796fb4c1e48613..a18c13954748e50680bd5e0aff12cfc2c30c4c57 100644 (file)
@@ -91,3 +91,52 @@ 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,
+       };
+       xfs_dahash_t                    ret;
+
+       /*
+        * Use the same dirent name hash as would be used on the directory, but
+        * mix in the parent inode number.
+        */
+       ret = xfs_dir2_hashname(mp, &xname);
+       ret ^= upper_32_bits(parent_ino);
+       ret ^= lower_32_bits(parent_ino);
+       return ret;
+}
+
+/* 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__ */