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 #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
,
36 return get_sb_pseudo(fs_type
, "kvm_anon_inode:", NULL
, 0x99700426, mnt
);
41 static struct super_block
*anon_inodefs_get_sb(struct file_system_type
*fs_type
,
42 int flags
, const char *dev_name
,
45 return get_sb_pseudo(fs_type
, "kvm_anon_inode:", NULL
, 0x99700426);
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
;
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
,
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"
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
)
88 struct dentry
*dentry
;
93 if (IS_ERR(anon_inode_inode
))
95 file
= get_empty_filp();
99 inode
= igrab(anon_inode_inode
);
101 error
= PTR_ERR(inode
);
105 error
= get_unused_fd();
111 * Link the inode to a directory entry by creating a unique name
112 * using the inode sequence number.
116 this.len
= strlen(name
);
118 dentry
= d_alloc(anon_inode_mnt
->mnt_sb
->s_root
, &this);
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
;
131 file
->f_flags
= O_RDWR
;
132 file
->f_op
= (struct file_operations
*)fops
;
133 file
->f_mode
= FMODE_READ
| FMODE_WRITE
;
135 file
->private_data
= priv
;
137 fd_install(fd
, file
);
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
);
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
;
178 static int anon_inode_init(void)
182 error
= register_filesystem(&anon_inode_fs_type
);
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
);
199 mntput(anon_inode_mnt
);
200 err_unregister_filesystem
:
201 unregister_filesystem(&anon_inode_fs_type
);
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
);
220 int kvm_init_anon_inodes(void)
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
)
242 r
= anon_inode_getfd(&fd
, &inode
, &file
, name
, fops
, priv
);
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
);
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
);