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