blob: c2b8663f5b00e5f3f14e949a7e518b072fd6bd44 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Davide Libenzi5dc8bf82007-05-10 22:23:11 -07002/*
3 * fs/anon_inodes.c
4 *
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 *
7 * Thanks to Arnd Bergmann for code review and suggestions.
8 * More changes for Thomas Gleixner suggestions.
9 *
10 */
11
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040012#include <linux/cred.h>
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070013#include <linux/file.h>
14#include <linux/poll.h>
Alexey Dobriyana99bbaf2009-10-04 16:11:37 +040015#include <linux/sched.h>
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070016#include <linux/init.h>
17#include <linux/fs.h>
18#include <linux/mount.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/magic.h>
22#include <linux/anon_inodes.h>
23
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070025
26static struct vfsmount *anon_inode_mnt __read_mostly;
27static struct inode *anon_inode_inode;
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070028
Nick Pigginb9aff022009-11-20 14:28:35 -080029/*
30 * anon_inodefs_dname() is called from d_path().
31 */
32static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen)
33{
34 return dynamic_dname(dentry, buffer, buflen, "anon_inode:%s",
35 dentry->d_name.name);
36}
37
Al Viroc74a1cb2011-01-12 16:59:34 -050038static const struct dentry_operations anon_inodefs_dentry_operations = {
39 .d_dname = anon_inodefs_dname,
40};
41
Al Viroca7068c2012-03-17 02:52:29 -040042static struct dentry *anon_inodefs_mount(struct file_system_type *fs_type,
43 int flags, const char *dev_name, void *data)
44{
Jan Kara75c5a522014-03-26 06:20:14 +010045 return mount_pseudo(fs_type, "anon_inode:", NULL,
Al Viroca7068c2012-03-17 02:52:29 -040046 &anon_inodefs_dentry_operations, ANON_INODE_FS_MAGIC);
Al Viroca7068c2012-03-17 02:52:29 -040047}
48
49static struct file_system_type anon_inode_fs_type = {
50 .name = "anon_inodefs",
51 .mount = anon_inodefs_mount,
52 .kill_sb = kill_anon_super,
53};
54
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070055/**
Namhyung Kimc0d87682010-12-10 12:11:50 +090056 * anon_inode_getfile - creates a new file instance by hooking it up to an
57 * anonymous inode, and a dentry that describe the "class"
58 * of the file
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070059 *
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070060 * @name: [in] name of the "class" of the new file
Ulrich Drepper7d9dbca2008-07-23 21:29:22 -070061 * @fops: [in] file operations for the new file
62 * @priv: [in] private data for the new file (will be file's private_data)
63 * @flags: [in] flags
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070064 *
65 * Creates a new file by hooking it on a single inode. This is useful for files
66 * that do not need to have a full-fledged inode in order to operate correctly.
Davide Libenzi562787a2009-09-22 16:43:57 -070067 * All the files created with anon_inode_getfile() will share a single inode,
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070068 * hence saving memory and avoiding code duplication for the file/inode/dentry
Davide Libenzi562787a2009-09-22 16:43:57 -070069 * setup. Returns the newly created file* or an error pointer.
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070070 */
Davide Libenzi562787a2009-09-22 16:43:57 -070071struct file *anon_inode_getfile(const char *name,
72 const struct file_operations *fops,
73 void *priv, int flags)
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070074{
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070075 struct file *file;
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070076
77 if (IS_ERR(anon_inode_inode))
Davide Libenzi562787a2009-09-22 16:43:57 -070078 return ERR_PTR(-ENODEV);
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070079
Christian Borntraegere3a2a0d2008-12-02 11:16:03 +010080 if (fops->owner && !try_module_get(fops->owner))
Davide Libenzi562787a2009-09-22 16:43:57 -070081 return ERR_PTR(-ENOENT);
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070082
83 /*
Davide Libenzi96fdc722007-10-16 23:30:19 -070084 * We know the anon_inode inode count is always greater than zero,
Al Viro7de9c6ee2010-10-23 11:11:40 -040085 * so ihold() is safe.
Davide Libenzi96fdc722007-10-16 23:30:19 -070086 */
Al Viro7de9c6ee2010-10-23 11:11:40 -040087 ihold(anon_inode_inode);
Al Viro52c91f82018-06-09 09:58:23 -040088 file = alloc_file_pseudo(anon_inode_inode, anon_inode_mnt, name,
89 flags & (O_ACCMODE | O_NONBLOCK), fops);
Anatol Pomozov39b65252012-09-12 20:11:55 -070090 if (IS_ERR(file))
Al Viro52c91f82018-06-09 09:58:23 -040091 goto err;
92
Davide Libenzi96fdc722007-10-16 23:30:19 -070093 file->f_mapping = anon_inode_inode->i_mapping;
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070094
Davide Libenzi5dc8bf82007-05-10 22:23:11 -070095 file->private_data = priv;
96
Davide Libenzi562787a2009-09-22 16:43:57 -070097 return file;
98
Al Viro52c91f82018-06-09 09:58:23 -040099err:
100 iput(anon_inode_inode);
Davide Libenzi562787a2009-09-22 16:43:57 -0700101 module_put(fops->owner);
Anatol Pomozov39b65252012-09-12 20:11:55 -0700102 return file;
Davide Libenzi562787a2009-09-22 16:43:57 -0700103}
104EXPORT_SYMBOL_GPL(anon_inode_getfile);
105
106/**
107 * anon_inode_getfd - creates a new file instance by hooking it up to an
108 * anonymous inode, and a dentry that describe the "class"
109 * of the file
110 *
111 * @name: [in] name of the "class" of the new file
112 * @fops: [in] file operations for the new file
113 * @priv: [in] private data for the new file (will be file's private_data)
114 * @flags: [in] flags
115 *
116 * Creates a new file by hooking it on a single inode. This is useful for files
117 * that do not need to have a full-fledged inode in order to operate correctly.
118 * All the files created with anon_inode_getfd() will share a single inode,
119 * hence saving memory and avoiding code duplication for the file/inode/dentry
120 * setup. Returns new descriptor or an error code.
121 */
122int anon_inode_getfd(const char *name, const struct file_operations *fops,
123 void *priv, int flags)
124{
125 int error, fd;
126 struct file *file;
127
128 error = get_unused_fd_flags(flags);
129 if (error < 0)
130 return error;
131 fd = error;
132
133 file = anon_inode_getfile(name, fops, priv, flags);
134 if (IS_ERR(file)) {
135 error = PTR_ERR(file);
136 goto err_put_unused_fd;
137 }
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700138 fd_install(fd, file);
139
Al Viro2030a422008-02-23 06:46:49 -0500140 return fd;
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700141
142err_put_unused_fd:
143 put_unused_fd(fd);
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700144 return error;
145}
Avi Kivityd6d28162007-06-28 08:38:16 -0400146EXPORT_SYMBOL_GPL(anon_inode_getfd);
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700147
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700148static int __init anon_inode_init(void)
149{
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700150 anon_inode_mnt = kern_mount(&anon_inode_fs_type);
Jan Kara75c5a522014-03-26 06:20:14 +0100151 if (IS_ERR(anon_inode_mnt))
152 panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700153
Jan Kara75c5a522014-03-26 06:20:14 +0100154 anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
155 if (IS_ERR(anon_inode_inode))
156 panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
157
158 return 0;
Davide Libenzi5dc8bf82007-05-10 22:23:11 -0700159}
160
161fs_initcall(anon_inode_init);
162