summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authorrsc <rsc>2007-08-22 06:01:32 +0000
committerrsc <rsc>2007-08-22 06:01:32 +0000
commiteaea18cb9cbb86018dae8f1decfa217ecbe85fa5 (patch)
tree98c4a9b852ad9b6aaf16016417cf5eeee0b3857e /file.c
parent3dcf889c1b5c2c5ddf5b4756f2a731c344f6f08e (diff)
downloadxv6-labs-eaea18cb9cbb86018dae8f1decfa217ecbe85fa5.tar.gz
xv6-labs-eaea18cb9cbb86018dae8f1decfa217ecbe85fa5.tar.bz2
xv6-labs-eaea18cb9cbb86018dae8f1decfa217ecbe85fa5.zip
PDF at http://am.lcs.mit.edu/~rsc/xv6.pdf
Various changes made while offline. + bwrite sector argument is redundant; use b->sector. + reformatting of files for nicer PDF page breaks + distinguish between locked, unlocked inodes in type signatures + change FD_FILE to FD_INODE + move userinit (nee proc0init) to proc.c + move ROOTDEV to param.h + always parenthesize sizeof argument
Diffstat (limited to 'file.c')
-rw-r--r--file.c38
1 files changed, 21 insertions, 17 deletions
diff --git a/file.c b/file.c
index 47e4629..981d474 100644
--- a/file.c
+++ b/file.c
@@ -11,9 +11,8 @@
#include "fs.h"
#include "fsvar.h"
-struct spinlock file_table_lock;
struct devsw devsw[NDEV];
-
+struct spinlock file_table_lock;
struct file file[NFILE];
void
@@ -22,7 +21,7 @@ fileinit(void)
initlock(&file_table_lock, "file_table");
}
-// Allocate a file structure
+// Allocate a file structure.
struct file*
filealloc(void)
{
@@ -57,16 +56,17 @@ int
fileread(struct file *f, char *addr, int n)
{
int r;
+ struct inode *ip;
if(f->readable == 0)
return -1;
if(f->type == FD_PIPE)
return pipe_read(f->pipe, addr, n);
- if(f->type == FD_FILE){
- ilock(f->ip);
- if((r = readi(f->ip, addr, f->off, n)) > 0)
+ if(f->type == FD_INODE){
+ ip = ilock(f->ip);
+ if((r = readi(ip, addr, f->off, n)) > 0)
f->off += r;
- iunlock(f->ip);
+ iunlock(ip);
return r;
}
panic("fileread");
@@ -77,16 +77,17 @@ int
filewrite(struct file *f, char *addr, int n)
{
int r;
+ struct inode *ip;
if(f->writable == 0)
return -1;
if(f->type == FD_PIPE)
return pipe_write(f->pipe, addr, n);
- if(f->type == FD_FILE){
- ilock(f->ip);
- if((r = writei(f->ip, addr, f->off, n)) > 0)
+ if(f->type == FD_INODE){
+ ip = ilock(f->ip);
+ if((r = writei(ip, addr, f->off, n)) > 0)
f->off += r;
- iunlock(f->ip);
+ iunlock(ip);
return r;
}
panic("filewrite");
@@ -96,10 +97,12 @@ filewrite(struct file *f, char *addr, int n)
int
filestat(struct file *f, struct stat *st)
{
- if(f->type == FD_FILE){
- ilock(f->ip);
- stati(f->ip, st);
- iunlock(f->ip);
+ struct inode *ip;
+
+ if(f->type == FD_INODE){
+ ip = ilock(f->ip);
+ stati(ip, st);
+ iunlock(ip);
return 0;
}
return -1;
@@ -110,6 +113,7 @@ void
fileclose(struct file *f)
{
struct file ff;
+
acquire(&file_table_lock);
if(f->ref < 1 || f->type == FD_CLOSED)
@@ -127,8 +131,8 @@ fileclose(struct file *f)
if(ff.type == FD_PIPE)
pipe_close(ff.pipe, ff.writable);
- else if(ff.type == FD_FILE)
- idecref(ff.ip);
+ else if(ff.type == FD_INODE)
+ iput(ff.ip);
else
panic("fileclose");
}