Skip to contentSkip to conversation

f2fs-tools: Add support for Casefolding

PATCH New Daniel Rosenberg via Linux-f2fs-devel 1 participant 1 message Jul 11, 2019 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs-tools: Add support for Casefolding

This adds support for f2fs casefolding. Similarly to ext4 casefolding, this is controlled per-folder via the +F attribute. It can be toggled on empty directories only. It is not currently compatible with encryption, but that will likely change.

When enabling the casefold feature, use the -C flag. The format is:
-C encoding[:flag1,flag2,etc]

---
 fsck/dir.c              |    9 +-
 fsck/f2fs.h             |    5 +
 fsck/fsck.c             |   42 +-
 fsck/fsck.h             |    8 +-
 fsck/main.c             |   22 +-
 fsck/mount.c            |   24 +-
 include/f2fs_fs.h       |   37 +-
 lib/Makefile.am         |    2 +-
 lib/libf2fs.c           |  112 +-
 lib/nls_utf8.c          |  936 +++++++++
 lib/utf8data.h          | 4109 +++++++++++++++++++++++++++++++++++++++
 mkfs/f2fs_format.c      |   11 +-
 mkfs/f2fs_format_main.c |   21 +-
 tools/fibmap.c          |    1 +
 14 files changed, 5302 insertions(+), 37 deletions(-)
 create mode 100644 lib/nls_utf8.c
 create mode 100644 lib/utf8data.h
fsck/dir.c +6 -3
index d20c18c..3a81ec2 100644
@@ -106,7 +106,7 @@ static struct f2fs_dir_entry *find_in_block(void *block,
 	return find_target_dentry(name, len, namehash, max_slots, &d);
 }
 
-static int find_in_level(struct f2fs_sb_info *sbi,struct f2fs_node *dir,
+static int find_in_level(struct f2fs_sb_info *sbi, struct f2fs_node *dir,
 		unsigned int level, struct dentry *de)
 {
 	unsigned int nbucket, nblock;
@@ -120,7 +120,8 @@ static int find_in_level(struct f2fs_sb_info *sbi,struct f2fs_node *dir,
 	unsigned int dir_level = dir->i.i_dir_level;
 	int ret = 0;
 
-	namehash = f2fs_dentry_hash(de->name, de->len);
+	namehash = f2fs_dentry_hash(get_encoding(sbi), IS_CASEFOLDED(&dir->i),
+					de->name, de->len);
 
 	nbucket = dir_buckets(level, dir_level);
 	nblock = bucket_blocks(level);
@@ -223,7 +224,9 @@ int f2fs_add_link(struct f2fs_sb_info *sbi, struct f2fs_node *parent,
 	int level = 0, current_depth, bit_pos;
 	int nbucket, nblock, bidx, block;
 	int slots = GET_DENTRY_SLOTS(name_len);
-	f2fs_hash_t dentry_hash = f2fs_dentry_hash(name, name_len);
+	f2fs_hash_t dentry_hash = f2fs_dentry_hash(get_encoding(sbi),
+						IS_CASEFOLDED(&parent->i),
+						name, name_len);
 	struct f2fs_dentry_block *dentry_blk;
 	struct f2fs_dentry_ptr d;
 	struct dnode_of_data dn;
fsck/f2fs.h +5 -0
index 4dc6698..3ea9250 100644
@@ -510,4 +510,9 @@ static inline int is_dot_dotdot(const unsigned char *name, const int len)
 	return 0;
 }
 
+static inline int get_encoding(struct f2fs_sb_info *sbi)
+{
+	return le16_to_cpu(F2FS_RAW_SUPER(sbi)->s_encoding);
+}
+
 #endif /* _F2FS_H_ */
fsck/fsck.c +23 -19

134 lines not on this page · raw message

fsck/fsck.h +4 -4
index f00036e..5498201 100644
@@ -148,10 +148,10 @@ extern int fsck_chk_idnode_blk(struct f2fs_sb_info *, struct f2fs_inode *,
 		enum FILE_TYPE, struct f2fs_node *, u32 *, struct child_info *);
 extern int fsck_chk_didnode_blk(struct f2fs_sb_info *, struct f2fs_inode *,
 		enum FILE_TYPE, struct f2fs_node *, u32 *, struct child_info *);
-extern int fsck_chk_data_blk(struct f2fs_sb_info *sbi, u32, struct child_info *,
-		int, enum FILE_TYPE, u32, u16, u8, int);
-extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, u32, struct child_info *,
-		int, int);
+extern int fsck_chk_data_blk(struct f2fs_sb_info *, int,
+		u32, struct child_info *, int, enum FILE_TYPE, u32, u16, u8, int);
+extern int fsck_chk_dentry_blk(struct f2fs_sb_info *, int,
+		u32, struct child_info *, int, int);
 int fsck_chk_inline_dentries(struct f2fs_sb_info *, struct f2fs_node *,
 		struct child_info *);
 void fsck_chk_checkpoint(struct f2fs_sb_info *sbi);
fsck/main.c +20 -2
index d844820..f522a43 100644
@@ -53,6 +53,7 @@ void fsck_usage()
 	MSG(0, "\nUsage: fsck.f2fs [options] device\n");
 	MSG(0, "[options]:\n");
 	MSG(0, "  -a check/fix potential corruption, reported by f2fs\n");
+	MSG(0, "  -C encoding[:flag1,flag2] Set options for enabling casefolding\n");
 	MSG(0, "  -d debug level [default:0]\n");
 	MSG(0, "  -f check/fix entire partition\n");
 	MSG(0, "  -g add default options\n");
@@ -185,8 +186,9 @@ void f2fs_parse_options(int argc, char *argv[])
 	}
 
 	if (!strcmp("fsck.f2fs", prog)) {
-		const char *option_string = ":ad:fg:O:p:q:StyV";
-		int opt = 0;
+		const char *option_string = ":aC:d:fg:O:p:q:StyV";
+		int opt = 0, val;
+		char *token;
 		struct option long_opt[] = {
 			{"dry-run", no_argument, 0, 1},
 			{0, 0, 0, 0}
@@ -277,6 +279,22 @@ void f2fs_parse_options(int argc, char *argv[])
 					break;
 				}
 				break;
+			case 'C':
+				token = strtok(optarg, ":");
+				val = f2fs_str2encoding(token);
+				if (val < 0) {
+					MSG(0, "\tError: Unknown encoding %s\n", token);
+					fsck_usage();
+				}
+				c.s_encoding = val;
+				token = strtok(NULL, "");
+				val = f2fs_str2encoding_flags(c.s_encoding, &token, &c.s_encoding_flags);
+				if (val) {
+					MSG(0, "\tError: Unknown flag %s\n", token);
+					fsck_usage();
+				}
+				c.feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
+				break;
 			case 'V':
 				show_version(prog);
 				exit(0);
fsck/mount.c +21 -3
index aecd0cd..3894a0d 100644
@@ -2606,24 +2606,41 @@ static int check_sector_size(struct f2fs_super_block *sb)
 	return 0;
 }
 
-static void tune_sb_features(struct f2fs_sb_info *sbi)
+static int tune_sb_features(struct f2fs_sb_info *sbi)
 {
 	int sb_changed = 0;
 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
 
+	if (c.feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT) &&
+		c.feature & cpu_to_le32(F2FS_FEATURE_CASEFOLD)) {
+		ERR_MSG("ERROR: Cannot set both encrypt and casefold. Skipping.\n");
+		return -1;
+	}
+
 	if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) &&
 			c.feature & cpu_to_le32(F2FS_FEATURE_ENCRYPT)) {
 		sb->feature |= cpu_to_le32(F2FS_FEATURE_ENCRYPT);
 		MSG(0, "Info: Set Encryption feature\n");
 		sb_changed = 1;
 	}
+	if (!(sb->feature & cpu_to_le32(F2FS_FEATURE_CASEFOLD)) &&
+		c.feature & cpu_to_le32(F2FS_FEATURE_CASEFOLD)) {
+		if (!c.s_encoding) {
+			ERR_MSG("ERROR: Must specify encoding to enable casefolding.\n");
+			return -1;
+		}
+		sb->feature |= cpu_to_le32(F2FS_FEATURE_CASEFOLD);
+		MSG(0, "Info: Set Casefold feature\n");
+		sb_changed = 1;
+	}
 	/* TODO: quota needs to allocate inode numbers */
 
 	c.feature = sb->feature;
 	if (!sb_changed)
-		return;
+		return 0;
 
 	update_superblock(sb, SB_MASK_ALL);
+	return 0;
 }
 
 int f2fs_do_mount(struct f2fs_sb_info *sbi)
@@ -2683,7 +2700,8 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi)
 
 	c.bug_on = 0;
 
-	tune_sb_features(sbi);
+	if (tune_sb_features(sbi))
+		return -1;
 
 	/* precompute checksum seed for metadata */
 	if (c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM))
include/f2fs_fs.h +35 -2
index a347033..c2ded47 100644
@@ -356,6 +356,8 @@ struct f2fs_configuration {
 	__u8 sb_version[VERSION_LEN + 1];
 	__u8 version[VERSION_LEN + 1];
 	char *vol_label;
+	u_int16_t s_encoding;
+	u_int16_t s_encoding_flags;
 	int heap;
 	int32_t kd;
 	int32_t dump_fd;
@@ -563,6 +565,9 @@ enum {
 
 #define FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
 
+#define F2FS_ENC_UTF8_12_1	1
+#define F2FS_ENC_STRICT_MODE_FL	(1 << 0)
+
 /* This flag is used by node and meta inodes, and by recovery */
 #define GFP_F2FS_ZERO	(GFP_NOFS | __GFP_ZERO)
 
@@ -588,6 +593,7 @@ enum {
 #define F2FS_FEATURE_LOST_FOUND		0x0200
 #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
 #define F2FS_FEATURE_SB_CHKSUM		0x0800
+#define F2FS_FEATURE_CASEFOLD		0x1000
 
 #define MAX_VOLUME_NAME		512
 
@@ -641,7 +647,9 @@ struct f2fs_super_block {
 	struct f2fs_device devs[MAX_DEVICES];	/* device list */
 	__le32 qf_ino[F2FS_MAX_QUOTAS];	/* quota inode numbers */
 	__u8 hot_ext_count;		/* # of hot file extension */
-	__u8 reserved[310];		/* valid reserved region */
+	__le16  s_encoding;		/* Filename charset encoding */
+	__le16  s_encoding_flags;	/* Filename charset encoding flags */
+	__u8 reserved[306];		/* valid reserved region */
 	__le32 crc;			/* checksum of superblock */
 } __attribute__((packed));
 
@@ -794,6 +802,9 @@ struct f2fs_extent {
 #define file_is_encrypt(fi)      ((fi)->i_advise & FADVISE_ENCRYPT_BIT)
 #define file_enc_name(fi)        ((fi)->i_advise & FADVISE_ENC_NAME_BIT)
 
+#define F2FS_CASEFOLD_FL	0x40000000 /* Casefolded file */
+#define IS_CASEFOLDED(dir)     ((dir)->i_flags & F2FS_CASEFOLD_FL)
+
 struct f2fs_inode {
 	__le16 i_mode;			/* file mode */
 	__u8 i_advise;			/* file hints */
@@ -1184,7 +1195,7 @@ extern int dev_reada_block(__u64);
 extern int dev_read_version(void *, __u64, size_t);
 extern void get_kernel_version(__u8 *);
 extern void get_kernel_uname_version(__u8 *);
-f2fs_hash_t f2fs_dentry_hash(const unsigned char *, int);
+f2fs_hash_t f2fs_dentry_hash(int, int, const unsigned char *, int);
 
 static inline bool f2fs_has_extra_isize(struct f2fs_inode *inode)
 {
@@ -1369,6 +1380,7 @@ struct feature feature_table[] = {					\
 	{ "lost_found",			F2FS_FEATURE_LOST_FOUND },	\
 	{ "verity",			F2FS_FEATURE_VERITY },	/* reserved */ \
 	{ "sb_checksum",		F2FS_FEATURE_SB_CHKSUM },	\
+	{ "casefold",			F2FS_FEATURE_CASEFOLD },	\
 	{ NULL,				0x0},				\
 };
 
@@ -1442,4 +1454,25 @@ static inline int parse_root_owner(char *ids,
 	return 0;
 }
 
+/*
+ * NLS definitions
+ */
+struct f2fs_nls_table {
+	int version;
+	const struct f2fs_nls_ops *ops;
+};
+
+struct f2fs_nls_ops {
+	int (*casefold)(const struct f2fs_nls_table *charset,
+			const unsigned char *str, size_t len,
+			unsigned char *dest, size_t dlen);
+};
+
+extern const struct f2fs_nls_table *f2fs_load_nls_table(int encoding);
+#define F2FS_ENC_UTF8_12_0	1
+
+extern int f2fs_str2encoding(const char *string);
+extern int f2fs_get_encoding_flags(int encoding);
+extern int f2fs_str2encoding_flags(int encoding, char **param, __u16 *flags);
+
 #endif	/*__F2FS_FS_H */
lib/Makefile.am +1 -1
index c1875a0..871d773 100644
@@ -2,7 +2,7 @@
 
 lib_LTLIBRARIES = libf2fs.la
 
-libf2fs_la_SOURCES = libf2fs.c libf2fs_io.c libf2fs_zoned.c
+libf2fs_la_SOURCES = libf2fs.c libf2fs_io.c libf2fs_zoned.c nls_utf8.c
 libf2fs_la_CFLAGS = -Wall
 libf2fs_la_CPPFLAGS = -I$(top_srcdir)/include
 libf2fs_la_LDFLAGS = -version-info $(LIBF2FS_CURRENT):$(LIBF2FS_REVISION):$(LIBF2FS_AGE)
lib/libf2fs.c +111 -1

133 lines not on this page · raw message

lib/nls_utf8.c new file +936 -0

936 lines not on this page · raw message

lib/utf8data.h new file +4109 -0

1,564 lines not on this page · raw message

mkfs/f2fs_format.c +10 -1
index bf4c6fe..ba903e6 100644

Lines of this file are not shown. View the raw message.

mkfs/f2fs_format_main.c +20 -1
index 9672fd2..4602077 100644

Lines of this file are not shown. View the raw message.

tools/fibmap.c +1 -0
index 9e96cb6..2b3c40f 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 8 lines
2.22.0.410.gd8fdbe21b5-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Part of the diff is not shown here. View the raw message.