From eaea18cb9cbb86018dae8f1decfa217ecbe85fa5 Mon Sep 17 00:00:00 2001 From: rsc Date: Wed, 22 Aug 2007 06:01:32 +0000 Subject: 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 --- file.c | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'file.c') 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"); } -- cgit v1.2.3