4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
16 #include <glib/gprintf.h>
20 #include "9p-iov-marshal.h"
21 #include "qemu/bswap.h"
23 static ssize_t
v9fs_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
24 size_t offset
, size_t size
, int pack
)
28 size_t req_size
= size
;
31 for (i
= 0; size
&& i
< sg_count
; i
++) {
33 if (offset
>= sg
[i
].iov_len
) {
35 offset
-= sg
[i
].iov_len
;
38 len
= MIN(sg
[i
].iov_len
- offset
, size
);
40 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
42 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
53 if (copied
< req_size
) {
55 * We copied less that requested size. error out
62 static ssize_t
v9fs_unpack(void *dst
, struct iovec
*out_sg
, int out_num
,
63 size_t offset
, size_t size
)
65 return v9fs_packunpack(dst
, out_sg
, out_num
, offset
, size
, 0);
68 ssize_t
v9fs_pack(struct iovec
*in_sg
, int in_num
, size_t offset
,
69 const void *src
, size_t size
)
71 return v9fs_packunpack((void *)src
, in_sg
, in_num
, offset
, size
, 1);
74 ssize_t
v9fs_iov_vunmarshal(struct iovec
*out_sg
, int out_num
, size_t offset
,
75 int bswap
, const char *fmt
, va_list ap
)
79 size_t old_offset
= offset
;
81 for (i
= 0; fmt
[i
]; i
++) {
84 uint8_t *valp
= va_arg(ap
, uint8_t *);
85 copied
= v9fs_unpack(valp
, out_sg
, out_num
, offset
, sizeof(*valp
));
90 valp
= va_arg(ap
, uint16_t *);
91 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
93 *valp
= le16_to_cpu(val
);
101 valp
= va_arg(ap
, uint32_t *);
102 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
104 *valp
= le32_to_cpu(val
);
112 valp
= va_arg(ap
, uint64_t *);
113 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
115 *valp
= le64_to_cpu(val
);
122 V9fsString
*str
= va_arg(ap
, V9fsString
*);
123 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
127 str
->data
= g_malloc(str
->size
+ 1);
128 copied
= v9fs_unpack(str
->data
, out_sg
, out_num
, offset
,
131 str
->data
[str
->size
] = 0;
133 v9fs_string_free(str
);
139 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
140 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
141 "bdq", &qidp
->type
, &qidp
->version
,
146 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
147 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
149 &statp
->size
, &statp
->type
,
150 &statp
->dev
, &statp
->qid
,
151 &statp
->mode
, &statp
->atime
,
152 &statp
->mtime
, &statp
->length
,
153 &statp
->name
, &statp
->uid
,
154 &statp
->gid
, &statp
->muid
,
156 &statp
->n_uid
, &statp
->n_gid
,
161 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
162 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
164 &iattr
->valid
, &iattr
->mode
,
165 &iattr
->uid
, &iattr
->gid
,
166 &iattr
->size
, &iattr
->atime_sec
,
181 return offset
- old_offset
;
184 ssize_t
v9fs_iov_unmarshal(struct iovec
*out_sg
, int out_num
, size_t offset
,
185 int bswap
, const char *fmt
, ...)
191 ret
= v9fs_iov_vunmarshal(out_sg
, out_num
, offset
, bswap
, fmt
, ap
);
197 ssize_t
v9fs_iov_vmarshal(struct iovec
*in_sg
, int in_num
, size_t offset
,
198 int bswap
, const char *fmt
, va_list ap
)
202 size_t old_offset
= offset
;
204 for (i
= 0; fmt
[i
]; i
++) {
207 uint8_t val
= va_arg(ap
, int);
208 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
214 cpu_to_le16w(&val
, va_arg(ap
, int));
216 val
= va_arg(ap
, int);
218 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
224 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
226 val
= va_arg(ap
, uint32_t);
228 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
234 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
236 val
= va_arg(ap
, uint64_t);
238 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
242 V9fsString
*str
= va_arg(ap
, V9fsString
*);
243 copied
= v9fs_iov_marshal(in_sg
, in_num
, offset
, bswap
,
247 copied
= v9fs_pack(in_sg
, in_num
, offset
, str
->data
, str
->size
);
252 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
253 copied
= v9fs_iov_marshal(in_sg
, in_num
, offset
, bswap
, "bdq",
254 qidp
->type
, qidp
->version
,
259 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
260 copied
= v9fs_iov_marshal(in_sg
, in_num
, offset
, bswap
,
262 statp
->size
, statp
->type
, statp
->dev
,
263 &statp
->qid
, statp
->mode
, statp
->atime
,
264 statp
->mtime
, statp
->length
,
266 &statp
->uid
, &statp
->gid
, &statp
->muid
,
267 &statp
->extension
, statp
->n_uid
,
268 statp
->n_gid
, statp
->n_muid
);
272 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
273 copied
= v9fs_iov_marshal(in_sg
, in_num
, offset
, bswap
,
274 "qQdddqqqqqqqqqqqqqqq",
275 statp
->st_result_mask
,
276 &statp
->qid
, statp
->st_mode
,
277 statp
->st_uid
, statp
->st_gid
,
278 statp
->st_nlink
, statp
->st_rdev
,
279 statp
->st_size
, statp
->st_blksize
,
280 statp
->st_blocks
, statp
->st_atime_sec
,
281 statp
->st_atime_nsec
,
283 statp
->st_mtime_nsec
,
285 statp
->st_ctime_nsec
,
287 statp
->st_btime_nsec
, statp
->st_gen
,
288 statp
->st_data_version
);
300 return offset
- old_offset
;
303 ssize_t
v9fs_iov_marshal(struct iovec
*in_sg
, int in_num
, size_t offset
,
304 int bswap
, const char *fmt
, ...)
310 ret
= v9fs_iov_vmarshal(in_sg
, in_num
, offset
, bswap
, fmt
, ap
);