Skip to content
Snippets Groups Projects
file.c 2.74 KiB
Newer Older
rtm's avatar
rtm committed
#include "types.h"
#include "defs.h"
rsc's avatar
rsc committed
#include "param.h"
#include "fs.h"
rsc's avatar
rsc committed
#include "file.h"
rtm's avatar
rtm committed
#include "spinlock.h"

kaashoek's avatar
kaashoek committed
struct devsw devsw[NDEV];
rsc's avatar
rsc committed
struct {
  struct spinlock lock;
  struct file file[NFILE];
} ftable;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
fileinit(void)
rsc's avatar
 
rsc committed
  initlock(&ftable.lock, "ftable");
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
{
rsc's avatar
rsc committed
  struct file *f;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  acquire(&ftable.lock);
  for(f = ftable.file; f < ftable.file + NFILE; f++){
    if(f->ref == 0){
      f->ref = 1;
      release(&ftable.lock);
      return f;
rtm's avatar
rtm committed
    }
  }
rsc's avatar
rsc committed
  release(&ftable.lock);
rtm's avatar
rtm committed
  return 0;
}

rsc's avatar
rsc committed
// Increment ref count for file f.
struct file*
filedup(struct file *f)
rtm's avatar
rtm committed
{
rsc's avatar
rsc committed
  acquire(&ftable.lock);
  if(f->ref < 1)
    panic("filedup");
rsc's avatar
rsc committed
  f->ref++;
rsc's avatar
rsc committed
  release(&ftable.lock);
  return f;
rtm's avatar
rtm committed
}

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

rsc's avatar
rsc committed
  acquire(&ftable.lock);
  if(f->ref < 1)
rsc's avatar
rsc committed
    panic("fileclose");
  if(--f->ref > 0){
rsc's avatar
rsc committed
    release(&ftable.lock);
rsc's avatar
rsc committed
    return;
  }
  ff = *f;
  f->ref = 0;
rsc's avatar
rsc committed
  f->type = FD_NONE;
  release(&ftable.lock);
rsc's avatar
rsc committed
  
  if(ff.type == FD_PIPE)
rsc's avatar
rsc committed
    pipeclose(ff.pipe, ff.writable);
Robert Morris's avatar
Robert Morris committed
  else if(ff.type == FD_INODE){
    begin_trans();
rsc's avatar
rsc committed
    iput(ff.ip);
Robert Morris's avatar
Robert Morris committed
    commit_trans();
  }
rsc's avatar
rsc committed
}

// Get metadata about file f.
int
filestat(struct file *f, struct stat *st)
{
  if(f->type == FD_INODE){
    ilock(f->ip);
    stati(f->ip, st);
    iunlock(f->ip);
    return 0;
  }
  return -1;
}

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
  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 piperead(f->pipe, addr, n);
rsc's avatar
 
rsc committed
  if(f->type == FD_INODE){
rsc's avatar
 
rsc committed
    ilock(f->ip);
    if((r = readi(f->ip, addr, f->off, n)) > 0)
rsc's avatar
rsc committed
      f->off += r;
rsc's avatar
 
rsc committed
    iunlock(f->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;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  if(f->writable == 0)
    return -1;
  if(f->type == FD_PIPE)
rsc's avatar
rsc committed
    return pipewrite(f->pipe, addr, n);
rsc's avatar
 
rsc committed
  if(f->type == FD_INODE){
Robert Morris's avatar
Robert Morris committed
    // write a few blocks at a time to avoid exceeding
    // the maximum log transaction size, including
    // i-node, indirect block, allocation blocks,
    // and 2 blocks of slop for non-aligned writes.
    // this really belongs lower down, since writei()
    // might be writing a device like the console.
    int max = ((LOGSIZE-1-1-2) / 2) * 512;
    int i = 0;
    while(i < n){
      int n1 = n - i;
      if(n1 > max)
        n1 = max;
Robert Morris's avatar
Robert Morris committed
      begin_trans();
Robert Morris's avatar
Robert Morris committed
      r = writei(f->ip, addr + i, f->off, n1);
Robert Morris's avatar
Robert Morris committed
      commit_trans();
Robert Morris's avatar
Robert Morris committed
      if(r < 0)
        break;
      if(r != n1)
        panic("short filewrite");
rsc's avatar
rsc committed
      f->off += r;
Robert Morris's avatar
Robert Morris committed
      i += r;
    }
    return i == n ? n : -1;
rsc's avatar
rsc committed
  panic("filewrite");
rtm's avatar
rtm committed
}