689 lines
14 KiB
C
689 lines
14 KiB
C
//
|
|
// 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 "spinlock.h"
|
|
#include "proc.h"
|
|
#include "fs.h"
|
|
#include "sleeplock.h"
|
|
#include "file.h"
|
|
#include "fcntl.h"
|
|
|
|
// Fetch the nth word-sized system call argument as a file descriptor
|
|
// and return both the descriptor and the corresponding struct file.
|
|
static 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[DIRSIZ], new[MAXPATH], old[MAXPATH];
|
|
struct inode *dp, *ip;
|
|
|
|
if(argstr(0, old, MAXPATH) < 0 || argstr(1, new, MAXPATH) < 0)
|
|
return -1;
|
|
|
|
begin_op();
|
|
if((ip = namei(old)) == 0){
|
|
end_op();
|
|
return -1;
|
|
}
|
|
|
|
ilock(ip);
|
|
if(ip->type == T_DIR){
|
|
iunlockput(ip);
|
|
end_op();
|
|
return -1;
|
|
}
|
|
|
|
ip->nlink++;
|
|
iupdate(ip);
|
|
iunlock(ip);
|
|
|
|
if((dp = nameiparent(new, name)) == 0)
|
|
goto bad;
|
|
ilock(dp);
|
|
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
|
|
iunlockput(dp);
|
|
goto bad;
|
|
}
|
|
iunlockput(dp);
|
|
iput(ip);
|
|
|
|
end_op();
|
|
|
|
return 0;
|
|
|
|
bad:
|
|
ilock(ip);
|
|
ip->nlink--;
|
|
iupdate(ip);
|
|
iunlockput(ip);
|
|
end_op();
|
|
return -1;
|
|
}
|
|
|
|
// Is the directory dp empty except for "." and ".." ?
|
|
static int
|
|
isdirempty(struct inode *dp)
|
|
{
|
|
int off;
|
|
struct dirent de;
|
|
|
|
for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
|
|
if(readi(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
|
|
panic("isdirempty: readi");
|
|
if(de.inum != 0)
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
uint64
|
|
sys_unlink(void)
|
|
{
|
|
struct inode *ip, *dp;
|
|
struct dirent de;
|
|
char name[DIRSIZ], path[MAXPATH];
|
|
uint off;
|
|
|
|
if(argstr(0, path, MAXPATH) < 0)
|
|
return -1;
|
|
|
|
begin_op();
|
|
if((dp = nameiparent(path, name)) == 0){
|
|
end_op();
|
|
return -1;
|
|
}
|
|
|
|
ilock(dp);
|
|
|
|
// Cannot unlink "." or "..".
|
|
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
|
|
goto bad;
|
|
|
|
if((ip = dirlookup(dp, name, &off)) == 0)
|
|
goto bad;
|
|
ilock(ip);
|
|
|
|
if(ip->nlink < 1)
|
|
panic("unlink: nlink < 1");
|
|
if(ip->type == T_DIR && !isdirempty(ip)){
|
|
iunlockput(ip);
|
|
goto bad;
|
|
}
|
|
|
|
memset(&de, 0, sizeof(de));
|
|
if(writei(dp, 0, (uint64)&de, off, sizeof(de)) != sizeof(de))
|
|
panic("unlink: writei");
|
|
if(ip->type == T_DIR){
|
|
dp->nlink--;
|
|
iupdate(dp);
|
|
}
|
|
iunlockput(dp);
|
|
|
|
ip->nlink--;
|
|
iupdate(ip);
|
|
iunlockput(ip);
|
|
|
|
end_op();
|
|
|
|
return 0;
|
|
|
|
bad:
|
|
iunlockput(dp);
|
|
end_op();
|
|
return -1;
|
|
}
|
|
|
|
static struct inode*
|
|
create(char *path, short type, short major, short minor)
|
|
{
|
|
struct inode *ip, *dp;
|
|
char name[DIRSIZ];
|
|
|
|
if((dp = nameiparent(path, name)) == 0)
|
|
return 0;
|
|
|
|
ilock(dp);
|
|
|
|
if((ip = dirlookup(dp, name, 0)) != 0){
|
|
iunlockput(dp);
|
|
ilock(ip);
|
|
if(type == T_FILE && (ip->type == T_FILE || ip->type == T_DEVICE))
|
|
return ip;
|
|
iunlockput(ip);
|
|
return 0;
|
|
}
|
|
|
|
if((ip = ialloc(dp->dev, type)) == 0){
|
|
iunlockput(dp);
|
|
return 0;
|
|
}
|
|
|
|
ilock(ip);
|
|
ip->major = major;
|
|
ip->minor = minor;
|
|
ip->nlink = 1;
|
|
iupdate(ip);
|
|
|
|
if(type == T_DIR){ // Create . and .. entries.
|
|
// No ip->nlink++ for ".": avoid cyclic ref count.
|
|
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
|
|
goto fail;
|
|
}
|
|
|
|
if(dirlink(dp, name, ip->inum) < 0)
|
|
goto fail;
|
|
|
|
if(type == T_DIR){
|
|
// now that success is guaranteed:
|
|
dp->nlink++; // for ".."
|
|
iupdate(dp);
|
|
}
|
|
|
|
iunlockput(dp);
|
|
|
|
return ip;
|
|
|
|
fail:
|
|
// something went wrong. de-allocate ip.
|
|
ip->nlink = 0;
|
|
iupdate(ip);
|
|
iunlockput(ip);
|
|
iunlockput(dp);
|
|
return 0;
|
|
}
|
|
|
|
uint64
|
|
sys_open(void)
|
|
{
|
|
char path[MAXPATH];
|
|
int fd, omode;
|
|
struct file *f;
|
|
struct inode *ip;
|
|
int n;
|
|
|
|
argint(1, &omode);
|
|
if((n = argstr(0, path, MAXPATH)) < 0)
|
|
return -1;
|
|
|
|
begin_op();
|
|
|
|
if(omode & O_CREATE){
|
|
ip = create(path, T_FILE, 0, 0);
|
|
if(ip == 0){
|
|
end_op();
|
|
return -1;
|
|
}
|
|
} else {
|
|
if((ip = namei(path)) == 0){
|
|
end_op();
|
|
return -1;
|
|
}
|
|
ilock(ip);
|
|
if(ip->type == T_DIR && omode != O_RDONLY){
|
|
iunlockput(ip);
|
|
end_op();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)){
|
|
iunlockput(ip);
|
|
end_op();
|
|
return -1;
|
|
}
|
|
|
|
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
|
|
if(f)
|
|
fileclose(f);
|
|
iunlockput(ip);
|
|
end_op();
|
|
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_WRONLY) || (omode & O_RDWR);
|
|
|
|
if((omode & O_TRUNC) && ip->type == T_FILE){
|
|
itrunc(ip);
|
|
}
|
|
|
|
iunlock(ip);
|
|
end_op();
|
|
|
|
return fd;
|
|
}
|
|
|
|
uint64
|
|
sys_mkdir(void)
|
|
{
|
|
char path[MAXPATH];
|
|
struct inode *ip;
|
|
|
|
begin_op();
|
|
if(argstr(0, path, MAXPATH) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){
|
|
end_op();
|
|
return -1;
|
|
}
|
|
iunlockput(ip);
|
|
end_op();
|
|
return 0;
|
|
}
|
|
|
|
uint64
|
|
sys_mknod(void)
|
|
{
|
|
struct inode *ip;
|
|
char path[MAXPATH];
|
|
int major, minor;
|
|
|
|
begin_op();
|
|
argint(1, &major);
|
|
argint(2, &minor);
|
|
if((argstr(0, path, MAXPATH)) < 0 ||
|
|
(ip = create(path, T_DEVICE, major, minor)) == 0){
|
|
end_op();
|
|
return -1;
|
|
}
|
|
iunlockput(ip);
|
|
end_op();
|
|
return 0;
|
|
}
|
|
|
|
uint64
|
|
sys_chdir(void)
|
|
{
|
|
char path[MAXPATH];
|
|
struct inode *ip;
|
|
struct proc *p = myproc();
|
|
|
|
begin_op();
|
|
if(argstr(0, path, MAXPATH) < 0 || (ip = namei(path)) == 0){
|
|
end_op();
|
|
return -1;
|
|
}
|
|
ilock(ip);
|
|
if(ip->type != T_DIR){
|
|
iunlockput(ip);
|
|
end_op();
|
|
return -1;
|
|
}
|
|
iunlock(ip);
|
|
iput(p->cwd);
|
|
end_op();
|
|
p->cwd = ip;
|
|
return 0;
|
|
}
|
|
|
|
uint64
|
|
sys_exec(void)
|
|
{
|
|
char path[MAXPATH], *argv[MAXARG];
|
|
int i;
|
|
uint64 uargv, uarg;
|
|
|
|
argaddr(1, &uargv);
|
|
if(argstr(0, path, MAXPATH) < 0) {
|
|
return -1;
|
|
}
|
|
memset(argv, 0, sizeof(argv));
|
|
for(i=0;; i++){
|
|
if(i >= NELEM(argv)){
|
|
goto bad;
|
|
}
|
|
if(fetchaddr(uargv+sizeof(uint64)*i, (uint64*)&uarg) < 0){
|
|
goto bad;
|
|
}
|
|
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;
|
|
}
|
|
|
|
int ret = exec(path, argv);
|
|
|
|
for(i = 0; i < NELEM(argv) && argv[i] != 0; i++)
|
|
kfree(argv[i]);
|
|
|
|
return ret;
|
|
|
|
bad:
|
|
for(i = 0; i < NELEM(argv) && argv[i] != 0; i++)
|
|
kfree(argv[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;
|
|
}
|
|
|
|
const struct vma Null= {.addr=0,.len=0,.prot=0,.flags=0,.file=(struct file*)0,.offset=0,.vis=0};
|
|
uint64 sys_mmap(void) {
|
|
uint64 addr,len;
|
|
int prot, flags, fd, offset;
|
|
argaddr(0, &addr);
|
|
argaddr(1, &len), argint(2, &prot), argint(3, &flags), argint(4, &fd), argint(5, &offset);
|
|
struct proc *p = myproc();
|
|
struct vma *vma = p->vma;
|
|
if((!p->ofile[fd]->readable) && (prot & PROT_READ))
|
|
goto err1;
|
|
if((!p->ofile[fd]->writable) && (prot &PROT_WRITE) && (flags & MAP_SHARED))
|
|
goto err1;
|
|
uint64 ret = p->sz;
|
|
for (int i = 0; i < NVMA; ++i) // don't process when it's full
|
|
if (vma[i].len == Null.len) { // getting a mem size of 0 seems crazy
|
|
vma[i].addr=ret;//addr is always 0, and ret is the position
|
|
vma[i].len=len;
|
|
vma[i].prot=prot;
|
|
vma[i].flags=flags;// which is MAP flags
|
|
vma[i].file=p->ofile[fd];
|
|
filedup(vma[i].file);//increased ref here
|
|
vma[i].offset=offset;
|
|
vma[i].vis=0;
|
|
p->sz+=len;
|
|
return ret;
|
|
}
|
|
err1:
|
|
return -1;
|
|
}
|
|
void shrink_vma(){
|
|
struct proc *p=myproc();
|
|
//DEBUG();
|
|
while(1){
|
|
int found=0;
|
|
int i;
|
|
for(i=0;i<NVMA;++i)
|
|
if(p->vma[i].vis==-1 && p->vma[i].addr+p->vma[i].len==p->sz){
|
|
found=1;
|
|
break;
|
|
}
|
|
if(!found)
|
|
break;
|
|
#ifdef CHECK_IF_EXISTS
|
|
printf("current sz=%lu\n",p->sz);
|
|
#endif
|
|
p->sz-=p->vma[i].len;
|
|
p->vma[i]=Null;
|
|
}
|
|
}
|
|
int munmap(uint64 Argaddr,uint64 Arglen){
|
|
struct proc *p=myproc();
|
|
// well it's possible that the passed addr and len does not match a munmap
|
|
// how do I know that all of the pages of an mmap zone has been removed --using vis
|
|
// don't do crazy things like unmap two different mmap area at same time --it's fine, no such operation in test
|
|
for(int i=0;i<NVMA;++i){// all of which are changable
|
|
uint64 addr=p->vma[i].addr;
|
|
const int offset=p->vma[i].offset;
|
|
struct file *file=p->vma[i].file;
|
|
struct inode *ip=file->ip;
|
|
const int len=p->vma[i].len;
|
|
int *vis=&p->vma[i].vis;
|
|
const int map_flag=p->vma[i].flags;
|
|
if(*vis!=-1 && len >0 && PGROUNDDOWN(addr) + len >= Argaddr+Arglen && PGROUNDDOWN(addr) <= Argaddr){
|
|
// for(int i=0;i<Arglen;i+=PGSIZE){
|
|
// const int cur = Argaddr + i;
|
|
// const int x = (cur-addr)/PGSIZE;
|
|
// delete it, even if being already deleted
|
|
//if((1<<x)&(*vis))
|
|
// goto err1; //attempting to visit a dellocated page
|
|
// }
|
|
for(int i=0;i<Arglen;i+=PGSIZE){
|
|
const uint64 cur = Argaddr + i;
|
|
const int x = (cur-addr)/PGSIZE;
|
|
pte_t *pte=walk(p->pagetable,cur,0);
|
|
if(pte!=0 && (*pte & PTE_V)!=0){
|
|
// this page may not actually allocated
|
|
// wait what happens for uvmunmap, if there's a mmap page between normal pages... --I bet this won't happen
|
|
if(map_flag & (MAP_SHARED)){// write back
|
|
begin_op(); // don't forget begin_op
|
|
ilock(ip);
|
|
const uint sz=ip->size;
|
|
const int pos=offset+(cur-addr);
|
|
const uint remain=sz-pos;
|
|
if(writei(ip,1,cur,pos,PGSIZE<remain?PGSIZE:remain)<0)
|
|
goto err2;
|
|
iunlock(ip);
|
|
end_op();
|
|
}
|
|
uvmunmap(p->pagetable,cur,1,1);
|
|
}
|
|
*vis |= 1<<x;// add dellocated sign
|
|
//kfree((char *)cur); it's not kernel memory space
|
|
// delete such memory page
|
|
}
|
|
int all_clear=1;
|
|
for(int i=0;i<len/PGSIZE;++i)
|
|
all_clear&=(*vis)>>i;
|
|
if(all_clear){ // clear this vma
|
|
// wait, what happens if this vma is the highest address?
|
|
// and, it's responsible for exit, to call munmap in a dereasing order
|
|
//if(p->sz == addr+len)
|
|
//p->sz-=len;
|
|
// release the file
|
|
fileclose(file);
|
|
// clean this vma
|
|
//p->vma[i]=Null;// well, I don't need those pointers at all...
|
|
p->vma[i].vis=-1;
|
|
shrink_vma();
|
|
}
|
|
return 0;
|
|
err2:
|
|
iunlock(ip);
|
|
goto err1;
|
|
}
|
|
}
|
|
err1:
|
|
// hey! why did a uint64 func return -1??
|
|
return -1; //default not found
|
|
}
|
|
uint64 sys_munmap(void) {
|
|
uint64 Argaddr,Arglen;
|
|
argaddr(0,&Argaddr),argaddr(1,&Arglen);
|
|
return munmap(Argaddr,Arglen);
|
|
}
|
|
int lazymap(uint64 va){
|
|
struct proc *p=myproc();
|
|
if(va>=p->sz)
|
|
goto err1;
|
|
uint64 scause=r_scause();
|
|
for(int i=0;i<NVMA;++i) {
|
|
const uint64 addr=p->vma[i].addr;
|
|
const int vma_flag=p->vma[i].prot;
|
|
const int offset=p->vma[i].offset;
|
|
struct file *file=p->vma[i].file;
|
|
struct inode *ip=file->ip;
|
|
const int len=p->vma[i].len;
|
|
const int vis=p->vma[i].vis;
|
|
// each addr and len won't intersect with each other, it's defined by sys_mmap process
|
|
// it's > not >=, however
|
|
// we must make sure that this va is within the range
|
|
if(vis!=-1 && len > 0 && PGROUNDDOWN(addr) + len>va && PGROUNDDOWN(addr)<=va){// it's considered that addr is page-aligned
|
|
const int x=(va-addr)/PGSIZE;
|
|
if((1<<x)&vis)
|
|
goto err1;// attempting to visit a dellocated page
|
|
if(scause == 13 && (vma_flag & PROT_READ)==0)
|
|
goto err1;
|
|
if(scause ==15 && (vma_flag & PROT_WRITE)==0)
|
|
goto err1;
|
|
char *mem=kalloc();
|
|
memset(mem,0,PGSIZE);
|
|
if(mem==0)
|
|
goto err1;
|
|
int pte_flag=0;
|
|
if(vma_flag & PROT_READ)
|
|
pte_flag|=PTE_R;
|
|
if(vma_flag & PROT_WRITE)
|
|
pte_flag|=PTE_W;
|
|
if(vma_flag & PROT_EXEC)
|
|
pte_flag|=PTE_X;
|
|
pte_flag|=PTE_U;
|
|
if(mappages(p->pagetable,va,PGSIZE,(uint64)mem,pte_flag)<0){
|
|
kfree(mem);
|
|
goto err1;
|
|
}
|
|
// why I need to call readi??
|
|
// oh, that's because I can't get file+offset directly...
|
|
ilock(ip);
|
|
// readi won't increase inode ref
|
|
if(readi(ip,0,(uint64)mem,offset + (va-addr),PGSIZE)<=0)
|
|
goto err2;
|
|
// file pointer handles ref to inode, so don't increase inode ref there
|
|
iunlock(ip);
|
|
return 0;
|
|
err2:
|
|
uvmunmap(p->pagetable,va,1,1);
|
|
iunlock(ip);
|
|
goto err1;
|
|
}
|
|
}
|
|
err1:
|
|
return -1;
|
|
}
|