-
Notifications
You must be signed in to change notification settings - Fork 0
/
mntfs.c
265 lines (209 loc) · 5.17 KB
/
mntfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* Copyright (C) 2011 Michael Spang */
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/fs_struct.h>
#include <linux/nsproxy.h>
#include <linux/namei.h>
#include <linux/mnt_namespace.h>
#include <linux/mount.h>
#include <linux/slab.h>
static struct inode *mntfs_iget(struct super_block *sb, unsigned long ino);
static int mnt_inode(struct vfsmount *mnt)
{
return mnt->mnt_id + 1000;
}
static int mntfs_dentry_delete(const struct dentry *dentry)
{
return 1;
}
static const struct dentry_operations mntfs_dentry_operations = {
.d_delete = mntfs_dentry_delete,
};
static struct vfsmount *find_mount_by_id(int id)
{
struct nsproxy *nsp = NULL;
struct mnt_namespace *ns = NULL;
struct vfsmount *mnt;
nsp = task_nsproxy(current);
if (nsp)
ns = nsp->mnt_ns;
if (!ns)
return ERR_PTR(-ENOENT);
/* racy */
list_for_each_entry(mnt, &ns->list, mnt_list) {
if (mnt->mnt_id == id) {
mntget(mnt);
return mnt;
}
}
return ERR_PTR(-ENOENT);
}
static struct vfsmount *find_mount_by_dentry(struct dentry *dentry)
{
struct vfsmount *mnt;
unsigned long id;
if (dentry->d_name.len > NAME_MAX)
return ERR_PTR(-ENAMETOOLONG);
if (dentry->d_name.name[0] == '0' && dentry->d_name.len > 1)
return ERR_PTR(-ENOENT);
if (strict_strtoul(dentry->d_name.name, 10, &id))
return ERR_PTR(-ENOENT);
mnt = find_mount_by_id(id);
if (IS_ERR(mnt))
return ERR_CAST(mnt);
return mnt;
}
static struct dentry *mntfs_lookup(struct inode *dir, struct dentry *dentry,
struct nameidata *nd)
{
struct inode *inode;
struct vfsmount *mnt;
mnt = find_mount_by_dentry(dentry);
if (IS_ERR(mnt))
return ERR_CAST(mnt);
inode = mntfs_iget(dir->i_sb, mnt_inode(mnt));
d_add(dentry, inode);
mntput(mnt);
return NULL;
}
static const struct inode_operations mntfs_root_inode_operations = {
.lookup = mntfs_lookup,
};
static int mntfs_readdir(struct file *filp,
void *dirent, filldir_t filldir)
{
struct nsproxy *nsp = NULL;
struct mnt_namespace *ns = NULL;
struct vfsmount *mnt;
int i = 0;
nsp = task_nsproxy(current);
if (nsp)
ns = nsp->mnt_ns;
if (!ns)
goto out;
/* racy */
list_for_each_entry(mnt, &ns->list, mnt_list) {
if (filp->f_pos <= i) {
char name[13];
int len = snprintf(name, sizeof(name), "%d",
mnt->mnt_id);
filldir(dirent, name, len, filp->f_pos,
mnt_inode(mnt), DT_LNK);
filp->f_pos = i + 1;
}
i++;
}
out:
return 0;
}
static const struct file_operations mntfs_root_file_operations = {
.read = generic_read_dir,
.readdir = mntfs_readdir,
.llseek = default_llseek,
};
static int mntfs_readlink(struct dentry *dentry, char __user *buffer,
int buflen)
{
struct vfsmount *mnt;
struct path path;
char *namebuf;
char *name;
int error = -ENOENT;
mnt = find_mount_by_dentry(dentry);
if (IS_ERR(mnt))
return PTR_ERR(mnt);
namebuf = kmalloc(PATH_MAX, GFP_KERNEL);
if (!namebuf)
goto out_mntput;
path.mnt = mnt;
path.dentry = mnt->mnt_root;
name = d_path(&path, namebuf, PATH_MAX);
if (IS_ERR(name))
goto out_free;
error = vfs_readlink(dentry, buffer, buflen, name);
out_free:
kfree(namebuf);
out_mntput:
mntput(mnt);
return error;
}
static void *mntfs_follow(struct dentry *dentry, struct nameidata *nd)
{
struct vfsmount *mnt;
path_put(&nd->path);
mnt = find_mount_by_dentry(dentry);
if (IS_ERR(mnt))
return ERR_CAST(mnt);
nd->path.mnt = mnt;
nd->path.dentry = mnt->mnt_root;
path_get(&nd->path);
mntput(mnt);
return NULL;
}
static const struct inode_operations mntfs_link_inode_operations = {
.readlink = mntfs_readlink,
.follow_link = mntfs_follow,
};
static struct inode *mntfs_iget(struct super_block *sb, unsigned long ino)
{
struct inode *inode;
inode = iget_locked(sb, ino);
if (!inode)
return ERR_PTR(-ENOMEM);
if (!(inode->i_state & I_NEW))
return inode;
inode->i_nlink = 1;
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
if (ino == 1) {
inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
inode->i_fop = &mntfs_root_file_operations;
inode->i_op = &mntfs_root_inode_operations;
} else {
inode->i_mode = S_IFLNK | S_IRWXUGO;
inode->i_op = &mntfs_link_inode_operations;
}
unlock_new_inode(inode);
return inode;
}
static struct super_operations mntfs_super_operations = {
.statfs = simple_statfs,
};
static int mntfs_fill_super(struct super_block *sb, void *data, int silent)
{
struct inode *root;
sb->s_op = &mntfs_super_operations;
root = mntfs_iget(sb, 1);
if (IS_ERR(root))
return PTR_ERR(root);
sb->s_root = d_alloc_root(root);
sb->s_d_op = &mntfs_dentry_operations;
if (!sb->s_root)
return -ENOMEM;
return 0;
}
static struct dentry *mntfs_mount(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data)
{
return mount_nodev(fs_type, flags, data, mntfs_fill_super);
}
static struct file_system_type mntfs_type = {
.name = "mntfs",
.mount = mntfs_mount,
.kill_sb = kill_anon_super,
.owner = THIS_MODULE,
};
static int __init mntfs_init(void)
{
register_filesystem(&mntfs_type);
return 0;
}
static void __exit mntfs_exit(void)
{
unregister_filesystem(&mntfs_type);
}
module_init(mntfs_init);
module_exit(mntfs_exit);
MODULE_AUTHOR("Michael Spang");
MODULE_LICENSE("Dual BSD/GPL");