テキストの74ページ
低レイヤが提供する関数を使って実装される多くのシステムコールは些細なものである。(sysfile.cを見よ)
その中で注目に値するシステムコールがいくつかある。
sys_linkとsys_unlinkシステムコールは、ディレクトリを変更し、inodeへの参照を生成したり消去したりする。
それらはトランザクションの便利さを示すいい例である。
sys_linkシステムコールは、その引数、oldとnewという文字列変数を取り出すところからはじめる。
oldは存在し、かつディレクトリではないと仮定し、sys_linkはそのip->nlinkをインクリメントする。
それからsys_linkは、newの親ディレクトリと最後のパス要素を探すためにnameiparent関数を呼び、そしてoldのinodeを参照する新しいディレクトリエントリを作成する。
newの親ディレクトリは存在している必要があり、oldのinodeと同じデバイス上になければならない。
inode番号は、一つのディスク上でのみ一意だからである。
以上のルールに沿わないようなエラーが起きた場合、sys_linkは途中まで行った操作を元に戻して、ip->nlinkをデクリメントしなければならない。
sys_linkは複数のディスクブロックを更新する必要があるので、トランザクションはその実装を単純化するが、どのような順番でブロックが更新されるかについては我々が心配する必要はない。
最終的には成功するか失敗するかのどちらかである。
例えば、トランザクション無しだと、リンクを作成するまえにip->nlinkを更新するときに、一時的にファイルシステムが危険な状態になり、その間にクラッシュが起きると、大破壊がもたらされるだろう。
トランザクションを使えば、このような事について心配する必要はなくなる。
sys_linkは、既存のinodeのための新しい名前を作成する。
create関数は、新しいinodeのための新しい名前を作成する。
create関数は、ファイル生成に関わる3つのシステムコールの処理を一般化したものである。
openシステムコールがO_CREATEフラグとともに呼ばれると、通常の新しいファイルを生成し、mkdirシステムコールは新しいディレクトリを作成し、mkdevシステムコールは新しいデバイスファイルを作成する。
sys_linkのように、create関数は、親ディレクトリのinodeを得るためにnameiparent関数を呼ぶことから初める。
それからdirlookup関数を使って、名前がすでに存在していないかチェックする。
名前がすでに存在していた場合、create関数の振る舞いはどのシステムコールに呼ばれたかに依存する。
名前がすでに存在しているという事実は、openでは、mkdirやmkdevとは違った意味を持つ。
createがopenのために呼ばれ(type == T_FILE)、名前がすでに存在し、それが通常のファイルだった場合、openは成功として扱い、createもそれに従う。
そうでなければエラーとなる。
名前が存在しない場合、createはiallocを使って新しいinodeを割り当てる。
新しいinodeがディレクトリである場合、createは”.”と”..”でそのエントリを初期化する。
最後に、そのデータが正常に初期化されたら、createはその親ディレクトリにそのディレクトリへのリンクを作成する。
createは、sys_linkのように、同時に2つのinode(ipとdp)のロックを保持する。
inode ipは新たに割り当てられたものなので、デッドロックは起こりえない。
まずipをロックし、それからdpをロックしようとするような他のプロセスは存在しない。
createを使うと、sys_open, sys_mkdir, sys_mknodの実装が簡単になる。
sys_openがその中では一番複雑である。
なぜなら新しいファイルを生成することは、それが出来ることの一部に過ぎないからである。
openにO_CREATEフラグが渡された場合、createを呼ぶ。
それ以外の場合は、nameiを呼ぶ。
createはロック済みのinodeを返すが、nameiはそうじゃないので、sys_openはそのinodeを自分自身でロックする。
これは、対象のinodeがディレクトリかつ読み込み専用で開かれてるだけかどうかをチェックするにはよい箇所である。
いずれにしても、inodeが得られたと仮定し、sys_openはファイルとファイルディスクリプタを割り当て、それからファイルのメタデータを設定する。
このファイルは、現在のプロセスのテーブルにしか存在しないので、初期化途中のファイルに他のプロセスがアクセスすることはないということに注意せよ。
第4章では、ファイルシステムの説明の前に、パイプの実装について説明した。
sys_pipe関数は、パイプの組を生成する方法を提供する事によって、ファイルシステムの実装への橋渡しをしている。
sys_pipeの引数は、2つの整数の領域を指すポインタであり、新しい2つのファイルディスクリプタを記録する場所になる。
sys_pipeはパイプを割り当て、ファイルディスクリプタにそのパイプを設定する。
sysfile.c
#include "types.h"
#include "defs.h"
#include "param.h"
#include "stat.h"
#include "mmu.h"
#include "proc.h"
#include "fs.h"
#include "file.h"
#include "fcntl.h"
// Fetch the nth word-sized system call argument as a file descriptor
// and return both the descriptor and the corresponding struct file.
static int
argfd(int n, int *pfd, struct file **pf)
{
int fd;
struct file *f;
if(argint(n, &fd) < 0)
return -1;
if(fd < 0 || fd >= NOFILE || (f=proc->ofile[fd]) == 0)
return -1;
if(pfd)
*pfd = fd;
if(pf)
*pf = f;
return 0;
}
// Allocate a file descriptor for the given file.
// Takes over file reference from caller on success.
static int
fdalloc(struct file *f)
{
int fd;
for(fd = 0; fd < NOFILE; fd++){
if(proc->ofile[fd] == 0){
proc->ofile[fd] = f;
return fd;
}
}
return -1;
}
int
sys_dup(void)
{
struct file *f;
int fd;
if(argfd(0, 0, &f) < 0)
return -1;
if((fd=fdalloc(f)) < 0)
return -1;
filedup(f);
return fd;
}
int
sys_read(void)
{
struct file *f;
int n;
char *p;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0)
return -1;
return fileread(f, p, n);
}
int
sys_write(void)
{
struct file *f;
int n;
char *p;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0)
return -1;
return filewrite(f, p, n);
}
int
sys_close(void)
{
int fd;
struct file *f;
if(argfd(0, &fd, &f) < 0)
return -1;
proc->ofile[fd] = 0;
fileclose(f);
return 0;
}
int
sys_fstat(void)
{
struct file *f;
struct stat *st;
if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
return -1;
return filestat(f, st);
}
// Create the path new as a link to the same inode as old.
int
sys_link(void)
{
char name[DIRSIZ], *new, *old;
struct inode *dp, *ip;
if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
return -1;
if((ip = namei(old)) == 0)
return -1;
begin_trans();
ilock(ip);
if(ip->type == T_DIR){
iunlockput(ip);
commit_trans();
return -1;
}
ip->nlink++;
iupdate(ip);
iunlock(ip);
if((dp = nameiparent(new, name)) == 0)
goto bad;
ilock(dp);
if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
iunlockput(dp);
goto bad;
}
iunlockput(dp);
iput(ip);
commit_trans();
return 0;
bad:
ilock(ip);
ip->nlink--;
iupdate(ip);
iunlockput(ip);
commit_trans();
return -1;
}
// Is the directory dp empty except for "." and ".." ?
static int
isdirempty(struct inode *dp)
{
int off;
struct dirent de;
for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("isdirempty: readi");
if(de.inum != 0)
return 0;
}
return 1;
}
//PAGEBREAK!
int
sys_unlink(void)
{
struct inode *ip, *dp;
struct dirent de;
char name[DIRSIZ], *path;
uint off;
if(argstr(0, &path) < 0)
return -1;
if((dp = nameiparent(path, name)) == 0)
return -1;
begin_trans();
ilock(dp);
// Cannot unlink "." or "..".
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0)
goto bad;
if((ip = dirlookup(dp, name, &off)) == 0)
goto bad;
ilock(ip);
if(ip->nlink < 1)
panic("unlink: nlink < 1");
if(ip->type == T_DIR && !isdirempty(ip)){
iunlockput(ip);
goto bad;
}
memset(&de, 0, sizeof(de));
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("unlink: writei");
if(ip->type == T_DIR){
dp->nlink--;
iupdate(dp);
}
iunlockput(dp);
ip->nlink--;
iupdate(ip);
iunlockput(ip);
commit_trans();
return 0;
bad:
iunlockput(dp);
commit_trans();
return -1;
}
static struct inode*
create(char *path, short type, short major, short minor)
{
uint off;
struct inode *ip, *dp;
char name[DIRSIZ];
if((dp = nameiparent(path, name)) == 0)
return 0;
ilock(dp);
if((ip = dirlookup(dp, name, &off)) != 0){
iunlockput(dp);
ilock(ip);
if(type == T_FILE && ip->type == T_FILE)
return ip;
iunlockput(ip);
return 0;
}
if((ip = ialloc(dp->dev, type)) == 0)
panic("create: ialloc");
ilock(ip);
ip->major = major;
ip->minor = minor;
ip->nlink = 1;
iupdate(ip);
if(type == T_DIR){ // Create . and .. entries.
dp->nlink++; // for ".."
iupdate(dp);
// No ip->nlink++ for ".": avoid cyclic ref count.
if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0)
panic("create dots");
}
if(dirlink(dp, name, ip->inum) < 0)
panic("create: dirlink");
iunlockput(dp);
return ip;
}
int
sys_open(void)
{
char *path;
int fd, omode;
struct file *f;
struct inode *ip;
if(argstr(0, &path) < 0 || argint(1, &omode) < 0)
return -1;
if(omode & O_CREATE){
begin_trans();
ip = create(path, T_FILE, 0, 0);
commit_trans();
if(ip == 0)
return -1;
} else {
if((ip = namei(path)) == 0)
return -1;
ilock(ip);
if(ip->type == T_DIR && omode != O_RDONLY){
iunlockput(ip);
return -1;
}
}
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f)
fileclose(f);
iunlockput(ip);
return -1;
}
iunlock(ip);
f->type = FD_INODE;
f->ip = ip;
f->off = 0;
f->readable = !(omode & O_WRONLY);
f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
return fd;
}
int
sys_mkdir(void)
{
char *path;
struct inode *ip;
begin_trans();
if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){
commit_trans();
return -1;
}
iunlockput(ip);
commit_trans();
return 0;
}
int
sys_mknod(void)
{
struct inode *ip;
char *path;
int len;
int major, minor;
begin_trans();
if((len=argstr(0, &path)) < 0 ||
argint(1, &major) < 0 ||
argint(2, &minor) < 0 ||
(ip = create(path, T_DEV, major, minor)) == 0){
commit_trans();
return -1;
}
iunlockput(ip);
commit_trans();
return 0;
}
int
sys_chdir(void)
{
char *path;
struct inode *ip;
if(argstr(0, &path) < 0 || (ip = namei(path)) == 0)
return -1;
ilock(ip);
if(ip->type != T_DIR){
iunlockput(ip);
return -1;
}
iunlock(ip);
iput(proc->cwd);
proc->cwd = ip;
return 0;
}
int
sys_exec(void)
{
char *path, *argv[MAXARG];
int i;
uint uargv, uarg;
if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0){
return -1;
}
memset(argv, 0, sizeof(argv));
for(i=0;; i++){
if(i >= NELEM(argv))
return -1;
if(fetchint(proc, uargv+4*i, (int*)&uarg) < 0)
return -1;
if(uarg == 0){
argv[i] = 0;
break;
}
if(fetchstr(proc, uarg, &argv[i]) < 0)
return -1;
}
return exec(path, argv);
}
int
sys_pipe(void)
{
int *fd;
struct file *rf, *wf;
int fd0, fd1;
if(argptr(0, (void*)&fd, 2*sizeof(fd[0])) < 0)
return -1;
if(pipealloc(&rf, &wf) < 0)
return -1;
fd0 = -1;
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if(fd0 >= 0)
proc->ofile[fd0] = 0;
fileclose(rf);
fileclose(wf);
return -1;
}
fd[0] = fd0;
fd[1] = fd1;
return 0;
}
ファイルシステム関連のシステムコールの実装についてです。
原文でも明記されてなかったりするので、システムコールという名称と関数という名称の使い分けが曖昧な部分があります。
まぁ関数のほうがより一般的な概念なのでどちらも関数と言っておけば間違いないと思いますが、システムコールに関しては、SYSCALLマクロで生成された(例えば)openのようなものだけをそう呼ぶのか、その実体である(例えば)sys_openまで含めるのかどうかが微妙なところです。
最近のコメント
たかたむ
はじめまして。初リアルフォース(R3ですが)で,同…
nokiyameego
ZFS poolのデバイスラベル破損で悩んていたと…
名前
しゅごい
Jane Doe
FYI Avoid Annoying Unexpe…
Jane Doe
ご存じとは思いますが、whileには、”~の間”と…
花粉症対策2019 – 日曜研究室
[…] 花粉症対策についてはこれまで次の記事を書いてきました。https://…
花粉症対策2019 – 日曜研究室
[…] 花粉症対策についてはこれまで次の記事を書いてきました。https://…
花粉症対策2019 – 日曜研究室
[…] 花粉症対策についてはこれまで次の記事を書いてきました。https://…
花粉症対策2019 – 日曜研究室
[…] 花粉症対策についてはこれまで次の記事を書いてきました。https://…
花粉症対策2019 – 日曜研究室
[…] 花粉症対策についてはこれまで次の記事を書いてきました。https://…