9ns: Fix issues with can_have_children
[akaros.git] / kern / src / ns / convS2M.c
1 /* Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
2  * Portions Copyright © 1997-1999 Vita Nuova Limited
3  * Portions Copyright © 2000-2007 Vita Nuova Holdings Limited
4  *                                (www.vitanuova.com)
5  * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
6  *
7  * Modified for the Akaros operating system:
8  * Copyright (c) 2013-2014 The Regents of the University of California
9  * Copyright (c) 2013-2015 Google Inc.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE. */
28
29 #include <slab.h>
30 #include <kmalloc.h>
31 #include <kref.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include <error.h>
36 #include <cpio.h>
37 #include <pmap.h>
38 #include <smp.h>
39 #include <net/ip.h>
40
41 static
42 uint8_t *pstring(uint8_t * p, char *s)
43 {
44         unsigned int n;
45
46         if (s == NULL) {
47                 PBIT16(p, 0);
48                 p += BIT16SZ;
49                 return p;
50         }
51
52         n = strlen(s);
53         PBIT16(p, n);
54         p += BIT16SZ;
55         memmove(p, s, n);
56         p += n;
57         return p;
58 }
59
60 static
61 uint8_t *pqid(uint8_t * p, struct qid *q)
62 {
63         PBIT8(p, q->type);
64         p += BIT8SZ;
65         PBIT32(p, q->vers);
66         p += BIT32SZ;
67         PBIT64(p, q->path);
68         p += BIT64SZ;
69         return p;
70 }
71
72 static
73 unsigned int stringsz(char *s)
74 {
75         if (s == NULL)
76                 return BIT16SZ;
77
78         return BIT16SZ + strlen(s);
79 }
80
81 unsigned int sizeS2M(struct fcall *f)
82 {
83         unsigned int n;
84         int i;
85
86         n = 0;
87         n += BIT32SZ;   /* size */
88         n += BIT8SZ;    /* type */
89         n += BIT16SZ;   /* tag */
90
91         switch (f->type) {
92                 default:
93                         return 0;
94
95                 case Tversion:
96                         n += BIT32SZ;
97                         n += stringsz(f->version);
98                         break;
99
100                 case Tflush:
101                         n += BIT16SZ;
102                         break;
103
104                 case Tauth:
105                         n += BIT32SZ;
106                         n += stringsz(f->uname);
107                         n += stringsz(f->aname);
108                         break;
109
110                 case Tattach:
111                         n += BIT32SZ;
112                         n += BIT32SZ;
113                         n += stringsz(f->uname);
114                         n += stringsz(f->aname);
115                         break;
116
117                 case Twalk:
118                         n += BIT32SZ;
119                         n += BIT32SZ;
120                         n += BIT16SZ;
121                         for (i = 0; i < f->nwname; i++)
122                                 n += stringsz(f->wname[i]);
123                         break;
124
125                 case Topen:
126                         n += BIT32SZ;
127                         n += BIT8SZ;
128                         break;
129
130                 case Tcreate:
131                         n += BIT32SZ;
132                         n += stringsz(f->name);
133                         n += BIT32SZ;
134                         n += BIT8SZ;
135                         break;
136
137                 case Tread:
138                         n += BIT32SZ;
139                         n += BIT64SZ;
140                         n += BIT32SZ;
141                         break;
142
143                 case Twrite:
144                         n += BIT32SZ;
145                         n += BIT64SZ;
146                         n += BIT32SZ;
147                         n += f->count;
148                         break;
149
150                 case Tclunk:
151                 case Tremove:
152                         n += BIT32SZ;
153                         break;
154
155                 case Tstat:
156                         n += BIT32SZ;
157                         break;
158
159                 case Twstat:
160                         n += BIT32SZ;
161                         n += BIT16SZ;
162                         n += f->nstat;
163                         break;
164 /*
165  */
166
167                 case Rversion:
168                         n += BIT32SZ;
169                         n += stringsz(f->version);
170                         break;
171
172                 case Rerror:
173                         n += stringsz(f->ename);
174                         break;
175
176                 case Rflush:
177                         break;
178
179                 case Rauth:
180                         n += QIDSZ;
181                         break;
182
183                 case Rattach:
184                         n += QIDSZ;
185                         break;
186
187                 case Rwalk:
188                         n += BIT16SZ;
189                         n += f->nwqid * QIDSZ;
190                         break;
191
192                 case Ropen:
193                 case Rcreate:
194                         n += QIDSZ;
195                         n += BIT32SZ;
196                         break;
197
198                 case Rread:
199                         n += BIT32SZ;
200                         n += f->count;
201                         break;
202
203                 case Rwrite:
204                         n += BIT32SZ;
205                         break;
206
207                 case Rclunk:
208                         break;
209
210                 case Rremove:
211                         break;
212
213                 case Rstat:
214                         n += BIT16SZ;
215                         n += f->nstat;
216                         break;
217
218                 case Rwstat:
219                         break;
220         }
221         return n;
222 }
223
224 unsigned int convS2M(struct fcall *f, uint8_t * ap, unsigned int nap)
225 {
226         uint8_t *p;
227         unsigned int i, size;
228
229         size = sizeS2M(f);
230         if (size == 0)
231                 return 0;
232         if (size > nap)
233                 return 0;
234
235         p = (uint8_t *) ap;
236
237         PBIT32(p, size);
238         p += BIT32SZ;
239         PBIT8(p, f->type);
240         p += BIT8SZ;
241         PBIT16(p, f->tag);
242         p += BIT16SZ;
243
244         switch (f->type) {
245                 default:
246                         return 0;
247
248                 case Tversion:
249                         PBIT32(p, f->msize);
250                         p += BIT32SZ;
251                         p = pstring(p, f->version);
252                         break;
253
254                 case Tflush:
255                         PBIT16(p, f->oldtag);
256                         p += BIT16SZ;
257                         break;
258
259                 case Tauth:
260                         PBIT32(p, f->afid);
261                         p += BIT32SZ;
262                         p = pstring(p, f->uname);
263                         p = pstring(p, f->aname);
264                         break;
265
266                 case Tattach:
267                         PBIT32(p, f->fid);
268                         p += BIT32SZ;
269                         PBIT32(p, f->afid);
270                         p += BIT32SZ;
271                         p = pstring(p, f->uname);
272                         p = pstring(p, f->aname);
273                         break;
274
275                 case Twalk:
276                         PBIT32(p, f->fid);
277                         p += BIT32SZ;
278                         PBIT32(p, f->newfid);
279                         p += BIT32SZ;
280                         PBIT16(p, f->nwname);
281                         p += BIT16SZ;
282                         if (f->nwname > MAXWELEM)
283                                 return 0;
284                         for (i = 0; i < f->nwname; i++)
285                                 p = pstring(p, f->wname[i]);
286                         break;
287
288                 case Topen:
289                         PBIT32(p, f->fid);
290                         p += BIT32SZ;
291                         PBIT8(p, f->mode);
292                         p += BIT8SZ;
293                         break;
294
295                 case Tcreate:
296                         PBIT32(p, f->fid);
297                         p += BIT32SZ;
298                         p = pstring(p, f->name);
299                         PBIT32(p, f->perm);
300                         p += BIT32SZ;
301                         PBIT8(p, f->mode);
302                         p += BIT8SZ;
303                         break;
304
305                 case Tread:
306                         PBIT32(p, f->fid);
307                         p += BIT32SZ;
308                         PBIT64(p, f->offset);
309                         p += BIT64SZ;
310                         PBIT32(p, f->count);
311                         p += BIT32SZ;
312                         break;
313
314                 case Twrite:
315                         PBIT32(p, f->fid);
316                         p += BIT32SZ;
317                         PBIT64(p, f->offset);
318                         p += BIT64SZ;
319                         PBIT32(p, f->count);
320                         p += BIT32SZ;
321                         memmove(p, f->data, f->count);
322                         p += f->count;
323                         break;
324
325                 case Tclunk:
326                 case Tremove:
327                         PBIT32(p, f->fid);
328                         p += BIT32SZ;
329                         break;
330
331                 case Tstat:
332                         PBIT32(p, f->fid);
333                         p += BIT32SZ;
334                         break;
335
336                 case Twstat:
337                         PBIT32(p, f->fid);
338                         p += BIT32SZ;
339                         PBIT16(p, f->nstat);
340                         p += BIT16SZ;
341                         memmove(p, f->stat, f->nstat);
342                         p += f->nstat;
343                         break;
344 /*
345  */
346
347                 case Rversion:
348                         PBIT32(p, f->msize);
349                         p += BIT32SZ;
350                         p = pstring(p, f->version);
351                         break;
352
353                 case Rerror:
354                         p = pstring(p, f->ename);
355                         break;
356
357                 case Rflush:
358                         break;
359
360                 case Rauth:
361                         p = pqid(p, &f->aqid);
362                         break;
363
364                 case Rattach:
365                         p = pqid(p, &f->qid);
366                         break;
367
368                 case Rwalk:
369                         PBIT16(p, f->nwqid);
370                         p += BIT16SZ;
371                         if (f->nwqid > MAXWELEM)
372                                 return 0;
373                         for (i = 0; i < f->nwqid; i++)
374                                 p = pqid(p, &f->wqid[i]);
375                         break;
376
377                 case Ropen:
378                 case Rcreate:
379                         p = pqid(p, &f->qid);
380                         PBIT32(p, f->iounit);
381                         p += BIT32SZ;
382                         break;
383
384                 case Rread:
385                         PBIT32(p, f->count);
386                         p += BIT32SZ;
387                         memmove(p, f->data, f->count);
388                         p += f->count;
389                         break;
390
391                 case Rwrite:
392                         PBIT32(p, f->count);
393                         p += BIT32SZ;
394                         break;
395
396                 case Rclunk:
397                         break;
398
399                 case Rremove:
400                         break;
401
402                 case Rstat:
403                         PBIT16(p, f->nstat);
404                         p += BIT16SZ;
405                         memmove(p, f->stat, f->nstat);
406                         p += f->nstat;
407                         break;
408
409                 case Rwstat:
410                         break;
411         }
412         if (size != p - ap)
413                 return 0;
414         return size;
415 }