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.
11 #include <linux/file.h>
12 #include <linux/poll.h>
13 #include <linux/slab.h>
14 #include <linux/init.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
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
,
43 return get_sb_pseudo(fs_type
, "kvm_anon_inode:", NULL
, 0x99700426, mnt
);
48 static struct super_block
*anon_inodefs_get_sb(struct file_system_type
*fs_type
,
49 int flags
, const char *dev_name
,
52 return get_sb_pseudo(fs_type
, "kvm_anon_inode:", NULL
, 0x99700426);
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
;
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
,
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"
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
)
95 struct dentry
*dentry
;
100 if (IS_ERR(anon_inode_inode
))
102 file
= get_empty_filp();
106 inode
= igrab(anon_inode_inode
);
108 error
= PTR_ERR(inode
);
112 error
= get_unused_fd();
118 * Link the inode to a directory entry by creating a unique name
119 * using the inode sequence number.
123 this.len
= strlen(name
);
125 dentry
= d_alloc(anon_inode_mnt
->mnt_sb
->s_root
, &this);
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
;
138 file
->f_flags
= O_RDWR
;
139 file
->f_op
= (struct file_operations
*)fops
;
140 file
->f_mode
= FMODE_READ
| FMODE_WRITE
;
142 file
->private_data
= priv
;
144 fd_install(fd
, file
);
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
);
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
;
185 static int anon_inode_init(void)
189 error
= register_filesystem(&anon_inode_fs_type
);
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
);
206 mntput(anon_inode_mnt
);
207 err_unregister_filesystem
:
208 unregister_filesystem(&anon_inode_fs_type
);
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
);
227 int kvm_init_anon_inodes(void)
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
)
249 r
= anon_inode_getfd(&fd
, &inode
, &file
, name
, fops
, priv
);
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
);
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
);