forked from SilverRainZ/OS67
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysfile.c
452 lines (364 loc) · 8.37 KB
/
sysfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// #define __LOG_ON 1
// std
#include <type.h>
#include <dbg.h>
#include <syscall.h>
#include <pipe.h>
// libs
#include <printk.h>
#include <string.h>
// proc
#include <proc.h>
// fs
#include <minix.h>
#include <stat.h>
#include <inode.h>
#include <dir.h>
#include <p2i.h>
#include <file.h>
#include <sysfile.h>
/* get syscall param as file descrpitor */
static int argfd(int n, int *pfd, struct file **pf){
int fd;
struct file *f;
if (argint(n, &fd) < 0){
goto bad;
}
if (fd < 0 || fd >= NOFILE || (f = proc->ofile[fd]) == 0){
goto bad;
}
if (pfd){
*pfd = fd;
}
if (pf){
*pf = f;
}
printl("argfd: fd: %d, file 0x%x\n", fd, f);
return 0;
bad:
printl("argfd: failed %d\n", n);
return -1;
}
/* alloc a file descrpitor for given file */
static int fdalloc(struct file *f){
int fd;
for (fd = 0; fd < NOFILE; fd++){
if (proc->ofile[fd] == 0){
proc->ofile[fd] = f;
return fd;
}
}
return -1;
}
int sys_dup(){
struct file *f;
int fd;
if (argfd(0, 0, &f) < 0){
return -1;
}
printl("sys_dup: file 0x%x\n", f);
if ((fd = fdalloc(f)) < 0){
return -1;
}
fdup(f);
return fd;
}
int sys_read(){
struct file *f;
int n;
char *p;
if (argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0){
return -1;
}
printl("sys_read: file 0x%x, dest: 0x%x, sz: %d\n", f, p, n);
return fread(f, p, n);
}
int sys_write(){
struct file *f;
int n;
char *p;
if (argfd(0, 0, &f) < 0 || argint(2, &n) < 0 || argptr(1, &p, n) < 0){
return -1;
}
return fwrite(f, p, n);
}
int sys_close(){
int fd;
struct file *f;
if (argfd(0, &fd, &f) < 0){
return -1;
}
proc->ofile[fd] = 0;
fclose(f);
return 0;
}
int sys_fstat(){
struct file *f;
struct stat *st;
if (argfd(0, 0, &f) < 0 || argptr(1, (void *)&st, sizeof(st)) < 0){
return -1;
}
printl("sys_fstat: fp: 0x%x, istat: 0x%x\n", f, st);
return fstat(f, st);
}
/* int _link(char *old, char *new); */
int sys_link(){
char name[NAME_LEN], *new, *old;
struct inode *dp, *ip;
if (argstr(0, &old) < 0 || argstr(1, &new) < 0){
return -1;
}
if ((ip = p2i(old)) == 0){
return -1;
}
printl("sys_link: old: [%s] new: [%s]\n", old, new);
ilock(ip);
if (S_ISDIR(ip->mode)){ // don't create link for directory
printl("sys_link: failed, can not link directory\n");
iunlockput(ip);
return -1;
}
ip->nlinks++;
iupdate(ip);
if ((dp = p2ip(new, name)) == 0){
printl("sys_link: failed, parent directory not found\n");
goto bad;
}
ilock(dp);
/* don't worry about dev number */
if (dir_link(dp, name, ip) < 0){
printl("sys_link: failed, dir_link\n");
iunlockput(dp);
goto bad;
}
iunlockput(dp);
iunlockput(ip);
return 0;
bad:
printl("sys_link: bad\n");
ip->nlinks--;
iupdate(ip);
iunlockput(ip);
return -1;
}
/* int _unlink(char *path) */
int sys_unlink(){
uint32_t off;
char name[NAME_LEN], *path;
struct inode *ip, *dip;
struct dir_entry de;
if (argstr(0, &path) < 0){
return -1;
}
printl("sys_unlink: unlink [%s]\n", path);
if ((dip = p2ip(path, name)) == 0){
return -1;
}
ilock(dip);
print_i(dip);
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0){
goto bad;
}
if ((ip = dir_lookup(dip, name, &off)) == 0){
goto bad;
}
ilock(ip);
print_i(ip);
assert(ip->nlinks >= 1, "sys_unlink: nlinks < 1");
/* we do not link any directory, so directory's nlinks always be 2 (self and ".") */
if (S_ISDIR(ip->mode) && !dir_isempty(ip)){
goto bad1;
}
memset(&de, 0, sizeof(de));
if (iwrite(dip, (char *)&de, off, sizeof(de)) != sizeof(de)){
panic("sys_unlink: wirte fault");
}
/* empty directory */
if (S_ISDIR(ip->mode)){
dip->nlinks--; // for ip/..
iupdate(dip);
}
iunlockput(dip);
ip->nlinks--;
iupdate(ip);
iunlockput(ip);
return 0;
bad1:
iunlockput(ip);
bad:
iunlockput(dip);
return -1;
}
static char illch[] = {' ', '\t', '\n', '\b', '/', '?', '*', '&', '>', '<', '|', 0};
static int islegal(char *name){
int i, j;
for (i = 0; i < strlen(name); i++){
for (j = 0; j < strlen(illch); j++){
if (name[i] == illch[j]) return 0;
}
}
return 1;
}
static struct inode *create(char *path, uint16_t mode, int di){
uint32_t off;
struct inode *ip, *dip;
char name[NAME_LEN];
if ((dip = p2ip(path, name)) == 0){
return 0;
}
if (!islegal(name)){ // illegal filename
iput(dip);
return 0;
}
ilock(dip);
if ((ip = dir_lookup(dip, name, &off)) != 0){
iunlockput(dip);
ilock(ip);
if (S_ISREG(mode) && S_ISDIR(ip->mode)){
return ip;
}
iunlockput(ip);
return 0;
}
if ((ip = ialloc(mode)) == 0){
panic("create: ialloc failed");
}
ilock(ip);
ip->mode = mode;
ip->nlinks = 1;
iupdate(ip);
if (S_ISDIR(mode)){
dip->nlinks++; // for ".."
iupdate(dip);
ip->nlinks++; // for "."
iupdate(ip);
if (dir_link(ip, ".", ip) < 0 || dir_link(ip, "..", dip) < 0){
panic("create: dir_link: make . & ..");
}
}
if (S_ISCHR(mode)){
/* device index */
ip->zone[0] = di;
iupdate(ip);
}
if (dir_link(dip, name, ip) < 0){
panic("create: dir_link: create name");
}
iunlockput(dip);
return ip;
}
int sys_open(){
char *path;
int fd, omode;
struct file *f;
struct inode *ip;
if (argstr(0, &path) < 0 || argint(1, &omode) < 0){
return -1;
}
printl("sys_open: path: [%s], mode: %d\n", path, omode);
if (omode & O_CREATE){
ip = create(path, S_IFREG, 0);
if (ip == 0){
return -1;
}
} else {
if ((ip = p2i(path)) == 0){
printl("sys_open: file not exist\n");
return -1;
}
ilock(ip);
if (S_ISDIR(ip->mode) && omode != O_RONLY){
iunlockput(ip);
return -1;
}
}
if ((f = falloc()) == 0 || (fd = fdalloc(f)) < 0){
if (f){
fclose(f);
}
iunlockput(ip);
return -1;
}
iunlock(ip);
f->type = F_INODE;
f->ip = ip;
f->off = 0;
f->readable = !(omode & O_WONLY); // 只当 WirteOnly 时 不可读
f->writeable = !(omode & O_RONLY);
return fd;
}
int sys_mkdir(){
char *path;
struct inode *ip;
if (argstr(0, &path) < 0){
return -1;
}
printl("sys_mkdir: path: [%s]\n", path);
if ((ip = create(path, S_IFDIR, 0)) == 0){
return -1;
}
iunlockput(ip);
return 0;
}
/* make up a device file */
int sys_mknod(){
struct inode *ip;
char *path;
int len, di;
if ((len = argstr(0, &path)) < 0 || argint(1, &di) < 0){
return -1;
}
printl("sys_mknod: path: [%s], device index: %d\n", path, di);
if ((ip = create(path, S_IFCHR, di)) == 0){
return -1;
}
iunlockput(ip);
return 0;
}
int sys_chdir(){
char *path;
struct inode *ip;
if (argstr(0, &path) < 0){
return -1;
}
printl("sys_chdir: path: [%s]\n", path);
if ((ip = p2i(path)) == 0){
return -1;
}
ilock(ip);
if (!S_ISDIR(ip->mode)){
printl("sys_chdir: no directory\n");
iunlock(ip);
return -1;
}
iunlock(ip);
iput(proc->cwd);
proc->cwd = ip;
return 0;
}
int sys_pipe(){
int *fd;
struct file *rf, *wf;
int fd0 = -1, fd1 = -1;
if (argptr(0, (void *)&fd, 2*sizeof(*fd)) < 0){
return -1;
}
printl("sys_pipe: ptr: 0x%x\n", fd);
if (pipe_alloc(&rf, &wf) < 0){
printl("sys_pipe: fail to alloc pipe\n");
return -1;
}
if ((fd0 = fdalloc(rf)) < 0 || (fd1 = fdalloc(wf)) < 0){
if (fd >= 0){
proc->ofile[fd0] = 0;
}
/* fd1 must be -1 */
fclose(rf);
fclose(wf);
printl("sys_pipe: fail to alloc fd\n");
return -1;
}
fd[0] = fd0;
fd[1] = fd1;
return 0;
}