libfs: Attempt exact-match comparison first during casefolded lookup
authorGabriel Krisman Bertazi <krisman@suse.de>
Wed, 24 Jan 2024 18:13:40 +0000 (15:13 -0300)
committerGabriel Krisman Bertazi <krisman@suse.de>
Tue, 27 Feb 2024 21:55:25 +0000 (16:55 -0500)
commit0906fbb2f7ad6ae4b1bee8777bf83171a12c10b0
tree3e95fa8918261c1a077a1bbd4dd47156508c7bf2
parent052d534373b7ed33712a63d5e17b2b6cdbce84fd
libfs: Attempt exact-match comparison first during casefolded lookup

Casefolded comparisons are (obviously) way more costly than a simple
memcmp.  Try the case-sensitive comparison first, falling-back to the
case-insensitive lookup only when needed.  This allows any exact-match
lookup to complete without having to walk the utf8 trie.

Note that, for strict mode, generic_ci_d_compare used to reject an
invalid UTF-8 string, which would now be considered valid if it
exact-matches the disk-name.  But, if that is the case, the filesystem
is corrupt.  More than that, it really doesn't matter in practice,
because the name-under-lookup will have already been rejected by
generic_ci_d_hash and we won't even get here.

The memcmp is safe under RCU because we are operating on str/len instead
of dentry->d_name directly, and the caller guarantees their consistency
between each other in __d_lookup_rcu_op_compare.

Link: https://lore.kernel.org/r/87ttn2sip7.fsf_-_@mailhost.krisman.be
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
fs/libfs.c