-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsuper.c
451 lines (357 loc) · 12.1 KB
/
super.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "ftpfs.h"
#include "net.h"
#include "operations.h"
MODULE_AUTHOR("Pawel Pikula, Michal Niec");
MODULE_DESCRIPTION("Ftp File System");
static ssize_t ftpfs_read_file(struct file *filp,char *buf, size_t count, loff_t *offset);
static ssize_t ftpfs_write_file(struct file *filp, const char *buf, size_t count, loff_t *offset);
static int ftpfs_create_file(struct inode *inode,struct dentry * dentry,int, struct nameidata *nameidata);
static int ftpfs_mkdir(struct inode *inode,struct dentry* dentry,int unk);
static int ftpfs_rmdir(struct inode *inode, struct dentry* dentry);
static int ftpfs_rename(struct inode* inode_src, struct dentry* dentry_src,struct inode* inode_dst,struct dentry* dentry_dst );
static int ftpfs_unlink(struct inode* inode, struct dentry* dentry);
static struct file_operations ftpfs_file_ops = {
.read = ftpfs_read_file,
.write = ftpfs_write_file,
};
static struct inode_operations ftpfs_inode_ops={
.create = ftpfs_create_file,
.mkdir = ftpfs_mkdir,
.rmdir = ftpfs_rmdir,
.rename = ftpfs_rename,
.lookup = simple_lookup,
.unlink = ftpfs_unlink,
};
struct super_operations ftpfs_super_ops = {
.statfs = simple_statfs,
.drop_inode = generic_delete_inode,
};
static const struct super_operations simple_super_operations = {
.statfs = simple_statfs,
};
struct tree_descr * ftpfs_ls(struct ftp_sb_info *info)
{
struct tree_descr *files;
char *send_buffer,*recv_buffer,*tmp;
int size;
char file_name[256];
int r;
int i;
char *creds;
send_buffer = kmalloc(SND_BUFFER_SIZE,GFP_KERNEL);
recv_buffer = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
tmp = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
creds=kmalloc(12,GFP_KERNEL);
sprintf(tmp, "LIST\r\n");
send_reply(info->control, tmp);
r=read_response(info->control, recv_buffer);
read_response(info->data, recv_buffer);
printk("FTPFS::ls %s\n\n",recv_buffer);
tmp = recv_buffer;
i=1;
while((tmp = strchr(tmp+1,'\n'))){
i++;
}
files = kmalloc(sizeof(struct tree_descr)*(i+2),GFP_KERNEL);
i=0;
tmp=recv_buffer;
while((tmp = strchr(tmp+1,'\n'))){
memset(file_name,0,256);
sscanf(tmp+1,"%s %*d %*d %*d %d %*s %*d %*d:%*d%s\n",creds,&size,file_name);
printk("%s => %d\n",file_name,size);
files[i].name = kmalloc(strlen(file_name)+1,GFP_KERNEL);
strcpy(files[i].name,file_name);
files[i].ops = &ftpfs_file_ops;
files[i].mode = S_IWUSR|S_IRUGO;
if(creds[0] == 'd' ) files[i].mode |= S_IFDIR;
else if(creds[0] =='-') files[i].mode |= S_IFREG;
i++;
if( unlikely(i==1)) {
files[i].name=0;
files[i].ops=NULL;
files[i].mode=0;
i++;
}
}
files[i].name="";
files[i].ops = NULL;
files[i].mode = 0;
read_response(info->control,recv_buffer);
kfree(creds);
kfree(send_buffer);
kfree(recv_buffer);
return files;
}
static int parse_address(const char* address,struct ftp_sb_params *params)
{
const char *host;
char host_buff[16];
char *pass;
char *init_dir;
int i1,i2,i3,i4;
memset(params->username,0,sizeof(params->username));
memset(params->password,0,sizeof(params->password));
memset(params->initial_dir,0,sizeof(params->initial_dir));
host = strchr(address,'@');
if(host){
pass = strchr(address,':');
memcpy(params->username,address,pass-address);
memcpy(params->password,pass+1,host-pass-1);
}else{
host=address;
}
init_dir = strchr(host,':');
if(init_dir){
memcpy(host_buff,host+1,16);
if(strlen(init_dir+1)!=0)
strcpy(params->initial_dir,init_dir+1);
else
strcpy(params->initial_dir,"~");
}else{
strncpy(host_buff,host,16);
strcpy(params->initial_dir,"/");
}
sscanf(host_buff,"%d.%d.%d.%d",&i1,&i2,&i3,&i4);
params->host = (i1<<24)+(i2<<16)+(i3<<8)+i4;
return 0;
}
// extending simple_fill_super adds inode operations
static int ftpfs_simple_fill_super(struct super_block *s,unsigned long magic,struct tree_descr* files)
{
struct inode* root_inode;
struct dentry* root;
struct dentry *dentry;
struct inode* inode;
int i;
s->s_blocksize = PAGE_CACHE_SIZE;
s->s_blocksize_bits = PAGE_CACHE_SHIFT;
s->s_magic = magic;
s->s_op = &simple_super_operations;
s->s_time_gran = 1;
root_inode = new_inode(s);
if(!root_inode) return -ENOMEM;
root_inode->i_op = &ftpfs_inode_ops;
//root_inode->i_op = &simple_dir_inode_operations;
root_inode->i_fop = &simple_dir_operations;
root_inode->i_ino = 1;
root_inode->i_mode = S_IFDIR | 0755;
root_inode->i_atime = root_inode->i_mtime = root_inode->i_ctime = CURRENT_TIME;
root_inode->i_nlink = 2;
root = d_alloc_root(root_inode);
if(!root){
iput(root_inode);
return -ENOMEM;
}
for (i = 0; !files->name || files->name[0]; i++, files++)
{
if (!files->name) continue;
if (unlikely(i == 1))
printk(KERN_WARNING "%s: %s passed in a files array with an index of 1!\n", __func__,s->s_type->name);
dentry = d_alloc_name(root, files->name);
if(!dentry) goto out;
inode =new_inode(s);
if(!inode) goto out;
inode->i_mode = files->mode;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
inode->i_fop = files->ops;
inode->i_ino = i;
d_add(dentry, inode);
}
s->s_root=root;
return 0;
out:
d_genocide(root);
dput(root);
return -ENOMEM;
}
static int ftpfs_fill_super(struct super_block *sb, void *data, int silent)
{
struct ftp_sb_params params;
struct ftp_sb_info *info;
struct tree_descr *files;
info = kmalloc(sizeof(struct ftp_sb_info),GFP_KERNEL);
parse_address(data,¶ms);
info->params = params;
ftpfs_init_connection(info);
ftpfs_cd(params.initial_dir,info);
sb->s_fs_info = info;
printk("FTPFS::params = username:%s, password:%s, host:%x, init:%s",params.username,params.password,params.host,params.initial_dir);
files= ftpfs_ls(info);
//return simple_fill_super(sb, FTPFS_MAGIC,files);
return ftpfs_simple_fill_super(sb,FTPFS_MAGIC,files);
}
static struct dentry* ftpfs_mount(struct file_system_type *fst, int flags, const char *dev_name, void *data)
{
return mount_nodev(fst, flags,(void *) dev_name, ftpfs_fill_super);
}
static struct file_system_type ftpfs_type = {
.owner = THIS_MODULE,
.name= "ftpfs",
.mount= ftpfs_mount,
.kill_sb= kill_litter_super,
};
static ssize_t ftpfs_read_file(struct file *filp, char *buf, size_t count, loff_t *offset)
{
int r;
const char* filename=filp->f_dentry->d_name.name;
char *recv_buffer,*command;
struct ftp_sb_info* ftp_info;
if(*offset==0){
ftp_info = extract_info(filp);
recv_buffer = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
command = kmalloc(RCV_BUFFER_SIZE, GFP_KERNEL);
sprintf(command,"TYPE I\r\n");
send_reply(ftp_info->control, command);
r=read_response(ftp_info->control, recv_buffer); /* "200 Switching
* to binary mode" */
ftpfs_init_data_connection(ftp_info); /* PASV stuff*/
sprintf(command,"RETR %s\r\n",filename);
send_reply(ftp_info->control, command);
r=read_response(ftp_info->control, recv_buffer); /* "Opening BINARY blalal"*/
r=read_response(ftp_info->data, recv_buffer); /* Actual data */
*offset +=r;
memcpy(buf,recv_buffer,r);
read_response(ftp_info->control, recv_buffer); /* Transer
* complete */
kfree(command);
kfree(recv_buffer);
return r;
}
else
return 0;
}
static ssize_t ftpfs_write_file(struct file *filp, const char *buf, size_t count, loff_t *offset)
{
int r;
const char* filename=filp->f_dentry->d_name.name;
char *recv_buffer,*command,*send_buffer;
struct ftp_sb_info* ftp_info = extract_info(filp);
if( *offset==0 && count < SND_BUFFER_SIZE ){
recv_buffer = kmalloc(RCV_BUFFER_SIZE, GFP_KERNEL);
send_buffer = kmalloc(SND_BUFFER_SIZE, GFP_KERNEL);
command = kmalloc(RCV_BUFFER_SIZE, GFP_KERNEL);
memset(send_buffer,0,SND_BUFFER_SIZE);
sprintf(command,"TYPE I\r\n");
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control, recv_buffer); /* switching to binary mode */
ftpfs_init_data_connection(ftp_info); /*PASV stuff*/
sprintf(command,"STOR %s\r\n",filename);
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control,recv_buffer); /* opening binary*/
memcpy(send_buffer,buf,count);
send_reply(ftp_info->data,send_buffer);
sock_release(ftp_info->data);
r=read_response(ftp_info->control,recv_buffer);
kfree(command);
kfree(send_buffer);
kfree(recv_buffer);
return count;
}else
return 0;
}
static int ftpfs_create_file(struct inode *inode,struct dentry * dentry,int mode, struct nameidata *nameidata)
{
const char* filename;
int r;
char *command,*receive_buffer;
//retreive control socket
struct ftp_sb_info* ftp_info = (struct ftp_sb_info*)inode->i_sb->s_fs_info;
filename=dentry->d_name.name;
command = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
receive_buffer = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
sprintf(command,"APPE %s\r\n",filename);
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control,receive_buffer);
ftpfs_init_data_connection(ftp_info);
sock_release(ftp_info->data);
r=read_response(ftp_info->control,receive_buffer);
kfree(command);
kfree(receive_buffer);
return 0;
}
static int ftpfs_mkdir(struct inode *inode,struct dentry* dentry,int mode)
{
const char* dirname;
int r;
char *command,*receive_buffer;
//retreive control socket
struct ftp_sb_info* ftp_info = (struct ftp_sb_info*)inode->i_sb->s_fs_info;
dirname=dentry->d_name.name;
command = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
receive_buffer = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
sprintf(command,"MKD %s\r\n",dirname);
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control,receive_buffer);
kfree(command);
kfree(receive_buffer);
return 0;
}
static int ftpfs_rmdir(struct inode *inode, struct dentry* dentry)
{
const char* dirname;
int r;
char *command,*receive_buffer;
//retreive control socket
struct ftp_sb_info* ftp_info = (struct ftp_sb_info*)inode->i_sb->s_fs_info;
dirname=dentry->d_name.name;
command = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
receive_buffer = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
sprintf(command,"RMD %s\r\n",dirname);
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control,receive_buffer);
kfree(command);
kfree(receive_buffer);
return 0;
}
static int ftpfs_rename(struct inode* inode_src, struct dentry* dentry_src,struct inode* inode_dst,struct dentry* dentry_dst )
{
const char* src;
const char* dst;
int r;
char *command,*receive_buffer;
//retreive control socket
struct ftp_sb_info* ftp_info = (struct ftp_sb_info*)inode_src->i_sb->s_fs_info;
src=dentry_src->d_name.name;
dst=dentry_dst->d_name.name;
command = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
receive_buffer = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
sprintf(command,"RNFR %s\r\n",src);
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control,receive_buffer);
sprintf(command,"RNTO %s\r\n",dst);
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control,receive_buffer);
kfree(command);
kfree(receive_buffer);
return 0;
}
static int ftpfs_unlink(struct inode *inode,struct dentry *dentry)
{
const char* filename;
int r;
char *command,*receive_buffer;
//retreive control socket
struct ftp_sb_info* ftp_info = (struct ftp_sb_info*)inode->i_sb->s_fs_info;
filename=dentry->d_name.name;
command = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
receive_buffer = kmalloc(RCV_BUFFER_SIZE,GFP_KERNEL);
sprintf(command,"DELE %s\r\n",filename);
send_reply(ftp_info->control,command);
r=read_response(ftp_info->control,receive_buffer);
kfree(command);
kfree(receive_buffer);
return 0;
}
static int init_ftp_fs(void)
{
printk("FTPFS initialized\n");
return register_filesystem(&ftpfs_type);
}
static void exit_ftp_fs(void)
{
printk("FTPFS exited\n");
unregister_filesystem(&ftpfs_type);
}
module_init(init_ftp_fs);
module_exit(exit_ftp_fs);
MODULE_LICENSE("GPL");