系列文章 : [6.1810] 跟著 MIT 6.1810 學習基礎作業系統觀念
struct file {
enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type;
int ref; // reference count
char readable;
char writable;
struct pipe *pipe; // FD_PIPE
struct inode *ip; // FD_INODE and FD_DEVICE
uint off; // FD_INODE
short major; // FD_DEVICE
};
filedup 都會增加 reference countkernel/file.c/struct-devsw 陣列的 index,讓我們知道該用什麼 read/write function。// map major device number to device functions.
struct devsw {
int (*read)(int, uint64, int);
int (*write)(int, uint64, int);
};
struct {
struct spinlock lock;
struct file file[NFILE];
} ftable;
ftable 去追蹤目前被任何 process 打開的檔案。當我們想透過 struct-file 去呼叫某個 device 的 read/write function 的時候,會用該 device 的 major-number ( struct-file->major ) 去找出相對應的 struct devsw
void
fileinit(void)
{
initlock(&ftable.lock, "ftable");
}
在開機的時候,會由 cpuid == 0 的 CPU 呼叫這個 function 來進行初始化。
這裡會對 ftable 的 spinlock 進行初始化。
// Allocate a file structure.
struct file*
filealloc(void)
{
global open file table 裡面去尋找可使用的 ( 目前沒被使用的 ) struct-file。 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)
{
acquire(&ftable.lock);
if(f->ref < 1)
panic("filedup");
f->ref++;
release(&ftable.lock);
return f;
}
會把 struct-file 的 ref + 1,代表多一個人想要使用該 struct-file。
// 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;
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){
begin_op();
iput(ff.ip);
end_op();
}
}
// 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){
ilock(f->ip);
stati(f->ip, &st);
iunlock(f->ip);
if(copyout(p->pagetable, addr, (char *)&st, sizeof(st)) < 0)
return -1;
return 0;
}
return -1;
}
kernel space to user space )。// Read from file f.
// addr is a user virtual address.
int
fileread(struct file *f, uint64 addr, int n)
{
struct file *f 讀取資料
struct file 讀取資料 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;
r = devsw[f->major].read(1, addr, n);
} else if(f->type == FD_INODE){
ilock(f->ip);
if((r = readi(f->ip, 1, addr, f->off, n)) > 0)
f->off += r;
iunlock(f->ip);
} else {
panic("fileread");
}
return r;
}
FD_PIPE,則呼叫 pipe 相對應的 read function。FD_DEVICE,則呼叫 device 對應的 read function。不同的 device 可以有自己的 read function。readi,嘗試去 disk-hardware ( VirtIO Block Device ) 讀取出資料。// Write to file f.
// addr is a user virtual address.
int
filewrite(struct file *f, uint64 addr, int n)
{
struct file *f。一樣會去看 struct-file->type 來決定要呼叫什麼樣的 fuction。 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;
ret = devsw[f->major].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.
int max = ((MAXOPBLOCKS-1-1-2) / 2) * BSIZE;
int i = 0;
while(i < n){
int n1 = n - i;
if(n1 > max)
n1 = max;
begin_op();
ilock(f->ip);
if ((r = writei(f->ip, 1, addr + i, f->off, n1)) > 0)
f->off += r;
iunlock(f->ip);
end_op();
if(r != n1){
// error from writei
break;
}
i += r;
}
ret = (i == n ? n : -1);
} else {
panic("filewrite");
}
return ret;
}
max = ((MAXOPBLOCKS-1-1-2) / 2) * BSIZE : 計算一次最多可以放多少個 block ( 每個 block 的 size 為 BSIZE ),因為 log layer 的 log blocks 有個數上的限制,所以沒辦法一次寫入太多 block。