~kris/9p

9hist

ref: fd21a503ae1d591cf9cea82f15cbada4c4e245e9 9hist/port/stfcall.c -rw-r--r-- 2.6 KiB
fd21a503 — David du Colombier Plan 9 from Bell Labs 1992-08-05 34 years 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
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"

enum
{
	Twritehdr	= 15,	/* Min bytes for Twrite */
	Rreadhdr	= 8,	/* Min bytes for Rread */
	Twritecnt	= 13,	/* Offset in byte stream of write count */
	Rreadcnt	= 5,	/* Offset for Readcnt */
};

static void fcalliput(Queue*, Block*);
static void fcalloput(Queue*, Block*);
static void fcallopen(Queue*, Stream*);
static void fcallclose(Queue*);
static void fcallreset(void);
Qinfo fcallinfo = { fcalliput, fcalloput, fcallopen, fcallclose, "fcall", fcallreset };

static uchar msglen[256] =
{
	[Tnop]		3,
	[Rnop]		3,
	[Tsession]	3,
	[Rsession]	3,
	[Terror]	0,
	[Rerror]	67,
	[Tflush]	5,
	[Rflush]	3,
	[Tattach]	89,
	[Rattach]	13,
	[Tclone]	7,
	[Rclone]	5,
	[Twalk]		33,
	[Rwalk]		13,
	[Topen]		6,
	[Ropen]		13,
	[Tcreate]	38,
	[Rcreate]	13,
	[Tread]		15,
	[Rread]		8,
	[Twrite]	16,
	[Rwrite]	7,
	[Tclunk]	5,
	[Rclunk]	5,
	[Tremove]	5,
	[Rremove]	5,
	[Tstat]		5,
	[Rstat]		121,
	[Twstat]	121,
	[Rwstat]	5,
	[Tclwalk]	35,
	[Rclwalk]	13,
	[Tauth]		69,
	[Rauth]		35,
};

static void
fcallreset(void)
{
}

static void
fcallopen(Queue *q, Stream *s)
{
	USED(q, s);
}

static void
fcallclose(Queue * q)
{
	USED(q);
}

void
fcalloput(Queue *q, Block *bp)
{
	PUTNEXT(q, bp);
}

void
upstream(Queue *q, ulong len)
{
	Block *bl, **tail, *bp;
	ulong l;

	tail = &bl;
	while(len) {
		l = BLEN(q->first);
		if(l > len)
			break;
		bp = getq(q);			/* Consume all of block */
		*tail = bp;
		tail = &bp->next;
		len -= l;
	}
	if(len) {				/* Consume partial block */
		lock(q);
		*tail = copyb(q->first, len);
		q->first->rptr += len;
		q->len -= len;
		unlock(q);
	}
	for(bp = bl; bp->next; bp = bp->next)
		;
	bp->flags |= S_DELIM;
	PUTNEXT(q, bl);
}

static void
fcalliput(Queue *q, Block *bp)
{
	ulong len, need, off;

	if(bp->type != M_DATA) {
		PUTNEXT(q, bp);
		return;
	}
	if(BLEN(bp) == 0) {
		freeb(bp);
		return;
	}

	/* Stash the data */
	bp->flags &= ~S_DELIM;
	putq(q, bp);

	for(;;) {
		bp = q->first;
		if(bp == 0)
			return;
		switch(bp->rptr[0]) {		/* This is the type */
		default:
			len = msglen[bp->rptr[0]];
			if(len == 0)
				error(Emountrpc);
			if(q->len < len)
				return;
	
			upstream(q, len);
			continue;

		case Twrite:			/* Fmt: TGGFFOOOOOOOOCC */
			len = Twritehdr;	/* T = type, G = tag, F = fid */
			off = Twritecnt;	/* O = offset, C = count */
			break;

		case Rread:			/* Fmt: TGGFFCC */
			len = Rreadhdr;
			off = Rreadcnt;
			break;
		}
	
		if(q->len < len)
			return;
	
		pullup(q->first, len);
		bp = q->first;
		need = len+bp->rptr[off]+(bp->rptr[off+1]<<8);
		if(q->len < need)
			return;
	
		upstream(q, need);
	}
}