Skip to content
Snippets Groups Projects
sysfile.c 6.99 KiB
Newer Older
#include "types.h"
rsc's avatar
rsc committed
#include "defs.h"
#include "param.h"
rsc's avatar
rsc committed
#include "stat.h"
#include "mmu.h"
#include "proc.h"
#include "fs.h"
#include "spinlock.h"
rsc's avatar
rsc committed
#include "file.h"
#include "fcntl.h"

rsc's avatar
rsc committed
// Fetch the nth word-sized system call argument as a file descriptor
// and return both the descriptor and the corresponding struct file.
static int
rsc's avatar
rsc committed
argfd(int n, int *pfd, struct file **pf)
rsc's avatar
rsc committed
  int fd;
  struct file *f;

rsc's avatar
rsc committed
  if(argint(n, &fd) < 0)
rsc's avatar
rsc committed
    return -1;
Russ Cox's avatar
Russ Cox committed
  if(fd < 0 || fd >= NOFILE || (f=proc->ofile[fd]) == 0)
rsc's avatar
rsc committed
    return -1;
  if(pfd)
    *pfd = fd;
  if(pf)
    *pf = f;
  return 0;
rsc's avatar
rsc committed
}
rsc's avatar
rsc committed
// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
static int
fdalloc(struct file *f)
{
  int fd;
rsc's avatar
rsc committed
  for(fd = 0; fd < NOFILE; fd++){
Russ Cox's avatar
Russ Cox committed
    if(proc->ofile[fd] == 0){
      proc->ofile[fd] = f;
rsc's avatar
rsc committed
      return fd;
    }
  }
int
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;
}

rsc's avatar
 
rsc committed
sys_read(void)
rsc's avatar
 
rsc committed
  struct file *f;
  int n;
rsc's avatar
rsc committed
  char *p;
rsc's avatar
rsc committed
  if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0)
    return -1;
rsc's avatar
rsc committed
  return fileread(f, p, n);
rsc's avatar
rsc committed
}

int
sys_write(void)
{
  struct file *f;
  int n;
rsc's avatar
rsc committed
  char *p;
rsc's avatar
rsc committed
  if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0)
rsc's avatar
rsc committed
    return -1;
rsc's avatar
rsc committed
  return filewrite(f, p, n);
}

int
sys_close(void)
{
  int fd;
rsc's avatar
rsc committed
  struct file *f;
  
  if(argfd(0, &fd, &f) < 0)
    return -1;
Russ Cox's avatar
Russ Cox committed
  proc->ofile[fd] = 0;
rsc's avatar
rsc committed
  fileclose(f);
rsc's avatar
rsc committed
int
sys_fstat(void)
{
  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);
}

rsc's avatar
 
rsc committed
// 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;
rsc's avatar
 
rsc committed
  if((ip = namei(old)) == 0)
rsc's avatar
 
rsc committed
    return -1;
rsc's avatar
 
rsc committed
  ilock(ip);
rsc's avatar
 
rsc committed
  if(ip->type == T_DIR){
rsc's avatar
 
rsc committed
    iunlockput(ip);
rsc's avatar
 
rsc committed
    return -1;
  }
Robert Morris's avatar
Robert Morris committed

rsc's avatar
 
rsc committed
  ip->nlink++;
  iupdate(ip);
rsc's avatar
 
rsc committed
  iunlock(ip);

  if((dp = nameiparent(new, name)) == 0)
rsc's avatar
rsc committed
    goto bad;
rsc's avatar
 
rsc committed
  ilock(dp);
rsc's avatar
rsc committed
  if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
    iunlockput(dp);
rsc's avatar
 
rsc committed
    goto bad;
rsc's avatar
rsc committed
  }
rsc's avatar
 
rsc committed
  iunlockput(dp);
  iput(ip);
Robert Morris's avatar
Robert Morris committed

  commit_trans();

rsc's avatar
 
rsc committed
  return 0;
rsc's avatar
 
rsc committed

bad:
  ilock(ip);
  ip->nlink--;
  iupdate(ip);
  iunlockput(ip);
Robert Morris's avatar
Robert Morris committed
  commit_trans();
rsc's avatar
 
rsc committed
  return -1;
rsc's avatar
 
rsc committed
}

// 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;
}

rsc's avatar
rsc committed
//PAGEBREAK!
rsc's avatar
 
rsc committed
int
sys_unlink(void)
{
  struct inode *ip, *dp;
  struct dirent de;
  char name[DIRSIZ], *path;
  uint off;

  if(argstr(0, &path) < 0)
    return -1;
rsc's avatar
 
rsc committed
  if((dp = nameiparent(path, name)) == 0)
rsc's avatar
 
rsc committed
    return -1;
rsc's avatar
 
rsc committed
  ilock(dp);
rsc's avatar
 
rsc committed

  // Cannot unlink "." or "..".
  if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0){
rsc's avatar
 
rsc committed
    iunlockput(dp);
rsc's avatar
 
rsc committed
    return -1;
  }

rsc's avatar
 
rsc committed
  if((ip = dirlookup(dp, name, &off)) == 0){
    iunlockput(dp);
rsc's avatar
 
rsc committed
    return -1;
  }
rsc's avatar
 
rsc committed
  ilock(ip);
rsc's avatar
 
rsc committed

  if(ip->nlink < 1)
    panic("unlink: nlink < 1");
  if(ip->type == T_DIR && !isdirempty(ip)){
rsc's avatar
 
rsc committed
    iunlockput(ip);
    iunlockput(dp);
rsc's avatar
 
rsc committed
    return -1;
  }

  memset(&de, 0, sizeof(de));
  if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
    panic("unlink: writei");
  if(ip->type == T_DIR){
    dp->nlink--;
    iupdate(dp);
  }
rsc's avatar
 
rsc committed
  iunlockput(dp);

rsc's avatar
 
rsc committed
  ip->nlink--;
  iupdate(ip);
rsc's avatar
 
rsc committed
  iunlockput(ip);
Robert Morris's avatar
Robert Morris committed

  commit_trans();

rsc's avatar
 
rsc committed
  return 0;
}

static struct inode*
rsc's avatar
rsc committed
create(char *path, short type, short major, short minor)
rsc's avatar
 
rsc committed
{
  uint off;
  struct inode *ip, *dp;
  char name[DIRSIZ];

rsc's avatar
 
rsc committed
  if((dp = nameiparent(path, name)) == 0)
rsc's avatar
 
rsc committed
    return 0;
rsc's avatar
 
rsc committed
  ilock(dp);
rsc's avatar
 
rsc committed

rsc's avatar
rsc committed
  if((ip = dirlookup(dp, name, &off)) != 0){
rsc's avatar
 
rsc committed
    iunlockput(dp);
    ilock(ip);
    if(type == T_FILE && ip->type == T_FILE)
      return ip;
    iunlockput(ip);
rsc's avatar
 
rsc committed
    return 0;
  }

  if((ip = ialloc(dp->dev, type)) == 0)
    panic("create: ialloc");

rsc's avatar
 
rsc committed
  ilock(ip);
rsc's avatar
 
rsc committed
  ip->major = major;
  ip->minor = minor;
rsc's avatar
rsc committed
  ip->nlink = 1;
rsc's avatar
 
rsc committed
  iupdate(ip);

  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)
rsc's avatar
rsc committed
      panic("create dots");
rsc's avatar
 
rsc committed
  }
rsc's avatar
rsc committed
  if(dirlink(dp, name, ip->inum) < 0)
    panic("create: dirlink");
rsc's avatar
 
rsc committed
  iunlockput(dp);
Robert Morris's avatar
Robert Morris committed

rsc's avatar
 
rsc committed
  return ip;
}

int
sys_open(void)
{
rsc's avatar
 
rsc committed
  char *path;
  int fd, omode;
rsc's avatar
rsc committed
  struct file *f;
rsc's avatar
 
rsc committed
  struct inode *ip;
rsc's avatar
rsc committed
  if(argstr(0, &path) < 0 || argint(1, &omode) < 0)
    return -1;
rsc's avatar
 
rsc committed
  if(omode & O_CREATE){
Robert Morris's avatar
Robert Morris committed
    begin_trans();
    ip = create(path, T_FILE, 0, 0);
    commit_trans();
    if(ip == 0)
rsc's avatar
 
rsc committed
      return -1;
rsc's avatar
rsc committed
  } else {
rsc's avatar
 
rsc committed
    if((ip = namei(path)) == 0)
rsc's avatar
 
rsc committed
      return -1;
rsc's avatar
 
rsc committed
    ilock(ip);
rsc's avatar
rsc committed
    if(ip->type == T_DIR && omode != O_RDONLY){
rsc's avatar
 
rsc committed
      iunlockput(ip);
rsc's avatar
 
rsc committed
      return -1;
    }
rsc's avatar
 
rsc committed
  if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
    if(f)
      fileclose(f);
rsc's avatar
 
rsc committed
    iunlockput(ip);
rsc's avatar
 
rsc committed
  iunlock(ip);
rsc's avatar
 
rsc committed
  f->type = FD_INODE;
rsc's avatar
 
rsc committed
  f->ip = ip;
rsc's avatar
 
rsc committed
  f->off = 0;
rsc's avatar
rsc committed
  f->readable = !(omode & O_WRONLY);
  f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
rsc's avatar
rsc committed
  return fd;
int
sys_mkdir(void)
{
  char *path;
  struct inode *ip;

Robert Morris's avatar
Robert Morris committed
  begin_trans();
  if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){
    commit_trans();
    return -1;
Robert Morris's avatar
Robert Morris committed
  }
  iunlockput(ip);
Robert Morris's avatar
Robert Morris committed
  commit_trans();
int
sys_mknod(void)
{
rsc's avatar
 
rsc committed
  struct inode *ip;
rsc's avatar
rsc committed
  char *path;
  int len;
rsc's avatar
rsc committed
  int major, minor;
rsc's avatar
rsc committed
  
Robert Morris's avatar
Robert Morris committed
  begin_trans();
rsc's avatar
 
rsc committed
  if((len=argstr(0, &path)) < 0 ||
     argint(1, &major) < 0 ||
     argint(2, &minor) < 0 ||
Robert Morris's avatar
Robert Morris committed
     (ip = create(path, T_DEV, major, minor)) == 0){
    commit_trans();
    return -1;
Robert Morris's avatar
Robert Morris committed
  }
rsc's avatar
 
rsc committed
  iunlockput(ip);
Robert Morris's avatar
Robert Morris committed
  commit_trans();
rsc's avatar
rsc committed
  return 0;
rsc's avatar
rsc committed
  char *path;
rsc's avatar
 
rsc committed
  struct inode *ip;
rsc's avatar
 
rsc committed
  if(argstr(0, &path) < 0 || (ip = namei(path)) == 0)
    return -1;
rsc's avatar
 
rsc committed
  ilock(ip);
rsc's avatar
rsc committed
  if(ip->type != T_DIR){
rsc's avatar
 
rsc committed
    iunlockput(ip);
rsc's avatar
 
rsc committed
  iunlock(ip);
Russ Cox's avatar
Russ Cox committed
  iput(proc->cwd);
  proc->cwd = ip;
  return 0;
}

int
sys_exec(void)
{
  char *path, *argv[MAXARG];
rsc's avatar
 
rsc committed
  int i;
  uint uargv, uarg;
rsc's avatar
rsc committed

Russ Cox's avatar
Russ Cox committed
  if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0){
    return -1;
rsc's avatar
 
rsc committed
  memset(argv, 0, sizeof(argv));
rsc's avatar
 
rsc committed
  for(i=0;; i++){
rsc's avatar
 
rsc committed
    if(i >= NELEM(argv))
rsc's avatar
 
rsc committed
      return -1;
Russ Cox's avatar
Russ Cox committed
    if(fetchint(proc, uargv+4*i, (int*)&uarg) < 0)
rsc's avatar
 
rsc committed
      return -1;
    if(uarg == 0){
      argv[i] = 0;
rsc's avatar
 
rsc committed
    }
Russ Cox's avatar
Russ Cox committed
    if(fetchstr(proc, uarg, &argv[i]) < 0)
rsc's avatar
 
rsc committed
      return -1;
rsc's avatar
 
rsc committed
  return exec(path, argv);
rsc's avatar
 
rsc committed

rsc's avatar
 
rsc committed
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;
rsc's avatar
rsc committed
  if(pipealloc(&rf, &wf) < 0)
rsc's avatar
 
rsc committed
    return -1;
  fd0 = -1;
  if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
    if(fd0 >= 0)
Russ Cox's avatar
Russ Cox committed
      proc->ofile[fd0] = 0;
rsc's avatar
 
rsc committed
    fileclose(rf);
    fileclose(wf);
    return -1;
  }
  fd[0] = fd0;
  fd[1] = fd1;
  return 0;
}