vfs: Convert oprofilefs to use the new mount API
[linux-2.6-block.git] / fs / anon_inodes.c
CommitLineData
5dc8bf81
DL
1/*
2 * fs/anon_inodes.c
3 *
4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
5 *
6 * Thanks to Arnd Bergmann for code review and suggestions.
7 * More changes for Thomas Gleixner suggestions.
8 *
9 */
10
a99bbaf5 11#include <linux/cred.h>
5dc8bf81
DL
12#include <linux/file.h>
13#include <linux/poll.h>
a99bbaf5 14#include <linux/sched.h>
5dc8bf81
DL
15#include <linux/init.h>
16#include <linux/fs.h>
17#include <linux/mount.h>
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/magic.h>
21#include <linux/anon_inodes.h>
33cada40 22#include <linux/pseudo_fs.h>
5dc8bf81 23
7c0f6ba6 24#include <linux/uaccess.h>
5dc8bf81
DL
25
26static struct vfsmount *anon_inode_mnt __read_mostly;
27static struct inode *anon_inode_inode;
5dc8bf81 28
b9aff027
NP
29/*
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
c74a1cbb
AV
38static const struct dentry_operations anon_inodefs_dentry_operations = {
39 .d_dname = anon_inodefs_dname,
40};
41
33cada40 42static int anon_inodefs_init_fs_context(struct fs_context *fc)
ca7068c4 43{
33cada40
DH
44 struct pseudo_fs_context *ctx = init_pseudo(fc, ANON_INODE_FS_MAGIC);
45 if (!ctx)
46 return -ENOMEM;
47 ctx->dops = &anon_inodefs_dentry_operations;
48 return 0;
ca7068c4
AV
49}
50
51static struct file_system_type anon_inode_fs_type = {
52 .name = "anon_inodefs",
33cada40 53 .init_fs_context = anon_inodefs_init_fs_context,
ca7068c4
AV
54 .kill_sb = kill_anon_super,
55};
56
5dc8bf81 57/**
c0d8768a
NK
58 * anon_inode_getfile - creates a new file instance by hooking it up to an
59 * anonymous inode, and a dentry that describe the "class"
60 * of the file
5dc8bf81 61 *
5dc8bf81 62 * @name: [in] name of the "class" of the new file
7d9dbca3
UD
63 * @fops: [in] file operations for the new file
64 * @priv: [in] private data for the new file (will be file's private_data)
65 * @flags: [in] flags
5dc8bf81
DL
66 *
67 * Creates a new file by hooking it on a single inode. This is useful for files
68 * that do not need to have a full-fledged inode in order to operate correctly.
562787a5 69 * All the files created with anon_inode_getfile() will share a single inode,
5dc8bf81 70 * hence saving memory and avoiding code duplication for the file/inode/dentry
562787a5 71 * setup. Returns the newly created file* or an error pointer.
5dc8bf81 72 */
562787a5
DL
73struct file *anon_inode_getfile(const char *name,
74 const struct file_operations *fops,
75 void *priv, int flags)
5dc8bf81 76{
5dc8bf81 77 struct file *file;
5dc8bf81
DL
78
79 if (IS_ERR(anon_inode_inode))
562787a5 80 return ERR_PTR(-ENODEV);
5dc8bf81 81
e3a2a0d4 82 if (fops->owner && !try_module_get(fops->owner))
562787a5 83 return ERR_PTR(-ENOENT);
5dc8bf81 84
96fdc72d
DL
85 /*
86 * We know the anon_inode inode count is always greater than zero,
7de9c6ee 87 * so ihold() is safe.
96fdc72d 88 */
7de9c6ee 89 ihold(anon_inode_inode);
52c91f8b
AV
90 file = alloc_file_pseudo(anon_inode_inode, anon_inode_mnt, name,
91 flags & (O_ACCMODE | O_NONBLOCK), fops);
39b65252 92 if (IS_ERR(file))
52c91f8b
AV
93 goto err;
94
96fdc72d 95 file->f_mapping = anon_inode_inode->i_mapping;
5dc8bf81 96
5dc8bf81
DL
97 file->private_data = priv;
98
562787a5
DL
99 return file;
100
52c91f8b
AV
101err:
102 iput(anon_inode_inode);
562787a5 103 module_put(fops->owner);
39b65252 104 return file;
562787a5
DL
105}
106EXPORT_SYMBOL_GPL(anon_inode_getfile);
107
108/**
109 * anon_inode_getfd - creates a new file instance by hooking it up to an
110 * anonymous inode, and a dentry that describe the "class"
111 * of the file
112 *
113 * @name: [in] name of the "class" of the new file
114 * @fops: [in] file operations for the new file
115 * @priv: [in] private data for the new file (will be file's private_data)
116 * @flags: [in] flags
117 *
118 * Creates a new file by hooking it on a single inode. This is useful for files
119 * that do not need to have a full-fledged inode in order to operate correctly.
120 * All the files created with anon_inode_getfd() will share a single inode,
121 * hence saving memory and avoiding code duplication for the file/inode/dentry
122 * setup. Returns new descriptor or an error code.
123 */
124int anon_inode_getfd(const char *name, const struct file_operations *fops,
125 void *priv, int flags)
126{
127 int error, fd;
128 struct file *file;
129
130 error = get_unused_fd_flags(flags);
131 if (error < 0)
132 return error;
133 fd = error;
134
135 file = anon_inode_getfile(name, fops, priv, flags);
136 if (IS_ERR(file)) {
137 error = PTR_ERR(file);
138 goto err_put_unused_fd;
139 }
5dc8bf81
DL
140 fd_install(fd, file);
141
2030a42c 142 return fd;
5dc8bf81
DL
143
144err_put_unused_fd:
145 put_unused_fd(fd);
5dc8bf81
DL
146 return error;
147}
d6d28168 148EXPORT_SYMBOL_GPL(anon_inode_getfd);
5dc8bf81 149
5dc8bf81
DL
150static int __init anon_inode_init(void)
151{
5dc8bf81 152 anon_inode_mnt = kern_mount(&anon_inode_fs_type);
75c5a52d
JK
153 if (IS_ERR(anon_inode_mnt))
154 panic("anon_inode_init() kernel mount failed (%ld)\n", PTR_ERR(anon_inode_mnt));
5dc8bf81 155
75c5a52d
JK
156 anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
157 if (IS_ERR(anon_inode_inode))
158 panic("anon_inode_init() inode allocation failed (%ld)\n", PTR_ERR(anon_inode_inode));
159
160 return 0;
5dc8bf81
DL
161}
162
163fs_initcall(anon_inode_init);
164