4 * Copyright IBM, Corp. 2010
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include <glib/gprintf.h>
19 #include "9p-iov-marshal.h"
20 #include "qemu/bswap.h"
22 static ssize_t v9fs_packunpack(void *addr, struct iovec *sg, int sg_count,
23 size_t offset, size_t size, int pack)
27 size_t req_size = size;
30 for (i = 0; size && i < sg_count; i++) {
32 if (offset >= sg[i].iov_len) {
34 offset -= sg[i].iov_len;
37 len = MIN(sg[i].iov_len - offset, size);
39 memcpy(sg[i].iov_base + offset, addr, len);
41 memcpy(addr, sg[i].iov_base + offset, len);
52 if (copied < req_size) {
54 * We copied less that requested size. error out
61 static ssize_t v9fs_unpack(void *dst, struct iovec *out_sg, int out_num,
62 size_t offset, size_t size)
64 return v9fs_packunpack(dst, out_sg, out_num, offset, size, 0);
67 ssize_t v9fs_pack(struct iovec *in_sg, int in_num, size_t offset,
68 const void *src, size_t size)
70 return v9fs_packunpack((void *)src, in_sg, in_num, offset, size, 1);
73 ssize_t v9fs_iov_vunmarshal(struct iovec *out_sg, int out_num, size_t offset,
74 int bswap, const char *fmt, va_list ap)
78 size_t old_offset = offset;
80 for (i = 0; fmt[i]; i++) {
83 uint8_t *valp = va_arg(ap, uint8_t *);
84 copied = v9fs_unpack(valp, out_sg, out_num, offset, sizeof(*valp));
89 valp = va_arg(ap, uint16_t *);
90 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
92 *valp = le16_to_cpu(val);
100 valp = va_arg(ap, uint32_t *);
101 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
103 *valp = le32_to_cpu(val);
111 valp = va_arg(ap, uint64_t *);
112 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
114 *valp = le64_to_cpu(val);
121 V9fsString *str = va_arg(ap, V9fsString *);
122 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
126 str->data = g_malloc(str->size + 1);
127 copied = v9fs_unpack(str->data, out_sg, out_num, offset,
130 str->data[str->size] = 0;
132 v9fs_string_free(str);
138 V9fsQID *qidp = va_arg(ap, V9fsQID *);
139 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
140 "bdq", &qidp->type, &qidp->version,
145 V9fsStat *statp = va_arg(ap, V9fsStat *);
146 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
148 &statp->size, &statp->type,
149 &statp->dev, &statp->qid,
150 &statp->mode, &statp->atime,
151 &statp->mtime, &statp->length,
152 &statp->name, &statp->uid,
153 &statp->gid, &statp->muid,
155 &statp->n_uid, &statp->n_gid,
160 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
161 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
163 &iattr->valid, &iattr->mode,
164 &iattr->uid, &iattr->gid,
165 &iattr->size, &iattr->atime_sec,
180 return offset - old_offset;
183 ssize_t v9fs_iov_unmarshal(struct iovec *out_sg, int out_num, size_t offset,
184 int bswap, const char *fmt, ...)
190 ret = v9fs_iov_vunmarshal(out_sg, out_num, offset, bswap, fmt, ap);
196 ssize_t v9fs_iov_vmarshal(struct iovec *in_sg, int in_num, size_t offset,
197 int bswap, const char *fmt, va_list ap)
201 size_t old_offset = offset;
203 for (i = 0; fmt[i]; i++) {
206 uint8_t val = va_arg(ap, int);
207 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
213 cpu_to_le16w(&val, va_arg(ap, int));
215 val = va_arg(ap, int);
217 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
223 cpu_to_le32w(&val, va_arg(ap, uint32_t));
225 val = va_arg(ap, uint32_t);
227 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
233 cpu_to_le64w(&val, va_arg(ap, uint64_t));
235 val = va_arg(ap, uint64_t);
237 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
241 V9fsString *str = va_arg(ap, V9fsString *);
242 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
246 copied = v9fs_pack(in_sg, in_num, offset, str->data, str->size);
251 V9fsQID *qidp = va_arg(ap, V9fsQID *);
252 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap, "bdq",
253 qidp->type, qidp->version,
258 V9fsStat *statp = va_arg(ap, V9fsStat *);
259 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
261 statp->size, statp->type, statp->dev,
262 &statp->qid, statp->mode, statp->atime,
263 statp->mtime, statp->length,
265 &statp->uid, &statp->gid, &statp->muid,
266 &statp->extension, statp->n_uid,
267 statp->n_gid, statp->n_muid);
271 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
272 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
273 "qQdddqqqqqqqqqqqqqqq",
274 statp->st_result_mask,
275 &statp->qid, statp->st_mode,
276 statp->st_uid, statp->st_gid,
277 statp->st_nlink, statp->st_rdev,
278 statp->st_size, statp->st_blksize,
279 statp->st_blocks, statp->st_atime_sec,
280 statp->st_atime_nsec,
282 statp->st_mtime_nsec,
284 statp->st_ctime_nsec,
286 statp->st_btime_nsec, statp->st_gen,
287 statp->st_data_version);
299 return offset - old_offset;
302 ssize_t v9fs_iov_marshal(struct iovec *in_sg, int in_num, size_t offset,
303 int bswap, const char *fmt, ...)
309 ret = v9fs_iov_vmarshal(in_sg, in_num, offset, bswap, fmt, ap);