fed up with those stupid warnings
[mmotm.git] / fs / anon_inodes.c
blob7ae3a96c8ed26e487f6bcbbb36fdfb35118ebbc8
1 /*
2 * fs/anon_inodes.c
4 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 * Thanks to Arnd Bergmann for code review and suggestions.
7 * More changes for Thomas Gleixner suggestions.
9 */
11 #include <linux/cred.h>
12 #include <linux/file.h>
13 #include <linux/poll.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #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>
24 #include <asm/uaccess.h>
26 static struct vfsmount *anon_inode_mnt __read_mostly;
27 static struct inode *anon_inode_inode;
28 static const struct file_operations anon_inode_fops;
30 static int anon_inodefs_get_sb(struct file_system_type *fs_type, int flags,
31 const char *dev_name, void *data,
32 struct vfsmount *mnt)
34 return get_sb_pseudo(fs_type, "anon_inode:", NULL, ANON_INODE_FS_MAGIC,
35 mnt);
38 static struct file_system_type anon_inode_fs_type = {
39 .name = "anon_inodefs",
40 .get_sb = anon_inodefs_get_sb,
41 .kill_sb = kill_anon_super,
45 * nop .set_page_dirty method so that people can use .page_mkwrite on
46 * anon inodes.
48 static int anon_set_page_dirty(struct page *page)
50 return 0;
53 static const struct address_space_operations anon_aops = {
54 .set_page_dirty = anon_set_page_dirty,
57 /**
58 * anon_inode_getfd - 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
62 * @name: [in] name of the "class" of the new file
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
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.
69 * All the files created with anon_inode_getfile() will share a single inode,
70 * hence saving memory and avoiding code duplication for the file/inode/dentry
71 * setup. Returns the newly created file* or an error pointer.
73 struct file *anon_inode_getfile(const char *name,
74 const struct file_operations *fops,
75 void *priv, int flags)
77 struct qstr this;
78 struct dentry *dentry;
79 struct file *file;
80 int error;
82 if (IS_ERR(anon_inode_inode))
83 return ERR_PTR(-ENODEV);
85 if (fops->owner && !try_module_get(fops->owner))
86 return ERR_PTR(-ENOENT);
89 * Link the inode to a directory entry by creating a unique name
90 * using the inode sequence number.
92 error = -ENOMEM;
93 this.name = name;
94 this.len = strlen(name);
95 this.hash = 0;
96 dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
97 if (!dentry)
98 goto err_module;
101 * We know the anon_inode inode count is always greater than zero,
102 * so we can avoid doing an igrab() and we can use an open-coded
103 * atomic_inc().
105 atomic_inc(&anon_inode_inode->i_count);
107 d_instantiate(dentry, anon_inode_inode);
109 error = -ENFILE;
110 file = alloc_file(anon_inode_mnt, dentry,
111 FMODE_READ | FMODE_WRITE, fops);
112 if (!file)
113 goto err_dput;
114 file->f_mapping = anon_inode_inode->i_mapping;
116 file->f_pos = 0;
117 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
118 file->f_version = 0;
119 file->private_data = priv;
121 return file;
123 err_dput:
124 dput(dentry);
125 err_module:
126 module_put(fops->owner);
127 return ERR_PTR(error);
129 EXPORT_SYMBOL_GPL(anon_inode_getfile);
132 * anon_inode_getfd - creates a new file instance by hooking it up to an
133 * anonymous inode, and a dentry that describe the "class"
134 * of the file
136 * @name: [in] name of the "class" of the new file
137 * @fops: [in] file operations for the new file
138 * @priv: [in] private data for the new file (will be file's private_data)
139 * @flags: [in] flags
141 * Creates a new file by hooking it on a single inode. This is useful for files
142 * that do not need to have a full-fledged inode in order to operate correctly.
143 * All the files created with anon_inode_getfd() will share a single inode,
144 * hence saving memory and avoiding code duplication for the file/inode/dentry
145 * setup. Returns new descriptor or an error code.
147 int anon_inode_getfd(const char *name, const struct file_operations *fops,
148 void *priv, int flags)
150 int error, fd;
151 struct file *file;
153 error = get_unused_fd_flags(flags);
154 if (error < 0)
155 return error;
156 fd = error;
158 file = anon_inode_getfile(name, fops, priv, flags);
159 if (IS_ERR(file)) {
160 error = PTR_ERR(file);
161 goto err_put_unused_fd;
163 fd_install(fd, file);
165 return fd;
167 err_put_unused_fd:
168 put_unused_fd(fd);
169 return error;
171 EXPORT_SYMBOL_GPL(anon_inode_getfd);
174 * A single inode exists for all anon_inode files. Contrary to pipes,
175 * anon_inode inodes have no associated per-instance data, so we need
176 * only allocate one of them.
178 static struct inode *anon_inode_mkinode(void)
180 struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
182 if (!inode)
183 return ERR_PTR(-ENOMEM);
185 inode->i_fop = &anon_inode_fops;
187 inode->i_mapping->a_ops = &anon_aops;
190 * Mark the inode dirty from the very beginning,
191 * that way it will never be moved to the dirty
192 * list because mark_inode_dirty() will think
193 * that it already _is_ on the dirty list.
195 inode->i_state = I_DIRTY;
196 inode->i_mode = S_IRUSR | S_IWUSR;
197 inode->i_uid = current_fsuid();
198 inode->i_gid = current_fsgid();
199 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
200 return inode;
203 static int __init anon_inode_init(void)
205 int error;
207 error = register_filesystem(&anon_inode_fs_type);
208 if (error)
209 goto err_exit;
210 anon_inode_mnt = kern_mount(&anon_inode_fs_type);
211 if (IS_ERR(anon_inode_mnt)) {
212 error = PTR_ERR(anon_inode_mnt);
213 goto err_unregister_filesystem;
215 anon_inode_inode = anon_inode_mkinode();
216 if (IS_ERR(anon_inode_inode)) {
217 error = PTR_ERR(anon_inode_inode);
218 goto err_mntput;
221 return 0;
223 err_mntput:
224 mntput(anon_inode_mnt);
225 err_unregister_filesystem:
226 unregister_filesystem(&anon_inode_fs_type);
227 err_exit:
228 panic(KERN_ERR "anon_inode_init() failed (%d)\n", error);
231 fs_initcall(anon_inode_init);