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
|
implement Styxconv;
include "sys.m";
sys: Sys;
include "osys.m";
include "nsys.m";
include "draw.m";
include "styx.m";
nstyx: Styx;
Tmsg, Rmsg: import nstyx;
include "ostyx.m";
ostyx: OStyx;
OTmsg, ORmsg: import ostyx;
include "styxconv.m";
# todo: map fids > ffff into 16 bits
DEBUG: con 1;
Fid: adt
{
fid: int;
isdir: int;
n: int; # size of last new client dirread request.
soff: int; # dir offset on old server.
coff: int; # dir offset on new client.
next: cyclic ref Fid;
};
Req: adt {
tag: int;
oldtag: int; # if it's a flush.
rp: ref Reqproc;
next: cyclic ref Req;
flushes: list of ref Rmsg.Flush; # flushes awaiting req finish.
};
Reqproc: adt {
newtmsg: chan of ref Tmsg; # convproc -> reqproc, once per req.
newrmsg: chan of ref Rmsg; # reqproc -> convproc, once per req
oldtmsg: chan of ref OTmsg; # reqproc -> convproc
oldrmsg: chan of ref ORmsg; # convproc -> reqproc
flushable: int;
new: fn(): ref Reqproc;
rpc: fn(rp: self ref Reqproc, otm: ref OTmsg): ref ORmsg;
};
tags: ref Req;
avail: chan of ref Reqproc;
fids: ref Fid;
nprocs := 0;
init()
{
sys = load Sys Sys->PATH;
if(sys == nil)
nomod("Sys", Sys->PATH);
nstyx = load Styx Styx->PATH;
if(nstyx == nil)
nomod("Styx", Styx->PATH);
ostyx = load OStyx OStyx->PATH;
if(ostyx == nil)
nomod("OStyx", OStyx->PATH);
ostyx->init();
nstyx->init();
avail = chan of ref Reqproc;
}
styxconv(newclient: ref Sys->FD, oldsrv: ref Sys->FD)
{
newtmsg := chan of ref Tmsg;
oldrmsg := chan of ref ORmsg;
killpids := chan[2] of int;
spawn readnewtmsgs(killpids, newclient, newtmsg);
spawn readoldrmsgs(killpids, oldsrv, oldrmsg);
converting:
for(;;)alt{
ntm := <-newtmsg =>
if(DEBUG)
sys->fprint(sys->fildes(2), "-> %s\n", ntm.text());
if(ntm == nil)
break converting;
ns2os(ntm, newclient, oldsrv);
orm := <-oldrmsg =>
if(DEBUG)
sys->fprint(sys->fildes(2), " <- %s\n", ostyx->rmsg2s(orm));
if(orm == nil)
break converting;
t := looktag(orm.tag);
if(t == nil){
warning("reply by old-server to non-existent tag");
break;
}
pick rm := orm {
Flush =>
ot := looktag(t.oldtag);
# if it's an Rflush of a request-in-progress,
# we send it to the reqproc, which
# can then clean up as it likes.
if(ot != nil){
if(ot.rp != nil){
if(ot.rp.flushable){
ot.rp.oldrmsg <-= rm;
# reqproc is bound to finish after a flush
reqreply(ot, newclient, oldsrv);
}else {
# hold flush reply for later
ot.flushes = ref Rmsg.Flush(rm.tag) :: ot.flushes;
}
break;
}
deletetag(t.oldtag);
}
NRsend(newclient, ref Rmsg.Flush(rm.tag));
deletetag(rm.tag);
* =>
if(t.rp != nil){
t.rp.oldrmsg <-= orm;
reqreply(t, newclient, oldsrv);
}else{
os2ns(orm, newclient);
deletetag(orm.tag);
}
}
}
# kill off active reqprocs
for(; tags != nil; tags = tags.next){
if(tags.rp != nil){
tags.rp.oldrmsg <-= nil;
nprocs--;
}
}
# kill off idle reqprocs
while(nprocs > 0){
rp := <-avail;
rp.newtmsg <-= nil;
nprocs--;
}
# kill off message readers
kill(<-killpids);
kill(<-killpids);
}
# process one response from the request proc.
# request proc can respond by sending a new tmsg to the old server
# or by sending an rmsg to the new client, in which case
# it implicitly signals that it has finished processing the request.
# the actual reply might be an Rflush, signifying that
# the request has been aborted.
reqreply(t: ref Req, newclient: ref Sys->FD, oldsrv: ref Sys->FD)
{
rp := t.rp;
alt{
nrm := <-rp.newrmsg =>
# request is done when process sends rmsg
pick rm := nrm {
Flush =>
deletetag(t.tag);
}
deletetag(nrm.tag);
NRsend(newclient, nrm);
for(; t.flushes != nil; t.flushes = tl t.flushes)
NRsend(newclient, hd t.flushes);
otm := <-rp.oldtmsg =>
OTsend(oldsrv, otm);
}
}
# T messages: forward on, reply immediately, or start processing.
ns2os(tm0: ref Tmsg, newclient, oldsrv: ref Sys->FD)
{
otm: ref OTmsg;
t := ref Req(tm0.tag, -1, nil, nil, nil);
pick tm := tm0{
Readerror =>
exit;
Version =>
(s, v) := nstyx->compatible(tm, nstyx->MAXRPC, nil);
NRsend(newclient, ref Rmsg.Version(tm.tag, s, v));
return;
Auth =>
NRsend(newclient, ref Rmsg.Error(tm.tag, "authorization not required"));
return;
Walk =>
storetag(t);
t.rp = Reqproc.new();
t.rp.newtmsg <-= tm;
reqreply(t, newclient, oldsrv);
return;
Attach =>
otm = ref OTmsg.Attach(tm.tag, tm.fid, tm.uname, tm.aname);
Flush =>
t.oldtag = tm.oldtag;
otm = ref OTmsg.Flush(tm.tag, tm.oldtag);
Open =>
otm = ref OTmsg.Open(tm.tag, tm.fid, tm.mode);
Create =>
otm = ref OTmsg.Create(tm.tag, tm.fid, tm.perm, tm.mode, tm.name);
Read =>
fp := findfid(tm.fid);
count := tm.count;
offset := tm.offset;
if(fp != nil && fp.isdir){
fp.n = count;
count = (count/OStyx->DIRLEN)*OStyx->DIRLEN;
if(int offset != fp.coff){
NRsend(newclient, ref Rmsg.Error(tm.tag, "unexpected offset in dirread"));
return;
}
offset = big fp.soff;
}
otm = ref OTmsg.Read(tm.tag, tm.fid, count, offset);
Write =>
otm = ref OTmsg.Write(tm.tag, tm.fid, tm.offset, tm.data);
Clunk =>
otm = ref OTmsg.Clunk(tm.tag, tm.fid);
Remove =>
otm = ref OTmsg.Remove(tm.tag, tm.fid);
Stat =>
otm = ref OTmsg.Stat(tm.tag, tm.fid);
Wstat =>
otm = ref OTmsg.Wstat(tm.tag, tm.fid, nd2od(tm.stat));
* =>
fatal("bad T message");
}
storetag(t);
OTsend(oldsrv, otm);
}
# R messages: old to new
os2ns(orm0: ref ORmsg, newclient: ref Sys->FD)
{
rm: ref Rmsg;
rm = nil;
pick orm := orm0 {
Error =>
rm = ref Rmsg.Error(orm.tag, orm.err);
Flush =>
rm = ref Rmsg.Flush(orm.tag);
Clone =>
rm = ref Rmsg.Walk(orm.tag, nil);
Walk =>
fatal("walk rmsgs should be dealt with be walkreqproc");
Open =>
setfid(orm.fid, orm.qid);
rm = ref Rmsg.Open(orm.tag, oq2nq(orm.qid), 0);
Create =>
setfid(orm.fid, orm.qid);
rm = ref Rmsg.Create(orm.tag, oq2nq(orm.qid), 0);
Read =>
fp := findfid(orm.fid);
data := orm.data;
if(fp != nil && fp.isdir)
data = ods2nds(data, fp.n);
fp.coff += len data;
fp.soff += len orm.data;
rm = ref Rmsg.Read(orm.tag, data);
Write =>
rm = ref Rmsg.Write(orm.tag, orm.count);
Clunk =>
rm = ref Rmsg.Clunk(orm.tag);
deletefid(orm.fid);
Remove =>
rm = ref Rmsg.Remove(orm.tag);
deletefid(orm.fid);
Stat =>
rm = ref Rmsg.Stat(orm.tag, od2nd(orm.stat));
Wstat =>
rm = ref Rmsg.Wstat(orm.tag);
Attach =>
newfid(orm.fid, orm.qid.path & OSys->CHDIR);
rm = ref Rmsg.Attach(orm.tag, oq2nq(orm.qid));
* =>
fatal("bad R message");
}
NRsend(newclient, rm);
}
Reqproc.rpc(rp: self ref Reqproc, otm: ref OTmsg): ref ORmsg
{
rp.oldtmsg <-= otm;
m := <-rp.oldrmsg;
if(m == nil)
exit;
return m;
}
Reqproc.new(): ref Reqproc
{
alt{
rp := <-avail =>
return rp;
* =>
rp := ref Reqproc(
chan of ref Tmsg,
chan of ref Rmsg,
chan of ref OTmsg,
chan of ref ORmsg,
1);
spawn reqproc(rp);
nprocs++;
return rp;
}
}
reqproc(rp: ref Reqproc)
{
for(;;){
tm := <-rp.newtmsg;
if(tm == nil)
return;
rm: ref Rmsg;
pick m := tm {
Walk =>
rm = walkreq(m, rp);
* =>
fatal("non-walk req passed to reqproc");
}
rp.flushable = 1;
rp.newrmsg <-= rm;
avail <-= rp;
}
}
# note that although this is in a separate process,
# whenever it's not in Reqproc.rpc, the styxconv
# process is blocked, so although state is shared,
# there are no race conditions.
walkreq(tm: ref Tmsg.Walk, rp: ref Reqproc): ref Rmsg
{
cloned := 0;
n := len tm.names;
if(tm.newfid != tm.fid){
cloned = 1;
pick rm := rp.rpc(ref OTmsg.Clone(tm.tag, tm.fid, tm.newfid)) {
Clone =>
;
Error =>
return ref Rmsg.Error(tm.tag, rm.err);
Flush =>
return ref Rmsg.Flush(rm.tag);
* =>
fatal("unexpected reply to OTmsg.Clone");
}
cloned = 1;
}
qids := array[n] of NSys->Qid;
finalqid: OSys->Qid;
# make sure we don't get flushed in an unwindable state.
rp.flushable = n == 1 || cloned;
for(i := 0; i < n; i++){
pick rm := rp.rpc(ref OTmsg.Walk(tm.tag, tm.newfid, tm.names[i])) {
Walk =>
qids[i] = oq2nq(rm.qid);
finalqid = rm.qid;
Flush =>
if(cloned){
rp.flushable = 0;
rp.rpc(ref OTmsg.Clunk(tm.tag, tm.newfid));
}
return ref Rmsg.Flush(rm.tag);
Error =>
if(cloned){
rp.flushable = 0;
rp.rpc(ref OTmsg.Clunk(tm.tag, tm.newfid));
}
if(i == 0)
return ref Rmsg.Error(tm.tag, rm.err);
return ref Rmsg.Walk(tm.tag, qids[0:i]);
}
}
if(cloned)
clonefid(tm.fid, tm.newfid);
if(n > 0)
setfid(tm.newfid, finalqid);
return ref Rmsg.Walk(tm.tag, qids);
}
storetag(t: ref Req)
{
t.next = tags;
tags = t;
}
looktag(tag: int): ref Req
{
for(t := tags; t != nil; t = t.next)
if(t.tag == tag)
return t;
return nil;
}
deletetag(tag: int)
{
prev: ref Req;
t := tags;
while(t != nil){
if(t.tag == tag){
next := t.next;
t.next = nil;
if(prev != nil)
prev.next = next;
else
tags = next;
t = next;
}else{
prev = t;
t = t.next;
}
}
}
newfid(fid: int, isdir: int): ref Fid
{
f := ref Fid;
f.fid = fid;
f.isdir = isdir;
f.n = f.soff = f.coff = 0;
f.next = fids;
fids = f;
return f;
}
clonefid(ofid: int, fid: int): ref Fid
{
if((f := findfid(ofid)) != nil){
nf := newfid(fid, f.isdir);
return nf;
}
warning("clone of non-existent fid");
return newfid(fid, 0);
}
deletefid(fid: int)
{
lf: ref Fid;
for(f := fids; f != nil; f = f.next){
if(f.fid == fid){
if(lf == nil)
fids = f.next;
else
lf.next = f.next;
return;
}
lf = f;
}
}
findfid(fid: int): ref Fid
{
for(f := fids; f != nil; f = f.next)
if(f.fid == fid)
return f;
return nil;
}
setfid(fid: int, qid: OSys->Qid)
{
f := findfid(fid);
if(f == nil){
warning(sys->sprint("cannot find fid %d", fid));
}else{
f.isdir = qid.path & OSys->CHDIR;
}
}
om2nm(om: int): int
{
# DMDIR == CHDIR
return om;
}
nm2om(m: int): int
{
# DMDIR == CHDIR
return m&~(NSys->DMAPPEND|NSys->DMEXCL|NSys->DMAUTH);
}
oq2nq(oq: OSys->Qid): NSys->Qid
{
q: NSys->Qid;
isdir := oq.path&OSys->CHDIR;
q.path = big (oq.path&~OSys->CHDIR);
q.vers = oq.vers;
q.qtype = 0;
if(isdir)
q.qtype |= NSys->QTDIR;
return q;
}
nq2oq(q: NSys->Qid): OSys->Qid
{
oq: OSys->Qid;
isdir := q.qtype&NSys->QTDIR;
oq.path = int q.path;
oq.vers = q.vers;
if(isdir)
oq.path |= OSys->CHDIR;
return oq;
}
od2nd(od: OSys->Dir): NSys->Dir
{
d: NSys->Dir;
d.name = od.name;
d.uid = od.uid;
d.gid = od.gid;
d.muid = od.uid;
d.qid = oq2nq(od.qid);
d.mode = om2nm(od.mode);
d.atime = od.atime;
d.mtime = od.mtime;
d.length = big od.length;
d.dtype = od.dtype;
d.dev = od.dev;
return d;
}
nd2od(d: NSys->Dir): OSys->Dir
{
od: OSys->Dir;
od.name = d.name;
od.uid = d.uid;
od.gid = d.gid;
od.qid = nq2oq(d.qid);
od.mode = nm2om(d.mode);
od.atime = d.atime;
od.mtime = d.mtime;
od.length = int d.length;
od.dtype = d.dtype;
od.dev = d.dev;
return od;
}
ods2nds(ob: array of byte, max: int): array of byte
{
od: OSys->Dir;
m := len ob;
if(m % OStyx->DIRLEN != 0)
fatal(sys->sprint("bad dir len %d", m));
m /= OStyx->DIRLEN;
n := 0;
p := ob;
for(i := 0; i < m; i++){
(p, od) = ostyx->convM2D(p);
d := od2nd(od);
nn := nstyx->packdirsize(d);
if(n+nn > max) # might just happen with long file names
break;
n += nn;
}
m = i;
b := array[n] of byte;
n = 0;
p = ob;
for(i = 0; i < m; i++){
(p, od) = ostyx->convM2D(p);
d := od2nd(od);
q := nstyx->packdir(d);
nn := len q;
b[n: ] = q[0: nn];
n += nn;
}
return b;
}
OTsend(fd: ref Sys->FD, otm: ref OTmsg): int
{
if(DEBUG)
sys->fprint(sys->fildes(2), " -> %s\n", ostyx->tmsg2s(otm));
s := array[OStyx->MAXRPC] of byte;
n := ostyx->tmsg2d(otm, s);
if(n < 0)
return -1;
return sys->write(fd, s, n);
}
NRsend(fd: ref Sys->FD, rm: ref Rmsg): int
{
if(DEBUG)
sys->fprint(sys->fildes(2), "<- %s\n", rm.text());
s := rm.pack();
if(s == nil)
return -1;
return sys->write(fd, s, len s);
}
readnewtmsgs(pidc: chan of int, newclient: ref Sys->FD, newtmsg: chan of ref Tmsg)
{
pidc <-= sys->pctl(0, nil);
for(;;){
newtmsg <-= Tmsg.read(newclient, nstyx->MAXRPC);
}
}
readoldrmsgs(pidc: chan of int, oldsrv: ref Sys->FD, oldrmsg: chan of ref ORmsg)
{
pidc <-= sys->pctl(0, nil);
for(;;){
oldrmsg <-= ORmsg.read(oldsrv);
}
}
warning(err: string)
{
sys->fprint(sys->fildes(2), "warning: %s\n", err);
}
fatal(err: string)
{
sys->fprint(sys->fildes(2), "%s\n", err);
exit;
}
nomod(mod: string, path: string)
{
fatal(sys->sprint("can't load %s(%s): %r", mod, path));
}
kill(pid: int)
{
sys->fprint(sys->open("#p/"+string pid+"/ctl", Sys->OWRITE), "kill");
}
|