-
Notifications
You must be signed in to change notification settings - Fork 1
/
ramdisk_kernel_lib.c
515 lines (396 loc) · 11.7 KB
/
ramdisk_kernel_lib.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
_____ ______ _
|_ _| | ___ \ | |
| | ___ __ _ _ __ ___ | |_/ /__ _ _ __ | |_ ___ _ __
| |/ _ \/ _` | '_ ` _ \ | // _` | '_ \| __|/ _ \| '__|
| | __/ (_| | | | | | | | |\ \ (_| | |_) | |_| (_) | |
\_/\___|\__,_|_| |_| |_| \_| \_\__,_| .__/ \__|\___/|_|
| |
|_|
Luis Carrasco, Diane Theriault, Gordon Towne
Ramdisk - Project 3 - CS552
ramdisk_kernel_lib.h
kernel level portion of RAM disk library, receives ioctls and marshals requests
to the ramdisk
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h> /* error codes */
#include <linux/proc_fs.h>
#include <linux/tty.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <asm/uaccess.h>
#include <asm/semaphore.h>
#include "ramdisk_kernel_lib.h"
#include "ramdisk.h"
MODULE_LICENSE("GPL");
struct semaphore mutex;
static struct file_operations pseudo_dev_proc_operations;
static struct proc_dir_entry *proc_entry;
int handle_test(unsigned long ioctl_arg)
{
struct test_args_t test_args;
if(0 != copy_from_user(&test_args, (struct test_args_t *)ioctl_arg,
sizeof(struct test_args_t)))
{
printk("Error copying test args from user\n");
return -1;
}
test_args.ret_val = rd_test(&(test_args.inodes), &(test_args.blocks));
if (0 != copy_to_user((void*) ioctl_arg, &test_args, sizeof(struct test_args_t)))
{
printk("Error copyting test return value to user level\n");
return -1;
}
printk("RD_CREAT SUCCESSFUL\n");
return 1;
}
int handle_init(void)
{
rd_init();
return 0;
}
int handle_creat_call(unsigned long ioctl_arg)
{
struct pathname_args_t creat_args;
char buff[1024];
//char buff[creat_args.str_len];
printk("RD_CREAT ioctl\n");
if (0 != copy_from_user(&creat_args, (struct pathname_args_t *)ioctl_arg,
sizeof(struct pathname_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
if (0 != copy_from_user(&buff, creat_args.pathname, creat_args.str_len))
{
printk("Error copying pathname from user\n");
return -1;
}
#ifdef DEBUG
printk("Received creat call with arg %x\n", buff);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
creat_args.ret_val = rd_creat(buff);
if (0 != copy_to_user((void*) ioctl_arg, &creat_args, sizeof(struct pathname_args_t)))
{
printk("Error copyting creat return value to user level\n");
return -1;
}
printk("RD_CREAT SUCCESSFUL\n");
return 1;
}
int handle_mkdir_call(unsigned long ioctl_arg)
{
struct pathname_args_t mkdir_args;
char buff[1024];
if (0 != copy_from_user(&mkdir_args, (struct pathname_args_t *)ioctl_arg,
sizeof(struct pathname_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
//char buff[mkdir_args.str_len];
if (0 != copy_from_user(&buff, mkdir_args.pathname, mkdir_args.str_len))
{
printk("Error copying pathname from user\n");
return -1;
}
#ifdef DEBUG
printk("Received mkdir call with arg %x\n", buff);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
mkdir_args.ret_val = rd_mkdir(buff);
if (0!= copy_to_user((void*) ioctl_arg, (void*) &mkdir_args, sizeof(struct pathname_args_t)))
{
printk("Error copyting mkdir return value to user level\n");
return -1;
}
return 1;
}
int handle_open_call(unsigned long ioctl_arg)
{
struct pathname_args_t open_args;
char buff[1024];
if (0 != copy_from_user(&open_args, (struct pathname_args_t *)ioctl_arg,
sizeof(struct pathname_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
//char buff[open_args.str_len];
if (0 != copy_from_user(&buff, open_args.pathname, open_args.str_len))
{
printk("Error copying pathname from user\n");
return -1;
}
#ifdef DEBUG
printk("Received open call with arg %x\n", buff);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
open_args.ret_val = rd_open(buff);
if (0 != copy_to_user((void*) ioctl_arg, (void*) &open_args, sizeof(struct pathname_args_t)))
{
printk("Error copying open return value to user level\n");
return -1;
}
return 1;
}
int handle_close_call(unsigned long ioctl_arg)
{
struct close_args_t close_args;
int fd;
if (0 != copy_from_user(&close_args, (struct close_args_t *)ioctl_arg,
sizeof(struct close_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
fd = close_args.fd;
#ifdef DEBUG
printk("Received close call with arg %i\n", fd);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
close_args.ret_val = rd_close(fd);
if (0 != copy_to_user((void*) ioctl_arg, (void*) &close_args, sizeof(struct close_args_t)))
{
printk("Error copyting close return value to user level\n");
return -1;
}
return 1;
}
int handle_read_call(unsigned long ioctl_arg)
{
struct read_write_args_t read_args;
int fd;
char* address;
int num_bytes;
#
if (0 != copy_from_user(&read_args, (struct read_write_args_t *)ioctl_arg,
sizeof(struct read_write_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
fd = read_args.fd;
address = read_args.address;
num_bytes = read_args.num_bytes;
#ifdef DEBUG
printk("Received read call with args %i, %i, %i\n", fd, (int) address, num_bytes);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
read_args.ret_val = rd_read(fd, address, num_bytes);
if (0 != copy_to_user((void*) ioctl_arg, (void*) &read_args, sizeof(struct read_write_args_t)))
{
printk("Error copyting read return value to user level\n");
return -1;
}
return 1;
}
int handle_write_call(unsigned long ioctl_arg)
{
struct read_write_args_t write_args;
int fd;
char* address;
int num_bytes;
if (0 != copy_from_user(&write_args, (struct read_write_args_t *)ioctl_arg,
sizeof(struct read_write_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
fd = write_args.fd;
address = write_args.address;
num_bytes = write_args.num_bytes;
#ifdef DEBUG
printk("Received write call with args %i, %i, %i\n", fd, (int) address, num_bytes);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
write_args.ret_val = rd_write(fd, address, num_bytes);
if (0 != copy_to_user((void*) ioctl_arg, (void*) &write_args, sizeof(struct read_write_args_t)))
{
printk("Error copyting write return value to user level\n");
return -1;
}
return 1;
}
int handle_seek_call(unsigned long ioctl_arg)
{
struct seek_args_t seek_args;
int fd;
int offset;
if (0 != copy_from_user(&seek_args, (struct seek_args_t *)ioctl_arg,
sizeof(struct seek_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
fd = seek_args.fd;
offset = seek_args.offset;
#ifdef DEBUG
printk("Received seek call with args %i, %i\n", fd, offset);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
seek_args.ret_val = rd_seek(fd, offset);
if (0 != copy_to_user((void*) ioctl_arg, (void*) &seek_args, sizeof(struct seek_args_t)))
{
printk("Error copying seek return value to user level\n");
return -1;
}
return 1;
}
int handle_readdir_call(unsigned long ioctl_arg)
{
struct read_write_args_t readdir_args;
int fd;
char* address;
if (0 != copy_from_user(&readdir_args, (struct read_write_args_t *)ioctl_arg,
sizeof(struct read_write_args_t)))
{
printk("Error copying readdir args from user\n");
return -1;
}
fd = readdir_args.fd;
address = readdir_args.address;
#ifdef DEBUG
printk("Received readdir call with args %i, %i\n", fd, (int) address);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
readdir_args.ret_val = rd_readdir(fd, address);
if (0 != copy_to_user((void*) ioctl_arg, (void*) &readdir_args, sizeof(struct read_write_args_t)))
{
printk("Error copying readdir return value to user level\n");
return -1;
}
return 1;
}
int handle_unlink_call(unsigned long ioctl_arg)
{
struct pathname_args_t unlink_args;
char buff[1024];
if (0 != copy_from_user(&unlink_args, (struct pathname_args_t *)ioctl_arg,
sizeof(struct pathname_args_t)))
{
printk("Error copying creat args from user\n");
return -1;
}
//char buff[unlink_args.str_len];
if (0 != copy_from_user(&buff, unlink_args.pathname, unlink_args.str_len))
{
printk("Error copying pathname from user\n");
return -1;
}
#ifdef DEBUG
printk("Received unlink call with args %s\n", buff);
#endif
// UNCOMMENT TO PASS TO RAMDISK
//
unlink_args.ret_val = rd_unlink(buff);
if (0 != copy_to_user((void*) ioctl_arg, (void*) &unlink_args, sizeof(struct pathname_args_t)))
{
printk("Error copyting unlink return value to user level\n");
return -1;
}
return 1;
}
static int pseudo_device_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case RD_INIT:
down_interruptible(&mutex);
handle_init();
up(&mutex);
break;
case RD_CREAT:
down_interruptible(&mutex);
handle_creat_call(arg);
up(&mutex);
break;
case RD_MKDIR:
down_interruptible(&mutex);
handle_mkdir_call(arg);
up(&mutex);
break;
case RD_OPEN:
down_interruptible(&mutex);
handle_open_call(arg);
up(&mutex);
break;
case RD_CLOSE:
down_interruptible(&mutex);
handle_close_call(arg);
up(&mutex);
break;
case RD_READ:
down_interruptible(&mutex);
handle_read_call(arg);
up(&mutex);
break;
case RD_WRITE:
down_interruptible(&mutex);
handle_write_call(arg);
up(&mutex);
break;
case RD_SEEK:
down_interruptible(&mutex);
handle_seek_call(arg);
up(&mutex);
break;
case RD_READDIR:
down_interruptible(&mutex);
handle_readdir_call(arg);
up(&mutex);
break;
case RD_UNLINK:
down_interruptible(&mutex);
handle_unlink_call(arg);
up(&mutex);
break;
case RD_TEST:
down_interruptible(&mutex);
handle_test(arg);
up(&mutex);
default:
return -EINVAL;
break;
}
return 0;
}
static int __init initialization_routine(void)
{
printk("<1> Loading ramdisk_kernel_lib module\n");
pseudo_dev_proc_operations.ioctl = pseudo_device_ioctl;
/* Start create proc entry */
proc_entry = create_proc_entry(PROC_MODULE_NAME, 0444, &proc_root);
if(!proc_entry)
{
printk("<1> Error creating /proc entry.\n");
return 1;
}
proc_entry->owner = THIS_MODULE;
proc_entry->proc_fops = &pseudo_dev_proc_operations;
sema_init(&mutex, 1);
RAMDISK = NULL;
return 0;
}
static void __exit cleanup_routine(void)
{
printk("<1> Dumping ramdisk_kernel_lib module\n");
remove_proc_entry(PROC_MODULE_NAME, &proc_root);
//@TODO: Deallocate RAMDISK
vfree(RAMDISK);
return;
}
module_init(initialization_routine);
module_exit(cleanup_routine);