OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.swp.api/] [openmcapi/] [1.0/] [libmcapi/] [shm/] [linux/] [kmod/] [compat_anon_inodes.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*
2
 *  Derived from kvm-kmod/anon_inodes.c
3
 */
4
 
5
/*
6
 *
7
 *  fs/anon_inodes.c
8
 *
9
 *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
10
 *
11
 *  Thanks to Arnd Bergmann for code review and suggestions.
12
 *  More changes for Thomas Gleixner suggestions.
13
 *
14
 */
15
 
16
#include <linux/version.h>
17
#include <linux/file.h>
18
#include <linux/poll.h>
19
#include <linux/slab.h>
20
#include <linux/init.h>
21
#include <linux/fs.h>
22
#include <linux/mount.h>
23
#include <linux/module.h>
24
#include <linux/kernel.h>
25
#include <linux/magic.h>
26
 
27
#include <asm/uaccess.h>
28
 
29
/* anon_inodes on RHEL >= 5.2 is equivalent to 2.6.27 version */
30
#ifdef RHEL_RELEASE_CODE
31
#  if (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(5,2)) && defined(CONFIG_ANON_INODES)
32
#    define RHEL_ANON_INODES
33
#  endif
34
#endif
35
 
36
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) && !defined(RHEL_ANON_INODES)
37
 
38
static struct vfsmount *anon_inode_mnt __read_mostly;
39
static struct inode *anon_inode_inode;
40
static struct file_operations anon_inode_fops;
41
 
42
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17)
43
 
44
static int anon_inodefs_get_sb(struct file_system_type *fs_type, int flags,
45
                               const char *dev_name, void *data,
46
                               struct vfsmount *mnt)
47
{
48
        return get_sb_pseudo(fs_type, "mcomm_anon_inode:", NULL, 0x99700426, mnt);
49
}
50
 
51
#else
52
 
53
static struct super_block *anon_inodefs_get_sb(struct file_system_type *fs_type,
54
                                            int flags, const char *dev_name,
55
                                            void *data)
56
{
57
        return get_sb_pseudo(fs_type, "mcomm_anon_inode:", NULL, 0x99700426);
58
}
59
 
60
#endif
61
 
62
static int anon_inodefs_delete_dentry(struct dentry *dentry)
63
{
64
        /*
65
         * We faked vfs to believe the dentry was hashed when we created it.
66
         * Now we restore the flag so that dput() will work correctly.
67
         */
68
        dentry->d_flags |= DCACHE_UNHASHED;
69
        return 1;
70
}
71
 
72
static struct file_system_type anon_inode_fs_type = {
73
        .name           = "mcomm_anon_inodefs",
74
        .get_sb         = anon_inodefs_get_sb,
75
        .kill_sb        = kill_anon_super,
76
};
77
static struct dentry_operations anon_inodefs_dentry_operations = {
78
        .d_delete       = anon_inodefs_delete_dentry,
79
};
80
 
81
/**
82
 * anon_inode_getfd - creates a new file instance by hooking it up to and
83
 *                    anonymous inode, and a dentry that describe the "class"
84
 *                    of the file
85
 *
86
 * @name:    [in]    name of the "class" of the new file
87
 * @fops     [in]    file operations for the new file
88
 * @priv     [in]    private data for the new file (will be file's private_data)
89
 *
90
 * Creates a new file by hooking it on a single inode. This is useful for files
91
 * that do not need to have a full-fledged inode in order to operate correctly.
92
 * All the files created with anon_inode_getfd() will share a single inode, by
93
 * hence saving memory and avoiding code duplication for the file/inode/dentry
94
 * setup. Returns new descriptor or -error.
95
 */
96
int mcomm_anon_inode_getfd(const char *name, const struct file_operations *fops,
97
                     void *priv, int flags)
98
{
99
        struct qstr this;
100
        struct dentry *dentry;
101
        struct inode *inode;
102
        struct file *file;
103
        int error, fd;
104
 
105
        if (IS_ERR(anon_inode_inode))
106
                return -ENODEV;
107
 
108
        if (fops->owner && !try_module_get(fops->owner))
109
                return ERR_PTR(-ENOENT);
110
 
111
        file = get_empty_filp();
112
        if (!file)
113
                return -ENFILE;
114
 
115
        inode = igrab(anon_inode_inode);
116
        if (IS_ERR(inode)) {
117
                error = PTR_ERR(inode);
118
                goto err_put_filp;
119
        }
120
 
121
        error = get_unused_fd();
122
        if (error < 0)
123
                goto err_iput;
124
        fd = error;
125
 
126
        /*
127
         * Link the inode to a directory entry by creating a unique name
128
         * using the inode sequence number.
129
         */
130
        error = -ENOMEM;
131
        this.name = name;
132
        this.len = strlen(name);
133
        this.hash = 0;
134
        dentry = d_alloc(anon_inode_mnt->mnt_sb->s_root, &this);
135
        if (!dentry)
136
                goto err_put_unused_fd;
137
        dentry->d_op = &anon_inodefs_dentry_operations;
138
        /* Do not publish this dentry inside the global dentry hash table */
139
        dentry->d_flags &= ~DCACHE_UNHASHED;
140
        d_instantiate(dentry, inode);
141
 
142
        file->f_vfsmnt = mntget(anon_inode_mnt);
143
        file->f_dentry = dentry;
144
        file->f_mapping = inode->i_mapping;
145
 
146
        file->f_pos = 0;
147
        file->f_flags = O_RDWR;
148
        file->f_op = (struct file_operations *)fops;
149
        file->f_mode = FMODE_READ | FMODE_WRITE;
150
        file->f_version = 0;
151
        file->private_data = priv;
152
 
153
        fd_install(fd, file);
154
 
155
        return fd;
156
 
157
err_put_unused_fd:
158
        put_unused_fd(fd);
159
err_iput:
160
        iput(inode);
161
err_put_filp:
162
        fput(file);
163
        module_put(fops->owner);
164
        return error;
165
}
166
 
167
/*
168
 * A single inode exist for all anon_inode files. Contrary to pipes,
169
 * anon_inode inodes has no per-instance data associated, so we can avoid
170
 * the allocation of multiple of them.
171
 */
172
static struct inode *anon_inode_mkinode(void)
173
{
174
        struct inode *inode = new_inode(anon_inode_mnt->mnt_sb);
175
 
176
        if (!inode)
177
                return ERR_PTR(-ENOMEM);
178
 
179
        inode->i_fop = &anon_inode_fops;
180
 
181
        /*
182
         * Mark the inode dirty from the very beginning,
183
         * that way it will never be moved to the dirty
184
         * list because mark_inode_dirty() will think
185
         * that it already _is_ on the dirty list.
186
         */
187
        inode->i_state = I_DIRTY;
188
        inode->i_mode = S_IRUSR | S_IWUSR;
189
        inode->i_uid = current->fsuid;
190
        inode->i_gid = current->fsgid;
191
        inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
192
        return inode;
193
}
194
 
195
static int anon_inode_init(void)
196
{
197
        int error;
198
 
199
        error = register_filesystem(&anon_inode_fs_type);
200
        if (error)
201
                goto err_exit;
202
        anon_inode_mnt = kern_mount(&anon_inode_fs_type);
203
        if (IS_ERR(anon_inode_mnt)) {
204
                error = PTR_ERR(anon_inode_mnt);
205
                goto err_unregister_filesystem;
206
        }
207
        anon_inode_inode = anon_inode_mkinode();
208
        if (IS_ERR(anon_inode_inode)) {
209
                error = PTR_ERR(anon_inode_inode);
210
                goto err_mntput;
211
        }
212
 
213
        return 0;
214
 
215
err_mntput:
216
        mntput(anon_inode_mnt);
217
err_unregister_filesystem:
218
        unregister_filesystem(&anon_inode_fs_type);
219
err_exit:
220
        return -ENOMEM;
221
}
222
 
223
int mcomm_init_anon_inodes(void)
224
{
225
        return anon_inode_init();
226
}
227
 
228
void mcomm_exit_anon_inodes(void)
229
{
230
        iput(anon_inode_inode);
231
        mntput(anon_inode_mnt);
232
        unregister_filesystem(&anon_inode_fs_type);
233
}
234
 
235
#else
236
 
237
int mcomm_init_anon_inodes(void)
238
{
239
        return 0;
240
}
241
 
242
void mcomm_exit_anon_inodes(void)
243
{
244
}
245
 
246
#undef anon_inode_getfd
247
 
248
int anon_inode_getfd(const char *name,
249
                         const struct file_operations *fops,
250
                         void *priv, int flags);
251
 
252
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26) && !defined(RHEL_ANON_INODES)
253
 
254
int mcomm_anon_inode_getfd(const char *name,
255
                         const struct file_operations *fops,
256
                         void *priv, int flags)
257
{
258
        int r;
259
        int fd;
260
        struct inode *inode;
261
        struct file *file;
262
 
263
        r = anon_inode_getfd(&fd, &inode, &file, name, fops, priv);
264
        if (r < 0)
265
                return r;
266
        return fd;
267
}
268
 
269
#elif LINUX_VERSION_CODE == KERNEL_VERSION(2,6,26) && !defined(RHEL_ANON_INODES)
270
 
271
int mcomm_anon_inode_getfd(const char *name,
272
                         const struct file_operations *fops,
273
                         void *priv, int flags)
274
{
275
        return anon_inode_getfd(name, fops, priv);
276
}
277
 
278
#else
279
 
280
int mcomm_anon_inode_getfd(const char *name,
281
                         const struct file_operations *fops,
282
                         void *priv, int flags)
283
{
284
        return anon_inode_getfd(name, fops, priv, flags);
285
}
286
 
287
#endif
288
 
289
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.