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