struct fd -> struct file

This commit is contained in:
rsc 2006-09-06 18:38:56 +00:00
parent 89ebd895b8
commit 39593d2f1a
9 changed files with 52 additions and 55 deletions

16
defs.h
View file

@ -84,8 +84,8 @@ void load_icode(struct proc*, uchar*, uint);
// pipe.c
struct pipe;
struct fd;
int pipe_alloc(struct fd**, struct fd**);
struct file;
int pipe_alloc(struct file**, struct file**);
void pipe_close(struct pipe*, int);
int pipe_write(struct pipe*, char*, int);
int pipe_read(struct pipe*, char*, int);
@ -94,12 +94,12 @@ int pipe_read(struct pipe*, char*, int);
struct stat;
void fd_init(void);
int fd_ualloc(void);
struct fd* fd_alloc(void);
void fd_close(struct fd*);
int fd_read(struct fd*, char*, int n);
int fd_write(struct fd*, char*, int n);
int fd_stat(struct fd*, struct stat*);
void fd_incref(struct fd*);
struct file* fd_alloc(void);
void fd_close(struct file*);
int fd_read(struct file*, char*, int n);
int fd_write(struct file*, char*, int n);
int fd_stat(struct file*, struct stat*);
void fd_incref(struct file*);
// ide.c
void ide_init(void);

28
fd.c
View file

@ -14,7 +14,7 @@
struct spinlock fd_table_lock;
struct devsw devsw[NDEV];
struct fd fds[NFD];
struct file file[NFILE];
void
fd_init(void)
@ -29,24 +29,24 @@ fd_ualloc(void)
int fd;
struct proc *p = curproc[cpu()];
for(fd = 0; fd < NOFILE; fd++)
if(p->fds[fd] == 0)
if(p->ofile[fd] == 0)
return fd;
return -1;
}
// Allocate a file descriptor structure
struct fd*
struct file*
fd_alloc(void)
{
int i;
acquire(&fd_table_lock);
for(i = 0; i < NFD; i++){
if(fds[i].type == FD_CLOSED){
fds[i].type = FD_NONE;
fds[i].ref = 1;
for(i = 0; i < NFILE; i++){
if(file[i].type == FD_CLOSED){
file[i].type = FD_NONE;
file[i].ref = 1;
release(&fd_table_lock);
return fds + i;
return file + i;
}
}
release(&fd_table_lock);
@ -56,7 +56,7 @@ fd_alloc(void)
// Write to file descriptor;
// addr is a kernel address, pointing into some process's p->mem.
int
fd_write(struct fd *fd, char *addr, int n)
fd_write(struct file *fd, char *addr, int n)
{
if(fd->writable == 0)
return -1;
@ -78,7 +78,7 @@ fd_write(struct fd *fd, char *addr, int n)
// Read from file descriptor.
int
fd_read(struct fd *fd, char *addr, int n)
fd_read(struct file *fd, char *addr, int n)
{
if(fd->readable == 0)
return -1;
@ -99,7 +99,7 @@ fd_read(struct fd *fd, char *addr, int n)
// Close file descriptor.
void
fd_close(struct fd *fd)
fd_close(struct file *fd)
{
acquire(&fd_table_lock);
@ -107,7 +107,7 @@ fd_close(struct fd *fd)
panic("fd_close");
if(--fd->ref == 0){
struct fd dummy = *fd;
struct file dummy = *fd;
fd->ref = 0;
fd->type = FD_CLOSED;
@ -127,7 +127,7 @@ fd_close(struct fd *fd)
// Get metadata about file descriptor.
int
fd_stat(struct fd *fd, struct stat *st)
fd_stat(struct file *fd, struct stat *st)
{
if(fd->type == FD_FILE){
ilock(fd->ip);
@ -140,7 +140,7 @@ fd_stat(struct fd *fd, struct stat *st)
// Increment file descriptor reference count.
void
fd_incref(struct fd *fd)
fd_incref(struct file *fd)
{
acquire(&fd_table_lock);
if(fd->ref < 1 || fd->type == FD_CLOSED)

4
fd.h
View file

@ -1,4 +1,4 @@
struct fd {
struct file {
enum { FD_CLOSED, FD_NONE, FD_PIPE, FD_FILE } type;
int ref; // reference count
char readable;
@ -7,5 +7,3 @@ struct fd {
struct inode *ip;
uint off;
};
extern struct fd fds[NFD];

View file

@ -2,8 +2,8 @@
#define PAGE 4096 // granularity of user-space memory allocation
#define KSTACKSIZE PAGE // size of per-process kernel stack
#define NCPU 8 // maximum number of CPUs
#define NOFILE 16 // file descriptors per process
#define NFD 100 // file descriptors per system
#define NOFILE 16 // open files per process
#define NFILE 100 // open files per system
#define NREQUEST 100 // outstanding disk requests
#define NBUF 10 // size of disk block cache
#define NINODE 100 // maximum number of active i-nodes

2
pipe.c
View file

@ -19,7 +19,7 @@ struct pipe {
};
int
pipe_alloc(struct fd **fd1, struct fd **fd2)
pipe_alloc(struct file **fd1, struct file **fd2)
{
*fd1 = *fd2 = 0;
struct pipe *p = 0;

12
proc.c
View file

@ -125,9 +125,9 @@ copyproc(struct proc *p)
// Copy file descriptors
for(i = 0; i < NOFILE; i++){
np->fds[i] = p->fds[i];
if(np->fds[i])
fd_incref(np->fds[i]);
np->ofile[i] = p->ofile[i];
if(np->ofile[i])
fd_incref(np->ofile[i]);
}
np->cwd = p->cwd;
@ -328,9 +328,9 @@ proc_exit(void)
// Close all open files.
for(fd = 0; fd < NOFILE; fd++){
if(cp->fds[fd]){
fd_close(cp->fds[fd]);
cp->fds[fd] = 0;
if(cp->ofile[fd]){
fd_close(cp->ofile[fd]);
cp->ofile[fd] = 0;
}
}

2
proc.h
View file

@ -39,7 +39,7 @@ struct proc{
int ppid;
void *chan; // sleep
int killed;
struct fd *fds[NOFILE];
struct file *ofile[NOFILE];
struct inode *cwd;
struct jmpbuf jmpbuf;
struct trapframe *tf; // points into kstack, used to find user regs

View file

@ -17,4 +17,3 @@
#define SYS_dup 17
#define SYS_getpid 18
#define SYS_sbrk 19

View file

@ -18,7 +18,7 @@
int
sys_pipe(void)
{
struct fd *rfd = 0, *wfd = 0;
struct file *rfd = 0, *wfd = 0;
int f1 = -1, f2 = -1;
struct proc *p = curproc[cpu()];
uint fdp;
@ -27,10 +27,10 @@ sys_pipe(void)
goto oops;
if((f1 = fd_ualloc()) < 0)
goto oops;
p->fds[f1] = rfd;
p->ofile[f1] = rfd;
if((f2 = fd_ualloc()) < 0)
goto oops;
p->fds[f2] = wfd;
p->ofile[f2] = wfd;
if(fetcharg(0, &fdp) < 0)
goto oops;
if(putint(p, fdp, f1) < 0)
@ -45,9 +45,9 @@ sys_pipe(void)
if(wfd)
fd_close(wfd);
if(f1 >= 0)
p->fds[f1] = 0;
p->ofile[f1] = 0;
if(f2 >= 0)
p->fds[f2] = 0;
p->ofile[f2] = 0;
return -1;
}
@ -62,12 +62,12 @@ sys_write(void)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(p->fds[fd] == 0)
if(p->ofile[fd] == 0)
return -1;
if(addr + n > p->sz)
return -1;
ret = fd_write(p->fds[fd], p->mem + addr, n);
ret = fd_write(p->ofile[fd], p->mem + addr, n);
return ret;
}
@ -82,11 +82,11 @@ sys_read(void)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(p->fds[fd] == 0)
if(p->ofile[fd] == 0)
return -1;
if(addr + n > p->sz)
return -1;
ret = fd_read(p->fds[fd], p->mem + addr, n);
ret = fd_read(p->ofile[fd], p->mem + addr, n);
return ret;
}
@ -100,10 +100,10 @@ sys_close(void)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(p->fds[fd] == 0)
if(p->ofile[fd] == 0)
return -1;
fd_close(p->fds[fd]);
p->fds[fd] = 0;
fd_close(p->ofile[fd]);
p->ofile[fd] = 0;
return 0;
}
@ -114,7 +114,7 @@ sys_open(void)
struct inode *ip, *dp;
uint arg0, arg1;
int ufd;
struct fd *fd;
struct file *fd;
int l;
char *last;
@ -170,7 +170,7 @@ sys_open(void)
}
fd->ip = ip;
fd->off = 0;
cp->fds[ufd] = fd;
cp->ofile[ufd] = fd;
return ufd;
}
@ -306,11 +306,11 @@ sys_fstat(void)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(cp->fds[fd] == 0)
if(cp->ofile[fd] == 0)
return -1;
if(addr + sizeof(struct stat) > cp->sz)
return -1;
r = fd_stat(cp->fds[fd], (struct stat*)(cp->mem + addr));
r = fd_stat(cp->ofile[fd], (struct stat*)(cp->mem + addr));
return r;
}
@ -324,12 +324,12 @@ sys_dup(void)
return -1;
if(fd < 0 || fd >= NOFILE)
return -1;
if(cp->fds[fd] == 0)
if(cp->ofile[fd] == 0)
return -1;
if((ufd1 = fd_ualloc()) < 0)
return -1;
cp->fds[ufd1] = cp->fds[fd];
fd_incref(cp->fds[ufd1]);
cp->ofile[ufd1] = cp->ofile[fd];
fd_incref(cp->ofile[ufd1]);
return ufd1;
}