kvm: qemu: fix option_rom_setup_reset address
[kvm-userspace.git] / kernel / anon_inodes.c
blob135adaea7bff21960e007fdea214bacd1e3c7a66
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 /* anon_inodes on RHEL >= 5.2 is equivalent to 2.6.27 version */
25 #ifdef RHEL_RELEASE_CODE
26 # if (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(5,2)) && defined(CONFIG_ANON_INODES)
27 # define RHEL_ANON_INODES
28 # endif
29 #endif
31 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) && !defined(RHEL_ANON_INODES)
33 static struct vfsmount *anon_inode_mnt __read_mostly;
34 static struct inode *anon_inode_inode;
35 static struct file_operations anon_inode_fops;
37 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17)
39 static int anon_inodefs_get_sb(struct file_system_type *fs_type, int flags,
40 const char *dev_name, void *data,
41 struct vfsmount *mnt)
43 return get_sb_pseudo(fs_type, "kvm_anon_inode:", NULL, 0x99700426, mnt);
46 #else
48 static struct super_block *anon_inodefs_get_sb(struct file_system_type *fs_type,
49 int flags, const char *dev_name,
50 void *data)
52 return get_sb_pseudo(fs_type, "kvm_anon_inode:", NULL, 0x99700426);
55 #endif
57 static int anon_inodefs_delete_dentry(struct dentry *dentry)
60 * We faked vfs to believe the dentry was hashed when we created it.
61 * Now we restore the flag so that dput() will work correctly.
63 dentry->d_flags |= DCACHE_UNHASHED;
64 return 1;
67 static struct file_system_type anon_inode_fs_type = {
68 .name = "kvm_anon_inodefs",
69 .get_sb = anon_inodefs_get_sb,
70 .kill_sb = kill_anon_super,
72 static struct dentry_operations anon_inodefs_dentry_operations = {
73 .d_delete = anon_inodefs_delete_dentry,
76 /**
77 * anon_inode_getfd - creates a new file instance by hooking it up to and
78 * anonymous inode, and a dentry that describe the "class"
79 * of the file
81 * @name: [in] name of the "class" of the new file
82 * @fops [in] file operations for the new file
83 * @priv [in] private data for the new file (will be file's private_data)
85 * Creates a new file by hooking it on a single inode. This is useful for files
86 * that do not need to have a full-fledged inode in order to operate correctly.
87 * All the files created with anon_inode_getfd() will share a single inode, by
88 * hence saving memory and avoiding code duplication for the file/inode/dentry
89 * setup. Returns new descriptor or -error.
91 int anon_inode_getfd(const char *name, const struct file_operations *fops,
92 void *priv, int flags)
94 struct qstr this;
95 struct dentry *dentry;
96 struct inode *inode;
97 struct file *file;
98 int error, fd;
100 if (IS_ERR(anon_inode_inode))
101 return -ENODEV;
102 file = get_empty_filp();
103 if (!file)
104 return -ENFILE;
106 inode = igrab(anon_inode_inode);
107 if (IS_ERR(inode)) {
108 error = PTR_ERR(inode);
109 goto err_put_filp;
112 error = get_unused_fd();
113 if (error < 0)
114 goto err_iput;
115 fd = error;
118 * Link the inode to a directory entry by creating a unique name
119 * using the inode sequence number.
121 error = -ENOMEM;
122 this.name = name;
123 this.len = strlen(name);
124 this.hash = 0;
125 dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
126 if (!dentry)
127 goto err_put_unused_fd;
128 dentry->d_op = &anon_inodefs_dentry_operations;
129 /* Do not publish this dentry inside the global dentry hash table */
130 dentry->d_flags &= ~DCACHE_UNHASHED;
131 d_instantiate(dentry, inode);
133 file->f_vfsmnt = mntget(anon_inode_mnt);
134 file->f_dentry = dentry;
135 file->f_mapping = inode->i_mapping;
137 file->f_pos = 0;
138 file->f_flags = O_RDWR;
139 file->f_op = (struct file_operations *)fops;
140 file->f_mode = FMODE_READ | FMODE_WRITE;
141 file->f_version = 0;
142 file->private_data = priv;
144 fd_install(fd, file);
146 return fd;
148 err_put_unused_fd:
149 put_unused_fd(fd);
150 err_iput:
151 iput(inode);
152 err_put_filp:
153 fput(file);
154 return error;
158 * A single inode exist for all anon_inode files. Contrary to pipes,
159 * anon_inode inodes has no per-instance data associated, so we can avoid
160 * the allocation of multiple of them.
162 static struct inode *anon_inode_mkinode(void)
164 struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
166 if (!inode)
167 return ERR_PTR(-ENOMEM);
169 inode->i_fop = &anon_inode_fops;
172 * Mark the inode dirty from the very beginning,
173 * that way it will never be moved to the dirty
174 * list because mark_inode_dirty() will think
175 * that it already _is_ on the dirty list.
177 inode->i_state = I_DIRTY;
178 inode->i_mode = S_IRUSR | S_IWUSR;
179 inode->i_uid = current->fsuid;
180 inode->i_gid = current->fsgid;
181 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
182 return inode;
185 static int anon_inode_init(void)
187 int error;
189 error = register_filesystem(&anon_inode_fs_type);
190 if (error)
191 goto err_exit;
192 anon_inode_mnt = kern_mount(&anon_inode_fs_type);
193 if (IS_ERR(anon_inode_mnt)) {
194 error = PTR_ERR(anon_inode_mnt);
195 goto err_unregister_filesystem;
197 anon_inode_inode = anon_inode_mkinode();
198 if (IS_ERR(anon_inode_inode)) {
199 error = PTR_ERR(anon_inode_inode);
200 goto err_mntput;
203 return 0;
205 err_mntput:
206 mntput(anon_inode_mnt);
207 err_unregister_filesystem:
208 unregister_filesystem(&anon_inode_fs_type);
209 err_exit:
210 return -ENOMEM;
213 int kvm_init_anon_inodes(void)
215 return anon_inode_init();
218 void kvm_exit_anon_inodes(void)
220 iput(anon_inode_inode);
221 mntput(anon_inode_mnt);
222 unregister_filesystem(&anon_inode_fs_type);
225 #else
227 int kvm_init_anon_inodes(void)
229 return 0;
232 void kvm_exit_anon_inodes(void)
236 #undef anon_inode_getfd
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26) && !defined(RHEL_ANON_INODES)
240 int kvm_anon_inode_getfd(const char *name,
241 const struct file_operations *fops,
242 void *priv, int flags)
244 int r;
245 int fd;
246 struct inode *inode;
247 struct file *file;
249 r = anon_inode_getfd(&fd, &inode, &file, name, fops, priv);
250 if (r < 0)
251 return r;
252 return fd;
255 #elif LINUX_VERSION_CODE == KERNEL_VERSION(2,6,26) && !defined(RHEL_ANON_INODES)
257 int kvm_anon_inode_getfd(const char *name,
258 const struct file_operations *fops,
259 void *priv, int flags)
261 return anon_inode_getfd(name, fops, priv);
264 #else
266 int kvm_anon_inode_getfd(const char *name,
267 const struct file_operations *fops,
268 void *priv, int flags)
270 return anon_inode_getfd(name, fops, priv, flags);
273 #endif
275 #endif