Skip to content

Commit 6a79a4e

Browse files
krisman-at-collaborabrauner
authored andcommitted
libfs: Introduce case-insensitive string comparison helper
generic_ci_match can be used by case-insensitive filesystems to compare strings under lookup with dirents in a case-insensitive way. This function is currently reimplemented by each filesystem supporting casefolding, so this reduces code duplication in filesystem-specific code. [[email protected]: rework to first test the exact match, cleanup and add error message] Signed-off-by: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: Eugen Hristev <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 632f405 commit 6a79a4e

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

fs/libfs.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,80 @@ static const struct dentry_operations generic_ci_dentry_ops = {
18541854
.d_revalidate = fscrypt_d_revalidate,
18551855
#endif
18561856
};
1857+
1858+
/**
1859+
* generic_ci_match() - Match a name (case-insensitively) with a dirent.
1860+
* This is a filesystem helper for comparison with directory entries.
1861+
* generic_ci_d_compare should be used in VFS' ->d_compare instead.
1862+
*
1863+
* @parent: Inode of the parent of the dirent under comparison
1864+
* @name: name under lookup.
1865+
* @folded_name: Optional pre-folded name under lookup
1866+
* @de_name: Dirent name.
1867+
* @de_name_len: dirent name length.
1868+
*
1869+
* Test whether a case-insensitive directory entry matches the filename
1870+
* being searched. If @folded_name is provided, it is used instead of
1871+
* recalculating the casefold of @name.
1872+
*
1873+
* Return: > 0 if the directory entry matches, 0 if it doesn't match, or
1874+
* < 0 on error.
1875+
*/
1876+
int generic_ci_match(const struct inode *parent,
1877+
const struct qstr *name,
1878+
const struct qstr *folded_name,
1879+
const u8 *de_name, u32 de_name_len)
1880+
{
1881+
const struct super_block *sb = parent->i_sb;
1882+
const struct unicode_map *um = sb->s_encoding;
1883+
struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
1884+
struct qstr dirent = QSTR_INIT(de_name, de_name_len);
1885+
int res = 0;
1886+
1887+
if (IS_ENCRYPTED(parent)) {
1888+
const struct fscrypt_str encrypted_name =
1889+
FSTR_INIT((u8 *) de_name, de_name_len);
1890+
1891+
if (WARN_ON_ONCE(!fscrypt_has_encryption_key(parent)))
1892+
return -EINVAL;
1893+
1894+
decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
1895+
if (!decrypted_name.name)
1896+
return -ENOMEM;
1897+
res = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
1898+
&decrypted_name);
1899+
if (res < 0) {
1900+
kfree(decrypted_name.name);
1901+
return res;
1902+
}
1903+
dirent.name = decrypted_name.name;
1904+
dirent.len = decrypted_name.len;
1905+
}
1906+
1907+
/*
1908+
* Attempt a case-sensitive match first. It is cheaper and
1909+
* should cover most lookups, including all the sane
1910+
* applications that expect a case-sensitive filesystem.
1911+
*/
1912+
1913+
if (dirent.len == name->len &&
1914+
!memcmp(name->name, dirent.name, dirent.len))
1915+
goto out;
1916+
1917+
if (folded_name->name)
1918+
res = utf8_strncasecmp_folded(um, folded_name, &dirent);
1919+
else
1920+
res = utf8_strncasecmp(um, name, &dirent);
1921+
1922+
out:
1923+
kfree(decrypted_name.name);
1924+
if (res < 0 && sb_has_strict_encoding(sb)) {
1925+
pr_err_ratelimited("Directory contains filename that is invalid UTF-8");
1926+
return 0;
1927+
}
1928+
return !res;
1929+
}
1930+
EXPORT_SYMBOL(generic_ci_match);
18571931
#endif
18581932

18591933
#ifdef CONFIG_FS_ENCRYPTION

include/linux/fs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,6 +3351,10 @@ extern int generic_file_fsync(struct file *, loff_t, loff_t, int);
33513351
extern int generic_check_addressable(unsigned, u64);
33523352

33533353
extern void generic_set_sb_d_ops(struct super_block *sb);
3354+
extern int generic_ci_match(const struct inode *parent,
3355+
const struct qstr *name,
3356+
const struct qstr *folded_name,
3357+
const u8 *de_name, u32 de_name_len);
33543358

33553359
static inline bool sb_has_encoding(const struct super_block *sb)
33563360
{

0 commit comments

Comments
 (0)