Skip to contentSkip to conversation

fs: Create anon_inode_getfile_fmode()

PATCH Superseded Dawid Osuchowski 2 participants 5 messages Apr 25, 2024 – Apr 26, 2024 VFS

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fs: Create anon_inode_getfile_fmode()

Creates an anon_inode_getfile_fmode() function that works similarly to anon_inode_getfile() with the addition of being able to set the fmode member.

---
Changes since v1:
* removed __anon_inode_create_getfile_fmode()
* streamlined if statement and got rid of goto

---
 fs/anon_inodes.c            | 33 +++++++++++++++++++++++++++++++++
 include/linux/anon_inodes.h |  3 +++
 2 files changed, 36 insertions(+)
fs/anon_inodes.c +33 -0
index 0496cb5b6eab..42bd1cb7c9cd 100644
@@ -148,6 +148,38 @@ struct file *anon_inode_getfile(const char *name,
 }
 EXPORT_SYMBOL_GPL(anon_inode_getfile);
 
+/**
+ * anon_inode_getfile_fmode - creates a new file instance by hooking it up to an
+ *                      anonymous inode, and a dentry that describe the "class"
+ *                      of the file
+ *
+ * @name:    [in]    name of the "class" of the new file
+ * @fops:    [in]    file operations for the new file
+ * @priv:    [in]    private data for the new file (will be file's private_data)
+ * @flags:   [in]    flags
+ * @f_mode:  [in]    fmode
+ *
+ * Creates a new file by hooking it on a single inode. This is useful for files
+ * that do not need to have a full-fledged inode in order to operate correctly.
+ * All the files created with anon_inode_getfile() will share a single inode,
+ * hence saving memory and avoiding code duplication for the file/inode/dentry
+ * setup. Allows setting the fmode. Returns the newly created file* or an error
+ * pointer.
+ */
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, fmode_t f_mode)
+{
+	struct file *file;
+
+	file = __anon_inode_getfile(name, fops, priv, flags, NULL, false);
+	if (!IS_ERR(file))
+		file->f_mode |= f_mode;
+
+	return file;
+}
+EXPORT_SYMBOL_GPL(anon_inode_getfile_fmode);
+
 /**
  * anon_inode_create_getfile - Like anon_inode_getfile(), but creates a new
  *                             !S_PRIVATE anon inode rather than reuse the
@@ -271,6 +303,7 @@ int anon_inode_create_getfd(const char *name, const struct file_operations *fops
 	return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
 }
 
+
 static int __init anon_inode_init(void)
 {
 	anon_inode_mnt = kern_mount(&anon_inode_fs_type);
include/linux/anon_inodes.h +3 -0
index 93a5f16d03f3..ee55f9c11a16 100644
@@ -15,6 +15,9 @@ struct inode;
 struct file *anon_inode_getfile(const char *name,
 				const struct file_operations *fops,
 				void *priv, int flags);
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, unsigned int f_mode);
 struct file *anon_inode_create_getfile(const char *name,
 				       const struct file_operations *fops,
 				       void *priv, int flags,
Signature · 1 line
2.44.0
maintainer
#1 lore raw

fs/anon_inodes.c struct file *anon_inode_getfile(const char *name, · line 171

Quoted 3 lines · Dawid Osuchowski
fs/anon_inodes.c +3 -0
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, fmode_t f_mode)
						       ^^^^^^^

include/linux/anon_inodes.h struct inode; · line 20

Quoted 3 lines
include/linux/anon_inodes.h +3 -0
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, unsigned int f_mode);
						       ^^^^^^^^^^^^

They ought to match (and fmode_t is the right type here).

#2 lore raw
Quoted 11 lines · Al Viro

fs/anon_inodes.c struct file *anon_inode_getfile(const char *name, · line 171

Quoted 3 lines · Dawid Osuchowski
fs/anon_inodes.c +3 -0
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, fmode_t f_mode)
						       ^^^^^^^

include/linux/anon_inodes.h struct inode; · line 20

Quoted 3 lines
include/linux/anon_inodes.h +3 -0
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, unsigned int f_mode);
						       ^^^^^^^^^^^^

They ought to match (and fmode_t is the right type here).

Should I include the <linux/fs.h> header (or a different one) into anon_inodes.h to get the fmode_t type or can I copy the typedef directly as a sort of "forward declare" instead?

The latter would mean something like this:

	struct inode;

+ typedef unsigned int fmode_t;

-- Dawid

maintainer
#3 lore raw
Quoted 20 lines · Dawid Osuchowski
Quoted 11 lines · Al Viro

fs/anon_inodes.c struct file *anon_inode_getfile(const char *name, · line 171

Quoted 3 lines · Dawid Osuchowski
fs/anon_inodes.c +3 -0
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, fmode_t f_mode)
						       ^^^^^^^

include/linux/anon_inodes.h struct inode; · line 20

Quoted 3 lines
include/linux/anon_inodes.h +3 -0
+struct file *anon_inode_getfile_fmode(const char *name,
+				const struct file_operations *fops,
+				void *priv, int flags, unsigned int f_mode);
						       ^^^^^^^^^^^^

They ought to match (and fmode_t is the right type here).

Should I include the <linux/fs.h> header (or a different one) into anon_inodes.h to get the fmode_t type or can I copy the typedef directly as a sort of "forward declare" instead?

The latter would mean something like this:

	struct inode;

Review

Quoted 1 line
(quoted diff) +1 -0
+	typedef unsigned int fmode_t;

linux/types.h, if anything.

#4 lore raw
Quoted 1 line · Al Viro

linux/types.h, if anything.

Thanks, will address in next revision.

-- Dawid