Skip to content
Snippets Groups Projects
file.c 2.38 KiB
Newer Older
rtm's avatar
rtm committed
#include "types.h"
kaashoek's avatar
kaashoek committed
#include "stat.h"
rtm's avatar
rtm committed
#include "param.h"
#include "x86.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
rsc's avatar
rsc committed
#include "file.h"
rtm's avatar
rtm committed
#include "spinlock.h"
kaashoek's avatar
kaashoek committed
#include "dev.h"
#include "fs.h"
#include "fsvar.h"
rtm's avatar
rtm committed

kaashoek's avatar
kaashoek committed
struct devsw devsw[NDEV];
rsc's avatar
 
rsc committed
struct spinlock file_table_lock;
rsc's avatar
rsc committed
struct file file[NFILE];
rtm's avatar
rtm committed

rsc's avatar
rsc committed
fileinit(void)
rsc's avatar
rsc committed
  initlock(&file_table_lock, "file_table");
rsc's avatar
 
rsc committed
// Allocate a file structure.
rsc's avatar
rsc committed
struct file*
rsc's avatar
rsc committed
filealloc(void)
rtm's avatar
rtm committed
{
  int i;

rsc's avatar
rsc committed
  acquire(&file_table_lock);
rsc's avatar
rsc committed
  for(i = 0; i < NFILE; i++){
    if(file[i].type == FD_CLOSED){
      file[i].type = FD_NONE;
      file[i].ref = 1;
rsc's avatar
rsc committed
      release(&file_table_lock);
rsc's avatar
rsc committed
      return file + i;
rtm's avatar
rtm committed
    }
  }
rsc's avatar
rsc committed
  release(&file_table_lock);
rtm's avatar
rtm committed
  return 0;
}

rsc's avatar
rsc committed
// Increment ref count for file f.
void
fileincref(struct file *f)
rtm's avatar
rtm committed
{
rsc's avatar
rsc committed
  acquire(&file_table_lock);
  if(f->ref < 1 || f->type == FD_CLOSED)
    panic("fileincref");
  f->ref++;
  release(&file_table_lock);
rtm's avatar
rtm committed
}

rsc's avatar
rsc committed
// Read from file f.  Addr is kernel address.
rtm's avatar
rtm committed
int
rsc's avatar
rsc committed
fileread(struct file *f, char *addr, int n)
rtm's avatar
rtm committed
{
rsc's avatar
rsc committed
  int r;
rsc's avatar
 
rsc committed
  struct inode *ip;
rsc's avatar
rsc committed

rsc's avatar
rsc committed
  if(f->readable == 0)
rtm's avatar
rtm committed
    return -1;
rsc's avatar
rsc committed
  if(f->type == FD_PIPE)
rsc's avatar
rsc committed
    return pipe_read(f->pipe, addr, n);
rsc's avatar
 
rsc committed
  if(f->type == FD_INODE){
    ip = ilock(f->ip);
    if((r = readi(ip, addr, f->off, n)) > 0)
rsc's avatar
rsc committed
      f->off += r;
rsc's avatar
 
rsc committed
    iunlock(ip);
rsc's avatar
rsc committed
    return r;
rtm's avatar
rtm committed
  }
rsc's avatar
rsc committed
  panic("fileread");
rtm's avatar
rtm committed
}
rsc's avatar
rsc committed
// Write to file f.  Addr is kernel address.
int
filewrite(struct file *f, char *addr, int n)
rsc's avatar
rsc committed
  int r;
rsc's avatar
 
rsc committed
  struct inode *ip;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  if(f->writable == 0)
    return -1;
  if(f->type == FD_PIPE)
    return pipe_write(f->pipe, addr, n);
rsc's avatar
 
rsc committed
  if(f->type == FD_INODE){
    ip = ilock(f->ip);
    if((r = writei(ip, addr, f->off, n)) > 0)
rsc's avatar
rsc committed
      f->off += r;
rsc's avatar
 
rsc committed
    iunlock(ip);
rsc's avatar
rsc committed
    return r;
rsc's avatar
rsc committed
  panic("filewrite");
rtm's avatar
rtm committed
}

rsc's avatar
rsc committed
// Get metadata about file f.
kaashoek's avatar
kaashoek committed
int
rsc's avatar
rsc committed
filestat(struct file *f, struct stat *st)
kaashoek's avatar
kaashoek committed
{
rsc's avatar
 
rsc committed
  struct inode *ip;

  if(f->type == FD_INODE){
    ip = ilock(f->ip);
    stati(ip, st);
    iunlock(ip);
kaashoek's avatar
kaashoek committed
    return 0;
rsc's avatar
rsc committed
  }
  return -1;
kaashoek's avatar
kaashoek committed
}

rsc's avatar
rsc committed
// Close file f.  (Decrement ref count, close when reaches 0.)
rtm's avatar
rtm committed
void
rsc's avatar
rsc committed
fileclose(struct file *f)
rtm's avatar
rtm committed
{
rsc's avatar
rsc committed
  struct file ff;
rsc's avatar
 
rsc committed

rsc's avatar
rsc committed
  acquire(&file_table_lock);
rsc's avatar
rsc committed

rsc's avatar
rsc committed
  if(f->ref < 1 || f->type == FD_CLOSED)
rsc's avatar
rsc committed
    panic("fileclose");

  if(--f->ref > 0){
    release(&file_table_lock);
    return;
  }
  
  ff = *f;
  f->ref = 0;
  f->type = FD_CLOSED;
rsc's avatar
rsc committed
  release(&file_table_lock);
rsc's avatar
rsc committed
  
  if(ff.type == FD_PIPE)
    pipe_close(ff.pipe, ff.writable);
rsc's avatar
 
rsc committed
  else if(ff.type == FD_INODE)
    iput(ff.ip);
rsc's avatar
rsc committed
  else
    panic("fileclose");
rsc's avatar
rsc committed