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"
|
2009-08-08 10:07:30 +02:00
|
|
|
#include "fs.h"
|
2011-08-26 16:08:29 +02:00
|
|
|
#include "file.h"
|
2011-08-29 23:18:40 +02:00
|
|
|
#include "spinlock.h"
|
2006-07-12 03:48:35 +02:00
|
|
|
|
2006-08-09 18:04:04 +02:00
|
|
|
struct devsw devsw[NDEV];
|
2009-05-31 04:07:26 +02:00
|
|
|
struct {
|
|
|
|
struct spinlock lock;
|
|
|
|
struct file file[NFILE];
|
|
|
|
} ftable;
|
2006-06-27 16:35:53 +02:00
|
|
|
|
2006-08-11 00:08:14 +02:00
|
|
|
void
|
2006-09-06 20:43:45 +02:00
|
|
|
fileinit(void)
|
2006-08-11 00:08:14 +02:00
|
|
|
{
|
2009-05-31 07:12:21 +02:00
|
|
|
initlock(&ftable.lock, "ftable");
|
2006-08-11 00:08:14 +02:00
|
|
|
}
|
|
|
|
|
2007-08-22 08:01:32 +02:00
|
|
|
// Allocate a file structure.
|
2006-09-06 20:38:56 +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);
|
|
|
|
for(f = ftable.file; f < ftable.file + NFILE; f++){
|
|
|
|
if(f->ref == 0){
|
|
|
|
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.
|
2007-08-27 16:35:09 +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)
|
2007-08-27 16:35:09 +02:00
|
|
|
panic("filedup");
|
2007-08-14 21:41:56 +02:00
|
|
|
f->ref++;
|
2009-05-31 04:07:26 +02:00
|
|
|
release(&ftable.lock);
|
2007-08-27 16:35:09 +02:00
|
|
|
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");
|
|
|
|
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);
|
2007-08-27 16:37:13 +02:00
|
|
|
|
|
|
|
if(ff.type == FD_PIPE)
|
2007-08-28 06:22:35 +02:00
|
|
|
pipeclose(ff.pipe, ff.writable);
|
2011-08-12 15:25:39 +02:00
|
|
|
else if(ff.type == FD_INODE){
|
|
|
|
begin_trans();
|
2007-08-27 16:37:13 +02:00
|
|
|
iput(ff.ip);
|
2011-08-12 15:25:39 +02:00
|
|
|
commit_trans();
|
|
|
|
}
|
2007-08-27 16:37:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get metadata about file f.
|
|
|
|
int
|
|
|
|
filestat(struct file *f, struct stat *st)
|
|
|
|
{
|
|
|
|
if(f->type == FD_INODE){
|
|
|
|
ilock(f->ip);
|
|
|
|
stati(f->ip, st);
|
|
|
|
iunlock(f->ip);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-09-07 16:28:12 +02:00
|
|
|
// Read from file f. Addr is kernel address.
|
2006-06-27 16:35:53 +02:00
|
|
|
int
|
2006-09-08 15:44:42 +02:00
|
|
|
fileread(struct file *f, char *addr, int n)
|
2006-06-27 16:35:53 +02:00
|
|
|
{
|
2007-08-14 21:41:56 +02:00
|
|
|
int r;
|
|
|
|
|
2006-09-08 15:44:42 +02:00
|
|
|
if(f->readable == 0)
|
2006-06-27 16:35:53 +02:00
|
|
|
return -1;
|
2007-08-14 21:41:56 +02:00
|
|
|
if(f->type == FD_PIPE)
|
2007-08-28 06:22:35 +02:00
|
|
|
return piperead(f->pipe, addr, n);
|
2007-08-22 08:01:32 +02:00
|
|
|
if(f->type == FD_INODE){
|
2007-08-24 22:54:23 +02:00
|
|
|
ilock(f->ip);
|
|
|
|
if((r = readi(f->ip, addr, f->off, n)) > 0)
|
2007-08-14 21:41:56 +02:00
|
|
|
f->off += r;
|
2007-08-24 22:54:23 +02:00
|
|
|
iunlock(f->ip);
|
2007-08-14 21:41:56 +02:00
|
|
|
return r;
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
2007-08-14 21:41:56 +02:00
|
|
|
panic("fileread");
|
2006-06-27 16:35:53 +02:00
|
|
|
}
|
2006-07-01 23:26:01 +02:00
|
|
|
|
2011-09-02 21:35:34 +02:00
|
|
|
//PAGEBREAK!
|
2007-08-14 21:41:56 +02:00
|
|
|
// Write to file f. Addr is kernel address.
|
|
|
|
int
|
|
|
|
filewrite(struct file *f, char *addr, int n)
|
2006-07-01 23:26:01 +02:00
|
|
|
{
|
2007-08-14 21:41:56 +02:00
|
|
|
int r;
|
2006-07-12 03:48:35 +02:00
|
|
|
|
2007-08-14 21:41:56 +02:00
|
|
|
if(f->writable == 0)
|
|
|
|
return -1;
|
|
|
|
if(f->type == FD_PIPE)
|
2007-08-28 06:22:35 +02:00
|
|
|
return pipewrite(f->pipe, addr, n);
|
2007-08-22 08:01:32 +02:00
|
|
|
if(f->type == FD_INODE){
|
2011-08-12 15:25:39 +02: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.
|
|
|
|
int max = ((LOGSIZE-1-1-2) / 2) * 512;
|
|
|
|
int i = 0;
|
|
|
|
while(i < n){
|
|
|
|
int n1 = n - i;
|
|
|
|
if(n1 > max)
|
|
|
|
n1 = max;
|
2011-08-15 18:44:20 +02:00
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
begin_trans();
|
2011-08-15 18:44:20 +02:00
|
|
|
ilock(f->ip);
|
2011-08-26 16:08:29 +02:00
|
|
|
if ((r = writei(f->ip, addr + i, f->off, n1)) > 0)
|
2011-08-29 22:12:01 +02:00
|
|
|
f->off += r;
|
2011-08-15 18:44:20 +02:00
|
|
|
iunlock(f->ip);
|
2011-08-12 15:25:39 +02:00
|
|
|
commit_trans();
|
2011-08-15 18:44:20 +02:00
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
if(r < 0)
|
|
|
|
break;
|
|
|
|
if(r != n1)
|
|
|
|
panic("short filewrite");
|
|
|
|
i += r;
|
|
|
|
}
|
|
|
|
return i == n ? n : -1;
|
2006-07-01 23:26:01 +02:00
|
|
|
}
|
2007-08-14 21:41:56 +02:00
|
|
|
panic("filewrite");
|
2006-07-12 03:48:35 +02:00
|
|
|
}
|
|
|
|
|