// 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);
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);
struct spinlock fd_table_lock;
struct devsw devsw[NDEV];
-struct fd fds[NFD];
+struct file file[NFILE];
void
fd_init(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);
// 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;
// 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;
// Close file descriptor.
void
-fd_close(struct fd *fd)
+fd_close(struct file *fd)
{
acquire(&fd_table_lock);
panic("fd_close");
if(--fd->ref == 0){
- struct fd dummy = *fd;
+ struct file dummy = *fd;
fd->ref = 0;
fd->type = FD_CLOSED;
// 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);
// 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)
-struct fd {
+struct file {
enum { FD_CLOSED, FD_NONE, FD_PIPE, FD_FILE } type;
int ref; // reference count
char readable;
struct inode *ip;
uint off;
};
-
-extern struct fd fds[NFD];
#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
};
int
-pipe_alloc(struct fd **fd1, struct fd **fd2)
+pipe_alloc(struct file **fd1, struct file **fd2)
{
*fd1 = *fd2 = 0;
struct pipe *p = 0;
// 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;
// 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;
}
}
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
#define SYS_dup 17
#define SYS_getpid 18
#define SYS_sbrk 19
-
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;
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)
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;
}
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;
}
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;
}
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;
}
struct inode *ip, *dp;
uint arg0, arg1;
int ufd;
- struct fd *fd;
+ struct file *fd;
int l;
char *last;
}
fd->ip = ip;
fd->off = 0;
- cp->fds[ufd] = fd;
+ cp->ofile[ufd] = fd;
return ufd;
}
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;
}
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;
}