2020-08-23 08:22:14 -05:00
|
|
|
#include <rpc.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <serdes.h>
|
2019-07-01 15:30:00 -05:00
|
|
|
#include <string.h>
|
2019-08-25 13:53:44 -05:00
|
|
|
#include <dbg.h>
|
2020-08-23 08:22:14 -05:00
|
|
|
#include <stdlib.h>
|
2019-06-22 11:11:12 -05:00
|
|
|
|
2020-08-23 08:22:14 -05:00
|
|
|
typedef struct mount_point {
|
2019-06-29 09:55:02 -05:00
|
|
|
char* path;
|
2020-08-23 08:22:14 -05:00
|
|
|
pid_t fs_pid;
|
2019-08-31 16:46:52 -05:00
|
|
|
void* fs_data;
|
2020-08-23 08:22:14 -05:00
|
|
|
struct mount_point* next;
|
|
|
|
} mount_point;
|
2019-09-02 06:58:23 -05:00
|
|
|
|
2020-08-23 08:22:14 -05:00
|
|
|
typedef struct fs_type {
|
|
|
|
char* name;
|
|
|
|
pid_t fs_pid;
|
|
|
|
struct fs_type* next;
|
|
|
|
} fs_type;
|
2019-06-29 09:14:59 -05:00
|
|
|
|
2019-06-29 09:27:41 -05:00
|
|
|
static int vfsstrcmp(const char* s1,const char* s2) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; s1[i] == s2[i]; i++) {
|
|
|
|
if (s1[i] == '\0') return 0;
|
|
|
|
}
|
|
|
|
if (s1[i] == '\0') return 0;
|
|
|
|
return s1[i] - s2[i];
|
|
|
|
}
|
|
|
|
|
2020-08-23 08:22:14 -05:00
|
|
|
mount_point* mount_point_list=NULL;
|
|
|
|
fs_type* fs_type_list=NULL;
|
2019-06-29 09:27:41 -05:00
|
|
|
|
2020-08-23 08:46:38 -05:00
|
|
|
void vfs_mount(void* args) {
|
2020-08-23 08:22:14 -05:00
|
|
|
serdes_state state;
|
|
|
|
start_deserialize(args,&state);
|
|
|
|
char* type=deserialize_str(&state);
|
|
|
|
char* dev=deserialize_str(&state);
|
|
|
|
char* mount_path=deserialize_str(&state);
|
|
|
|
rpc_deallocate_buf(args,state.sizeorpos);
|
|
|
|
fs_type* fstype=fs_type_list;
|
|
|
|
pid_t fs_pid=0;
|
|
|
|
for (;fstype!=NULL;fstype=fstype->next) {
|
|
|
|
if (vfsstrcmp(type,fstype->name)==0) {
|
|
|
|
fs_pid=fstype->fs_pid;
|
|
|
|
break;
|
|
|
|
}
|
2019-09-02 06:58:23 -05:00
|
|
|
}
|
2020-08-23 08:22:14 -05:00
|
|
|
if (!fs_pid) {
|
|
|
|
int err=1;
|
|
|
|
rpc_return(&err,sizeof(int));
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
char* retbuf=rpc_call(fs_pid,"mount",dev,strlen(dev)+1);
|
|
|
|
start_deserialize(retbuf,&state);
|
|
|
|
int err=deserialize_int(&state);
|
|
|
|
void* data=deserialize_ptr(&state);
|
|
|
|
rpc_deallocate_buf(retbuf, state.sizeorpos);
|
|
|
|
int* errbuf=malloc(sizeof(int));
|
|
|
|
*errbuf=err;
|
|
|
|
if (err) {
|
|
|
|
rpc_return(errbuf,sizeof(int));
|
|
|
|
free(errbuf);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
mount_point* mnt_pnt=malloc(sizeof(mnt_pnt));
|
|
|
|
mnt_pnt->fs_data=data;
|
|
|
|
mnt_pnt->fs_pid=fs_pid;
|
|
|
|
mnt_pnt->path=mount_path;
|
|
|
|
mnt_pnt->next=mount_point_list;
|
|
|
|
mount_point_list=mnt_pnt;
|
|
|
|
rpc_return(errbuf,sizeof(int));
|
|
|
|
free(errbuf);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2020-08-23 08:46:38 -05:00
|
|
|
void vfs_register_fs(void* args) {
|
2020-08-23 08:22:14 -05:00
|
|
|
serdes_state state;
|
|
|
|
start_deserialize(args,&state);
|
|
|
|
char* name=deserialize_str(&state);
|
|
|
|
pid_t pid=deserialize_int(&state);
|
|
|
|
fs_type* type=malloc(sizeof(fs_type));
|
|
|
|
rpc_deallocate_buf(args,state.sizeorpos);
|
|
|
|
type->name=name;
|
|
|
|
type->fs_pid=pid;
|
|
|
|
type->next=fs_type_list;
|
|
|
|
fs_type_list=type;
|
|
|
|
rpc_return(NULL,0);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2020-08-23 08:46:38 -05:00
|
|
|
void open(void* args) {
|
2020-08-23 08:22:14 -05:00
|
|
|
serdes_state state;
|
|
|
|
start_deserialize(args,&state);
|
|
|
|
char* path=deserialize_str(&state);
|
|
|
|
rpc_deallocate_buf(args,state.sizeorpos);
|
|
|
|
mount_point* mnt=mount_point_list;
|
|
|
|
mount_point* mnt_pnt=NULL;
|
2020-07-23 11:50:23 -05:00
|
|
|
size_t mntpnt_len=0;
|
2019-07-01 15:13:47 -05:00
|
|
|
for (;mnt!=NULL;mnt=mnt->next) {
|
2020-08-23 08:22:14 -05:00
|
|
|
char* root=mnt->path;
|
2019-07-01 15:13:47 -05:00
|
|
|
if (strlen(root)>mntpnt_len) {
|
2020-08-23 08:22:14 -05:00
|
|
|
if (vfsstrcmp(root,path)==0) {
|
|
|
|
mnt_pnt=mnt;
|
2019-07-01 15:13:47 -05:00
|
|
|
mntpnt_len=strlen(root);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 08:22:14 -05:00
|
|
|
if (mnt_pnt==NULL) {
|
|
|
|
serial_print("NO MOUNTPOINT\n");
|
|
|
|
state.buf=NULL;
|
|
|
|
state.sizeorpos=0;
|
|
|
|
serialize_int(1,&state);
|
|
|
|
serialize_ptr(NULL,&state);
|
|
|
|
serialize_int(0,&state);
|
|
|
|
rpc_return(state.buf,state.sizeorpos);
|
|
|
|
free(state.buf);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
state.buf=NULL;
|
|
|
|
state.sizeorpos=0;
|
|
|
|
serialize_str(path+(strlen(mnt_pnt->path)+1),&state);
|
2020-08-23 14:05:38 -05:00
|
|
|
serialize_ptr(mnt_pnt->fs_data,&state);
|
2020-08-23 08:22:14 -05:00
|
|
|
char* retbuf=rpc_call(mnt_pnt->fs_pid,"open",state.buf,state.sizeorpos);
|
|
|
|
free(state.buf);
|
|
|
|
start_deserialize(retbuf,&state);
|
|
|
|
int err=deserialize_int(&state);
|
|
|
|
void* data=deserialize_ptr(&state);
|
|
|
|
pid_t alt_pid=deserialize_int(&state);
|
|
|
|
pid_t fs_pid = alt_pid ? alt_pid : mnt_pnt->fs_pid;
|
|
|
|
state.buf=NULL;
|
|
|
|
state.sizeorpos=0;
|
|
|
|
serialize_int(err,&state);
|
|
|
|
serialize_ptr(data,&state);
|
|
|
|
serialize_int(fs_pid,&state);
|
|
|
|
rpc_return(state.buf,state.sizeorpos);
|
|
|
|
free(state.buf);
|
|
|
|
pthread_exit(NULL);
|
2019-09-10 19:15:02 -05:00
|
|
|
}
|
2019-09-02 06:58:23 -05:00
|
|
|
|
2019-05-24 11:52:13 -05:00
|
|
|
int main() {
|
2020-08-23 08:22:14 -05:00
|
|
|
rpc_register_func("mount",&vfs_mount);
|
|
|
|
rpc_register_func("open",&open);
|
|
|
|
rpc_register_func("register_fs",&vfs_register_fs);
|
|
|
|
rpc_mark_as_init();
|
2019-05-24 11:52:13 -05:00
|
|
|
}
|