vfs: Remove KFS, blockdev and devfs
[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 <vfs.h>
30 #include <slab.h>
31 #include <kmalloc.h>
32 #include <kref.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <assert.h>
36 #include <error.h>
37 #include <cpio.h>
38 #include <pmap.h>
39 #include <smp.h>
40 #include <net/ip.h>
41
42 static
43 uint8_t *pstring(uint8_t * p, char *s)
44 {
45         unsigned int n;
46
47         if (s == NULL) {
48                 PBIT16(p, 0);
49                 p += BIT16SZ;
50                 return p;
51         }
52
53         n = strlen(s);
54         PBIT16(p, n);
55         p += BIT16SZ;
56         memmove(p, s, n);
57         p += n;
58         return p;
59 }
60
61 static
62 uint8_t *pqid(uint8_t * p, struct qid *q)
63 {
64         PBIT8(p, q->type);
65         p += BIT8SZ;
66         PBIT32(p, q->vers);
67         p += BIT32SZ;
68         PBIT64(p, q->path);
69         p += BIT64SZ;
70         return p;
71 }
72
73 static
74 unsigned int stringsz(char *s)
75 {
76         if (s == NULL)
77                 return BIT16SZ;
78
79         return BIT16SZ + strlen(s);
80 }
81
82 unsigned int sizeS2M(struct fcall *f)
83 {
84         unsigned int n;
85         int i;
86
87         n = 0;
88         n += BIT32SZ;   /* size */
89         n += BIT8SZ;    /* type */
90         n += BIT16SZ;   /* tag */
91
92         switch (f->type) {
93                 default:
94                         return 0;
95
96                 case Tversion:
97                         n += BIT32SZ;
98                         n += stringsz(f->version);
99                         break;
100
101                 case Tflush:
102                         n += BIT16SZ;
103                         break;
104
105                 case Tauth:
106                         n += BIT32SZ;
107                         n += stringsz(f->uname);
108                         n += stringsz(f->aname);
109                         break;
110
111                 case Tattach:
112                         n += BIT32SZ;
113                         n += BIT32SZ;
114                         n += stringsz(f->uname);
115                         n += stringsz(f->aname);
116                         break;
117
118                 case Twalk:
119                         n += BIT32SZ;
120                         n += BIT32SZ;
121                         n += BIT16SZ;
122                         for (i = 0; i < f->nwname; i++)
123                                 n += stringsz(f->wname[i]);
124                         break;
125
126                 case Topen:
127                         n += BIT32SZ;
128                         n += BIT8SZ;
129                         break;
130
131                 case Tcreate:
132                         n += BIT32SZ;
133                         n += stringsz(f->name);
134                         n += BIT32SZ;
135                         n += BIT8SZ;
136                         break;
137
138                 case Tread:
139                         n += BIT32SZ;
140                         n += BIT64SZ;
141                         n += BIT32SZ;
142                         break;
143
144                 case Twrite:
145                         n += BIT32SZ;
146                         n += BIT64SZ;
147                         n += BIT32SZ;
148                         n += f->count;
149                         break;
150
151                 case Tclunk:
152                 case Tremove:
153                         n += BIT32SZ;
154                         break;
155
156                 case Tstat:
157                         n += BIT32SZ;
158                         break;
159
160                 case Twstat:
161                         n += BIT32SZ;
162                         n += BIT16SZ;
163                         n += f->nstat;
164                         break;
165 /*
166  */
167
168                 case Rversion:
169                         n += BIT32SZ;
170                         n += stringsz(f->version);
171                         break;
172
173                 case Rerror:
174                         n += stringsz(f->ename);
175                         break;
176
177                 case Rflush:
178                         break;
179
180                 case Rauth:
181                         n += QIDSZ;
182                         break;
183
184                 case Rattach:
185                         n += QIDSZ;
186                         break;
187
188                 case Rwalk:
189                         n += BIT16SZ;
190                         n += f->nwqid * QIDSZ;
191                         break;
192
193                 case Ropen:
194                 case Rcreate:
195                         n += QIDSZ;
196                         n += BIT32SZ;
197                         break;
198
199                 case Rread:
200                         n += BIT32SZ;
201                         n += f->count;
202                         break;
203
204                 case Rwrite:
205                         n += BIT32SZ;
206                         break;
207
208                 case Rclunk:
209                         break;
210
211                 case Rremove:
212                         break;
213
214                 case Rstat:
215                         n += BIT16SZ;
216                         n += f->nstat;
217                         break;
218
219                 case Rwstat:
220                         break;
221         }
222         return n;
223 }
224
225 unsigned int convS2M(struct fcall *f, uint8_t * ap, unsigned int nap)
226 {
227         uint8_t *p;
228         unsigned int i, size;
229
230         size = sizeS2M(f);
231         if (size == 0)
232                 return 0;
233         if (size > nap)
234                 return 0;
235
236         p = (uint8_t *) ap;
237
238         PBIT32(p, size);
239         p += BIT32SZ;
240         PBIT8(p, f->type);
241         p += BIT8SZ;
242         PBIT16(p, f->tag);
243         p += BIT16SZ;
244
245         switch (f->type) {
246                 default:
247                         return 0;
248
249                 case Tversion:
250                         PBIT32(p, f->msize);
251                         p += BIT32SZ;
252                         p = pstring(p, f->version);
253                         break;
254
255                 case Tflush:
256                         PBIT16(p, f->oldtag);
257                         p += BIT16SZ;
258                         break;
259
260                 case Tauth:
261                         PBIT32(p, f->afid);
262                         p += BIT32SZ;
263                         p = pstring(p, f->uname);
264                         p = pstring(p, f->aname);
265                         break;
266
267                 case Tattach:
268                         PBIT32(p, f->fid);
269                         p += BIT32SZ;
270                         PBIT32(p, f->afid);
271                         p += BIT32SZ;
272                         p = pstring(p, f->uname);
273                         p = pstring(p, f->aname);
274                         break;
275
276                 case Twalk:
277                         PBIT32(p, f->fid);
278                         p += BIT32SZ;
279                         PBIT32(p, f->newfid);
280                         p += BIT32SZ;
281                         PBIT16(p, f->nwname);
282                         p += BIT16SZ;
283                         if (f->nwname > MAXWELEM)
284                                 return 0;
285                         for (i = 0; i < f->nwname; i++)
286                                 p = pstring(p, f->wname[i]);
287                         break;
288
289                 case Topen:
290                         PBIT32(p, f->fid);
291                         p += BIT32SZ;
292                         PBIT8(p, f->mode);
293                         p += BIT8SZ;
294                         break;
295
296                 case Tcreate:
297                         PBIT32(p, f->fid);
298                         p += BIT32SZ;
299                         p = pstring(p, f->name);
300                         PBIT32(p, f->perm);
301                         p += BIT32SZ;
302                         PBIT8(p, f->mode);
303                         p += BIT8SZ;
304                         break;
305
306                 case Tread:
307                         PBIT32(p, f->fid);
308                         p += BIT32SZ;
309                         PBIT64(p, f->offset);
310                         p += BIT64SZ;
311                         PBIT32(p, f->count);
312                         p += BIT32SZ;
313                         break;
314
315                 case Twrite:
316                         PBIT32(p, f->fid);
317                         p += BIT32SZ;
318                         PBIT64(p, f->offset);
319                         p += BIT64SZ;
320                         PBIT32(p, f->count);
321                         p += BIT32SZ;
322                         memmove(p, f->data, f->count);
323                         p += f->count;
324                         break;
325
326                 case Tclunk:
327                 case Tremove:
328                         PBIT32(p, f->fid);
329                         p += BIT32SZ;
330                         break;
331
332                 case Tstat:
333                         PBIT32(p, f->fid);
334                         p += BIT32SZ;
335                         break;
336
337                 case Twstat:
338                         PBIT32(p, f->fid);
339                         p += BIT32SZ;
340                         PBIT16(p, f->nstat);
341                         p += BIT16SZ;
342                         memmove(p, f->stat, f->nstat);
343                         p += f->nstat;
344                         break;
345 /*
346  */
347
348                 case Rversion:
349                         PBIT32(p, f->msize);
350                         p += BIT32SZ;
351                         p = pstring(p, f->version);
352                         break;
353
354                 case Rerror:
355                         p = pstring(p, f->ename);
356                         break;
357
358                 case Rflush:
359                         break;
360
361                 case Rauth:
362                         p = pqid(p, &f->aqid);
363                         break;
364
365                 case Rattach:
366                         p = pqid(p, &f->qid);
367                         break;
368
369                 case Rwalk:
370                         PBIT16(p, f->nwqid);
371                         p += BIT16SZ;
372                         if (f->nwqid > MAXWELEM)
373                                 return 0;
374                         for (i = 0; i < f->nwqid; i++)
375                                 p = pqid(p, &f->wqid[i]);
376                         break;
377
378                 case Ropen:
379                 case Rcreate:
380                         p = pqid(p, &f->qid);
381                         PBIT32(p, f->iounit);
382                         p += BIT32SZ;
383                         break;
384
385                 case Rread:
386                         PBIT32(p, f->count);
387                         p += BIT32SZ;
388                         memmove(p, f->data, f->count);
389                         p += f->count;
390                         break;
391
392                 case Rwrite:
393                         PBIT32(p, f->count);
394                         p += BIT32SZ;
395                         break;
396
397                 case Rclunk:
398                         break;
399
400                 case Rremove:
401                         break;
402
403                 case Rstat:
404                         PBIT16(p, f->nstat);
405                         p += BIT16SZ;
406                         memmove(p, f->stat, f->nstat);
407                         p += f->nstat;
408                         break;
409
410                 case Rwstat:
411                         break;
412         }
413         if (size != p - ap)
414                 return 0;
415         return size;
416 }