vdpa-dev: Fix initialisation order to restore VDUSE compatibility
[qemu/kevin.git] / fsdev / 9p-iov-marshal.c
bloba1c9beddd2e78c193a88d20c5ce3f72dc724e839
1 /*
2 * 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
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"
15 #include <glib/gprintf.h>
16 #include <utime.h>
18 #include "9p-iov-marshal.h"
19 #include "qemu/bswap.h"
21 static ssize_t v9fs_packunpack(void *addr, struct iovec *sg, int sg_count,
22 size_t offset, size_t size, int pack)
24 int i = 0;
25 size_t copied = 0;
26 size_t req_size = size;
29 for (i = 0; size && i < sg_count; i++) {
30 size_t len;
31 if (offset >= sg[i].iov_len) {
32 /* skip this sg */
33 offset -= sg[i].iov_len;
34 continue;
35 } else {
36 len = MIN(sg[i].iov_len - offset, size);
37 if (pack) {
38 memcpy(sg[i].iov_base + offset, addr, len);
39 } else {
40 memcpy(addr, sg[i].iov_base + offset, len);
42 size -= len;
43 copied += len;
44 addr += len;
45 if (size) {
46 offset = 0;
47 continue;
51 if (copied < req_size) {
53 * We copied less that requested size. error out
55 return -ENOBUFS;
57 return copied;
60 static ssize_t v9fs_unpack(void *dst, struct iovec *out_sg, int out_num,
61 size_t offset, size_t size)
63 return v9fs_packunpack(dst, out_sg, out_num, offset, size, 0);
66 ssize_t v9fs_pack(struct iovec *in_sg, int in_num, size_t offset,
67 const void *src, size_t size)
69 return v9fs_packunpack((void *)src, in_sg, in_num, offset, size, 1);
72 ssize_t v9fs_iov_vunmarshal(struct iovec *out_sg, int out_num, size_t offset,
73 int bswap, const char *fmt, va_list ap)
75 int i;
76 ssize_t copied = 0;
77 size_t old_offset = offset;
79 for (i = 0; fmt[i]; i++) {
80 switch (fmt[i]) {
81 case 'b': {
82 uint8_t *valp = va_arg(ap, uint8_t *);
83 copied = v9fs_unpack(valp, out_sg, out_num, offset, sizeof(*valp));
84 break;
86 case 'w': {
87 uint16_t val, *valp;
88 valp = va_arg(ap, uint16_t *);
89 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
90 if (bswap) {
91 *valp = le16_to_cpu(val);
92 } else {
93 *valp = val;
95 break;
97 case 'd': {
98 uint32_t val, *valp;
99 valp = va_arg(ap, uint32_t *);
100 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
101 if (bswap) {
102 *valp = le32_to_cpu(val);
103 } else {
104 *valp = val;
106 break;
108 case 'q': {
109 uint64_t val, *valp;
110 valp = va_arg(ap, uint64_t *);
111 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
112 if (bswap) {
113 *valp = le64_to_cpu(val);
114 } else {
115 *valp = val;
117 break;
119 case 's': {
120 V9fsString *str = va_arg(ap, V9fsString *);
121 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
122 "w", &str->size);
123 if (copied > 0) {
124 offset += copied;
125 str->data = g_malloc(str->size + 1);
126 copied = v9fs_unpack(str->data, out_sg, out_num, offset,
127 str->size);
128 if (copied >= 0) {
129 str->data[str->size] = 0;
130 } else {
131 v9fs_string_free(str);
134 break;
136 case 'Q': {
137 V9fsQID *qidp = va_arg(ap, V9fsQID *);
138 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
139 "bdq", &qidp->type, &qidp->version,
140 &qidp->path);
141 break;
143 case 'S': {
144 V9fsStat *statp = va_arg(ap, V9fsStat *);
145 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
146 "wwdQdddqsssssddd",
147 &statp->size, &statp->type,
148 &statp->dev, &statp->qid,
149 &statp->mode, &statp->atime,
150 &statp->mtime, &statp->length,
151 &statp->name, &statp->uid,
152 &statp->gid, &statp->muid,
153 &statp->extension,
154 &statp->n_uid, &statp->n_gid,
155 &statp->n_muid);
156 break;
158 case 'I': {
159 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
160 copied = v9fs_iov_unmarshal(out_sg, out_num, offset, bswap,
161 "ddddqqqqq",
162 &iattr->valid, &iattr->mode,
163 &iattr->uid, &iattr->gid,
164 &iattr->size, &iattr->atime_sec,
165 &iattr->atime_nsec,
166 &iattr->mtime_sec,
167 &iattr->mtime_nsec);
168 break;
170 default:
171 g_assert_not_reached();
173 if (copied < 0) {
174 return copied;
176 offset += copied;
179 return offset - old_offset;
182 ssize_t v9fs_iov_unmarshal(struct iovec *out_sg, int out_num, size_t offset,
183 int bswap, const char *fmt, ...)
185 ssize_t ret;
186 va_list ap;
188 va_start(ap, fmt);
189 ret = v9fs_iov_vunmarshal(out_sg, out_num, offset, bswap, fmt, ap);
190 va_end(ap);
192 return ret;
195 ssize_t v9fs_iov_vmarshal(struct iovec *in_sg, int in_num, size_t offset,
196 int bswap, const char *fmt, va_list ap)
198 int i;
199 ssize_t copied = 0;
200 size_t old_offset = offset;
202 for (i = 0; fmt[i]; i++) {
203 switch (fmt[i]) {
204 case 'b': {
205 uint8_t val = va_arg(ap, int);
206 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
207 break;
209 case 'w': {
210 uint16_t val = va_arg(ap, int);
211 if (bswap) {
212 val = cpu_to_le16(val);
214 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
215 break;
217 case 'd': {
218 uint32_t val = va_arg(ap, uint32_t);
219 if (bswap) {
220 val = cpu_to_le32(val);
222 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
223 break;
225 case 'q': {
226 uint64_t val = va_arg(ap, uint64_t);
227 if (bswap) {
228 val = cpu_to_le64(val);
230 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
231 break;
233 case 's': {
234 V9fsString *str = va_arg(ap, V9fsString *);
235 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
236 "w", str->size);
237 if (copied > 0) {
238 offset += copied;
239 copied = v9fs_pack(in_sg, in_num, offset, str->data, str->size);
241 break;
243 case 'Q': {
244 V9fsQID *qidp = va_arg(ap, V9fsQID *);
245 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap, "bdq",
246 qidp->type, qidp->version,
247 qidp->path);
248 break;
250 case 'S': {
251 V9fsStat *statp = va_arg(ap, V9fsStat *);
252 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
253 "wwdQdddqsssssddd",
254 statp->size, statp->type, statp->dev,
255 &statp->qid, statp->mode, statp->atime,
256 statp->mtime, statp->length,
257 &statp->name,
258 &statp->uid, &statp->gid, &statp->muid,
259 &statp->extension, statp->n_uid,
260 statp->n_gid, statp->n_muid);
261 break;
263 case 'A': {
264 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
265 copied = v9fs_iov_marshal(in_sg, in_num, offset, bswap,
266 "qQdddqqqqqqqqqqqqqqq",
267 statp->st_result_mask,
268 &statp->qid, statp->st_mode,
269 statp->st_uid, statp->st_gid,
270 statp->st_nlink, statp->st_rdev,
271 statp->st_size, statp->st_blksize,
272 statp->st_blocks, statp->st_atime_sec,
273 statp->st_atime_nsec,
274 statp->st_mtime_sec,
275 statp->st_mtime_nsec,
276 statp->st_ctime_sec,
277 statp->st_ctime_nsec,
278 statp->st_btime_sec,
279 statp->st_btime_nsec, statp->st_gen,
280 statp->st_data_version);
281 break;
283 default:
284 g_assert_not_reached();
286 if (copied < 0) {
287 return copied;
289 offset += copied;
292 return offset - old_offset;
295 ssize_t v9fs_iov_marshal(struct iovec *in_sg, int in_num, size_t offset,
296 int bswap, const char *fmt, ...)
298 ssize_t ret;
299 va_list ap;
301 va_start(ap, fmt);
302 ret = v9fs_iov_vmarshal(in_sg, in_num, offset, bswap, fmt, ap);
303 va_end(ap);
305 return ret;