Skip to contentSkip to conversation

fs: common implementation of file type

RFC PATCH Superseded Phillip Potter 3 participants 4 messages Jan 21, 2019 – Jan 22, 2019 VFS

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v5 1/9 fs: common implementation of file type

Many file systems use a copy&paste implementation
of dirent to on-disk file type conversions.

Create a common implementation to be used by file systems with some useful conversion helpers to reduce open coded file type conversions in file system code.

---
 MAINTAINERS              |   1 +
 fs/Makefile              |   3 +-
 fs/fs_types.c            | 105 +++++++++++++++++++++++++++++++++++++++
 include/linux/fs.h       |  17 +------
 include/linux/fs_types.h |  75 ++++++++++++++++++++++++++++
 5 files changed, 184 insertions(+), 17 deletions(-)
 create mode 100644 fs/fs_types.c
 create mode 100644 include/linux/fs_types.h
MAINTAINERS +1 -0
index 4d04cebb4a71..f0f7d7750a38 100644
@@ -5882,6 +5882,7 @@ L:	[email protected]
 S:	Maintained
 F:	fs/*
 F:	include/linux/fs.h
+F:	include/linux/fs_types.h
 F:	include/uapi/linux/fs.h
 
 FINTEK F75375S HARDWARE MONITOR AND FAN CONTROLLER DRIVER
fs/Makefile +2 -1
index 293733f61594..23fcd8c164a3 100644
@@ -12,7 +12,8 @@ obj-y :=	open.o read_write.o file_table.o super.o \
 		attr.o bad_inode.o file.o filesystems.o namespace.o \
 		seq_file.o xattr.o libfs.o fs-writeback.o \
 		pnode.o splice.o sync.o utimes.o d_path.o \
-		stack.o fs_struct.o statfs.o fs_pin.o nsfs.o
+		stack.o fs_struct.o statfs.o fs_pin.o nsfs.o \
+		fs_types.o
 
 ifeq ($(CONFIG_BLOCK),y)
 obj-y +=	buffer.o block_dev.o direct-io.o mpage.o
fs/fs_types.c new file +105 -0

105 lines not on this page · raw message

include/linux/fs.h +1 -16
index 811c77743dad..92966678539d 100644
@@ -37,6 +37,7 @@
 #include <linux/uuid.h>
 #include <linux/errseq.h>
 #include <linux/ioprio.h>
+#include <linux/fs_types.h>
 
 #include <asm/byteorder.h>
 #include <uapi/linux/fs.h>
@@ -1699,22 +1700,6 @@ int fiemap_fill_next_extent(struct fiemap_extent_info *info, u64 logical,
 			    u64 phys, u64 len, u32 flags);
 int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags);
 
-/*
- * File types
- *
- * NOTE! These match bits 12..15 of stat.st_mode
- * (ie "(i_mode >> 12) & 15").
- */
-#define DT_UNKNOWN	0
-#define DT_FIFO		1
-#define DT_CHR		2
-#define DT_DIR		4
-#define DT_BLK		6
-#define DT_REG		8
-#define DT_LNK		10
-#define DT_SOCK		12
-#define DT_WHT		14
-
 /*
  * This is the "filldir" function type, used by readdir() to let
  * the kernel specify what kind of dirent layout it wants to have.
include/linux/fs_types.h new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

Hello Phillip,

Just minor nits.

Quoted 13 lines · Phillip Potter
fs/fs_types.c new file +7 -0
new file mode 100644
index 000000000000..6fc57f4b1dcb
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/fs.h>
+#include <linux/export.h>
+
+/*
+ * fs on-disk file type to dirent file type conversion
+ */

fs/fs_types.c line 17

Quoted 10 lines
fs/fs_types.c new file +10 -0
+static unsigned char fs_dtype_by_ftype[FT_MAX] = {
+	[FT_UNKNOWN]	= DT_UNKNOWN,
+	[FT_REG_FILE]	= DT_REG,
+	[FT_DIR]	= DT_DIR,
+	[FT_CHRDEV]	= DT_CHR,
+	[FT_BLKDEV]	= DT_BLK,
+	[FT_FIFO]	= DT_FIFO,
+	[FT_SOCK]	= DT_SOCK,
+	[FT_SYMLINK]	= DT_LNK
+};

This array should be const so that it ends up in .rodata.

fs/fs_types.c line 58

Quoted 13 lines
fs/fs_types.c new file +13 -0
+/*
+ * dirent file type to fs on-disk file type conversion
+ * Values not initialized explicitly are FT_UNKNOWN (0).
+ */
+static unsigned char fs_ftype_by_dtype[DT_MAX] = {
+	[DT_REG]	= FT_REG_FILE,
+	[DT_DIR]	= FT_DIR,
+	[DT_LNK]	= FT_SYMLINK,
+	[DT_CHR]	= FT_CHRDEV,
+	[DT_BLK]	= FT_BLKDEV,
+	[DT_FIFO]	= FT_FIFO,
+	[DT_SOCK]	= FT_SOCK,
+};

This array should be const so that it ends up in .rodata.

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
reviewer
#2 lore raw
Quoted 33 lines · Thiago Jung Bauermann

Hello Phillip,

Just minor nits.

Quoted 13 lines · Phillip Potter
fs/fs_types.c new file +7 -0
new file mode 100644
index 000000000000..6fc57f4b1dcb
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/fs.h>
+#include <linux/export.h>
+
+/*
+ * fs on-disk file type to dirent file type conversion
+ */

fs/fs_types.c line 17

Quoted 10 lines
fs/fs_types.c new file +10 -0
+static unsigned char fs_dtype_by_ftype[FT_MAX] = {
+	[FT_UNKNOWN]	= DT_UNKNOWN,
+	[FT_REG_FILE]	= DT_REG,
+	[FT_DIR]	= DT_DIR,
+	[FT_CHRDEV]	= DT_CHR,
+	[FT_BLKDEV]	= DT_BLK,
+	[FT_FIFO]	= DT_FIFO,
+	[FT_SOCK]	= DT_SOCK,
+	[FT_SYMLINK]	= DT_LNK
+};

This array should be const so that it ends up in .rodata.

Quoted 15 lines

fs/fs_types.c line 58

Quoted 13 lines
fs/fs_types.c new file +13 -0
+/*
+ * dirent file type to fs on-disk file type conversion
+ * Values not initialized explicitly are FT_UNKNOWN (0).
+ */
+static unsigned char fs_ftype_by_dtype[DT_MAX] = {
+	[DT_REG]	= FT_REG_FILE,
+	[DT_DIR]	= FT_DIR,
+	[DT_LNK]	= FT_SYMLINK,
+	[DT_CHR]	= FT_CHRDEV,
+	[DT_BLK]	= FT_BLKDEV,
+	[DT_FIFO]	= FT_FIFO,
+	[DT_SOCK]	= FT_SOCK,
+};

This array should be const so that it ends up in .rodata.

Good points! I can update this when pushing the patch to my tree. Thanks for your review.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw
In reply to Thiago Jung Bauermann
Quoted 33 lines · Thiago Jung Bauermann

Hello Phillip,

Just minor nits.

Quoted 13 lines · Phillip Potter
fs/fs_types.c new file +7 -0
new file mode 100644
index 000000000000..6fc57f4b1dcb
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/fs.h>
+#include <linux/export.h>
+
+/*
+ * fs on-disk file type to dirent file type conversion
+ */

fs/fs_types.c line 17

Quoted 10 lines
fs/fs_types.c new file +10 -0
+static unsigned char fs_dtype_by_ftype[FT_MAX] = {
+	[FT_UNKNOWN]	= DT_UNKNOWN,
+	[FT_REG_FILE]	= DT_REG,
+	[FT_DIR]	= DT_DIR,
+	[FT_CHRDEV]	= DT_CHR,
+	[FT_BLKDEV]	= DT_BLK,
+	[FT_FIFO]	= DT_FIFO,
+	[FT_SOCK]	= DT_SOCK,
+	[FT_SYMLINK]	= DT_LNK
+};

This array should be const so that it ends up in .rodata.

Quoted 20 lines

fs/fs_types.c line 58

Quoted 13 lines
fs/fs_types.c new file +13 -0
+/*
+ * dirent file type to fs on-disk file type conversion
+ * Values not initialized explicitly are FT_UNKNOWN (0).
+ */
+static unsigned char fs_ftype_by_dtype[DT_MAX] = {
+	[DT_REG]	= FT_REG_FILE,
+	[DT_DIR]	= FT_DIR,
+	[DT_LNK]	= FT_SYMLINK,
+	[DT_CHR]	= FT_CHRDEV,
+	[DT_BLK]	= FT_BLKDEV,
+	[DT_FIFO]	= FT_FIFO,
+	[DT_SOCK]	= FT_SOCK,
+};

This array should be const so that it ends up in .rodata.

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center

Dear Thiago,

Thank you for these suggestions. Don't know why I didn't spot this myself. As Jan has kindly offered to make this change upon including this in his tree, I will forgo publishing the corrections on this occasion.

Regards,
Phil