0
|
1 //
|
|
2 // File-system system calls.
|
|
3 // Mostly argument checking, since we don't trust
|
|
4 // user code, and calls into file.c and fs.c.
|
|
5 //
|
|
6
|
|
7 #include "types.h"
|
|
8 #include "defs.h"
|
|
9 #include "param.h"
|
|
10 #include "stat.h"
|
|
11 #include "mmu.h"
|
|
12 #include "proc.h"
|
|
13 #include "fs.h"
|
|
14 #include "file.h"
|
|
15 #include "fcntl.h"
|
|
16
|
|
17 // Fetch the nth word-sized system call argument as a file descriptor
|
|
18 // and return both the descriptor and the corresponding struct file.
|
|
19 static int argfd(int n, int *pfd, struct file **pf)
|
|
20 {
|
|
21 int fd;
|
|
22 struct file *f;
|
|
23
|
|
24 if(argint(n, &fd) < 0) {
|
|
25 return -1;
|
|
26 }
|
|
27
|
|
28 if(fd < 0 || fd >= NOFILE || (f=proc->ofile[fd]) == 0) {
|
|
29 return -1;
|
|
30 }
|
|
31
|
|
32 if(pfd) {
|
|
33 *pfd = fd;
|
|
34 }
|
|
35
|
|
36 if(pf) {
|
|
37 *pf = f;
|
|
38 }
|
|
39
|
|
40 return 0;
|
|
41 }
|
|
42
|
|
43 // Allocate a file descriptor for the given file.
|
|
44 // Takes over file reference from caller on success.
|
|
45 static int fdalloc(struct file *f)
|
|
46 {
|
|
47 int fd;
|
|
48
|
|
49 for(fd = 0; fd < NOFILE; fd++){
|
|
50 if(proc->ofile[fd] == 0){
|
|
51 proc->ofile[fd] = f;
|
|
52 return fd;
|
|
53 }
|
|
54 }
|
|
55
|
|
56 return -1;
|
|
57 }
|
|
58
|
|
59 int sys_dup(void)
|
|
60 {
|
|
61 struct file *f;
|
|
62 int fd;
|
|
63
|
|
64 if(argfd(0, 0, &f) < 0) {
|
|
65 return -1;
|
|
66 }
|
|
67
|
|
68 if((fd=fdalloc(f)) < 0) {
|
|
69 return -1;
|
|
70 }
|
|
71
|
|
72 filedup(f);
|
|
73
|
|
74 return fd;
|
|
75 }
|
|
76
|
24
|
77 __code cbc_read(__code (*next)(int ret)){
|
|
78 struct file *f;
|
|
79 int n;
|
|
80 char *p;
|
|
81
|
|
82 if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) {
|
|
83 goto next(-1);
|
|
84 }
|
|
85 goto cbc_fileread(f, p, n, next);
|
|
86 }
|
|
87
|
0
|
88 int sys_read(void)
|
|
89 {
|
|
90 struct file *f;
|
|
91 int n;
|
|
92 char *p;
|
|
93
|
|
94 if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) {
|
|
95 return -1;
|
|
96 }
|
|
97
|
|
98 return fileread(f, p, n);
|
|
99 }
|
|
100
|
|
101 int sys_write(void)
|
|
102 {
|
|
103 struct file *f;
|
|
104 int n;
|
|
105 char *p;
|
|
106
|
|
107 if(argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0) {
|
|
108 return -1;
|
|
109 }
|
|
110
|
|
111 return filewrite(f, p, n);
|
|
112 }
|
|
113
|
|
114 int sys_close(void)
|
|
115 {
|
|
116 int fd;
|
|
117 struct file *f;
|
|
118
|
|
119 if(argfd(0, &fd, &f) < 0) {
|
|
120 return -1;
|
|
121 }
|
|
122
|
|
123 proc->ofile[fd] = 0;
|
|
124 fileclose(f);
|
|
125
|
|
126 return 0;
|
|
127 }
|
|
128
|
|
129 int sys_fstat(void)
|
|
130 {
|
|
131 struct file *f;
|
|
132 struct stat *st;
|
|
133
|
|
134 if(argfd(0, 0, &f) < 0 || argptr(1, (void*)&st, sizeof(*st)) < 0) {
|
|
135 return -1;
|
|
136 }
|
|
137
|
|
138 return filestat(f, st);
|
|
139 }
|
|
140
|
|
141 // Create the path new as a link to the same inode as old.
|
|
142 int sys_link(void)
|
|
143 {
|
|
144 char name[DIRSIZ], *new, *old;
|
|
145 struct inode *dp, *ip;
|
|
146
|
|
147 if(argstr(0, &old) < 0 || argstr(1, &new) < 0) {
|
|
148 return -1;
|
|
149 }
|
|
150
|
|
151 if((ip = namei(old)) == 0) {
|
|
152 return -1;
|
|
153 }
|
|
154
|
|
155 begin_trans();
|
|
156
|
|
157 ilock(ip);
|
|
158
|
|
159 if(ip->type == T_DIR){
|
|
160 iunlockput(ip);
|
|
161 commit_trans();
|
|
162 return -1;
|
|
163 }
|
|
164
|
|
165 ip->nlink++;
|
|
166 iupdate(ip);
|
|
167 iunlock(ip);
|
|
168
|
|
169 if((dp = nameiparent(new, name)) == 0) {
|
|
170 goto bad;
|
|
171 }
|
|
172
|
|
173 ilock(dp);
|
|
174
|
|
175 if(dp->dev != ip->dev || dirlink(dp, name, ip->inum) < 0){
|
|
176 iunlockput(dp);
|
|
177 goto bad;
|
|
178 }
|
|
179
|
|
180 iunlockput(dp);
|
|
181 iput(ip);
|
|
182
|
|
183 commit_trans();
|
|
184
|
|
185 return 0;
|
|
186
|
|
187 bad:
|
|
188 ilock(ip);
|
|
189 ip->nlink--;
|
|
190 iupdate(ip);
|
|
191 iunlockput(ip);
|
|
192 commit_trans();
|
|
193 return -1;
|
|
194 }
|
|
195
|
|
196 // Is the directory dp empty except for "." and ".." ?
|
|
197 static int isdirempty(struct inode *dp)
|
|
198 {
|
|
199 int off;
|
|
200 struct dirent de;
|
|
201
|
|
202 for(off=2*sizeof(de); off<dp->size; off+=sizeof(de)){
|
|
203 if(readi(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) {
|
|
204 panic("isdirempty: readi");
|
|
205 }
|
|
206
|
|
207 if(de.inum != 0) {
|
|
208 return 0;
|
|
209 }
|
|
210 }
|
|
211 return 1;
|
|
212 }
|
|
213
|
|
214 //PAGEBREAK!
|
|
215 int sys_unlink(void)
|
|
216 {
|
|
217 struct inode *ip, *dp;
|
|
218 struct dirent de;
|
|
219 char name[DIRSIZ], *path;
|
|
220 uint off;
|
|
221
|
|
222 if(argstr(0, &path) < 0) {
|
|
223 return -1;
|
|
224 }
|
|
225
|
|
226 if((dp = nameiparent(path, name)) == 0) {
|
|
227 return -1;
|
|
228 }
|
|
229
|
|
230 begin_trans();
|
|
231
|
|
232 ilock(dp);
|
|
233
|
|
234 // Cannot unlink "." or "..".
|
|
235 if(namecmp(name, ".") == 0 || namecmp(name, "..") == 0) {
|
|
236 goto bad;
|
|
237 }
|
|
238
|
|
239 if((ip = dirlookup(dp, name, &off)) == 0) {
|
|
240 goto bad;
|
|
241 }
|
|
242
|
|
243 ilock(ip);
|
|
244
|
|
245 if(ip->nlink < 1) {
|
|
246 panic("unlink: nlink < 1");
|
|
247 }
|
|
248
|
|
249 if(ip->type == T_DIR && !isdirempty(ip)){
|
|
250 iunlockput(ip);
|
|
251 goto bad;
|
|
252 }
|
|
253
|
|
254 memset(&de, 0, sizeof(de));
|
|
255
|
|
256 if(writei(dp, (char*)&de, off, sizeof(de)) != sizeof(de)) {
|
|
257 panic("unlink: writei");
|
|
258 }
|
|
259
|
|
260 if(ip->type == T_DIR){
|
|
261 dp->nlink--;
|
|
262 iupdate(dp);
|
|
263 }
|
|
264
|
|
265 iunlockput(dp);
|
|
266
|
|
267 ip->nlink--;
|
|
268 iupdate(ip);
|
|
269 iunlockput(ip);
|
|
270
|
|
271 commit_trans();
|
|
272
|
|
273 return 0;
|
|
274
|
|
275 bad:
|
|
276 iunlockput(dp);
|
|
277 commit_trans();
|
|
278 return -1;
|
|
279 }
|
|
280
|
|
281 static struct inode* create(char *path, short type, short major, short minor)
|
|
282 {
|
|
283 uint off;
|
|
284 struct inode *ip, *dp;
|
|
285 char name[DIRSIZ];
|
|
286
|
|
287 if((dp = nameiparent(path, name)) == 0) {
|
|
288 return 0;
|
|
289 }
|
|
290
|
|
291 ilock(dp);
|
|
292
|
|
293 if((ip = dirlookup(dp, name, &off)) != 0){
|
|
294 iunlockput(dp);
|
|
295 ilock(ip);
|
|
296
|
|
297 if(type == T_FILE && ip->type == T_FILE) {
|
|
298 return ip;
|
|
299 }
|
|
300
|
|
301 iunlockput(ip);
|
|
302
|
|
303 return 0;
|
|
304 }
|
|
305
|
|
306 if((ip = ialloc(dp->dev, type)) == 0) {
|
|
307 panic("create: ialloc");
|
|
308 }
|
|
309
|
|
310 ilock(ip);
|
|
311 ip->major = major;
|
|
312 ip->minor = minor;
|
|
313 ip->nlink = 1;
|
|
314 iupdate(ip);
|
|
315
|
|
316 if(type == T_DIR){ // Create . and .. entries.
|
|
317 dp->nlink++; // for ".."
|
|
318 iupdate(dp);
|
|
319
|
|
320 // No ip->nlink++ for ".": avoid cyclic ref count.
|
|
321 if(dirlink(ip, ".", ip->inum) < 0 || dirlink(ip, "..", dp->inum) < 0) {
|
|
322 panic("create dots");
|
|
323 }
|
|
324 }
|
|
325
|
|
326 if(dirlink(dp, name, ip->inum) < 0) {
|
|
327 panic("create: dirlink");
|
|
328 }
|
|
329
|
|
330 iunlockput(dp);
|
|
331
|
|
332 return ip;
|
|
333 }
|
|
334
|
|
335 int sys_open(void)
|
|
336 {
|
|
337 char *path;
|
|
338 int fd, omode;
|
|
339 struct file *f;
|
|
340 struct inode *ip;
|
|
341
|
|
342 if(argstr(0, &path) < 0 || argint(1, &omode) < 0) {
|
|
343 return -1;
|
|
344 }
|
|
345
|
|
346 if(omode & O_CREATE){
|
|
347 begin_trans();
|
|
348 ip = create(path, T_FILE, 0, 0);
|
|
349 commit_trans();
|
|
350
|
|
351 if(ip == 0) {
|
|
352 return -1;
|
|
353 }
|
|
354
|
|
355 } else {
|
|
356 if((ip = namei(path)) == 0) {
|
|
357 return -1;
|
|
358 }
|
|
359
|
|
360 ilock(ip);
|
|
361
|
|
362 if(ip->type == T_DIR && omode != O_RDONLY){
|
|
363 iunlockput(ip);
|
|
364 return -1;
|
|
365 }
|
|
366 }
|
|
367
|
|
368 if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
|
|
369 if(f) {
|
|
370 fileclose(f);
|
|
371 }
|
|
372
|
|
373 iunlockput(ip);
|
|
374 return -1;
|
|
375 }
|
|
376
|
|
377 iunlock(ip);
|
|
378
|
|
379 f->type = FD_INODE;
|
|
380 f->ip = ip;
|
|
381 f->off = 0;
|
|
382 f->readable = !(omode & O_WRONLY);
|
|
383 f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
|
|
384
|
|
385 return fd;
|
|
386 }
|
|
387
|
|
388 int sys_mkdir(void)
|
|
389 {
|
|
390 char *path;
|
|
391 struct inode *ip;
|
|
392
|
|
393 begin_trans();
|
|
394
|
|
395 if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){
|
|
396 commit_trans();
|
|
397 return -1;
|
|
398 }
|
|
399
|
|
400 iunlockput(ip);
|
|
401 commit_trans();
|
|
402
|
|
403 return 0;
|
|
404 }
|
|
405
|
|
406 int sys_mknod(void)
|
|
407 {
|
|
408 struct inode *ip;
|
|
409 char *path;
|
|
410 int len;
|
|
411 int major, minor;
|
|
412
|
|
413 begin_trans();
|
|
414
|
|
415 if((len=argstr(0, &path)) < 0 ||
|
|
416 argint(1, &major) < 0 || argint(2, &minor) < 0 ||
|
|
417 (ip = create(path, T_DEV, major, minor)) == 0){
|
|
418
|
|
419 commit_trans();
|
|
420 return -1;
|
|
421 }
|
|
422
|
|
423 iunlockput(ip);
|
|
424 commit_trans();
|
|
425
|
|
426 return 0;
|
|
427 }
|
|
428
|
|
429 int sys_chdir(void)
|
|
430 {
|
|
431 char *path;
|
|
432 struct inode *ip;
|
|
433
|
|
434 if(argstr(0, &path) < 0 || (ip = namei(path)) == 0) {
|
|
435 return -1;
|
|
436 }
|
|
437
|
|
438 ilock(ip);
|
|
439
|
|
440 if(ip->type != T_DIR){
|
|
441 iunlockput(ip);
|
|
442 return -1;
|
|
443 }
|
|
444
|
|
445 iunlock(ip);
|
|
446
|
|
447 iput(proc->cwd);
|
|
448 proc->cwd = ip;
|
|
449
|
|
450 return 0;
|
|
451 }
|
|
452
|
|
453 int sys_exec(void)
|
|
454 {
|
|
455 char *path, *argv[MAXARG];
|
|
456 int i;
|
|
457 uint uargv, uarg;
|
|
458
|
|
459 if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0){
|
|
460 return -1;
|
|
461 }
|
|
462
|
|
463 memset(argv, 0, sizeof(argv));
|
|
464
|
|
465 for(i=0;; i++){
|
|
466 if(i >= NELEM(argv)) {
|
|
467 return -1;
|
|
468 }
|
|
469
|
|
470 if(fetchint(uargv+4*i, (int*)&uarg) < 0) {
|
|
471 return -1;
|
|
472 }
|
|
473
|
|
474 if(uarg == 0){
|
|
475 argv[i] = 0;
|
|
476 break;
|
|
477 }
|
|
478
|
|
479 if(fetchstr(uarg, &argv[i]) < 0) {
|
|
480 return -1;
|
|
481 }
|
|
482 }
|
|
483
|
|
484 return exec(path, argv);
|
|
485 }
|
|
486
|
|
487 int sys_pipe(void)
|
|
488 {
|
|
489 int *fd;
|
|
490 struct file *rf, *wf;
|
|
491 int fd0, fd1;
|
|
492
|
|
493 if(argptr(0, (void*)&fd, 2*sizeof(fd[0])) < 0) {
|
|
494 return -1;
|
|
495 }
|
|
496
|
|
497 if(pipealloc(&rf, &wf) < 0) {
|
|
498 return -1;
|
|
499 }
|
|
500
|
|
501 fd0 = -1;
|
|
502
|
|
503 if((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
|
|
504 if(fd0 >= 0) {
|
|
505 proc->ofile[fd0] = 0;
|
|
506 }
|
|
507
|
|
508 fileclose(rf);
|
|
509 fileclose(wf);
|
|
510
|
|
511 return -1;
|
|
512 }
|
|
513
|
|
514 fd[0] = fd0;
|
|
515 fd[1] = fd1;
|
|
516
|
|
517 return 0;
|
|
518 }
|