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