slkern/sysfile.c

568 lines
13 KiB
C
Raw Normal View History

// TODO: CHECK/REPLACE/UPDATE OLD CODE (this file is based on xv6)
//
// File-system system calls.
// Mostly argument checking, since we don't trust
// user code, and calls into file.c and fs.c.
//
#include "types.h"
#include "riscv.h"
#include "defs.h"
#include "param.h"
#include "stat.h"
#include "sched.h"
#include "proc.h"
#include "fs.h"
#include "mkfs/fsformat.h"
#include "sched.h"
#include "file.h"
#include "fcntl.h"
#include "drives.h"
#include "kprintf.h"
// Fetch the nth word-sized system call argument as a file descriptor
// and return both the descriptor and the corresponding struct file.
int
argfd(int n, int *pfd, struct file **pf)
{
int fd;
struct file *f;
argint(n, &fd);
if(fd < 0 || fd >= NOFILE || (f=myproc()->ofile[fd]) == 0)
return -1;
if(pfd)
*pfd = fd;
if(pf)
*pf = f;
return 0;
}
// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
static int
fdalloc(struct file *f)
{
int fd;
struct proc *p = myproc();
for(fd = 0; fd < NOFILE; fd++){
if(p->ofile[fd] == 0){
p->ofile[fd] = f;
return fd;
}
}
return -1;
}
uint64
sys_dup(void)
{
struct file *f;
int fd;
if(argfd(0, 0, &f) < 0)
return -1;
if((fd=fdalloc(f)) < 0)
return -1;
filedup(f);
return fd;
}
uint64
sys_read(void)
{
struct file *f;
int n;
uint64 p;
argaddr(1, &p);
argint(2, &n);
if(argfd(0, 0, &f) < 0)
return -1;
return fileread(f, p, n);
}
uint64
sys_write(void)
{
struct file *f;
int n;
uint64 p;
argaddr(1, &p);
argint(2, &n);
if(argfd(0, 0, &f) < 0)
return -1;
return filewrite(f, p, n);
}
uint64
sys_close(void)
{
int fd;
struct file *f;
if(argfd(0, &fd, &f) < 0)
return -1;
myproc()->ofile[fd] = 0;
fileclose(f);
return 0;
}
uint64
sys_fstat(void)
{
struct file *f;
uint64 st; // user pointer to struct stat
argaddr(1, &st);
if(argfd(0, 0, &f) < 0)
return -1;
return filestat(f, st);
}
// Create the path new as a link to the same inode as old.
uint64
sys_link(void)
{
char name[FSFORMAT_NAMESIZE_NEW], new[MAXPATH], old[MAXPATH];
fsinstance_inode_t *dp, *ip;
if(argstr(0, old, MAXPATH) < 0 || argstr(1, new, MAXPATH) < 0)
return -1;
fsinstance_t* instance = drives_fsbegin(myproc()->drives, myproc()->cwdrive, old);
if((ip = drives_fsnode(myproc()->drives, myproc()->cwdrive, old, 0)) == NULL) { // /*fsinstance_lookup(old)*/) == 0){
drives_fsend(myproc()->drives, instance);
return -1;
}
fsinstance_inode_lockandload(ip);
if(ip->type == T_DIR){
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return -1;
}
ip->nlink++;
fsinstance_inode_save(ip);
fsinstance_inode_unlock(ip);
if((dp = drives_fsparent(myproc()->drives, myproc()->cwdrive, new, 0, name, FSFORMAT_NAMESIZE_NEW) /*fsinstance_lookupparent(new, name)*/) == NULL)
goto bad;
fsinstance_inode_lockandload(dp);
if(dp->instance != ip->instance || dp->device != ip->device || fsinstance_inode_insert(dp, name, ip->inodenumber) < 0){
fsinstance_inode_unlockandunget(dp);
goto bad;
}
fsinstance_inode_unlockandunget(dp);
fsinstance_inode_unget(ip);
drives_fsend(myproc()->drives, instance);
return 0;
bad:
fsinstance_inode_lockandload(ip);
ip->nlink--;
fsinstance_inode_save(ip);
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return -1;
}
// Is the directory dp empty except for "." and ".." ?
static int
isdirempty(fsinstance_inode_t *dp)
{
if (dp->instance->fsversion == 0) {
int off;
fsformat_dirent_v0_t de;
for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
if(fsinstance_inode_read(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
panic("isdirempty: readi");
if(de.inodenumber != 0)
return 0;
}
return 1;
} else {
int off;
fsformat_dirent_v1_t de;
for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
if(fsinstance_inode_read(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
panic("isdirempty: readi");
if(de.datainode != 0)
return 0;
}
return 1;
}
}
uint64
sys_unlink(void)
{
fsinstance_inode_t *ip, *dp;
fsformat_dirent_v0_t de_v0;
fsformat_dirent_v1_t de_v1;
char name[FSFORMAT_NAMESIZE_NEW], path[MAXPATH];
uint off;
if(argstr(0, path, MAXPATH) < 0)
return -1;
struct proc* p = myproc();
fsinstance_t* instance = drives_fsbegin(p->drives, p->cwdrive, path);
if((dp = drives_fsparent(myproc()->drives, myproc()->cwdrive, path, 0, name, FSFORMAT_NAMESIZE_NEW) /*fsinstance_lookupparent(path, name)*/) == 0){
drives_fsend(myproc()->drives, instance);
return -1;
}
fsinstance_inode_lockandload(dp);
// Cannot unlink "." or "..".
if(fsinstance_nameseq(instance, name, ".") || fsinstance_nameseq(instance, name, ".."))
goto bad;
if((ip = fsinstance_inode_lookup(dp, name, &off)) == 0)
goto bad;
fsinstance_inode_lockandload(ip);
if(ip->nlink < 1)
panic("unlink: nlink < 1");
if(ip->type == T_DIR && !isdirempty(ip)){
fsinstance_inode_unlockandunget(ip);
goto bad;
}
if (instance->fsversion == 0) {
memset(&de_v0, 0, sizeof(de_v0));
if(fsinstance_inode_write(dp, 0, (uint64)&de_v0, off, sizeof(de_v0)) != sizeof(de_v0))
panic("unlink: writei");
} else {
memset(&de_v1, 0, sizeof(de_v1));
if(fsinstance_inode_write(dp, 0, (uint64)&de_v1, off, sizeof(de_v1)) != sizeof(de_v1))
panic("unlink: writei");
}
if(ip->type == T_DIR){
dp->nlink--;
fsinstance_inode_save(dp);
}
fsinstance_inode_unlockandunget(dp);
ip->nlink--;
fsinstance_inode_save(ip);
fsinstance_inode_unlockandunget(ip);
drives_fsend(p->drives, instance);
return 0;
bad:
fsinstance_inode_unlockandunget(dp);
drives_fsend(myproc()->drives, instance);
return -1;
}
static fsinstance_inode_t*
create(char *path, short type, short major, short minor)
{
fsinstance_inode_t *ip, *dp;
char name[FSFORMAT_NAMESIZE_NEW];
if((dp = drives_fsparent(myproc()->drives, myproc()->cwdrive, path, 0, name, FSFORMAT_NAMESIZE_NEW) /*fsinstance_lookupparent(path, name)*/) == 0)
return 0;
fsinstance_inode_lockandload(dp);
if((ip = fsinstance_inode_lookup(dp, name, 0)) != 0){
fsinstance_inode_unlockandunget(dp);
fsinstance_inode_lockandload(ip);
if(type == T_FILE && (ip->type == T_FILE || ip->type == T_DEVICE))
return ip;
fsinstance_inode_unlockandunget(ip);
return 0;
}
if((ip = fsinstance_allocinode(dp->instance, dp->device, type)) == 0){
fsinstance_inode_unlockandunget(dp);
return 0;
}
fsinstance_inode_lockandload(ip);
ip->major = major;
ip->minor = minor;
ip->nlink = 1;
fsinstance_inode_save(ip);
if(type == T_DIR){ // Create . and .. entries.
// No ip->nlink++ for ".": avoid cyclic ref count.
if(fsinstance_inode_insert(ip, ".", ip->inodenumber) < 0 || fsinstance_inode_insert(ip, "..", dp->inodenumber) < 0)
goto fail;
}
if(fsinstance_inode_insert(dp, name, ip->inodenumber) < 0)
goto fail;
if(type == T_DIR){
// now that success is guaranteed:
dp->nlink++; // for ".."
fsinstance_inode_save(dp);
}
fsinstance_inode_unlockandunget(dp);
return ip;
fail:
// something went wrong. de-allocate ip.
ip->nlink = 0;
fsinstance_inode_save(ip);
fsinstance_inode_unlockandunget(ip);
fsinstance_inode_unlockandunget(dp);
return 0;
}
uint64
sys_open(void)
{
char path[MAXPATH];
int fd, omode;
struct file *f;
fsinstance_inode_t *ip;
int n;
argint(1, &omode);
if((n = argstr(0, path, MAXPATH)) < 0)
return -1;
fsinstance_t* instance = drives_fsbegin(myproc()->drives, myproc()->cwdrive, path);
if(omode & O_CREATE){
ip = create(path, T_FILE, 0, 0);
if(ip == 0){
drives_fsend(myproc()->drives, instance);
return -1;
}
} else {
if((ip = drives_fsnode(myproc()->drives, myproc()->cwdrive, path, 0) /*fsinstance_lookup(path)*/) == 0){
drives_fsend(myproc()->drives, instance);
return -1;
}
fsinstance_inode_lockandload(ip);
if(ip->type == T_DIR && omode != O_RDONLY){
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return -1;
}
}
if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)){
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return -1;
}
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f)
fileclose(f);
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return -1;
}
if(ip->type == T_DEVICE){
f->type = FD_DEVICE;
f->major = ip->major;
} else {
f->type = FD_INODE;
f->off = 0;
}
f->ip = ip;
f->readable = !(omode & O_WRONLY);
f->writable = (omode & O_APPEND) ? 2 : (((omode & O_WRONLY) || (omode & O_RDWR)) ? 1 : 0);
if((omode & O_TRUNC) && ip->type == T_FILE){
fsinstance_inode_deletecontents(ip);
}
fsinstance_inode_unlock(ip);
drives_fsend(myproc()->drives, instance);
return fd;
}
uint64
sys_mkdir(void)
{
char path[MAXPATH];
fsinstance_inode_t *ip;
if (argstr(0, path, MAXPATH) < 0) {
return -1;
}
fsinstance_t* instance = drives_fsbegin(myproc()->drives, myproc()->cwdrive, path);
if ((ip = create(path, T_DIR, 0, 0)) == 0){
drives_fsend(myproc()->drives, instance);
return -1;
}
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return 0;
}
uint64
sys_mknod(void)
{
fsinstance_inode_t *ip;
char path[MAXPATH];
int major, minor;
fsinstance_t* instance = drives_fsbegin(myproc()->drives, myproc()->cwdrive, path);
argint(1, &major);
argint(2, &minor);
if((argstr(0, path, MAXPATH)) < 0 ||
(ip = create(path, T_DEVICE, major, minor)) == 0){
drives_fsend(myproc()->drives, instance);
return -1;
}
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return 0;
}
uint64
sys_chdir(void)
{
char path[MAXPATH];
fsinstance_inode_t *ip;
struct proc *p = myproc();
fsinstance_t* instance = drives_fsbegin(myproc()->drives, myproc()->cwdrive, path);
if(argstr(0, path, MAXPATH) < 0 || (ip = drives_fsnode(myproc()->drives, myproc()->cwdrive, path, 0) /*fsinstance_lookup(path)*/) == 0){
drives_fsend(myproc()->drives, instance);
return -1;
}
fsinstance_inode_lockandload(ip);
if(ip->type != T_DIR){
fsinstance_inode_unlockandunget(ip);
drives_fsend(myproc()->drives, instance);
return -1;
}
fsinstance_inode_unlock(ip);
fsinstance_inode_unget(p->cwd);
drives_fsend(myproc()->drives, instance);
p->cwd = ip;
return 0;
}
uint64
sys_execve(void)
{
char path[MAXPATH], *argv[MAXARG];
int i;
uint64 uargv, uarg;
char *envv[MAXARG];
uint64 uenvv, uenv;
//printf("Doing execve syscall...\n");
//vmrd_settracing(1);
argaddr(1, &uargv);
if(argstr(0, path, MAXPATH) < 0) {
printf("BAD PATH\n");
return -1;
}
argaddr(2, &uenvv);
memset(argv, 0, sizeof(void*)*MAXARG /*sizeof(argv)*/);
memset(envv, 0, sizeof(void*)*MAXARG /*sizeof(argv)*/);
//printf("Path is '%s' uargv is %p uenvv is %p\n", path, uargv, uenv);
for(i=0;; i++){
//printf("i=%d\n", i);
if(i >= MAXARG /*NELEM(argv)*/){
goto bad;
}
if(fetchaddr(uargv+sizeof(uint64)*i, (uint64*)&uarg) < 0){
goto bad;
}
//printf("uarg=%p (from address %p or %p+%p)\n", uarg, uargv+sizeof(uint64)*i, uargv, sizeof(uint64)*i);
if(uarg == 0){
argv[i] = 0;
break;
}
argv[i] = kalloc();
if(argv[i] == 0)
goto bad;
if(fetchstr(uarg, argv[i], PGSIZE) < 0)
goto bad;
}
for(i=0;; i++){
if(i >= MAXARG /*NELEM(argv)*/){
goto bad;
}
if(fetchaddr(uenvv+sizeof(uint64)*i, (uint64*)&uenv) < 0){
goto bad;
}
if(uenv == 0){
envv[i] = 0;
break;
}
envv[i] = kalloc();
if(envv[i] == 0)
goto bad;
if(fetchstr(uenv, envv[i], PGSIZE) < 0)
goto bad;
}
int ret = execve(path, argv, envv);
for(i = 0; i < MAXARG /* NELEM(argv)*/ && argv[i] != 0; i++)
kfree(argv[i]);
return ret;
bad:
printf("Bad execve call\n");
for(i = 0; i < MAXARG /*NELEM(argv)*/ && argv[i] != 0; i++)
kfree(argv[i]);
for(i = 0; i < MAXARG /*NELEM(argv)*/ && envv[i] != 0; i++)
kfree(envv[i]);
return -1;
}
uint64
sys_pipe(void)
{
uint64 fdarray; // user pointer to array of two integers
struct file *rf, *wf;
int fd0, fd1;
struct proc *p = myproc();
argaddr(0, &fdarray);
if(pipealloc(&rf, &wf) < 0)
return -1;
fd0 = -1;
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if(fd0 >= 0)
p->ofile[fd0] = 0;
fileclose(rf);
fileclose(wf);
return -1;
}
if(copyout(p->pagetable, fdarray, (char*)&fd0, sizeof(fd0)) < 0 ||
copyout(p->pagetable, fdarray+sizeof(fd0), (char *)&fd1, sizeof(fd1)) < 0){
p->ofile[fd0] = 0;
p->ofile[fd1] = 0;
fileclose(rf);
fileclose(wf);
return -1;
}
return 0;
}