-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
274 lines (224 loc) · 5.8 KB
/
main.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
266
267
268
269
270
271
272
273
274
/*
* Copyright (c) 1998-2011 Erez Zadok
* Copyright (c) 2009 Shrikar Archak
* Copyright (c) 2003-2011 Stony Brook University
* Copyright (c) 2003-2011 The Research Foundation of SUNY
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include "wrapfs.h"
#include <linux/module.h>
#include <linux/string.h>
#include <linux/parser.h>
static int wrapfs_parse_mount_options(char *options, struct super_block *sb);
/*
* There is no need to lock the wrapfs_super_info's rwsem as there is no
* way anyone can have a reference to the superblock at this point in time.
*/
static int wrapfs_read_super(struct super_block *sb, void *raw_data, int silent)
{
int err = 0;
struct super_block *lower_sb;
struct path lower_path;
char *dev_name = (char *) raw_data;
struct inode *inode;
if (!dev_name) {
printk(KERN_ERR
"wrapfs: read_super: missing dev_name argument\n");
err = -EINVAL;
goto out;
}
/* parse lower path */
err = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
&lower_path);
if (err) {
printk(KERN_ERR "wrapfs: error accessing "
"lower directory '%s'\n", dev_name);
goto out;
}
/* allocate superblock private data */
sb->s_fs_info = kzalloc(sizeof(struct wrapfs_sb_info), GFP_KERNEL);
if (!WRAPFS_SB(sb)) {
printk(KERN_CRIT "wrapfs: read_super: out of memory\n");
err = -ENOMEM;
goto out_free;
}
/* set the lower superblock field of upper superblock */
lower_sb = lower_path.dentry->d_sb;
atomic_inc(&lower_sb->s_active);
wrapfs_set_lower_super(sb, lower_sb);
/* inherit maxbytes from lower file system */
sb->s_maxbytes = lower_sb->s_maxbytes;
/*
* Our c/m/atime granularity is 1 ns because we may stack on file
* systems whose granularity is as good.
*/
sb->s_time_gran = 1;
sb->s_op = &wrapfs_sops;
/* get a new inode and allocate our root dentry */
inode = wrapfs_iget(sb, lower_path.dentry->d_inode);
if (IS_ERR(inode)) {
err = PTR_ERR(inode);
goto out_sput;
}
sb->s_root = d_alloc_root(inode);
if (!sb->s_root) {
err = -ENOMEM;
goto out_iput;
}
d_set_d_op(sb->s_root, &wrapfs_dops);
/* link the upper and lower dentries */
sb->s_root->d_fsdata = NULL;
err = new_dentry_private_data(sb->s_root);
if (err)
goto out_freeroot;
/* if get here: cannot have error */
/* set the lower dentries for s_root */
wrapfs_set_lower_path(sb->s_root, &lower_path);
/*
* No need to call interpose because we already have a positive
* dentry, which was instantiated by d_alloc_root. Just need to
* d_rehash it.
*/
d_rehash(sb->s_root);
if (!silent)
printk(KERN_INFO
"wrapfs: mounted on top of %s type %s\n",
dev_name, lower_sb->s_type->name);
goto out; /* all is well */
/* no longer needed: free_dentry_private_data(sb->s_root); */
out_freeroot:
dput(sb->s_root);
out_iput:
iput(inode);
out_sput:
/* drop refs we took earlier */
atomic_dec(&lower_sb->s_active);
kfree(WRAPFS_SB(sb));
sb->s_fs_info = NULL;
out_free:
path_put(&lower_path);
out:
return err;
}
enum {
Opt_debug,
Opt_mmap
};
static const match_table_t wrapfs_tokens = {
{Opt_debug, "debug=%u"},
{Opt_mmap, "mmap"},
};
static int wrapfs_parse_mount_options(char *options, struct super_block *sb)
{
int err = 0;
char *p;
substring_t args[MAX_OPT_ARGS];
unsigned long opt = 0; /* FIXME: for test, set in the sb info struct instead */
int option;
//WDBG("entry\n");
if (!options) {
err = -EINVAL;
goto out;
}
WRAPFS_SB(sb)->aops_enabled = 0;
/* Tokenize string on commas */
while ((p = strsep(&options, ",")) != NULL) {
int token;
if (!p || !*p)
continue;
/* initialize to 0 */
args[0].to = args[0].from = 0;
token = match_token(p, wrapfs_tokens, args);
switch (token) {
case Opt_debug:
if (&args[0]) {
if (match_int(&args[0], &option)) {
err = -EINVAL;
goto out;
}
opt = option;
} else {
opt = 2; /* default */
}
WDBG("debug=%lu", (long unsigned int)opt);
break;
case Opt_mmap:
WRAPFS_SB(sb)->aops_enabled = 1;
break;
default:
goto out;
}
}
out:
return err;
}
struct dentry *wrapfs_mount(struct file_system_type *fs_type, int flags,
const char *dev_name, void *raw_data)
{
void *lower_path_name = (void *) dev_name;
struct dentry *d = NULL;
d = mount_nodev(fs_type, flags, lower_path_name,
wrapfs_read_super);
if (d) {
wrapfs_parse_mount_options((char *)raw_data, d->d_sb);
#ifdef WRAPFS_CRYPTO
wrapfs_set_enc_key(d->d_sb , NULL);
#endif
}
return d;
}
static struct file_system_type wrapfs_fs_type = {
.owner = THIS_MODULE,
.name = WRAPFS_NAME,
.mount = wrapfs_mount,
.kill_sb = generic_shutdown_super,
.fs_flags = FS_REVAL_DOT,
};
/*
long ioctl_funcs(struct file *filp,unsigned int cmd, unsigned long arg)
{
int ret=0;
printk("In IOCTL Func");
switch(cmd) {
case IOCTL_SETEKEY:
printk("Hello ioctl world");
break;
}
return ret;
}
*/
static int __init init_wrapfs_fs(void)
{
int err;
pr_info("Registering wrapfs " WRAPFS_VERSION "\n");
err = wrapfs_init_inode_cache();
if (err)
goto out;
err = wrapfs_init_dentry_cache();
if (err)
goto out;
err = register_filesystem(&wrapfs_fs_type);
out:
if (err) {
wrapfs_destroy_inode_cache();
wrapfs_destroy_dentry_cache();
}
return err;
}
static void __exit exit_wrapfs_fs(void)
{
wrapfs_destroy_inode_cache();
wrapfs_destroy_dentry_cache();
unregister_filesystem(&wrapfs_fs_type);
pr_info("Completed wrapfs module unload\n");
}
MODULE_AUTHOR("Erez Zadok, Filesystems and Storage Lab, Stony Brook University"
" (http://www.fsl.cs.sunysb.edu/)");
MODULE_DESCRIPTION("Wrapfs " WRAPFS_VERSION
" (http://wrapfs.filesystems.org/)");
MODULE_LICENSE("GPL");
module_init(init_wrapfs_fs);
module_exit(exit_wrapfs_fs);