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/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>
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
,
34 return get_sb_pseudo(fs_type
, "anon_inode:", NULL
, ANON_INODE_FS_MAGIC
,
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
48 static int anon_set_page_dirty(struct page
*page
)
53 static const struct address_space_operations anon_aops
= {
54 .set_page_dirty
= anon_set_page_dirty
,
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"
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)
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
)
78 struct dentry
*dentry
;
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.
94 this.len
= strlen(name
);
96 dentry
= d_alloc(anon_inode_mnt
->mnt_sb
->s_root
, &this);
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
105 atomic_inc(&anon_inode_inode
->i_count
);
107 d_instantiate(dentry
, anon_inode_inode
);
110 file
= alloc_file(anon_inode_mnt
, dentry
,
111 FMODE_READ
| FMODE_WRITE
, fops
);
114 file
->f_mapping
= anon_inode_inode
->i_mapping
;
117 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
119 file
->private_data
= priv
;
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"
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)
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
)
153 error
= get_unused_fd_flags(flags
);
158 file
= anon_inode_getfile(name
, fops
, priv
, flags
);
160 error
= PTR_ERR(file
);
161 goto err_put_unused_fd
;
163 fd_install(fd
, file
);
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
);
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
;
203 static int __init
anon_inode_init(void)
207 error
= register_filesystem(&anon_inode_fs_type
);
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
);
224 mntput(anon_inode_mnt
);
225 err_unregister_filesystem
:
226 unregister_filesystem(&anon_inode_fs_type
);
228 panic(KERN_ERR
"anon_inode_init() failed (%d)\n", error
);
231 fs_initcall(anon_inode_init
);