aboutsummaryrefslogtreecommitdiffstats
path: root/fs/anon_inodes.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/anon_inodes.c')
-rw-r--r--fs/anon_inodes.c70
1 files changed, 53 insertions, 17 deletions
diff --git a/fs/anon_inodes.c b/fs/anon_inodes.c
index 47d4a01c5393..2ca7a7cafdbf 100644
--- a/fs/anon_inodes.c
+++ b/fs/anon_inodes.c
@@ -8,8 +8,10 @@
8 * 8 *
9 */ 9 */
10 10
11#include <linux/cred.h>
11#include <linux/file.h> 12#include <linux/file.h>
12#include <linux/poll.h> 13#include <linux/poll.h>
14#include <linux/sched.h>
13#include <linux/slab.h> 15#include <linux/slab.h>
14#include <linux/init.h> 16#include <linux/init.h>
15#include <linux/fs.h> 17#include <linux/fs.h>
@@ -77,28 +79,24 @@ static const struct address_space_operations anon_aops = {
77 * 79 *
78 * Creates a new file by hooking it on a single inode. This is useful for files 80 * Creates a new file by hooking it on a single inode. This is useful for files
79 * that do not need to have a full-fledged inode in order to operate correctly. 81 * that do not need to have a full-fledged inode in order to operate correctly.
80 * All the files created with anon_inode_getfd() will share a single inode, 82 * All the files created with anon_inode_getfile() will share a single inode,
81 * hence saving memory and avoiding code duplication for the file/inode/dentry 83 * hence saving memory and avoiding code duplication for the file/inode/dentry
82 * setup. Returns new descriptor or -error. 84 * setup. Returns the newly created file* or an error pointer.
83 */ 85 */
84int anon_inode_getfd(const char *name, const struct file_operations *fops, 86struct file *anon_inode_getfile(const char *name,
85 void *priv, int flags) 87 const struct file_operations *fops,
88 void *priv, int flags)
86{ 89{
87 struct qstr this; 90 struct qstr this;
88 struct dentry *dentry; 91 struct dentry *dentry;
89 struct file *file; 92 struct file *file;
90 int error, fd; 93 int error;
91 94
92 if (IS_ERR(anon_inode_inode)) 95 if (IS_ERR(anon_inode_inode))
93 return -ENODEV; 96 return ERR_PTR(-ENODEV);
94 97
95 if (fops->owner && !try_module_get(fops->owner)) 98 if (fops->owner && !try_module_get(fops->owner))
96 return -ENOENT; 99 return ERR_PTR(-ENOENT);
97
98 error = get_unused_fd_flags(flags);
99 if (error < 0)
100 goto err_module;
101 fd = error;
102 100
103 /* 101 /*
104 * Link the inode to a directory entry by creating a unique name 102 * Link the inode to a directory entry by creating a unique name
@@ -110,7 +108,7 @@ int anon_inode_getfd(const char *name, const struct file_operations *fops,
110 this.hash = 0; 108 this.hash = 0;
111 dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this); 109 dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
112 if (!dentry) 110 if (!dentry)
113 goto err_put_unused_fd; 111 goto err_module;
114 112
115 /* 113 /*
116 * We know the anon_inode inode count is always greater than zero, 114 * We know the anon_inode inode count is always greater than zero,
@@ -136,16 +134,54 @@ int anon_inode_getfd(const char *name, const struct file_operations *fops,
136 file->f_version = 0; 134 file->f_version = 0;
137 file->private_data = priv; 135 file->private_data = priv;
138 136
137 return file;
138
139err_dput:
140 dput(dentry);
141err_module:
142 module_put(fops->owner);
143 return ERR_PTR(error);
144}
145EXPORT_SYMBOL_GPL(anon_inode_getfile);
146
147/**
148 * anon_inode_getfd - creates a new file instance by hooking it up to an
149 * anonymous inode, and a dentry that describe the "class"
150 * of the file
151 *
152 * @name: [in] name of the "class" of the new file
153 * @fops: [in] file operations for the new file
154 * @priv: [in] private data for the new file (will be file's private_data)
155 * @flags: [in] flags
156 *
157 * Creates a new file by hooking it on a single inode. This is useful for files
158 * that do not need to have a full-fledged inode in order to operate correctly.
159 * All the files created with anon_inode_getfd() will share a single inode,
160 * hence saving memory and avoiding code duplication for the file/inode/dentry
161 * setup. Returns new descriptor or an error code.
162 */
163int anon_inode_getfd(const char *name, const struct file_operations *fops,
164 void *priv, int flags)
165{
166 int error, fd;
167 struct file *file;
168
169 error = get_unused_fd_flags(flags);
170 if (error < 0)
171 return error;
172 fd = error;
173
174 file = anon_inode_getfile(name, fops, priv, flags);
175 if (IS_ERR(file)) {
176 error = PTR_ERR(file);
177 goto err_put_unused_fd;
178 }
139 fd_install(fd, file); 179 fd_install(fd, file);
140 180
141 return fd; 181 return fd;
142 182
143err_dput:
144 dput(dentry);
145err_put_unused_fd: 183err_put_unused_fd:
146 put_unused_fd(fd); 184 put_unused_fd(fd);
147err_module:
148 module_put(fops->owner);
149 return error; 185 return error;
150} 186}
151EXPORT_SYMBOL_GPL(anon_inode_getfd); 187EXPORT_SYMBOL_GPL(anon_inode_getfd);