xv6-riscv-kernel/kernel/file.c

180 lines
3.5 KiB
C
Raw Normal View History

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