// TODO: CHECK/REPLACE/UPDATE OLD CODE (this file is based on xv6) // // Support functions for system calls that involve file descriptors. // #include "types.h" #include "riscv.h" #include "defs.h" #include "param.h" #include "fs.h" #include "sched.h" #include "file.h" #include "stat.h" #include "proc.h" #include "kprintf.h" struct devsw devsw[NDEV]; //struct { sched_spinlock_t ftable_lock; struct file ftable_file[NFILE]; //} ftable; void fileinit(void) { initlock(&ftable_lock, "ftable"); } // Allocate a file structure. struct file* filealloc(void) { struct file *f; acquire(&ftable_lock); for(f = ftable_file; f < ftable_file + NFILE; f++){ if(f->ref == 0){ f->ref = 1; release(&ftable_lock); return f; } } release(&ftable_lock); return 0; } // Increment ref count for file f. struct file* filedup(struct file *f) { // kqueue objects are not duplicated on a call to fork etc. if (f->type == FD_KQUEUE) { return NULL; } acquire(&ftable_lock); if(f->ref < 1) panic("filedup"); f->ref++; release(&ftable_lock); return f; } // Close file f. (Decrement ref count, close when reaches 0.) void fileclose(struct file *f) { struct file ff; acquire(&ftable_lock); if(f->ref < 1) panic("fileclose"); if(--f->ref > 0){ release(&ftable_lock); return; } //ff = *f; memmove(&ff, f, sizeof(struct file)); f->ref = 0; f->type = FD_NONE; release(&ftable_lock); if(ff.type == FD_PIPE){ pipeclose(ff.pipe, ff.writable); } else if(ff.type == FD_INODE || ff.type == FD_DEVICE){ fsinstance_t* instance = ff.ip->instance; fsinstance_begin(instance); fsinstance_inode_unget(ff.ip); fsinstance_end(instance); } } // Get metadata about file f. // addr is a user virtual address, pointing to a struct stat. int filestat(struct file *f, uint64 addr) { struct proc *p = myproc(); struct stat st; if(f->type == FD_INODE || f->type == FD_DEVICE){ fsinstance_inode_lockandload(f->ip); fsinstance_inode_getstatinfo(f->ip, &st); fsinstance_inode_unlock(f->ip); if(copyout(p->pagetable, addr, (char *)&st, sizeof(st)) < 0) return -1; return 0; } return -1; } // Read from file f. // addr is a user virtual address. int fileread(struct file *f, uint64 addr, int n) { int (*read)(int, uint64, int); int r = 0; if(f->readable == 0) return -1; if(f->type == FD_PIPE){ r = piperead(f->pipe, addr, n); } else if(f->type == FD_DEVICE){ if(f->major < 0 || f->major >= NDEV || !devsw[f->major].read) return -1; read = devsw[f->major].read; r = read(1, addr, n); } else if(f->type == FD_INODE){ fsinstance_inode_lockandload(f->ip); if((r = fsinstance_inode_read(f->ip, 1, addr, f->off, n)) > 0) f->off += r; fsinstance_inode_unlock(f->ip); } else { panic("fileread"); } return r; } // Write to file f. // addr is a user virtual address. int filewrite(struct file *f, uint64 addr, int n) { int (*write)(int, uint64, int); int r, ret = 0; if(f->writable == 0) return -1; if(f->type == FD_PIPE){ ret = pipewrite(f->pipe, addr, n); } else if(f->type == FD_DEVICE){ if(f->major < 0 || f->major >= NDEV || !devsw[f->major].write) return -1; write = devsw[f->major].write; ret = write(1, addr, n); } else if(f->type == FD_INODE){ // write a few blocks at a time to avoid exceeding // the maximum log transaction size, including // i-node, indirect block, allocation blocks, // and 2 blocks of slop for non-aligned writes. // this really belongs lower down, since fsinstance_inode_write() // might be writing a device like the console. int max = ((MAXOPBLOCKS-1-1-2) / 2) * BSIZE; int i = 0; while(i < n){ int n1 = n - i; if(n1 > max) n1 = max; fsinstance_begin(f->ip->instance); fsinstance_inode_lockandload(f->ip); // Here is where it needs modification for append if (f->writable == 2) { // Append r = fsinstance_inode_write(f->ip, 1, addr + i, f->ip->size, n1); } else if ((r = fsinstance_inode_write(f->ip, 1, addr + i, f->off, n1)) > 0) { f->off += r; } fsinstance_inode_unlock(f->ip); fsinstance_end(f->ip->instance); if(r != n1){ // error from writei break; } i += r; } ret = (i == n ? n : -1); } else { panic("filewrite"); } return ret; }