This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
/
fsys.c
709 lines (639 loc) · 13.4 KB
/
fsys.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
* This file is part of the UCB release of Plan 9. It is subject to the license
* terms in the LICENSE file found in the top-level directory of this
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
* part of the UCB release of Plan 9, including this file, may be copied,
* modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
#include <mouse.h>
#include <keyboard.h>
#include <frame.h>
#include <fcall.h>
#include "dat.h"
#include "fns.h"
char Eperm[] = "permission denied";
char Eexist[] = "file does not exist";
char Enotdir[] = "not a directory";
char Ebadfcall[] = "bad fcall type";
char Eoffset[] = "illegal offset";
int messagesize = 8192+IOHDRSZ; /* good start */
enum{
DEBUG = 0
};
Dirtab dirtab[]=
{
{ ".", QTDIR, Qdir, 0500|DMDIR },
{ "cons", QTFILE, Qcons, 0600 },
{ "cursor", QTFILE, Qcursor, 0600 },
{ "consctl", QTFILE, Qconsctl, 0200 },
{ "winid", QTFILE, Qwinid, 0400 },
{ "winname", QTFILE, Qwinname, 0400 },
{ "kbdin", QTFILE, Qkbdin, 0200 },
{ "label", QTFILE, Qlabel, 0600 },
{ "mouse", QTFILE, Qmouse, 0600 },
{ "screen", QTFILE, Qscreen, 0400 },
{ "snarf", QTFILE, Qsnarf, 0600 },
{ "text", QTFILE, Qtext, 0400 },
{ "wdir", QTFILE, Qwdir, 0600 },
{ "wctl", QTFILE, Qwctl, 0600 },
{ "window", QTFILE, Qwindow, 0400 },
{ "wsys", QTDIR, Qwsys, 0500|DMDIR },
{ nil, }
};
static uint getclock(void);
static void filsysproc(void*);
static Fid* newfid(Filsys*, int);
static int dostat(Filsys*, int, Dirtab*, uint8_t*, int, uint);
int clockfd;
int firstmessage = 1;
char srvpipe[64];
char srvwctl[64];
static Xfid* filsysflush(Filsys*, Xfid*, Fid*);
static Xfid* filsysversion(Filsys*, Xfid*, Fid*);
static Xfid* filsysauth(Filsys*, Xfid*, Fid*);
static Xfid* filsysnop(Filsys*, Xfid*, Fid*);
static Xfid* filsysattach(Filsys*, Xfid*, Fid*);
static Xfid* filsyswalk(Filsys*, Xfid*, Fid*);
static Xfid* filsysopen(Filsys*, Xfid*, Fid*);
static Xfid* filsyscreate(Filsys*, Xfid*, Fid*);
static Xfid* filsysread(Filsys*, Xfid*, Fid*);
static Xfid* filsyswrite(Filsys*, Xfid*, Fid*);
static Xfid* filsysclunk(Filsys*, Xfid*, Fid*);
static Xfid* filsysremove(Filsys*, Xfid*, Fid*);
static Xfid* filsysstat(Filsys*, Xfid*, Fid*);
static Xfid* filsyswstat(Filsys*, Xfid*, Fid*);
Xfid* (*fcall[Tmax])(Filsys*, Xfid*, Fid*) =
{
[Tflush] = filsysflush,
[Tversion] = filsysversion,
[Tauth] = filsysauth,
[Tattach] = filsysattach,
[Twalk] = filsyswalk,
[Topen] = filsysopen,
[Tcreate] = filsyscreate,
[Tread] = filsysread,
[Twrite] = filsyswrite,
[Tclunk] = filsysclunk,
[Tremove]= filsysremove,
[Tstat] = filsysstat,
[Twstat] = filsyswstat,
};
void
post(char *name, char *envname, int srvfd)
{
int fd;
char buf[32];
fd = create(name, OWRITE|ORCLOSE|OCEXEC, 0600);
if(fd < 0)
error(name);
sprint(buf, "%d",srvfd);
if(write(fd, buf, strlen(buf)) != strlen(buf))
error("srv write");
putenv(envname, name);
}
/*
* Build pipe with OCEXEC set on second fd.
* Can't put it on both because we want to post one in /srv.
*/
int
cexecpipe(int *p0, int *p1)
{
/* pipe the hard way to get close on exec */
if(bind("#|", "/mnt/temp", MREPL) < 0)
return -1;
*p0 = open("/mnt/temp/data", ORDWR);
*p1 = open("/mnt/temp/data1", ORDWR|OCEXEC);
unmount(nil, "/mnt/temp");
if(*p0<0 || *p1<0)
return -1;
return 0;
}
Filsys*
filsysinit(Channel *cxfidalloc)
{
int n, fd, pid, p0;
Filsys *fs;
Channel *c;
char buf[128];
fs = emalloc(sizeof(Filsys));
if(cexecpipe(&fs->cfd, &fs->sfd) < 0)
goto Rescue;
fmtinstall('F', fcallfmt);
clockfd = open("/dev/time", OREAD|OCEXEC);
fd = open("/dev/user", OREAD);
strcpy(buf, "Jean-Paul_Belmondo");
if(fd >= 0){
n = read(fd, buf, sizeof buf-1);
if(n > 0)
buf[n] = 0;
close(fd);
}
fs->user = estrdup(buf);
fs->cxfidalloc = cxfidalloc;
pid = getpid();
/*
* Create and post wctl pipe
*/
if(cexecpipe(&p0, &wctlfd) < 0)
goto Rescue;
sprint(srvwctl, "/srv/riowctl.%s.%d", fs->user, pid);
post(srvwctl, "wctl", p0);
close(p0);
/*
* Start server processes
*/
c = chancreate(sizeof(char*), 0);
if(c == nil)
error("wctl channel");
proccreate(wctlproc, c, 4096);
threadcreate(wctlthread, c, 4096);
proccreate(filsysproc, fs, 10000);
/*
* Post srv pipe
*/
sprint(srvpipe, "/srv/rio.%s.%d", fs->user, pid);
post(srvpipe, "wsys", fs->cfd);
return fs;
Rescue:
free(fs);
return nil;
}
static
void
filsysproc(void *arg)
{
int n;
Xfid *x;
Fid *f;
Fcall t;
uint8_t *buf;
Filsys *fs;
threadsetname("FILSYSPROC");
fs = arg;
fs->pid = getpid();
x = nil;
for(;;){
buf = emalloc(messagesize+UTFmax); /* UTFmax for appending partial rune in xfidwrite */
n = read9pmsg(fs->sfd, buf, messagesize);
if(n <= 0){
yield(); /* if threadexitsall'ing, will not return */
fprint(2, "rio: %d: read9pmsg: %d %r\n", getpid(), n);
errorshouldabort = 0;
error("eof or i/o error on server channel");
}
if(x == nil){
send(fs->cxfidalloc, nil);
recv(fs->cxfidalloc, &x);
x->fs = fs;
}
x->buf = buf;
if(convM2S(buf, n, x) != n)
error("convert error in convM2S");
if(DEBUG)
fprint(2, "rio:<-%F\n", &x->Fcall);
if(fcall[x->type] == nil)
x = filsysrespond(fs, x, &t, Ebadfcall);
else{
if(x->type==Tversion || x->type==Tauth)
f = nil;
else
f = newfid(fs, x->fid);
x->f = f;
x = (*fcall[x->type])(fs, x, f);
}
firstmessage = 0;
}
}
/*
* Called only from a different FD group
*/
int
filsysmount(Filsys *fs, int id)
{
char buf[32];
close(fs->sfd); /* close server end so mount won't hang if exiting */
sprint(buf, "%d", id);
if(mount(fs->cfd, -1, "/mnt/wsys", MREPL, buf, 'M') < 0){
fprint(2, "mount failed: %r\n");
return -1;
}
if(bind("/mnt/wsys", "/dev", MBEFORE) < 0){
fprint(2, "bind failed: %r\n");
return -1;
}
return 0;
}
Xfid*
filsysrespond(Filsys *fs, Xfid *x, Fcall *t, char *err)
{
int n;
if(err){
t->type = Rerror;
t->ename = err;
}else
t->type = x->type+1;
t->fid = x->fid;
t->tag = x->tag;
if(x->buf == nil)
x->buf = malloc(messagesize);
n = convS2M(t, x->buf, messagesize);
if(n <= 0)
error("convert error in convS2M");
if(write(fs->sfd, x->buf, n) != n)
error("write error in respond");
if(DEBUG)
fprint(2, "rio:->%F\n", t);
free(x->buf);
x->buf = nil;
return x;
}
void
filsyscancel(Xfid *x)
{
if(x->buf){
free(x->buf);
x->buf = nil;
}
}
static
Xfid*
filsysversion(Filsys *fs, Xfid *x, Fid* f)
{
Fcall t;
if(!firstmessage)
return filsysrespond(x->fs, x, &t, "version request not first message");
if(x->msize < 256)
return filsysrespond(x->fs, x, &t, "version: message size too small");
messagesize = x->msize;
t.msize = messagesize;
if(strncmp(x->version, "9P2000", 6) != 0)
return filsysrespond(x->fs, x, &t, "unrecognized 9P version");
t.version = "9P2000";
return filsysrespond(fs, x, &t, nil);
}
static
Xfid*
filsysauth(Filsys *fs, Xfid *x, Fid* f)
{
Fcall t;
return filsysrespond(fs, x, &t, "rio: authentication not required");
}
static
Xfid*
filsysflush(Filsys* fs, Xfid *x, Fid* f)
{
sendp(x->c, xfidflush);
return nil;
}
static
Xfid*
filsysattach(Filsys * fs, Xfid *x, Fid *f)
{
Fcall t;
if(strcmp(x->uname, x->fs->user) != 0)
return filsysrespond(x->fs, x, &t, Eperm);
f->busy = TRUE;
f->open = FALSE;
f->qid.path = Qdir;
f->qid.type = QTDIR;
f->qid.vers = 0;
f->dir = dirtab;
f->nrpart = 0;
sendp(x->c, xfidattach);
return nil;
}
static
int
numeric(char *s)
{
for(; *s!='\0'; s++)
if(*s<'0' || '9'<*s)
return 0;
return 1;
}
static
Xfid*
filsyswalk(Filsys *fs, Xfid *x, Fid *f)
{
Fcall t;
Fid *nf;
int i, id;
uint8_t type;
uint32_t path;
Dirtab *d, *dir;
Window *w;
char *err;
Qid qid;
if(f->open)
return filsysrespond(fs, x, &t, "walk of open file");
nf = nil;
if(x->fid != x->newfid){
/* BUG: check exists */
nf = newfid(fs, x->newfid);
if(nf->busy)
return filsysrespond(fs, x, &t, "clone to busy fid");
nf->busy = TRUE;
nf->open = FALSE;
nf->dir = f->dir;
nf->qid = f->qid;
nf->w = f->w;
incref(f->w);
nf->nrpart = 0; /* not open, so must be zero */
f = nf; /* walk f */
}
t.nwqid = 0;
err = nil;
/* update f->qid, f->dir only if walk completes */
qid = f->qid;
dir = f->dir;
if(x->nwname > 0){
for(i=0; i<x->nwname; i++){
if((qid.type & QTDIR) == 0){
err = Enotdir;
break;
}
if(strcmp(x->wname[i], "..") == 0){
type = QTDIR;
path = Qdir;
dir = dirtab;
if(FILE(qid) == Qwsysdir)
path = Qwsys;
id = 0;
Accept:
if(i == MAXWELEM){
err = "name too long";
break;
}
qid.type = type;
qid.vers = 0;
qid.path = QID(id, path);
t.wqid[t.nwqid++] = qid;
continue;
}
if(qid.path == Qwsys){
/* is it a numeric name? */
if(!numeric(x->wname[i]))
break;
/* yes: it's a directory */
id = atoi(x->wname[i]);
qlock(&all);
w = wlookid(id);
if(w == nil){
qunlock(&all);
break;
}
path = Qwsysdir;
type = QTDIR;
qunlock(&all);
incref(w);
sendp(winclosechan, f->w);
f->w = w;
dir = dirtab;
goto Accept;
}
if(snarffd>=0 && strcmp(x->wname[i], "snarf")==0)
break; /* don't serve /dev/snarf if it's provided in the environment */
id = WIN(f->qid);
d = dirtab;
d++; /* skip '.' */
for(; d->name; d++)
if(strcmp(x->wname[i], d->name) == 0){
path = d->qid;
type = d->type;
dir = d;
goto Accept;
}
break; /* file not found */
}
if(i==0 && err==nil)
err = Eexist;
}
if(err!=nil || t.nwqid<x->nwname){
if(nf){
if(nf->w)
sendp(winclosechan, nf->w);
nf->open = FALSE;
nf->busy = FALSE;
}
}else if(t.nwqid == x->nwname){
f->dir = dir;
f->qid = qid;
}
return filsysrespond(fs, x, &t, err);
}
static
Xfid*
filsysopen(Filsys *fs, Xfid *x, Fid *f)
{
Fcall t;
int m;
/* can't truncate anything, so just disregard */
x->mode &= ~(OTRUNC|OCEXEC);
/* can't execute or remove anything */
if(x->mode==OEXEC || (x->mode&ORCLOSE))
goto Deny;
switch(x->mode){
default:
goto Deny;
case OREAD:
m = 0400;
break;
case OWRITE:
m = 0200;
break;
case ORDWR:
m = 0600;
break;
}
if(((f->dir->perm&~(DMDIR|DMAPPEND))&m) != m)
goto Deny;
sendp(x->c, xfidopen);
return nil;
Deny:
return filsysrespond(fs, x, &t, Eperm);
}
static
Xfid*
filsyscreate(Filsys *fs, Xfid *x, Fid*f)
{
Fcall t;
return filsysrespond(fs, x, &t, Eperm);
}
static
int
idcmp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
}
static
Xfid*
filsysread(Filsys *fs, Xfid *x, Fid *f)
{
Fcall t;
uint8_t *b;
int i, n, o, e, len, j, k, *ids;
Dirtab *d, dt;
uint clock;
char buf[16];
if((f->qid.type & QTDIR) == 0){
sendp(x->c, xfidread);
return nil;
}
o = x->offset;
e = x->offset+x->count;
clock = getclock();
b = malloc(messagesize-IOHDRSZ); /* avoid memset of emalloc */
if(b == nil)
return filsysrespond(fs, x, &t, "out of memory");
n = 0;
switch(FILE(f->qid)){
case Qdir:
case Qwsysdir:
d = dirtab;
d++; /* first entry is '.' */
for(i=0; d->name!=nil && i<e; i+=len){
len = dostat(fs, WIN(x->f->qid), d, b+n, x->count-n, clock);
if(len <= BIT16SZ)
break;
if(i >= o)
n += len;
d++;
}
break;
case Qwsys:
qlock(&all);
ids = emalloc(nwindow*sizeof(int));
for(j=0; j<nwindow; j++)
ids[j] = window[j]->id;
qunlock(&all);
qsort(ids, nwindow, sizeof ids[0], idcmp);
dt.name = buf;
for(i=0, j=0; j<nwindow && i<e; i+=len){
k = ids[j];
sprint(dt.name, "%d", k);
dt.qid = QID(k, Qdir);
dt.type = QTDIR;
dt.perm = DMDIR|0700;
len = dostat(fs, k, &dt, b+n, x->count-n, clock);
if(len == 0)
break;
if(i >= o)
n += len;
j++;
}
free(ids);
break;
}
t.data = (char*)b;
t.count = n;
filsysrespond(fs, x, &t, nil);
free(b);
return x;
}
static
Xfid*
filsyswrite(Filsys* fs, Xfid *x, Fid*f)
{
sendp(x->c, xfidwrite);
return nil;
}
static
Xfid*
filsysclunk(Filsys *fs, Xfid *x, Fid *f)
{
Fcall t;
if(f->open){
f->busy = FALSE;
f->open = FALSE;
sendp(x->c, xfidclose);
return nil;
}
if(f->w)
sendp(winclosechan, f->w);
f->busy = FALSE;
f->open = FALSE;
return filsysrespond(fs, x, &t, nil);
}
static
Xfid*
filsysremove(Filsys *fs, Xfid *x, Fid*f)
{
Fcall t;
return filsysrespond(fs, x, &t, Eperm);
}
static
Xfid*
filsysstat(Filsys *fs, Xfid *x, Fid *f)
{
Fcall t;
t.stat = emalloc(messagesize-IOHDRSZ);
t.nstat = dostat(fs, WIN(x->f->qid), f->dir, t.stat, messagesize-IOHDRSZ, getclock());
x = filsysrespond(fs, x, &t, nil);
free(t.stat);
return x;
}
static
Xfid*
filsyswstat(Filsys *fs, Xfid *x, Fid*f)
{
Fcall t;
return filsysrespond(fs, x, &t, Eperm);
}
static
Fid*
newfid(Filsys *fs, int fid)
{
Fid *f, *ff, **fh;
ff = nil;
fh = &fs->fids[fid&(Nhash-1)];
for(f=*fh; f; f=f->next)
if(f->fid == fid)
return f;
else if(ff==nil && f->busy==FALSE)
ff = f;
if(ff){
ff->fid = fid;
return ff;
}
f = emalloc(sizeof *f);
f->fid = fid;
f->next = *fh;
*fh = f;
return f;
}
static
uint
getclock(void)
{
char buf[32];
seek(clockfd, 0, 0);
read(clockfd, buf, sizeof buf);
return atoi(buf);
}
static
int
dostat(Filsys *fs, int id, Dirtab *dir, uint8_t *buf, int nbuf, uint clock)
{
Dir d;
d.qid.path = QID(id, dir->qid);
if(dir->qid == Qsnarf)
d.qid.vers = snarfversion;
else
d.qid.vers = 0;
d.qid.type = dir->type;
d.mode = dir->perm;
d.length = 0; /* would be nice to do better */
d.name = dir->name;
d.uid = fs->user;
d.gid = fs->user;
d.muid = fs->user;
d.atime = clock;
d.mtime = clock;
return convD2M(&d, buf, nbuf);
}