2011-12-14 08:19:06 +00:00
|
|
|
/*
|
2015-11-30 16:14:29 +00:00
|
|
|
* 9p backend
|
2011-12-14 08:19:06 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 18:17:10 +00:00
|
|
|
#include "qemu/osdep.h"
|
2011-12-14 08:19:06 +00:00
|
|
|
#include <glib/gprintf.h>
|
|
|
|
#include <utime.h>
|
|
|
|
|
2015-11-30 16:14:29 +00:00
|
|
|
#include "9p-iov-marshal.h"
|
2012-12-17 17:20:00 +00:00
|
|
|
#include "qemu/bswap.h"
|
2011-12-14 08:19:06 +00:00
|
|
|
|
2011-12-14 08:19:13 +00:00
|
|
|
static ssize_t v9fs_packunpack(void *addr, struct iovec *sg, int sg_count,
|
|
|
|
size_t offset, size_t size, int pack)
|
2011-12-14 08:19:06 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
size_t copied = 0;
|
2011-12-14 08:19:13 +00:00
|
|
|
size_t req_size = size;
|
|
|
|
|
2011-12-14 08:19:06 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-14 08:19:13 +00:00
|
|
|
if (copied < req_size) {
|
|
|
|
/*
|
|
|
|
* We copied less that requested size. error out
|
|
|
|
*/
|
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
2011-12-14 08:19:06 +00:00
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
2011-12-14 08:19:13 +00:00
|
|
|
static ssize_t v9fs_unpack(void *dst, struct iovec *out_sg, int out_num,
|
|
|
|
size_t offset, size_t size)
|
2011-12-14 08:19:06 +00:00
|
|
|
{
|
|
|
|
return v9fs_packunpack(dst, out_sg, out_num, offset, size, 0);
|
|
|
|
}
|
|
|
|
|
2011-12-14 08:19:13 +00:00
|
|
|
ssize_t v9fs_pack(struct iovec *in_sg, int in_num, size_t offset,
|
|
|
|
const void *src, size_t size)
|
2011-12-14 08:19:06 +00:00
|
|
|
{
|
|
|
|
return v9fs_packunpack((void *)src, in_sg, in_num, offset, size, 1);
|
|
|
|
}
|
|
|
|
|
2015-12-02 14:22:04 +00:00
|
|
|
ssize_t v9fs_iov_vunmarshal(struct iovec *out_sg, int out_num, size_t offset,
|
|
|
|
int bswap, const char *fmt, va_list ap)
|
2011-12-14 08:19:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
2011-12-14 08:19:13 +00:00
|
|
|
ssize_t copied = 0;
|
2011-12-14 08:19:06 +00:00
|
|
|
size_t old_offset = offset;
|
|
|
|
|
|
|
|
for (i = 0; fmt[i]; i++) {
|
|
|
|
switch (fmt[i]) {
|
|
|
|
case 'b': {
|
|
|
|
uint8_t *valp = va_arg(ap, uint8_t *);
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_unpack(valp, out_sg, out_num, offset, sizeof(*valp));
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w': {
|
|
|
|
uint16_t val, *valp;
|
|
|
|
valp = va_arg(ap, uint16_t *);
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
|
2011-12-14 08:19:06 +00:00
|
|
|
if (bswap) {
|
|
|
|
*valp = le16_to_cpu(val);
|
|
|
|
} else {
|
|
|
|
*valp = val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd': {
|
|
|
|
uint32_t val, *valp;
|
|
|
|
valp = va_arg(ap, uint32_t *);
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
|
2011-12-14 08:19:06 +00:00
|
|
|
if (bswap) {
|
|
|
|
*valp = le32_to_cpu(val);
|
|
|
|
} else {
|
|
|
|
*valp = val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'q': {
|
|
|
|
uint64_t val, *valp;
|
|
|
|
valp = va_arg(ap, uint64_t *);
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
|
2011-12-14 08:19:06 +00:00
|
|
|
if (bswap) {
|
|
|
|
*valp = le64_to_cpu(val);
|
|
|
|
} else {
|
|
|
|
*valp = val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': {
|
|
|
|
V9fsString *str = va_arg(ap, V9fsString *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
|
|
|
|
"w", &str->size);
|
2011-12-14 08:19:13 +00:00
|
|
|
if (copied > 0) {
|
|
|
|
offset += copied;
|
|
|
|
str->data = g_malloc(str->size + 1);
|
|
|
|
copied = v9fs_unpack(str->data, out_sg, out_num, offset,
|
|
|
|
str->size);
|
2016-10-17 12:13:58 +00:00
|
|
|
if (copied >= 0) {
|
2011-12-14 08:19:13 +00:00
|
|
|
str->data[str->size] = 0;
|
|
|
|
} else {
|
|
|
|
v9fs_string_free(str);
|
|
|
|
}
|
|
|
|
}
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Q': {
|
|
|
|
V9fsQID *qidp = va_arg(ap, V9fsQID *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
|
|
|
|
"bdq", &qidp->type, &qidp->version,
|
|
|
|
&qidp->path);
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'S': {
|
|
|
|
V9fsStat *statp = va_arg(ap, V9fsStat *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_unmarshal(out_sg, out_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);
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'I': {
|
|
|
|
V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_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);
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2017-05-25 08:30:13 +00:00
|
|
|
g_assert_not_reached();
|
2011-12-14 08:19:06 +00:00
|
|
|
}
|
2011-12-14 08:19:13 +00:00
|
|
|
if (copied < 0) {
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
offset += copied;
|
2011-12-14 08:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return offset - old_offset;
|
|
|
|
}
|
|
|
|
|
2015-12-02 14:22:04 +00:00
|
|
|
ssize_t v9fs_iov_unmarshal(struct iovec *out_sg, int out_num, size_t offset,
|
|
|
|
int bswap, const char *fmt, ...)
|
2011-12-14 08:19:06 +00:00
|
|
|
{
|
2015-12-02 14:22:04 +00:00
|
|
|
ssize_t ret;
|
2011-12-14 08:19:06 +00:00
|
|
|
va_list ap;
|
2015-12-02 14:22:04 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = v9fs_iov_vunmarshal(out_sg, out_num, offset, bswap, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t v9fs_iov_vmarshal(struct iovec *in_sg, int in_num, size_t offset,
|
|
|
|
int bswap, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
int i;
|
2011-12-14 08:19:13 +00:00
|
|
|
ssize_t copied = 0;
|
2011-12-14 08:19:06 +00:00
|
|
|
size_t old_offset = offset;
|
|
|
|
|
|
|
|
for (i = 0; fmt[i]; i++) {
|
|
|
|
switch (fmt[i]) {
|
|
|
|
case 'b': {
|
|
|
|
uint8_t val = va_arg(ap, int);
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'w': {
|
2016-07-07 16:20:56 +00:00
|
|
|
uint16_t val = va_arg(ap, int);
|
2011-12-14 08:19:06 +00:00
|
|
|
if (bswap) {
|
2016-07-07 16:20:56 +00:00
|
|
|
val = cpu_to_le16(val);
|
2011-12-14 08:19:06 +00:00
|
|
|
}
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd': {
|
2016-07-07 16:20:56 +00:00
|
|
|
uint32_t val = va_arg(ap, uint32_t);
|
2011-12-14 08:19:06 +00:00
|
|
|
if (bswap) {
|
2016-07-07 16:20:56 +00:00
|
|
|
val = cpu_to_le32(val);
|
2011-12-14 08:19:06 +00:00
|
|
|
}
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'q': {
|
2016-07-07 16:20:56 +00:00
|
|
|
uint64_t val = va_arg(ap, uint64_t);
|
2011-12-14 08:19:06 +00:00
|
|
|
if (bswap) {
|
2016-07-07 16:20:56 +00:00
|
|
|
val = cpu_to_le64(val);
|
2011-12-14 08:19:06 +00:00
|
|
|
}
|
2011-12-14 08:19:13 +00:00
|
|
|
copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's': {
|
|
|
|
V9fsString *str = va_arg(ap, V9fsString *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
|
|
|
|
"w", str->size);
|
2011-12-14 08:19:13 +00:00
|
|
|
if (copied > 0) {
|
|
|
|
offset += copied;
|
|
|
|
copied = v9fs_pack(in_sg, in_num, offset, str->data, str->size);
|
|
|
|
}
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Q': {
|
|
|
|
V9fsQID *qidp = va_arg(ap, V9fsQID *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap, "bdq",
|
|
|
|
qidp->type, qidp->version,
|
|
|
|
qidp->path);
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'S': {
|
|
|
|
V9fsStat *statp = va_arg(ap, V9fsStat *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_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);
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'A': {
|
|
|
|
V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
|
2015-11-30 16:14:29 +00:00
|
|
|
copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
|
|
|
|
"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);
|
2011-12-14 08:19:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2017-05-25 08:30:13 +00:00
|
|
|
g_assert_not_reached();
|
2011-12-14 08:19:06 +00:00
|
|
|
}
|
2011-12-14 08:19:13 +00:00
|
|
|
if (copied < 0) {
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
offset += copied;
|
2011-12-14 08:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return offset - old_offset;
|
|
|
|
}
|
2015-12-02 14:22:04 +00:00
|
|
|
|
|
|
|
ssize_t v9fs_iov_marshal(struct iovec *in_sg, int in_num, size_t offset,
|
|
|
|
int bswap, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = v9fs_iov_vmarshal(in_sg, in_num, offset, bswap, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|