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"
15 #include <glib/gprintf.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
)
26 size_t req_size
= size
;
29 for (i
= 0; size
&& i
< sg_count
; i
++) {
31 if (offset
>= sg
[i
].iov_len
) {
33 offset
-= sg
[i
].iov_len
;
36 len
= MIN(sg
[i
].iov_len
- offset
, size
);
38 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
40 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
51 if (copied
< req_size
) {
53 * We copied less that requested size. error out
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
)
77 size_t old_offset
= offset
;
79 for (i
= 0; fmt
[i
]; i
++) {
82 uint8_t *valp
= va_arg(ap
, uint8_t *);
83 copied
= v9fs_unpack(valp
, out_sg
, out_num
, offset
, sizeof(*valp
));
88 valp
= va_arg(ap
, uint16_t *);
89 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
91 *valp
= le16_to_cpu(val
);
99 valp
= va_arg(ap
, uint32_t *);
100 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
102 *valp
= le32_to_cpu(val
);
110 valp
= va_arg(ap
, uint64_t *);
111 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
113 *valp
= le64_to_cpu(val
);
120 V9fsString
*str
= va_arg(ap
, V9fsString
*);
121 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
125 str
->data
= g_malloc(str
->size
+ 1);
126 copied
= v9fs_unpack(str
->data
, out_sg
, out_num
, offset
,
129 str
->data
[str
->size
] = 0;
131 v9fs_string_free(str
);
137 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
138 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
139 "bdq", &qidp
->type
, &qidp
->version
,
144 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
145 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
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
,
154 &statp
->n_uid
, &statp
->n_gid
,
159 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
160 copied
= v9fs_iov_unmarshal(out_sg
, out_num
, offset
, bswap
,
162 &iattr
->valid
, &iattr
->mode
,
163 &iattr
->uid
, &iattr
->gid
,
164 &iattr
->size
, &iattr
->atime_sec
,
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
, ...)
189 ret
= v9fs_iov_vunmarshal(out_sg
, out_num
, offset
, bswap
, fmt
, ap
);
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
)
200 size_t old_offset
= offset
;
202 for (i
= 0; fmt
[i
]; i
++) {
205 uint8_t val
= va_arg(ap
, int);
206 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
210 uint16_t val
= va_arg(ap
, int);
212 val
= cpu_to_le16(val
);
214 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
218 uint32_t val
= va_arg(ap
, uint32_t);
220 val
= cpu_to_le32(val
);
222 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
226 uint64_t val
= va_arg(ap
, uint64_t);
228 val
= cpu_to_le64(val
);
230 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
234 V9fsString
*str
= va_arg(ap
, V9fsString
*);
235 copied
= v9fs_iov_marshal(in_sg
, in_num
, offset
, bswap
,
239 copied
= v9fs_pack(in_sg
, in_num
, offset
, str
->data
, str
->size
);
244 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
245 copied
= v9fs_iov_marshal(in_sg
, in_num
, offset
, bswap
, "bdq",
246 qidp
->type
, qidp
->version
,
251 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
252 copied
= v9fs_iov_marshal(in_sg
, in_num
, offset
, bswap
,
254 statp
->size
, statp
->type
, statp
->dev
,
255 &statp
->qid
, statp
->mode
, statp
->atime
,
256 statp
->mtime
, statp
->length
,
258 &statp
->uid
, &statp
->gid
, &statp
->muid
,
259 &statp
->extension
, statp
->n_uid
,
260 statp
->n_gid
, statp
->n_muid
);
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
,
275 statp
->st_mtime_nsec
,
277 statp
->st_ctime_nsec
,
279 statp
->st_btime_nsec
, statp
->st_gen
,
280 statp
->st_data_version
);
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
, ...)
302 ret
= v9fs_iov_vmarshal(in_sg
, in_num
, offset
, bswap
, fmt
, ap
);