2011-10-14 14:23:23 +00:00
|
|
|
//
|
2019-06-01 09:33:38 +00:00
|
|
|
// Support functions for system calls that involve file descriptors.
|
2011-10-14 14:23:23 +00:00
|
|
|
//
|
|
|
|
|
2006-06-27 14:35:53 +00:00
|
|
|
#include "types.h"
|
2019-05-31 15:45:42 +00:00
|
|
|
#include "riscv.h"
|
2006-06-27 14:35:53 +00:00
|
|
|
#include "defs.h"
|
2007-08-27 23:26:33 +00:00
|
|
|
#include "param.h"
|
2009-08-08 08:07:30 +00:00
|
|
|
#include "fs.h"
|
2011-08-29 21:18:40 +00:00
|
|
|
#include "spinlock.h"
|
2016-09-12 00:59:57 +00:00
|
|
|
#include "sleeplock.h"
|
|
|
|
#include "file.h"
|
2019-06-01 09:33:38 +00:00
|
|
|
#include "stat.h"
|
|
|
|
#include "proc.h"
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2006-08-09 16:04:04 +00:00
|
|
|
struct devsw devsw[NDEV];
|
2009-05-31 02:07:26 +00:00
|
|
|
struct {
|
|
|
|
struct spinlock lock;
|
|
|
|
struct file file[NFILE];
|
|
|
|
} ftable;
|
2006-06-27 14:35:53 +00:00
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
void
|
2006-09-06 18:43:45 +00:00
|
|
|
fileinit(void)
|
2006-08-10 22:08:14 +00:00
|
|
|
{
|
2009-05-31 05:12:21 +00:00
|
|
|
initlock(&ftable.lock, "ftable");
|
2006-08-10 22:08:14 +00:00
|
|
|
}
|
|
|
|
|
2007-08-22 06:01:32 +00:00
|
|
|
// Allocate a file structure.
|
2006-09-06 18:38:56 +00:00
|
|
|
struct file*
|
2006-09-06 18:43:45 +00:00
|
|
|
filealloc(void)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2009-05-31 02:07:26 +00:00
|
|
|
struct file *f;
|
2006-06-27 14:35:53 +00:00
|
|
|
|
2009-05-31 02:07:26 +00:00
|
|
|
acquire(&ftable.lock);
|
|
|
|
for(f = ftable.file; f < ftable.file + NFILE; f++){
|
|
|
|
if(f->ref == 0){
|
|
|
|
f->ref = 1;
|
|
|
|
release(&ftable.lock);
|
|
|
|
return f;
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-31 02:07:26 +00:00
|
|
|
release(&ftable.lock);
|
2006-06-27 14:35:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-08-14 19:41:56 +00:00
|
|
|
// Increment ref count for file f.
|
2007-08-27 14:35:09 +00:00
|
|
|
struct file*
|
|
|
|
filedup(struct file *f)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2009-05-31 02:07:26 +00:00
|
|
|
acquire(&ftable.lock);
|
|
|
|
if(f->ref < 1)
|
2007-08-27 14:35:09 +00:00
|
|
|
panic("filedup");
|
2007-08-14 19:41:56 +00:00
|
|
|
f->ref++;
|
2009-05-31 02:07:26 +00:00
|
|
|
release(&ftable.lock);
|
2007-08-27 14:35:09 +00:00
|
|
|
return f;
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
2007-08-27 14:37:13 +00:00
|
|
|
// Close file f. (Decrement ref count, close when reaches 0.)
|
|
|
|
void
|
|
|
|
fileclose(struct file *f)
|
|
|
|
{
|
|
|
|
struct file ff;
|
|
|
|
|
2009-05-31 02:07:26 +00:00
|
|
|
acquire(&ftable.lock);
|
|
|
|
if(f->ref < 1)
|
2007-08-27 14:37:13 +00:00
|
|
|
panic("fileclose");
|
|
|
|
if(--f->ref > 0){
|
2009-05-31 02:07:26 +00:00
|
|
|
release(&ftable.lock);
|
2007-08-27 14:37:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ff = *f;
|
|
|
|
f->ref = 0;
|
2009-05-31 02:07:26 +00:00
|
|
|
f->type = FD_NONE;
|
|
|
|
release(&ftable.lock);
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2019-06-13 14:29:27 +00:00
|
|
|
if(ff.type == FD_PIPE){
|
2007-08-28 04:22:35 +00:00
|
|
|
pipeclose(ff.pipe, ff.writable);
|
2019-06-13 14:29:27 +00:00
|
|
|
} else if(ff.type == FD_INODE || ff.type == FD_DEVICE){
|
2014-08-27 21:15:30 +00:00
|
|
|
begin_op();
|
2007-08-27 14:37:13 +00:00
|
|
|
iput(ff.ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2011-08-12 13:25:39 +00:00
|
|
|
}
|
2007-08-27 14:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get metadata about file f.
|
2019-06-01 09:33:38 +00:00
|
|
|
// addr is a user virtual address, pointing to a struct stat.
|
2007-08-27 14:37:13 +00:00
|
|
|
int
|
2019-06-01 09:33:38 +00:00
|
|
|
filestat(struct file *f, uint64 addr)
|
2007-08-27 14:37:13 +00:00
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
struct proc *p = myproc();
|
|
|
|
struct stat st;
|
|
|
|
|
2019-06-13 14:29:27 +00:00
|
|
|
if(f->type == FD_INODE || f->type == FD_DEVICE){
|
2007-08-27 14:37:13 +00:00
|
|
|
ilock(f->ip);
|
2019-06-01 09:33:38 +00:00
|
|
|
stati(f->ip, &st);
|
2007-08-27 14:37:13 +00:00
|
|
|
iunlock(f->ip);
|
2019-06-01 09:33:38 +00:00
|
|
|
if(copyout(p->pagetable, addr, (char *)&st, sizeof(st)) < 0)
|
|
|
|
return -1;
|
2007-08-27 14:37:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-14 14:23:23 +00:00
|
|
|
// Read from file f.
|
2019-06-01 09:33:38 +00:00
|
|
|
// addr is a user virtual address.
|
2006-06-27 14:35:53 +00:00
|
|
|
int
|
2019-06-01 09:33:38 +00:00
|
|
|
fileread(struct file *f, uint64 addr, int n)
|
2006-06-27 14:35:53 +00:00
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
int r = 0;
|
2007-08-14 19:41:56 +00:00
|
|
|
|
2006-09-08 13:44:42 +00:00
|
|
|
if(f->readable == 0)
|
2006-06-27 14:35:53 +00:00
|
|
|
return -1;
|
2019-06-01 09:33:38 +00:00
|
|
|
|
|
|
|
if(f->type == FD_PIPE){
|
2019-06-04 09:57:47 +00:00
|
|
|
r = piperead(f->pipe, addr, n);
|
2019-06-13 14:29:27 +00:00
|
|
|
} else if(f->type == FD_DEVICE){
|
|
|
|
r = devsw[f->major].read(1, addr, n);
|
2019-06-01 09:33:38 +00:00
|
|
|
} else if(f->type == FD_INODE){
|
2007-08-24 20:54:23 +00:00
|
|
|
ilock(f->ip);
|
2019-06-04 09:57:47 +00:00
|
|
|
if((r = readi(f->ip, 1, addr, f->off, n)) > 0)
|
2007-08-14 19:41:56 +00:00
|
|
|
f->off += r;
|
2007-08-24 20:54:23 +00:00
|
|
|
iunlock(f->ip);
|
2019-06-01 09:33:38 +00:00
|
|
|
} else {
|
|
|
|
panic("fileread");
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2019-06-01 09:33:38 +00:00
|
|
|
|
|
|
|
return r;
|
2006-06-27 14:35:53 +00:00
|
|
|
}
|
2006-07-01 21:26:01 +00:00
|
|
|
|
2011-09-02 19:35:34 +00:00
|
|
|
//PAGEBREAK!
|
2011-10-14 14:23:23 +00:00
|
|
|
// Write to file f.
|
2019-06-01 09:33:38 +00:00
|
|
|
// addr is a user virtual address.
|
2007-08-14 19:41:56 +00:00
|
|
|
int
|
2019-06-01 09:33:38 +00:00
|
|
|
filewrite(struct file *f, uint64 addr, int n)
|
2006-07-01 21:26:01 +00:00
|
|
|
{
|
2019-06-01 09:33:38 +00:00
|
|
|
int r, ret = 0;
|
2006-07-12 01:48:35 +00:00
|
|
|
|
2007-08-14 19:41:56 +00:00
|
|
|
if(f->writable == 0)
|
|
|
|
return -1;
|
2019-06-01 09:33:38 +00:00
|
|
|
|
|
|
|
if(f->type == FD_PIPE){
|
2019-06-04 09:57:47 +00:00
|
|
|
ret = pipewrite(f->pipe, addr, n);
|
2019-06-13 14:29:27 +00:00
|
|
|
} else if(f->type == FD_DEVICE){
|
|
|
|
ret = devsw[f->major].write(1, addr, n);
|
2019-06-01 09:33:38 +00:00
|
|
|
} else if(f->type == FD_INODE){
|
2011-08-12 13:25:39 +00:00
|
|
|
// 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 writei()
|
|
|
|
// might be writing a device like the console.
|
2017-10-24 12:49:31 +00:00
|
|
|
int max = ((MAXOPBLOCKS-1-1-2) / 2) * 512;
|
2011-08-12 13:25:39 +00:00
|
|
|
int i = 0;
|
|
|
|
while(i < n){
|
|
|
|
int n1 = n - i;
|
|
|
|
if(n1 > max)
|
|
|
|
n1 = max;
|
2011-08-15 16:44:20 +00:00
|
|
|
|
2014-08-27 21:15:30 +00:00
|
|
|
begin_op();
|
2011-08-15 16:44:20 +00:00
|
|
|
ilock(f->ip);
|
2019-06-04 09:57:47 +00:00
|
|
|
if ((r = writei(f->ip, 1, addr + i, f->off, n1)) > 0)
|
2011-08-29 20:12:01 +00:00
|
|
|
f->off += r;
|
2011-08-15 16:44:20 +00:00
|
|
|
iunlock(f->ip);
|
2014-08-27 21:15:30 +00:00
|
|
|
end_op();
|
2011-08-15 16:44:20 +00:00
|
|
|
|
2011-08-12 13:25:39 +00:00
|
|
|
if(r < 0)
|
|
|
|
break;
|
|
|
|
if(r != n1)
|
|
|
|
panic("short filewrite");
|
|
|
|
i += r;
|
|
|
|
}
|
2019-06-01 09:33:38 +00:00
|
|
|
ret = (i == n ? n : -1);
|
|
|
|
} else {
|
|
|
|
panic("filewrite");
|
2006-07-01 21:26:01 +00:00
|
|
|
}
|
2019-06-01 09:33:38 +00:00
|
|
|
|
|
|
|
return ret;
|
2006-07-12 01:48:35 +00:00
|
|
|
}
|
|
|
|
|