~kvik/mq

ref: 668a30f10fce153d238bd784f632338d735ad6e3 mq/src/mq.c -rw-r--r-- 11.3 KiB
668a30f1kvik mq: various code quality improvements 1 year, 4 months ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

#include "list.h"
#include "util.h"

typedef struct Group Group;
typedef struct Stream Stream;
typedef struct Client Client;
typedef struct Read Read;
typedef struct Write Write;

struct Read {
	Listelem;

	Req *req;
};

struct Write {
	Listelem;

	/* Twrite.ifcall */
	vlong offset;
	uint count;
	uchar *data;
};

struct Stream {
	Listelem;

	Group *group;
	Write *wqueue;
	Read *rqueue;
};

struct Group {
	Stream *streams;
	Stream *order;

	enum {Message, Coalesce} mode;
	enum {Replayoff, Replaylast, Replayall} replay;
};

struct Client {
	Write *cursor;
	vlong offset;
	int blocked;
};

enum {
	/* Dirty trick to help clients tell our
	 * root from most others, see pin(1). */
	Qroot = 0xA,
	Qgroup = 0x1,
		Qstream,
		Qorder,
		Qctl,
};
void
filesettype(File *f, ushort type)
{
	/*
	 * Use four most-significant bits to store the type.
	 * This depends on the 9pfile(2) library generating
	 * simple incremental qid paths.
	 */
	f->qid.path &= ~((uvlong)0xF<<60);
	f->qid.path |= (uvlong)(type&0xF)<<60;
}

ushort
filetype(File *f)
{
	return (f->qid.path>>60)&0xF;
}

File*
groupcreate(File *dir, char *name, char *uid, ulong perm)
{
	Stream *streamalloc(Group*);
	void *streamclose(Stream*);
	File *d, *ctl, *order;
	Group *group;

	group = emalloc(sizeof(Group));
	group->streams = (Stream*)listinit(emalloc(sizeof(Stream)));
	group->order = streamalloc(group);
	group->mode = Message;
	group->replay = Replayoff;

	ctl = order = nil;
	if(strcmp(name, "/") == 0){
		d = dir;
		d->aux = group;
	}
	else
		d = createfile(dir, name, uid, perm, group);
	if(d == nil)
		goto err;
	filesettype(d, Qgroup);

	if((ctl = createfile(d, "ctl", nil, 0664, group)) == nil)
		goto err;
	filesettype(ctl, Qctl);
	closefile(ctl);

	if((order = createfile(d, "order", nil, 0444, group->order)) == nil)
		goto err;
	filesettype(order, Qorder);
	closefile(order);

	return d;
err:
	streamclose(group->order);
	if(d) closefile(d);
	if(ctl) closefile(ctl);
	if(order) closefile(order);
	return nil;
}

void
groupclose(Group *g)
{
	free(g);
}

Stream*
streamalloc(Group *g)
{
	Stream *s;
	
	s = emalloc(sizeof(Stream));
	s->group = g;
	s->wqueue = (Write*)listinit(emalloc(sizeof(Write)));
	s->rqueue = (Read*)listinit(emalloc(sizeof(Read)));
	return s;
}

void
streamclose(Stream *s)
{
	Read *r;
	Write *w;

	listeach(Read*, s->rqueue, r){
		listunlink(r);
		free(r);
	}
	free(s->wqueue);
	listeach(Write*, s->wqueue, w){
		listunlink(w);
		free(w);
	}
	free(s->wqueue);
	listunlink(s);
	free(s);
}

File*
streamcreate(File *dir, char *name, char *uid, ulong perm)
{
	File *f;
	Group *group;
	Stream *s;

	group = dir->aux;
	s = streamalloc(group);
	if((f = createfile(dir, name, uid, perm, s)) == nil){
		streamclose(s);
		return nil;
	}
	filesettype(f, Qstream);
	listlink(group->streams, s);
	return f;
}

void
streamopen(Stream *s, Req *req)
{
	Client *c;
	
	c = req->fid->aux = emalloc(sizeof(Client));
	switch(s->group->replay){
	case Replayoff:
		c->offset = 0;
		c->blocked = 1;
		c->cursor = nil;
		break;

	case Replayall:
		c->offset = 0;
		if(listisempty(s->wqueue)){
			c->blocked = 1;
			c->cursor = nil;
		}else{
			c->blocked = 0;
			c->cursor = s->wqueue->front;
		}
		break;

	case Replaylast:
		c->offset = 0;
		if(listisempty(s->wqueue)){
			c->blocked = 1;
			c->cursor = nil;
		}else{
			c->blocked = 0;
			c->cursor = s->wqueue->back;
		}
		break;
	}
}

void
streamrespond(Req *req, int mode)
{
	Client *c = req->fid->aux;
	Stream *s = req->fid->file->aux;
	Write *w;
	/* request size, response buffer offset */
	vlong rn, ro;
	/* chunk size and offset, total read */
	vlong n, o, t;

	t = 0;
	rn = req->ifcall.count;
	ro = 0;
	w = c->cursor;
	o = c->offset;
	listrange(Write*, s->wqueue, w){
		if(mode == Message && w != c->cursor)
			break;
		for(; n = w->count - o, n > 0; o += n, ro += n, t += n){
			if(t == rn)
				goto done;
			if(n > rn - ro)
				n = rn - ro;
			memmove(req->ofcall.data+ro, w->data+o, n);
		}
		o = 0;
	}
done:
	req->ofcall.count = t;
	respond(req, nil);
	
	/* Determine the Client state */
	if(w == s->wqueue){
		c->offset = 0;
		c->blocked = 1;
		c->cursor = nil;
		return;
	}
	c->offset = o;
	c->blocked = 0;
	c->cursor = w;
}

void
streamread(Req *req)
{
	Client *c = req->fid->aux;
	Stream *s = req->fid->file->aux;
	Read *r;
	
	if(c->blocked){
		r = emalloc(sizeof(Read));
		r->req = req;
		listlink(s->rqueue, r);
		return;
	}
	streamrespond(req, s->group->mode);
}

Write*
writealloc(long n)
{
	Write *w;
	
	w = emalloc(sizeof(Write)+n);
	w->data = (uchar*)&w[1];
	return w;
}

void
streamwrite(Req *req)
{
	File *f = req->fid->file;
	Stream *s = req->fid->file->aux;
	Group *group = s->group;
	Write *w, *wq, *o, *oq;
	Read *r;
	Client *c;
	long n;
	
	wq = s->wqueue;
	oq = group->order->wqueue;

	/* Commit to queue */
	w = writealloc(req->ifcall.count);
	w->count = req->ifcall.count;
	w->offset = req->ifcall.offset;
	memmove(w->data, req->ifcall.data, w->count);
	listlink(wq->back, w);

	/* Commit to group order queue */
	n = strlen(f->name)+1;
	o = writealloc(n);
	o->offset = 0;
	o->count = n;
	memmove(o->data, f->name, n);
	listlink(oq->back, o);
 
	/* Kick the blocked stream readers */
	listeach(Read*, s->rqueue, r){
		c = r->req->fid->aux;
		
		c->cursor = w;
		c->offset = 0;
		c->blocked = 0;
		streamrespond(r->req, group->mode);
		listunlink(r);
		free(r);
	}

	/* Kick the blocked order readers */
	listeach(Read*, group->order->rqueue, r){
		c = r->req->fid->aux;
		
		c->cursor = o;
		c->offset = 0;
		c->blocked = 0;
		streamrespond(r->req, Message);
		listunlink(r);
		free(r);
	}

	req->ofcall.count = req->ifcall.count;
	respond(req, nil);
}

void
ctlread(Req *req)
{
	Group *group = req->fid->file->aux;
	char buf[256];

	char *mode2str[] = {
		[Message] "message",
		[Coalesce] "coalesce",
	};
	char *replay2str[] = {
		[Replayoff] "off",
		[Replaylast] "last",
		[Replayall] "all",
	};
	snprint(buf, sizeof buf, "data %s\nreplay %s\n",
		mode2str[group->mode], replay2str[group->replay]);
	readstr(req, buf);
	respond(req, nil);
}

enum {
	Cmddata,
	Cmdreplay,
	Cmddebug, Cmddebug9p,
};
Cmdtab groupcmd[] = {
	/* data message|coalesce */
	{Cmddata, "data", 2},
	/* replay off|last|all */
	{Cmdreplay, "replay", 2},

	/* debug on|off */
	{Cmddebug, "debug", 2},
	/* debug9p on|off */
	{Cmddebug9p, "debug9p", 2},
};

void
ctlwrite(Req *req)
{
	Group *group = req->fid->file->aux;
	char *e = nil;
	Cmdbuf *cmd;
	Cmdtab *t;

	cmd = parsecmd(req->ifcall.data, req->ifcall.count);
	t = lookupcmd(cmd, groupcmd, nelem(groupcmd));
	if(t == nil){
		respondcmderror(req, cmd, "%r");
		free(cmd);
		return;
	}
	switch(t->index){
	case Cmddata: {
		if(strncmp(cmd->f[1], "message", 7) == 0)
			group->mode = Message;
		else
		if(strncmp(cmd->f[1], "coalesce", 8) == 0)
			group->mode = Coalesce;
		else
			e = "usage: data message|coalesce";
		break;
	}
	case Cmdreplay: {
		if(strncmp(cmd->f[1], "off", 3) == 0)
			group->replay = Replayoff;
		else
		if(strncmp(cmd->f[1], "last", 4) == 0)
			group->replay = Replaylast;
		else
		if(strncmp(cmd->f[1], "all", 3) == 0)
			group->replay = Replayall;
		else
			e = "usage: replay off|last|all";
		break;
	}
	case Cmddebug: {
		if(strncmp(cmd->f[1], "on", 2) == 0)
			DEBUG = 1;
		else
		if(strncmp(cmd->f[1], "off", 3) == 0)
			DEBUG = 0;
		else
			e = "usage: debug on|off";
		break;
	}
	case Cmddebug9p: {
		if(strncmp(cmd->f[1], "on", 2) == 0)
			chatty9p = 1;
		else
		if(strncmp(cmd->f[1], "off", 3) == 0)
			chatty9p = 0;
		else
			e = "usage: debug9p on|off";
		break;
	}}
	free(cmd);
	respond(req, e);
}

void
xcreate(Req *req)
{
	char *name = req->ifcall.name;
	char *uid = req->fid->uid;
	ulong perm = req->ifcall.perm;
	File *group = req->fid->file;
	File *f = nil;

	switch(filetype(group)){
	case Qroot:
	case Qgroup:
		if(perm&DMDIR)
			f = groupcreate(group, name, uid, perm);
		else{
			f = streamcreate(group, name, uid, perm);
			req->fid->file = f;
			req->ofcall.qid = f->qid;
			streamopen(f->aux, req);
		}
		break;
	}
	if(f == nil)
		respond(req, "internal failure");
	else
		respond(req, nil);
}

void
xopen(Req *req)
{
	File *f = req->fid->file;

	switch(filetype(f)){
	case Qstream:
	case Qorder:
		streamopen(f->aux, req);
		break;
	}
	respond(req, nil);
}

void
xwrite(Req *req)
{
	File *f = req->fid->file;

	switch(filetype(f)){
	case Qstream:
		streamwrite(req);
		break;
	case Qctl:
		ctlwrite(req);
		break;
	default:
		respond(req, "forbidden");
		return;
	}
}

void
xread(Req *req)
{
	File *f = req->fid->file;

	switch(filetype(f)){
	case Qstream:
	case Qorder:
		streamread(req);
		break;
	case Qctl:
		ctlread(req);
		break;
	default:
		respond(req, "forbidden");
	}
}

void
xflush(Req *req)
{
	Req *old = req->oldreq;
	File *f = old->fid->file;
	Read *r;

	switch(filetype(f)){
	case Qstream:
	case Qorder: {
		Stream *s = f->aux;

		if(old->ifcall.type != Tread)
			break;
		listeach(Read*, s->rqueue, r){
			if(r->req == old){
				listunlink(r);
				free(r);
				break;
			}
		}
		respond(old, "interrupted");
	}}
	respond(req, nil);
}

void
xwstat(Req *req)
{
	File *w, *f = req->fid->file;
	char *uid = req->fid->uid;

	/* To change name, must have write permission in group. */
	if(req->d.name[0] != '\0' && strcmp(req->d.name, f->name) != 0){
		if((w = f->parent) == nil)
			goto perm;
		incref(w);
	 	if(!hasperm(w, uid, AWRITE)){
			closefile(w);
			goto perm;
		}
		if((w = walkfile(w, req->d.name)) != nil){
			closefile(w);
			respond(req, "file already exists");
			return;
		}
	}

	/* To change group, must be owner and member of new group,
	 * or leader of current group and leader of new group.
	 * Second case cannot happen, but we check anyway. */
	while(req->d.gid[0] != '\0' && strcmp(f->gid, req->d.gid) != 0){
		if(strcmp(uid, f->uid) == 0)
			break;
		if(strcmp(uid, f->gid) == 0)
		if(strcmp(uid, req->d.gid) == 0)
			break;
		respond(req, "not owner");
		return;
	}

	/* To change mode, must be owner or group leader.
	 * Because of lack of users file, leader=>group itself. */
	if(req->d.mode != ~0 && f->mode != req->d.mode){
		if(strcmp(uid, f->uid) != 0)
		if(strcmp(uid, f->gid) != 0){
			respond(req, "not owner");
			return;
		}
	}

	if(req->d.name[0] != '\0'){
		free(f->name);
		f->name = estrdup(req->d.name);
	}
	if(req->d.uid[0] != '\0'){
		free(f->uid);
		f->uid = estrdup(req->d.uid);
	}
	if(req->d.gid[0] != '\0'){
		free(f->gid);
		f->gid = estrdup(req->d.gid);
	}
	if(req->d.mode != ~0){
		f->mode = req->d.mode;
		f->qid.type = f->mode >> 24;
	}

	respond(req, nil);
	return;
perm:
	respond(req, "permission denied");
}

void
xdestroyfid(Fid *fid)
{
	Client *f = fid->aux;

	free(f);
}

void
xdestroyfile(File *f)
{
	switch(filetype(f)){
	case Qgroup:
		groupclose(f->aux);
		break;
	case Qstream:
		streamclose(f->aux);
		break;
	}
	return;
}

Srv fs = {
	.create = xcreate,
	.open = xopen,
	.write = xwrite,
	.read = xread,
	.flush = xflush,
	.wstat = xwstat,
	.destroyfid = xdestroyfid,
};

void
usage(void)
{
	fprint(2, "usage: %s [-D] [-s name] [-m mtpt]\n", argv0);
	exits("usage");
}

void
main(int argc, char *argv[])
{
	char *name = nil;
	char *mtpt = nil;

	ARGBEGIN{
	case 's': name = EARGF(usage()); break;
	case 'm': mtpt = EARGF(usage()); break;
	case 'D': chatty9p++; break;
	default: usage();
	}ARGEND;

	fs.tree = alloctree(nil, nil, DMDIR|0775, xdestroyfile);
	groupcreate(fs.tree->root, "/", nil, 0);
	filesettype(fs.tree->root, Qroot);

	if(name || mtpt){
		postmountsrv(&fs, name, mtpt, MREPL|MCREATE);
		exits(nil);
	}
	fs.infd = fs.outfd = 0;
	dup(2, 1);
	srv(&fs);
	exits(nil);
}