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