kvm: qemu: remove KVM_CAP_USER_MEMORY reference from qemu-kvm.c
[kvm-userspace.git] / kernel / anon_inodes.c
blob510303f02009e86bc8e509dc86abd8bbc3e88c8c
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/file.h>
12 #include <linux/poll.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <linux/mount.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/magic.h>
20 #include <linux/anon_inodes.h>
22 #include <asm/uaccess.h>
24 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
26 static struct vfsmount *anon_inode_mnt __read_mostly;
27 static struct inode *anon_inode_inode;
28 static struct file_operations anon_inode_fops;
30 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17)
32 static int anon_inodefs_get_sb(struct file_system_type *fs_type, int flags,
33 const char *dev_name, void *data,
34 struct vfsmount *mnt)
36 return get_sb_pseudo(fs_type, "kvm_anon_inode:", NULL, 0x99700426, mnt);
39 #else
41 static struct super_block *anon_inodefs_get_sb(struct file_system_type *fs_type,
42 int flags, const char *dev_name,
43 void *data)
45 return get_sb_pseudo(fs_type, "kvm_anon_inode:", NULL, 0x99700426);
48 #endif
50 static int anon_inodefs_delete_dentry(struct dentry *dentry)
53 * We faked vfs to believe the dentry was hashed when we created it.
54 * Now we restore the flag so that dput() will work correctly.
56 dentry->d_flags |= DCACHE_UNHASHED;
57 return 1;
60 static struct file_system_type anon_inode_fs_type = {
61 .name = "kvm_anon_inodefs",
62 .get_sb = anon_inodefs_get_sb,
63 .kill_sb = kill_anon_super,
65 static struct dentry_operations anon_inodefs_dentry_operations = {
66 .d_delete = anon_inodefs_delete_dentry,
69 /**
70 * anon_inode_getfd - creates a new file instance by hooking it up to and
71 * anonymous inode, and a dentry that describe the "class"
72 * of the file
74 * @name: [in] name of the "class" of the new file
75 * @fops [in] file operations for the new file
76 * @priv [in] private data for the new file (will be file's private_data)
78 * 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.
80 * All the files created with anon_inode_getfd() will share a single inode, by
81 * hence saving memory and avoiding code duplication for the file/inode/dentry
82 * setup. Returns new descriptor or -error.
84 int anon_inode_getfd(const char *name, const struct file_operations *fops,
85 void *priv, int flags)
87 struct qstr this;
88 struct dentry *dentry;
89 struct inode *inode;
90 struct file *file;
91 int error, fd;
93 if (IS_ERR(anon_inode_inode))
94 return -ENODEV;
95 file = get_empty_filp();
96 if (!file)
97 return -ENFILE;
99 inode = igrab(anon_inode_inode);
100 if (IS_ERR(inode)) {
101 error = PTR_ERR(inode);
102 goto err_put_filp;
105 error = get_unused_fd();
106 if (error < 0)
107 goto err_iput;
108 fd = error;
111 * Link the inode to a directory entry by creating a unique name
112 * using the inode sequence number.
114 error = -ENOMEM;
115 this.name = name;
116 this.len = strlen(name);
117 this.hash = 0;
118 dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
119 if (!dentry)
120 goto err_put_unused_fd;
121 dentry->d_op = &anon_inodefs_dentry_operations;
122 /* Do not publish this dentry inside the global dentry hash table */
123 dentry->d_flags &= ~DCACHE_UNHASHED;
124 d_instantiate(dentry, inode);
126 file->f_vfsmnt = mntget(anon_inode_mnt);
127 file->f_dentry = dentry;
128 file->f_mapping = inode->i_mapping;
130 file->f_pos = 0;
131 file->f_flags = O_RDWR;
132 file->f_op = (struct file_operations *)fops;
133 file->f_mode = FMODE_READ | FMODE_WRITE;
134 file->f_version = 0;
135 file->private_data = priv;
137 fd_install(fd, file);
139 return fd;
141 err_put_unused_fd:
142 put_unused_fd(fd);
143 err_iput:
144 iput(inode);
145 err_put_filp:
146 fput(file);
147 return error;
151 * A single inode exist for all anon_inode files. Contrary to pipes,
152 * anon_inode inodes has no per-instance data associated, so we can avoid
153 * the allocation of multiple of them.
155 static struct inode *anon_inode_mkinode(void)
157 struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
159 if (!inode)
160 return ERR_PTR(-ENOMEM);
162 inode->i_fop = &anon_inode_fops;
165 * Mark the inode dirty from the very beginning,
166 * that way it will never be moved to the dirty
167 * list because mark_inode_dirty() will think
168 * that it already _is_ on the dirty list.
170 inode->i_state = I_DIRTY;
171 inode->i_mode = S_IRUSR | S_IWUSR;
172 inode->i_uid = current->fsuid;
173 inode->i_gid = current->fsgid;
174 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
175 return inode;
178 static int anon_inode_init(void)
180 int error;
182 error = register_filesystem(&anon_inode_fs_type);
183 if (error)
184 goto err_exit;
185 anon_inode_mnt = kern_mount(&anon_inode_fs_type);
186 if (IS_ERR(anon_inode_mnt)) {
187 error = PTR_ERR(anon_inode_mnt);
188 goto err_unregister_filesystem;
190 anon_inode_inode = anon_inode_mkinode();
191 if (IS_ERR(anon_inode_inode)) {
192 error = PTR_ERR(anon_inode_inode);
193 goto err_mntput;
196 return 0;
198 err_mntput:
199 mntput(anon_inode_mnt);
200 err_unregister_filesystem:
201 unregister_filesystem(&anon_inode_fs_type);
202 err_exit:
203 return -ENOMEM;
206 int kvm_init_anon_inodes(void)
208 return anon_inode_init();
211 void kvm_exit_anon_inodes(void)
213 iput(anon_inode_inode);
214 mntput(anon_inode_mnt);
215 unregister_filesystem(&anon_inode_fs_type);
218 #else
220 int kvm_init_anon_inodes(void)
222 return 0;
225 void kvm_exit_anon_inodes(void)
229 #undef anon_inode_getfd
231 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
233 int kvm_anon_inode_getfd(const char *name,
234 const struct file_operations *fops,
235 void *priv, int flags)
237 int r;
238 int fd;
239 struct inode *inode;
240 struct file *file;
242 r = anon_inode_getfd(&fd, &inode, &file, name, fops, priv);
243 if (r < 0)
244 return r;
245 return fd;
248 #elif LINUX_VERSION_CODE == KERNEL_VERSION(2,6,26)
250 int kvm_anon_inode_getfd(const char *name,
251 const struct file_operations *fops,
252 void *priv, int flags)
254 return anon_inode_getfd(name, fops, priv);
257 #else
259 int kvm_anon_inode_getfd(const char *name,
260 const struct file_operations *fops,
261 void *priv, int flags)
263 return anon_inode_getfd(name, fops, priv, flags);
266 #endif
268 #endif