Newer
Older
#include "param.h"
#include "x86.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "fs.h"
#include "fsvar.h"
for(i = 0; i < NFILE; i++){
if(file[i].type == FD_CLOSED){
file[i].type = FD_NONE;
file[i].ref = 1;
// Increment ref count for file f.
void
fileincref(struct file *f)
acquire(&file_table_lock);
if(f->ref < 1 || f->type == FD_CLOSED)
panic("fileincref");
f->ref++;
release(&file_table_lock);
// Write to file f. Addr is kernel address.
int
filewrite(struct file *f, char *addr, int n)
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)
f->off += r;
iunlock(f->ip);
return r;
if(f->type == FD_FILE){
ilock(f->ip);
stati(f->ip, st);
iunlock(f->ip);
panic("fileclose");
if(--f->ref > 0){
release(&file_table_lock);
return;
}
ff = *f;
f->ref = 0;
f->type = FD_CLOSED;
if(ff.type == FD_PIPE)
pipe_close(ff.pipe, ff.writable);
else if(ff.type == FD_FILE)
idecref(ff.ip);
else
panic("fileclose");