virtio-9p-marshal.c 9.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Virtio 9p backend
 *
 * Copyright IBM, Corp. 2010
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

#include <glib.h>
#include <glib/gprintf.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/time.h>
#include <utime.h>
#include <sys/uio.h>
#include <string.h>
#include <stdint.h>
23
#include <errno.h>
24

25
#include "qemu/compiler.h"
26
#include "virtio-9p-marshal.h"
27
#include "qemu/bswap.h"
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

void v9fs_string_free(V9fsString *str)
{
    g_free(str->data);
    str->data = NULL;
    str->size = 0;
}

void v9fs_string_null(V9fsString *str)
{
    v9fs_string_free(str);
}

void GCC_FMT_ATTR(2, 3)
v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
{
    va_list ap;

    v9fs_string_free(str);

    va_start(ap, fmt);
    str->size = g_vasprintf(&str->data, fmt, ap);
    va_end(ap);
}

void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
{
    v9fs_string_free(lhs);
    v9fs_string_sprintf(lhs, "%s", rhs->data);
}


60 61
static ssize_t v9fs_packunpack(void *addr, struct iovec *sg, int sg_count,
                               size_t offset, size_t size, int pack)
62 63 64
{
    int i = 0;
    size_t copied = 0;
65 66
    size_t req_size = size;

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    for (i = 0; size && i < sg_count; i++) {
        size_t len;
        if (offset >= sg[i].iov_len) {
            /* skip this sg */
            offset -= sg[i].iov_len;
            continue;
        } else {
            len = MIN(sg[i].iov_len - offset, size);
            if (pack) {
                memcpy(sg[i].iov_base + offset, addr, len);
            } else {
                memcpy(addr, sg[i].iov_base + offset, len);
            }
            size -= len;
            copied += len;
            addr += len;
            if (size) {
                offset = 0;
                continue;
            }
        }
    }
90 91 92 93 94 95
    if (copied < req_size) {
        /*
         * We copied less that requested size. error out
         */
        return -ENOBUFS;
    }
96 97 98
    return copied;
}

99 100
static ssize_t v9fs_unpack(void *dst, struct iovec *out_sg, int out_num,
                           size_t offset, size_t size)
101 102 103 104
{
    return v9fs_packunpack(dst, out_sg, out_num, offset, size, 0);
}

105 106
ssize_t v9fs_pack(struct iovec *in_sg, int in_num, size_t offset,
                  const void *src, size_t size)
107 108 109 110
{
    return v9fs_packunpack((void *)src, in_sg, in_num, offset, size, 1);
}

111 112
ssize_t v9fs_unmarshal(struct iovec *out_sg, int out_num, size_t offset,
                       int bswap, const char *fmt, ...)
113 114 115
{
    int i;
    va_list ap;
116
    ssize_t copied = 0;
117 118 119 120 121 122 123
    size_t old_offset = offset;

    va_start(ap, fmt);
    for (i = 0; fmt[i]; i++) {
        switch (fmt[i]) {
        case 'b': {
            uint8_t *valp = va_arg(ap, uint8_t *);
124
            copied = v9fs_unpack(valp, out_sg, out_num, offset, sizeof(*valp));
125 126 127 128 129
            break;
        }
        case 'w': {
            uint16_t val, *valp;
            valp = va_arg(ap, uint16_t *);
130
            copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
131 132 133 134 135 136 137 138 139 140
            if (bswap) {
                *valp = le16_to_cpu(val);
            } else {
                *valp = val;
            }
            break;
        }
        case 'd': {
            uint32_t val, *valp;
            valp = va_arg(ap, uint32_t *);
141
            copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
142 143 144 145 146 147 148 149 150 151
            if (bswap) {
                *valp = le32_to_cpu(val);
            } else {
                *valp = val;
            }
            break;
        }
        case 'q': {
            uint64_t val, *valp;
            valp = va_arg(ap, uint64_t *);
152
            copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
153 154 155 156 157 158 159 160 161
            if (bswap) {
                *valp = le64_to_cpu(val);
            } else {
                *valp = val;
            }
            break;
        }
        case 's': {
            V9fsString *str = va_arg(ap, V9fsString *);
162 163 164 165 166 167 168 169 170 171 172 173 174
            copied = v9fs_unmarshal(out_sg, out_num, offset, bswap,
                                    "w", &str->size);
            if (copied > 0) {
                offset += copied;
                str->data = g_malloc(str->size + 1);
                copied = v9fs_unpack(str->data, out_sg, out_num, offset,
                                     str->size);
                if (copied > 0) {
                    str->data[str->size] = 0;
                } else {
                    v9fs_string_free(str);
                }
            }
175 176 177 178
            break;
        }
        case 'Q': {
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
179 180
            copied = v9fs_unmarshal(out_sg, out_num, offset, bswap, "bdq",
                                    &qidp->type, &qidp->version, &qidp->path);
181 182 183 184
            break;
        }
        case 'S': {
            V9fsStat *statp = va_arg(ap, V9fsStat *);
185
            copied = v9fs_unmarshal(out_sg, out_num, offset, bswap,
186
                                    "wwdQdddqsssssddd",
187 188 189 190 191 192 193
                                    &statp->size, &statp->type, &statp->dev,
                                    &statp->qid, &statp->mode, &statp->atime,
                                    &statp->mtime, &statp->length,
                                    &statp->name, &statp->uid, &statp->gid,
                                    &statp->muid, &statp->extension,
                                    &statp->n_uid, &statp->n_gid,
                                    &statp->n_muid);
194 195 196 197
            break;
        }
        case 'I': {
            V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
198 199 200 201 202 203
            copied = v9fs_unmarshal(out_sg, out_num, offset, bswap,
                                    "ddddqqqqq",
                                    &iattr->valid, &iattr->mode,
                                    &iattr->uid, &iattr->gid, &iattr->size,
                                    &iattr->atime_sec, &iattr->atime_nsec,
                                    &iattr->mtime_sec, &iattr->mtime_nsec);
204 205 206 207 208
            break;
        }
        default:
            break;
        }
209 210 211 212 213
        if (copied < 0) {
            va_end(ap);
            return copied;
        }
        offset += copied;
214 215 216 217 218 219
    }
    va_end(ap);

    return offset - old_offset;
}

220 221
ssize_t v9fs_marshal(struct iovec *in_sg, int in_num, size_t offset,
                     int bswap, const char *fmt, ...)
222 223 224
{
    int i;
    va_list ap;
225
    ssize_t copied = 0;
226 227 228 229 230 231 232
    size_t old_offset = offset;

    va_start(ap, fmt);
    for (i = 0; fmt[i]; i++) {
        switch (fmt[i]) {
        case 'b': {
            uint8_t val = va_arg(ap, int);
233
            copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
234 235 236 237 238 239 240 241 242
            break;
        }
        case 'w': {
            uint16_t val;
            if (bswap) {
                cpu_to_le16w(&val, va_arg(ap, int));
            } else {
                val =  va_arg(ap, int);
            }
243
            copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
244 245 246 247 248 249 250 251 252
            break;
        }
        case 'd': {
            uint32_t val;
            if (bswap) {
                cpu_to_le32w(&val, va_arg(ap, uint32_t));
            } else {
                val =  va_arg(ap, uint32_t);
            }
253
            copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
254 255 256 257 258 259 260 261 262
            break;
        }
        case 'q': {
            uint64_t val;
            if (bswap) {
                cpu_to_le64w(&val, va_arg(ap, uint64_t));
            } else {
                val =  va_arg(ap, uint64_t);
            }
263
            copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
264 265 266 267
            break;
        }
        case 's': {
            V9fsString *str = va_arg(ap, V9fsString *);
268 269 270 271 272 273
            copied = v9fs_marshal(in_sg, in_num, offset, bswap,
                                  "w", str->size);
            if (copied > 0) {
                offset += copied;
                copied = v9fs_pack(in_sg, in_num, offset, str->data, str->size);
            }
274 275 276 277
            break;
        }
        case 'Q': {
            V9fsQID *qidp = va_arg(ap, V9fsQID *);
278 279
            copied = v9fs_marshal(in_sg, in_num, offset, bswap, "bdq",
                                  qidp->type, qidp->version, qidp->path);
280 281 282 283
            break;
        }
        case 'S': {
            V9fsStat *statp = va_arg(ap, V9fsStat *);
284 285 286 287 288 289 290 291
            copied = v9fs_marshal(in_sg, in_num, offset, bswap,
                                  "wwdQdddqsssssddd",
                                  statp->size, statp->type, statp->dev,
                                  &statp->qid, statp->mode, statp->atime,
                                  statp->mtime, statp->length, &statp->name,
                                  &statp->uid, &statp->gid, &statp->muid,
                                  &statp->extension, statp->n_uid,
                                  statp->n_gid, statp->n_muid);
292 293 294 295
            break;
        }
        case 'A': {
            V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
296
            copied = v9fs_marshal(in_sg, in_num, offset, bswap,
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
                                   "qQdddqqqqqqqqqqqqqqq",
                                   statp->st_result_mask,
                                   &statp->qid, statp->st_mode,
                                   statp->st_uid, statp->st_gid,
                                   statp->st_nlink, statp->st_rdev,
                                   statp->st_size, statp->st_blksize,
                                   statp->st_blocks, statp->st_atime_sec,
                                   statp->st_atime_nsec, statp->st_mtime_sec,
                                   statp->st_mtime_nsec, statp->st_ctime_sec,
                                   statp->st_ctime_nsec, statp->st_btime_sec,
                                   statp->st_btime_nsec, statp->st_gen,
                                   statp->st_data_version);
            break;
        }
        default:
            break;
        }
314 315 316 317 318
        if (copied < 0) {
            va_end(ap);
            return copied;
        }
        offset += copied;
319 320 321 322 323
    }
    va_end(ap);

    return offset - old_offset;
}