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