xv6/sysfile.c

409 lines
6.9 KiB
C
Raw Normal View History

#include "types.h"
#include "stat.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "traps.h"
#include "syscall.h"
#include "spinlock.h"
#include "buf.h"
#include "fs.h"
#include "fsvar.h"
2006-09-06 18:40:28 +00:00
#include "file.h"
#include "fcntl.h"
2006-09-07 14:13:26 +00:00
// 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 argno, int *pfd, struct file **pf)
{
2006-09-07 14:13:26 +00:00
int fd;
struct file *f;
if(argint(argno, &fd) < 0)
return -1;
2007-08-09 17:32:40 +00:00
if(fd < 0 || fd >= NOFILE || (f=cp->ofile[fd]) == 0)
2006-09-07 14:13:26 +00:00
return -1;
if(pfd)
*pfd = fd;
if(pf)
*pf = f;
return 0;
2006-09-07 14:13:26 +00:00
}
2006-09-07 14:13:26 +00:00
// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
static int
fdalloc(struct file *f)
{
int fd;
2007-08-09 17:32:40 +00:00
2006-09-07 14:13:26 +00:00
for(fd = 0; fd < NOFILE; fd++){
2007-08-09 17:32:40 +00:00
if(cp->ofile[fd] == 0){
cp->ofile[fd] = f;
2006-09-07 14:13:26 +00:00
return fd;
}
}
return -1;
}
int
sys_read(void)
{
struct file *f;
int n;
char *cp;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
return -1;
return fileread(f, cp, n);
2006-09-07 14:13:26 +00:00
}
int
sys_write(void)
{
struct file *f;
int n;
char *cp;
2006-09-07 14:13:26 +00:00
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
return -1;
return filewrite(f, cp, n);
}
int
sys_fstat(void)
{
2006-09-07 14:13:26 +00:00
struct file *f;
struct stat *st;
if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
return -1;
return filestat(f, st);
}
int
sys_close(void)
{
int fd;
2006-09-07 14:13:26 +00:00
struct file *f;
if(argfd(0, &fd, &f) < 0)
return -1;
2007-08-10 16:37:27 +00:00
cp->ofile[fd] = 0;
2006-09-07 14:13:26 +00:00
fileclose(f);
return 0;
}
// Create the path new as a link to the same inode as old.
int
sys_link(void)
{
char name[DIRSIZ], *new, *old;
struct inode *dp, *ip;
if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
return -1;
if((ip = namei(old)) == 0)
return -1;
ilock(ip);
if(ip->type == T_DIR){
iunlockput(ip);
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)
goto bad;
iunlockput(dp);
iput(ip);
return 0;
bad:
if(dp)
iunlockput(dp);
ilock(ip);
ip->nlink--;
iupdate(ip);
iunlockput(ip);
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, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("isdirempty: readi");
if(de.inum != 0)
return 0;
}
return 1;
}
int
sys_unlink(void)
{
struct inode *ip, *dp;
struct dirent de;
char name[DIRSIZ], *path;
uint off;
if(argstr(0, &path) < 0)
return -1;
if((dp = nameiparent(path, name)) == 0)
return -1;
ilock(dp);
// Cannot unlink "." or "..".
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0){
iunlockput(dp);
return -1;
}
if((ip = dirlookup(dp, name, &off)) == 0){
iunlockput(dp);
return -1;
}
ilock(ip);
if(ip->nlink < 1)
panic("unlink: nlink < 1");
if(ip->type == T_DIR && !isdirempty(ip)){
iunlockput(ip);
iunlockput(dp);
return -1;
}
memset(&de, 0, sizeof(de));
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("unlink: writei");
iunlockput(dp);
ip->nlink--;
iupdate(ip);
iunlockput(ip);
return 0;
}
static struct inode*
mkpath(char *path, int canexist, short type, short major, short minor)
{
uint off;
struct inode *ip, *dp;
char name[DIRSIZ];
if((dp = nameiparent(path, name)) == 0)
return 0;
ilock(dp);
if(canexist && (ip = dirlookup(dp, name, &off)) != 0){
iunlockput(dp);
ilock(ip);
if(ip->type != type || ip->major != major || ip->minor != minor){
iunlockput(ip);
return 0;
}
return ip;
}
if((ip = ialloc(dp->dev, type)) == 0){
iunlockput(dp);
return 0;
}
ilock(ip);
ip->major = major;
ip->minor = minor;
ip->size = 0;
ip->nlink = 1;
iupdate(ip);
if(dirlink(dp, name, ip->inum) < 0){
ip->nlink = 0;
iunlockput(ip);
iunlockput(dp);
return 0;
}
if(type == T_DIR){ // Create . and .. entries.
dp->nlink++; // for ".."
iupdate(dp);
// No ip->nlink++ for ".": avoid cyclic ref count.
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
panic("mkpath dots");
}
iunlockput(dp);
return ip;
}
int
sys_open(void)
{
char *path;
int fd, omode;
2006-09-07 14:13:26 +00:00
struct file *f;
struct inode *ip;
2006-09-07 14:13:26 +00:00
if(argstr(0, &path) < 0 || argint(1, &omode) < 0)
return -1;
if(omode & O_CREATE){
if((ip = mkpath(path, 1, T_FILE, 0, 0)) == 0)
return -1;
}else{
if((ip = namei(path)) == 0)
return -1;
ilock(ip);
if(ip->type == T_DIR && (omode & (O_RDWR|O_WRONLY))){
iunlockput(ip);
return -1;
}
}
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f)
fileclose(f);
iunlockput(ip);
return -1;
}
iunlock(ip);
f->type = FD_INODE;
f->ip = ip;
f->off = 0;
2006-09-07 14:13:26 +00:00
if(omode & O_RDWR) {
f->readable = 1;
f->writable = 1;
} else if(omode & O_WRONLY) {
f->readable = 0;
f->writable = 1;
} else {
2006-09-07 14:13:26 +00:00
f->readable = 1;
f->writable = 0;
}
2006-09-07 14:13:26 +00:00
return fd;
}
int
sys_mknod(void)
{
struct inode *ip;
2006-09-07 14:13:26 +00:00
char *path;
int len;
2007-08-24 20:59:43 +00:00
int major, minor;
2006-09-07 14:13:26 +00:00
if((len=argstr(0, &path)) < 0 ||
argint(1, &major) < 0 ||
argint(2, &minor) < 0 ||
(ip = mkpath(path, 0, T_DEV, major, minor)) == 0)
return -1;
iunlockput(ip);
2006-09-07 14:13:26 +00:00
return 0;
}
int
sys_mkdir(void)
{
char *path;
struct inode *ip;
if(argstr(0, &path) < 0 || (ip = mkpath(path, 0, T_DIR, 0, 0)) == 0)
return -1;
iunlockput(ip);
return 0;
}
int
sys_chdir(void)
{
2006-09-07 14:13:26 +00:00
char *path;
struct inode *ip;
if(argstr(0, &path) < 0 || (ip = namei(path)) == 0)
return -1;
ilock(ip);
if(ip->type != T_DIR) {
iunlockput(ip);
return -1;
}
iunlock(ip);
iput(cp->cwd);
cp->cwd = ip;
return 0;
}
int
sys_dup(void)
{
2006-09-07 14:13:26 +00:00
struct file *f;
int fd;
if(argfd(0, 0, &f) < 0)
return -1;
2006-09-07 14:13:26 +00:00
if((fd=fdalloc(f)) < 0)
return -1;
2006-09-07 14:13:26 +00:00
fileincref(f);
return fd;
}
int
sys_exec(void)
{
char *path, *argv[20];
int i;
uint uargv, uarg;
2006-09-07 14:13:26 +00:00
if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0)
return -1;
memset(argv, 0, sizeof(argv));
for(i=0;; i++){
if(i >= NELEM(argv))
return -1;
if(fetchint(cp, uargv+4*i, (int*)&uarg) < 0)
return -1;
if(uarg == 0){
argv[i] = 0;
break;
}
if(fetchstr(cp, uarg, &argv[i]) < 0)
return -1;
}
return exec(path, argv);
}
int
sys_pipe(void)
{
int *fd;
struct file *rf, *wf;
int fd0, fd1;
if(argptr(0, (void*)&fd, 2*sizeof(fd[0])) < 0)
return -1;
if(pipe_alloc(&rf, &wf) < 0)
return -1;
fd0 = -1;
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if(fd0 >= 0)
cp->ofile[fd0] = 0;
fileclose(rf);
fileclose(wf);
return -1;
}
fd[0] = fd0;
fd[1] = fd1;
return 0;
}