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