Skip to content

Commit 0e152be

Browse files
andrealmeidbrauner
authored andcommitted
libfs: Create the helper function generic_ci_validate_strict_name()
Create a helper function for filesystems do the checks required for casefold directories and strict encoding. Suggested-by: Gabriel Krisman Bertazi <[email protected]> Reviewed-by: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: André Almeida <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 42f7652 commit 0e152be

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

include/linux/fs.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <linux/slab.h>
4646
#include <linux/maple_tree.h>
4747
#include <linux/rw_hint.h>
48+
#include <linux/unicode.h>
4849

4950
#include <asm/byteorder.h>
5051
#include <uapi/linux/fs.h>
@@ -3456,6 +3457,50 @@ extern int generic_ci_match(const struct inode *parent,
34563457
const struct qstr *folded_name,
34573458
const u8 *de_name, u32 de_name_len);
34583459

3460+
#if IS_ENABLED(CONFIG_UNICODE)
3461+
/**
3462+
* generic_ci_validate_strict_name - Check if a given name is suitable
3463+
* for a directory
3464+
*
3465+
* This functions checks if the proposed filename is valid for the
3466+
* parent directory. That means that only valid UTF-8 filenames will be
3467+
* accepted for casefold directories from filesystems created with the
3468+
* strict encoding flag. That also means that any name will be
3469+
* accepted for directories that doesn't have casefold enabled, or
3470+
* aren't being strict with the encoding.
3471+
*
3472+
* @dir: inode of the directory where the new file will be created
3473+
* @name: name of the new file
3474+
*
3475+
* Return:
3476+
* * True if the filename is suitable for this directory. It can be
3477+
* true if a given name is not suitable for a strict encoding
3478+
* directory, but the directory being used isn't strict
3479+
* * False if the filename isn't suitable for this directory. This only
3480+
* happens when a directory is casefolded and the filesystem is strict
3481+
* about its encoding.
3482+
*/
3483+
static inline bool generic_ci_validate_strict_name(struct inode *dir, struct qstr *name)
3484+
{
3485+
if (!IS_CASEFOLDED(dir) || !sb_has_strict_encoding(dir->i_sb))
3486+
return true;
3487+
3488+
/*
3489+
* A casefold dir must have a encoding set, unless the filesystem
3490+
* is corrupted
3491+
*/
3492+
if (WARN_ON_ONCE(!dir->i_sb->s_encoding))
3493+
return true;
3494+
3495+
return !utf8_validate(dir->i_sb->s_encoding, name);
3496+
}
3497+
#else
3498+
static inline bool generic_ci_validate_strict_name(struct inode *dir, struct qstr *name)
3499+
{
3500+
return true;
3501+
}
3502+
#endif
3503+
34593504
static inline bool sb_has_encoding(const struct super_block *sb)
34603505
{
34613506
#if IS_ENABLED(CONFIG_UNICODE)

0 commit comments

Comments
 (0)