Newer
Older
#include "types.h"
#include "stat.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "x86.h"
#include "traps.h"
#include "syscall.h"
#include "spinlock.h"
#include "buf.h"
#include "fs.h"
#include "fsvar.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 argno, int *pfd, struct file **pf)
int fd;
struct file *f;
if(argint(argno, &fd) < 0)
return -1;
if(fd < 0 || fd >= NOFILE || (f=cp->ofile[fd]) == 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;
if(cp->ofile[fd] == 0){
cp->ofile[fd] = f;
}
int
sys_write(void)
{
struct file *f;
int n;
char *cp;
if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &cp, n) < 0)
return -1;
return filewrite(f, cp, n);
struct stat *st;
if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0)
}
int
sys_close(void)
{
int fd;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// 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;
struct uinode *ipu;
if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
return -1;
if((ip = ilock(namei(old))) == 0)
return -1;
if(ip->type == T_DIR){
iput(iunlock(ip));
return -1;
}
ip->nlink++;
iupdate(ip);
ipu = iunlock(ip); ip = 0;
if((dp = ilock(nameiparent(new, name))) == 0 ||
dp->dev != ipu->dev || dirlink(dp, name, ipu->inum) < 0){
if(dp)
iput(iunlock(dp));
ip = ilock(ipu);
ip->nlink--;
iupdate(ip);
iput(iunlock(ip));
return -1;
}
iput(iunlock(dp));
iput(ipu);
return 0;
}
// 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;
}
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 = ilock(nameiparent(path, name))) == 0)
return -1;
// Cannot unlink "." or "..".
if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0){
iput(iunlock(dp));
return -1;
}
if((ip = ilock(dirlookup(dp, name, &off))) == 0){
iput(iunlock(dp));
return -1;
}
if(ip->nlink < 1)
panic("unlink: nlink < 1");
if(ip->type == T_DIR && !isdirempty(ip)){
iput(iunlock(ip));
iput(iunlock(dp));
return -1;
}
memset(&de, 0, sizeof(de));
if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de))
panic("unlink: writei");
iput(iunlock(dp));
ip->nlink--;
iupdate(ip);
iput(iunlock(ip));
return 0;
}
// Create the path and return its unlocked inode structure.
static struct inode*
mkpath(char *path, int canexist, short type, short major, short minor)
{
uint off;
struct inode *ip, *dp;
struct uinode *ipu;
char name[DIRSIZ];
if((dp = ilock(nameiparent(path, name))) == 0)
return 0;
if(canexist && (ipu = dirlookup(dp, name, &off)) != 0){
iput(iunlock(dp));
ip = ilock(ipu);
if(ip->type != type || ip->major != major || ip->minor != minor){
iput(iunlock(ip));
return 0;
}
return ip;
}
if((ip = ilock(ialloc(dp->dev, type))) == 0){
iput(iunlock(dp));
return 0;
}
ip->major = major;
ip->minor = minor;
ip->size = 0;
ip->nlink = 1;
iupdate(ip);
if(dirlink(dp, name, ip->inum) < 0){
ip->nlink = 0;
iput(iunlock(ip));
iput(iunlock(dp));
return 0;
}
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("mkpath dots");
}
iput(iunlock(dp));
return ip;
}
if(omode & O_CREATE){
if((ip = mkpath(path, 1, T_FILE, 0, 0)) == 0)
return -1;
}else{
if((ip = ilock(namei(path))) == 0)
return -1;
if(ip->type == T_DIR && (omode & (O_RDWR|O_WRONLY))){
iput(iunlock(ip));
return -1;
}
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f)
fileclose(f);
iput(iunlock(ip));
if(omode & O_RDWR) {
f->readable = 1;
f->writable = 1;
} else if(omode & O_WRONLY) {
f->readable = 0;
f->writable = 1;
}
int
sys_mknod(void)
{
char *path;
int len;
int type, major, minor;
if((len=argstr(0, &path)) < 0 || argint(1, &type) < 0 ||
argint(2, &major) < 0 || argint(3, &minor) < 0)
// XXX check that type == T_DEV or eliminate type arg?
if((ip = mkpath(path, 0, type, major, minor)) == 0)
}
int
sys_mkdir(void)
{
}
int
sys_chdir(void)
{
return -1;
if(ip->type != T_DIR) {
return 0;
}
int
sys_dup(void)
{
}
int
sys_exec(void)
{
return -1;
if(fetchint(cp, uargv+4*i, (int*)&uarg) < 0)
return -1;
if(uarg == 0){
argv[i] = 0;
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(pipe_alloc(&rf, &wf) < 0)
return -1;
fd0 = -1;
if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if(fd0 >= 0)
cp->ofile[fd0] = 0;
fileclose(rf);
fileclose(wf);
return -1;
}
fd[0] = fd0;
fd[1] = fd1;
return 0;
}