-
Notifications
You must be signed in to change notification settings - Fork 46
/
read_write.c
61 lines (52 loc) · 1.8 KB
/
read_write.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
#include <fs/fs.h>
#include <kernel/clock.h>
int filp_read(struct proc* who, struct filp* file, void *buf, size_t count){
int ret;
SET_CALLER(who);
ret = file->filp_dev->fops->read(file, buf, count, file->filp_pos);
return ret;
}
int sys_read(struct proc *who,int fd, void *buf, size_t count){
struct filp* file;
int ret;
if(!is_fd_opened_and_valid(who, fd))
return -EBADF;
file = who->fp_filp[fd];
if (file->filp_ino->i_mode & S_IFDIR)
return -EISDIR;
ret = filp_read(who, file, buf, count);
// kdebug("read ret %d, call %d, fops %p\n", ret, file->filp_dev->dev_id, (void *)file->filp_dev->fops->read);
return ret;
}
int filp_write(struct proc* who, struct filp* file, void *buf, size_t count){
int ret;
SET_CALLER(who);
ret = file->filp_dev->fops->write(file, buf, count, file->filp_pos);
return ret;
}
int sys_write(struct proc *who,int fd, void *buf, size_t count){
struct filp* file;
int ret;
if(!is_fd_opened_and_valid(who, fd))
return -EBADF;
file = who->fp_filp[fd];
if (file->filp_ino->i_mode & S_IFDIR)
return -EISDIR;
// kdebug("write fd %d for dev %s\n", fd, file->filp_dev->init_name);
ret = filp_write(who, file, buf, count);
if(ret > 0)
file->filp_ino->i_mtime = get_unix_time();
return ret;
}
int do_read(struct proc* who, struct message* msg){
char* buf = (char *) get_physical_addr(msg->m1_p1, who);
if(!is_vaddr_accessible(msg->m1_p1, who))
return -EFAULT;
return sys_read(who, msg->m1_i1, buf, msg->m1_i2);
}
int do_write(struct proc* who, struct message* msg){
char* buf = (char *) get_physical_addr(msg->m1_p1, who);
if(!is_vaddr_accessible(msg->m1_p1, who))
return -EFAULT;
return sys_write(who, msg->m1_i1, buf, msg->m1_i2);
}