Blame view
fs/anon_inodes.c
4.49 KB
5dc8bf813
|
1 2 3 4 5 6 7 8 9 |
/* * fs/anon_inodes.c * * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> * * Thanks to Arnd Bergmann for code review and suggestions. * More changes for Thomas Gleixner suggestions. * */ |
a99bbaf5e
|
10 |
#include <linux/cred.h> |
5dc8bf813
|
11 12 |
#include <linux/file.h> #include <linux/poll.h> |
a99bbaf5e
|
13 |
#include <linux/sched.h> |
5dc8bf813
|
14 15 16 17 18 19 20 |
#include <linux/init.h> #include <linux/fs.h> #include <linux/mount.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/magic.h> #include <linux/anon_inodes.h> |
7c0f6ba68
|
21 |
#include <linux/uaccess.h> |
5dc8bf813
|
22 23 24 |
static struct vfsmount *anon_inode_mnt __read_mostly; static struct inode *anon_inode_inode; |
5dc8bf813
|
25 |
|
b9aff027b
|
26 27 28 29 30 31 32 33 |
/* * anon_inodefs_dname() is called from d_path(). */ static char *anon_inodefs_dname(struct dentry *dentry, char *buffer, int buflen) { return dynamic_dname(dentry, buffer, buflen, "anon_inode:%s", dentry->d_name.name); } |
c74a1cbb3
|
34 35 36 |
static const struct dentry_operations anon_inodefs_dentry_operations = { .d_dname = anon_inodefs_dname, }; |
ca7068c41
|
37 38 39 |
static struct dentry *anon_inodefs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data) { |
75c5a52da
|
40 |
return mount_pseudo(fs_type, "anon_inode:", NULL, |
ca7068c41
|
41 |
&anon_inodefs_dentry_operations, ANON_INODE_FS_MAGIC); |
ca7068c41
|
42 43 44 45 46 47 48 |
} static struct file_system_type anon_inode_fs_type = { .name = "anon_inodefs", .mount = anon_inodefs_mount, .kill_sb = kill_anon_super, }; |
5dc8bf813
|
49 |
/** |
c0d8768af
|
50 51 52 |
* anon_inode_getfile - creates a new file instance by hooking it up to an * anonymous inode, and a dentry that describe the "class" * of the file |
5dc8bf813
|
53 |
* |
5dc8bf813
|
54 |
* @name: [in] name of the "class" of the new file |
7d9dbca34
|
55 56 57 |
* @fops: [in] file operations for the new file * @priv: [in] private data for the new file (will be file's private_data) * @flags: [in] flags |
5dc8bf813
|
58 59 60 |
* * Creates a new file by hooking it on a single inode. This is useful for files * that do not need to have a full-fledged inode in order to operate correctly. |
562787a5c
|
61 |
* All the files created with anon_inode_getfile() will share a single inode, |
5dc8bf813
|
62 |
* hence saving memory and avoiding code duplication for the file/inode/dentry |
562787a5c
|
63 |
* setup. Returns the newly created file* or an error pointer. |
5dc8bf813
|
64 |
*/ |
562787a5c
|
65 66 67 |
struct file *anon_inode_getfile(const char *name, const struct file_operations *fops, void *priv, int flags) |
5dc8bf813
|
68 |
{ |
5dc8bf813
|
69 |
struct file *file; |
5dc8bf813
|
70 71 |
if (IS_ERR(anon_inode_inode)) |
562787a5c
|
72 |
return ERR_PTR(-ENODEV); |
5dc8bf813
|
73 |
|
e3a2a0d4e
|
74 |
if (fops->owner && !try_module_get(fops->owner)) |
562787a5c
|
75 |
return ERR_PTR(-ENOENT); |
5dc8bf813
|
76 77 |
/* |
96fdc72dd
|
78 |
* We know the anon_inode inode count is always greater than zero, |
7de9c6ee3
|
79 |
* so ihold() is safe. |
96fdc72dd
|
80 |
*/ |
7de9c6ee3
|
81 |
ihold(anon_inode_inode); |
52c91f8b3
|
82 83 |
file = alloc_file_pseudo(anon_inode_inode, anon_inode_mnt, name, flags & (O_ACCMODE | O_NONBLOCK), fops); |
39b652527
|
84 |
if (IS_ERR(file)) |
52c91f8b3
|
85 |
goto err; |
96fdc72dd
|
86 |
file->f_mapping = anon_inode_inode->i_mapping; |
5dc8bf813
|
87 |
|
5dc8bf813
|
88 |
file->private_data = priv; |
562787a5c
|
89 |
return file; |
52c91f8b3
|
90 91 |
err: iput(anon_inode_inode); |
562787a5c
|
92 |
module_put(fops->owner); |
39b652527
|
93 |
return file; |
562787a5c
|
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
} EXPORT_SYMBOL_GPL(anon_inode_getfile); /** * anon_inode_getfd - creates a new file instance by hooking it up to an * anonymous inode, and a dentry that describe the "class" * of the file * * @name: [in] name of the "class" of the new file * @fops: [in] file operations for the new file * @priv: [in] private data for the new file (will be file's private_data) * @flags: [in] flags * * Creates a new file by hooking it on a single inode. This is useful for files * that do not need to have a full-fledged inode in order to operate correctly. * All the files created with anon_inode_getfd() will share a single inode, * hence saving memory and avoiding code duplication for the file/inode/dentry * setup. Returns new descriptor or an error code. */ int anon_inode_getfd(const char *name, const struct file_operations *fops, void *priv, int flags) { int error, fd; struct file *file; error = get_unused_fd_flags(flags); if (error < 0) return error; fd = error; file = anon_inode_getfile(name, fops, priv, flags); if (IS_ERR(file)) { error = PTR_ERR(file); goto err_put_unused_fd; } |
5dc8bf813
|
129 |
fd_install(fd, file); |
2030a42ce
|
130 |
return fd; |
5dc8bf813
|
131 132 133 |
err_put_unused_fd: put_unused_fd(fd); |
5dc8bf813
|
134 135 |
return error; } |
d6d281684
|
136 |
EXPORT_SYMBOL_GPL(anon_inode_getfd); |
5dc8bf813
|
137 |
|
5dc8bf813
|
138 139 |
static int __init anon_inode_init(void) { |
5dc8bf813
|
140 |
anon_inode_mnt = kern_mount(&anon_inode_fs_type); |
75c5a52da
|
141 142 143 |
if (IS_ERR(anon_inode_mnt)) panic("anon_inode_init() kernel mount failed (%ld) ", PTR_ERR(anon_inode_mnt)); |
5dc8bf813
|
144 |
|
75c5a52da
|
145 146 147 148 149 150 |
anon_inode_inode = alloc_anon_inode(anon_inode_mnt->mnt_sb); if (IS_ERR(anon_inode_inode)) panic("anon_inode_init() inode allocation failed (%ld) ", PTR_ERR(anon_inode_inode)); return 0; |
5dc8bf813
|
151 152 153 |
} fs_initcall(anon_inode_init); |