Skip to content

Commit 55394d2

Browse files
whythefyouflyingbrauner
authored andcommitted
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. Signed-off-by: Dawid Osuchowski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 652efde commit 55394d2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

fs/anon_inodes.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,38 @@ struct file *anon_inode_getfile(const char *name,
148148
}
149149
EXPORT_SYMBOL_GPL(anon_inode_getfile);
150150

151+
/**
152+
* anon_inode_getfile_fmode - creates a new file instance by hooking it up to an
153+
* anonymous inode, and a dentry that describe the "class"
154+
* of the file
155+
*
156+
* @name: [in] name of the "class" of the new file
157+
* @fops: [in] file operations for the new file
158+
* @priv: [in] private data for the new file (will be file's private_data)
159+
* @flags: [in] flags
160+
* @f_mode: [in] fmode
161+
*
162+
* Creates a new file by hooking it on a single inode. This is useful for files
163+
* that do not need to have a full-fledged inode in order to operate correctly.
164+
* All the files created with anon_inode_getfile() will share a single inode,
165+
* hence saving memory and avoiding code duplication for the file/inode/dentry
166+
* setup. Allows setting the fmode. Returns the newly created file* or an error
167+
* pointer.
168+
*/
169+
struct file *anon_inode_getfile_fmode(const char *name,
170+
const struct file_operations *fops,
171+
void *priv, int flags, fmode_t f_mode)
172+
{
173+
struct file *file;
174+
175+
file = __anon_inode_getfile(name, fops, priv, flags, NULL, false);
176+
if (!IS_ERR(file))
177+
file->f_mode |= f_mode;
178+
179+
return file;
180+
}
181+
EXPORT_SYMBOL_GPL(anon_inode_getfile_fmode);
182+
151183
/**
152184
* anon_inode_create_getfile - Like anon_inode_getfile(), but creates a new
153185
* !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
271303
return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
272304
}
273305

306+
274307
static int __init anon_inode_init(void)
275308
{
276309
anon_inode_mnt = kern_mount(&anon_inode_fs_type);

include/linux/anon_inodes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
#ifndef _LINUX_ANON_INODES_H
1010
#define _LINUX_ANON_INODES_H
1111

12+
#include <linux/types.h>
13+
1214
struct file_operations;
1315
struct inode;
1416

1517
struct file *anon_inode_getfile(const char *name,
1618
const struct file_operations *fops,
1719
void *priv, int flags);
20+
struct file *anon_inode_getfile_fmode(const char *name,
21+
const struct file_operations *fops,
22+
void *priv, int flags, fmode_t f_mode);
1823
struct file *anon_inode_create_getfile(const char *name,
1924
const struct file_operations *fops,
2025
void *priv, int flags,

0 commit comments

Comments
 (0)