Skip to contentSkip to conversation

fs: consolidate dt_type() helper definitions

PATCH In review Jeff Layton 3 participants 5 messages Mar 30, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fs: consolidate dt_type() helper definitions

There are 4 functions named dt_type() in the kernel. There is also the S_DT macro in fs_types.h.

Replace the S_DT macro with a static inline named dt_type, and have all of the existing copies call that instead. The v9fs helper is renamed to distinguish it from the others.

Cc Chuck Lever <[email protected]>
Cc Phillip Potter <[email protected]>
---
 fs/9p/vfs_dir.c          | 6 +++---
 fs/configfs/dir.c        | 8 +-------
 fs/fs_types.c            | 2 +-
 fs/kernfs/dir.c          | 8 +-------
 fs/libfs.c               | 9 ++-------
 include/linux/fs_types.h | 7 ++++++-
 6 files changed, 14 insertions(+), 26 deletions(-)

What about this one instead? This consolidates another copy and we use Phillip's version that uses named constants instead of magic numbers.

There are some scary warnings in fs_types.h about not changing the definitions, but hopefully the rename from S_DT() to dt_type() is OK.

fs/9p/vfs_dir.c +3 -3
index 3d74b04fe0de..80b331f7f446 100644
@@ -41,12 +41,12 @@ struct p9_rdir {
 };
 
 /**
- * dt_type - return file type
+ * v9fs_dt_type - return file type
  * @mistat: mistat structure
  *
  */
 
-static inline int dt_type(struct p9_wstat *mistat)
+static inline int v9fs_dt_type(struct p9_wstat *mistat)
 {
 	unsigned long perm = mistat->mode;
 	int rettype = DT_REG;
@@ -128,7 +128,7 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
 			}
 
 			over = !dir_emit(ctx, st.name, strlen(st.name),
-					 v9fs_qid2ino(&st.qid), dt_type(&st));
+					 v9fs_qid2ino(&st.qid), v9fs_dt_type(&st));
 			p9stat_free(&st);
 			if (over)
 				return 0;
fs/configfs/dir.c +1 -7
index 4afcbbe63e68..43863a1696eb 100644
@@ -1599,12 +1599,6 @@ static int configfs_dir_close(struct inode *inode, struct file *file)
 	return 0;
 }
 
-/* Relationship between s_mode and the DT_xxx types */
-static inline unsigned char dt_type(struct configfs_dirent *sd)
-{
-	return (sd->s_mode >> 12) & 15;
-}
-
 static int configfs_readdir(struct file *file, struct dir_context *ctx)
 {
 	struct dentry *dentry = file->f_path.dentry;
@@ -1654,7 +1648,7 @@ static int configfs_readdir(struct file *file, struct dir_context *ctx)
 		name = configfs_get_name(next);
 		len = strlen(name);
 
-		if (!dir_emit(ctx, name, len, ino, dt_type(next)))
+		if (!dir_emit(ctx, name, len, ino, dt_type(next->s_mode)))
 			return 0;
 
 		spin_lock(&configfs_dirent_lock);
fs/fs_types.c +1 -1
index 78365e5dc08c..7dd5c0fb74fb 100644
@@ -76,7 +76,7 @@ static const unsigned char fs_ftype_by_dtype[DT_MAX] = {
  */
 unsigned char fs_umode_to_ftype(umode_t mode)
 {
-	return fs_ftype_by_dtype[S_DT(mode)];
+	return fs_ftype_by_dtype[dt_type(mode)];
 }
 EXPORT_SYMBOL_GPL(fs_umode_to_ftype);
 
fs/kernfs/dir.c +1 -7
index ef00b5fe8cee..0b7e9b8ee93e 100644
@@ -1748,12 +1748,6 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
 	return error;
 }
 
-/* Relationship between mode and the DT_xxx types */
-static inline unsigned char dt_type(struct kernfs_node *kn)
-{
-	return (kn->mode >> 12) & 15;
-}
-
 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
 {
 	kernfs_put(filp->private_data);
@@ -1831,7 +1825,7 @@ static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
 	     pos;
 	     pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
 		const char *name = pos->name;
-		unsigned int type = dt_type(pos);
+		unsigned int type = dt_type(pos->mode);
 		int len = strlen(name);
 		ino_t ino = kernfs_ino(pos);
 
fs/libfs.c +2 -7
index 4eda519c3002..d0f0cdae9ff7 100644
@@ -174,12 +174,6 @@ loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
 }
 EXPORT_SYMBOL(dcache_dir_lseek);
 
-/* Relationship between i_mode and the DT_xxx types */
-static inline unsigned char dt_type(struct inode *inode)
-{
-	return (inode->i_mode >> 12) & 15;
-}
-
 /*
  * Directory is locked and all positive dentries in it are safe, since
  * for ramfs-type trees they can't go away without unlink() or rmdir(),
@@ -206,7 +200,8 @@ int dcache_readdir(struct file *file, struct dir_context *ctx)
 
 	while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
 		if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
-			      d_inode(next)->i_ino, dt_type(d_inode(next))))
+			      d_inode(next)->i_ino,
+			      dt_type(d_inode(next)->i_mode)))
 			break;
 		ctx->pos++;
 		p = &next->d_child;
include/linux/fs_types.h +6 -1
index 54816791196f..1e25a7654a86 100644
@@ -27,9 +27,14 @@
  * (ie "(i_mode >> 12) & 15").
  */
 #define S_DT_SHIFT	12
-#define S_DT(mode)	(((mode) & S_IFMT) >> S_DT_SHIFT)
 #define S_DT_MASK	(S_IFMT >> S_DT_SHIFT)
 
+/* Relationship between i_mode and the DT_xxx types */
+static inline unsigned char dt_type(umode_t mode)
+{
+	return ((mode) & S_IFMT) >> S_DT_SHIFT;
+}
+
 /* these are defined by POSIX and also present in glibc's dirent.h */
 #define DT_UNKNOWN	0
 #define DT_FIFO		1
Signature · 1 line
2.39.2
#1 lore raw

fs/9p/vfs_dir.c static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) · line 131

Quoted 2 lines
fs/9p/vfs_dir.c +1 -1
-					 v9fs_qid2ino(&st.qid), dt_type(&st));
+					 v9fs_qid2ino(&st.qid), v9fs_dt_type(&st));

This adds an overly long line. Also renaming the v9fs dt_type seems like it should be a prep patch.

Quoted 1 line

+/* Relationship between i_mode and the DT_xxx types */

This comment seems a bit terse.

include/linux/fs_types.h line 35

Quoted 3 lines
include/linux/fs_types.h +3 -0
+static inline unsigned char dt_type(umode_t mode)
+{
+	return ((mode) & S_IFMT) >> S_DT_SHIFT;

No need for the inner braces.

#2 lore raw
Quoted 9 lines · Christoph Hellwig

fs/9p/vfs_dir.c static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) · line 131

Quoted 2 lines
fs/9p/vfs_dir.c +1 -1
-					 v9fs_qid2ino(&st.qid), dt_type(&st));
+					 v9fs_qid2ino(&st.qid), v9fs_dt_type(&st));

This adds an overly long line. Also renaming the v9fs dt_type seems like it should be a prep patch.

Quoted 1 line

+/* Relationship between i_mode and the DT_xxx types */

This comment seems a bit terse.

Agreed. Would be nice if we could just do proper kernel doc. Even for static inline functions it can't hurt.

#3 lore raw
In reply to Jeff Layton
Quoted 84 lines · Jeff Layton

There are 4 functions named dt_type() in the kernel. There is also the S_DT macro in fs_types.h.

Replace the S_DT macro with a static inline named dt_type, and have all of the existing copies call that instead. The v9fs helper is renamed to distinguish it from the others.

Cc Chuck Lever <[email protected]>
Cc Phillip Potter <[email protected]>
---
 fs/9p/vfs_dir.c          | 6 +++---
 fs/configfs/dir.c        | 8 +-------
 fs/fs_types.c            | 2 +-
 fs/kernfs/dir.c          | 8 +-------
 fs/libfs.c               | 9 ++-------
 include/linux/fs_types.h | 7 ++++++-
 6 files changed, 14 insertions(+), 26 deletions(-)

What about this one instead? This consolidates another copy and we use Phillip's version that uses named constants instead of magic numbers.

There are some scary warnings in fs_types.h about not changing the definitions, but hopefully the rename from S_DT() to dt_type() is OK.

fs/9p/vfs_dir.c +3 -3
index 3d74b04fe0de..80b331f7f446 100644
@@ -41,12 +41,12 @@ struct p9_rdir {
 };
 
 /**
- * dt_type - return file type
+ * v9fs_dt_type - return file type
  * @mistat: mistat structure
  *
  */
 
-static inline int dt_type(struct p9_wstat *mistat)
+static inline int v9fs_dt_type(struct p9_wstat *mistat)
 {
 	unsigned long perm = mistat->mode;
 	int rettype = DT_REG;
@@ -128,7 +128,7 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
 			}
 
 			over = !dir_emit(ctx, st.name, strlen(st.name),
-					 v9fs_qid2ino(&st.qid), dt_type(&st));
+					 v9fs_qid2ino(&st.qid), v9fs_dt_type(&st));
 			p9stat_free(&st);
 			if (over)
 				return 0;
fs/configfs/dir.c +1 -7
index 4afcbbe63e68..43863a1696eb 100644
@@ -1599,12 +1599,6 @@ static int configfs_dir_close(struct inode *inode, struct file *file)
 	return 0;
 }
 
-/* Relationship between s_mode and the DT_xxx types */
-static inline unsigned char dt_type(struct configfs_dirent *sd)
-{
-	return (sd->s_mode >> 12) & 15;
-}
-
 static int configfs_readdir(struct file *file, struct dir_context *ctx)
 {
 	struct dentry *dentry = file->f_path.dentry;
@@ -1654,7 +1648,7 @@ static int configfs_readdir(struct file *file, struct dir_context *ctx)
 		name = configfs_get_name(next);
 		len = strlen(name);
 
-		if (!dir_emit(ctx, name, len, ino, dt_type(next)))
+		if (!dir_emit(ctx, name, len, ino, dt_type(next->s_mode)))
 			return 0;
 
 		spin_lock(&configfs_dirent_lock);
fs/fs_types.c +0 -0
index 78365e5dc08c..7dd5c0fb74fb 100644

fs/fs_types.c static const unsigned char fs_ftype_by_dtype[DT_MAX] = { · line 81

Quoted 7 lines
fs/fs_types.c +1 -1
@@ -76,7 +76,7 @@ static const unsigned char fs_ftype_by_dtype[DT_MAX] = {
  */
 unsigned char fs_umode_to_ftype(umode_t mode)
 {
-	return fs_ftype_by_dtype[S_DT(mode)];
+	return fs_ftype_by_dtype[dt_type(mode)];
 }
 EXPORT_SYMBOL_GPL(fs_umode_to_ftype);

Nice cleanup. But looking at this a bit it makes me wonder a little. It seems there's a bit of indirection going on:

fs_umode_to_dtype()
-> fs_type_to_dtype()
-> fs_umode_to_ftype()

      -> fs_ftype_by_dtype()
         -> dt_type()

Presumably it exists so that unexpected return values from dt_type() are caught and DT_UNKNOWN is returned instead of whatever raw value dt_type() returned.

If none of the filesystems we convert to dt_type() here expects "custom" return values from dt_type(), i.e., would never get DT_UNKNOWN, we should consider just switching all those places to fs_umode_to_dtype().

However, if they do expect custom dt_type() values and so we really need to have them use dt_type() then we should remove fs_umode_to_dtype() because it is curerntly unused if my grepping skills haven't left me.

#4 lore raw
Quoted 106 lines · Christian Brauner
Quoted 84 lines · Jeff Layton

There are 4 functions named dt_type() in the kernel. There is also the S_DT macro in fs_types.h.

Replace the S_DT macro with a static inline named dt_type, and have all of the existing copies call that instead. The v9fs helper is renamed to distinguish it from the others.

Cc Chuck Lever <[email protected]>
Cc Phillip Potter <[email protected]>
---
 fs/9p/vfs_dir.c          | 6 +++---
 fs/configfs/dir.c        | 8 +-------
 fs/fs_types.c            | 2 +-
 fs/kernfs/dir.c          | 8 +-------
 fs/libfs.c               | 9 ++-------
 include/linux/fs_types.h | 7 ++++++-
 6 files changed, 14 insertions(+), 26 deletions(-)

What about this one instead? This consolidates another copy and we use Phillip's version that uses named constants instead of magic numbers.

There are some scary warnings in fs_types.h about not changing the definitions, but hopefully the rename from S_DT() to dt_type() is OK.

fs/9p/vfs_dir.c +3 -3
index 3d74b04fe0de..80b331f7f446 100644
@@ -41,12 +41,12 @@ struct p9_rdir {
 };
 
 /**
- * dt_type - return file type
+ * v9fs_dt_type - return file type
  * @mistat: mistat structure
  *
  */
 
-static inline int dt_type(struct p9_wstat *mistat)
+static inline int v9fs_dt_type(struct p9_wstat *mistat)
 {
 	unsigned long perm = mistat->mode;
 	int rettype = DT_REG;
@@ -128,7 +128,7 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
 			}
 
 			over = !dir_emit(ctx, st.name, strlen(st.name),
-					 v9fs_qid2ino(&st.qid), dt_type(&st));
+					 v9fs_qid2ino(&st.qid), v9fs_dt_type(&st));
 			p9stat_free(&st);
 			if (over)
 				return 0;
fs/configfs/dir.c +1 -7
index 4afcbbe63e68..43863a1696eb 100644
@@ -1599,12 +1599,6 @@ static int configfs_dir_close(struct inode *inode, struct file *file)
 	return 0;
 }
 
-/* Relationship between s_mode and the DT_xxx types */
-static inline unsigned char dt_type(struct configfs_dirent *sd)
-{
-	return (sd->s_mode >> 12) & 15;
-}
-
 static int configfs_readdir(struct file *file, struct dir_context *ctx)
 {
 	struct dentry *dentry = file->f_path.dentry;
@@ -1654,7 +1648,7 @@ static int configfs_readdir(struct file *file, struct dir_context *ctx)
 		name = configfs_get_name(next);
 		len = strlen(name);
 
-		if (!dir_emit(ctx, name, len, ino, dt_type(next)))
+		if (!dir_emit(ctx, name, len, ino, dt_type(next->s_mode)))
 			return 0;
 
 		spin_lock(&configfs_dirent_lock);
fs/fs_types.c +0 -0
index 78365e5dc08c..7dd5c0fb74fb 100644

fs/fs_types.c static const unsigned char fs_ftype_by_dtype[DT_MAX] = { · line 81

Quoted 7 lines
fs/fs_types.c +1 -1
@@ -76,7 +76,7 @@ static const unsigned char fs_ftype_by_dtype[DT_MAX] = {
  */
 unsigned char fs_umode_to_ftype(umode_t mode)
 {
-	return fs_ftype_by_dtype[S_DT(mode)];
+	return fs_ftype_by_dtype[dt_type(mode)];
 }
 EXPORT_SYMBOL_GPL(fs_umode_to_ftype);

Nice cleanup. But looking at this a bit it makes me wonder a little. It seems there's a bit of indirection going on:

fs_umode_to_dtype()
-> fs_type_to_dtype()
-> fs_umode_to_ftype()

      -> fs_ftype_by_dtype()
         -> dt_type()

Presumably it exists so that unexpected return values from dt_type() are caught and DT_UNKNOWN is returned instead of whatever raw value dt_type() returned.

Quoted 7 lines

If none of the filesystems we convert to dt_type() here expects "custom" return values from dt_type(), i.e., would never get DT_UNKNOWN, we should consider just switching all those places to fs_umode_to_dtype().

However, if they do expect custom dt_type() values and so we really need to have them use dt_type() then we should remove fs_umode_to_dtype() because it is curerntly unused if my grepping skills haven't left me.

Good point.

The dt_type returns are all handed to dir_emit, and it looks like most of the readdir actor functions just take that value as-is and stuff it into the appropriate readdir response.

Given that, we probably don't want to hand the actors any "custom" values and should switch these callers over to fs_umode_to_dtype instead.

I'll plan to spin up a v3 series (and address HCH's comments in that too).

Thanks for the review, everyone!

Signature · 1 line
Jeff Layton <[email protected]>