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.
15 #include <glib/gprintf.h>
16 #include <sys/types.h>
26 #include "virtio-9p-marshal.h"
29 void v9fs_string_free(V9fsString
*str
)
36 void v9fs_string_null(V9fsString
*str
)
38 v9fs_string_free(str
);
41 void GCC_FMT_ATTR(2, 3)
42 v9fs_string_sprintf(V9fsString
*str
, const char *fmt
, ...)
46 v9fs_string_free(str
);
49 str
->size
= g_vasprintf(&str
->data
, fmt
, ap
);
53 void v9fs_string_copy(V9fsString
*lhs
, V9fsString
*rhs
)
55 v9fs_string_free(lhs
);
56 v9fs_string_sprintf(lhs
, "%s", rhs
->data
);
60 static ssize_t
v9fs_packunpack(void *addr
, struct iovec
*sg
, int sg_count
,
61 size_t offset
, size_t size
, int pack
)
65 size_t req_size
= size
;
68 for (i
= 0; size
&& i
< sg_count
; i
++) {
70 if (offset
>= sg
[i
].iov_len
) {
72 offset
-= sg
[i
].iov_len
;
75 len
= MIN(sg
[i
].iov_len
- offset
, size
);
77 memcpy(sg
[i
].iov_base
+ offset
, addr
, len
);
79 memcpy(addr
, sg
[i
].iov_base
+ offset
, len
);
90 if (copied
< req_size
) {
92 * We copied less that requested size. error out
99 static ssize_t
v9fs_unpack(void *dst
, struct iovec
*out_sg
, int out_num
,
100 size_t offset
, size_t size
)
102 return v9fs_packunpack(dst
, out_sg
, out_num
, offset
, size
, 0);
105 ssize_t
v9fs_pack(struct iovec
*in_sg
, int in_num
, size_t offset
,
106 const void *src
, size_t size
)
108 return v9fs_packunpack((void *)src
, in_sg
, in_num
, offset
, size
, 1);
111 ssize_t
v9fs_unmarshal(struct iovec
*out_sg
, int out_num
, size_t offset
,
112 int bswap
, const char *fmt
, ...)
117 size_t old_offset
= offset
;
120 for (i
= 0; fmt
[i
]; i
++) {
123 uint8_t *valp
= va_arg(ap
, uint8_t *);
124 copied
= v9fs_unpack(valp
, out_sg
, out_num
, offset
, sizeof(*valp
));
129 valp
= va_arg(ap
, uint16_t *);
130 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
132 *valp
= le16_to_cpu(val
);
140 valp
= va_arg(ap
, uint32_t *);
141 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
143 *valp
= le32_to_cpu(val
);
151 valp
= va_arg(ap
, uint64_t *);
152 copied
= v9fs_unpack(&val
, out_sg
, out_num
, offset
, sizeof(val
));
154 *valp
= le64_to_cpu(val
);
161 V9fsString
*str
= va_arg(ap
, V9fsString
*);
162 copied
= v9fs_unmarshal(out_sg
, out_num
, offset
, bswap
,
166 str
->data
= g_malloc(str
->size
+ 1);
167 copied
= v9fs_unpack(str
->data
, out_sg
, out_num
, offset
,
170 str
->data
[str
->size
] = 0;
172 v9fs_string_free(str
);
178 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
179 copied
= v9fs_unmarshal(out_sg
, out_num
, offset
, bswap
, "bdq",
180 &qidp
->type
, &qidp
->version
, &qidp
->path
);
184 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
185 copied
= v9fs_unmarshal(out_sg
, out_num
, offset
, bswap
,
187 &statp
->size
, &statp
->type
, &statp
->dev
,
188 &statp
->qid
, &statp
->mode
, &statp
->atime
,
189 &statp
->mtime
, &statp
->length
,
190 &statp
->name
, &statp
->uid
, &statp
->gid
,
191 &statp
->muid
, &statp
->extension
,
192 &statp
->n_uid
, &statp
->n_gid
,
197 V9fsIattr
*iattr
= va_arg(ap
, V9fsIattr
*);
198 copied
= v9fs_unmarshal(out_sg
, out_num
, offset
, bswap
,
200 &iattr
->valid
, &iattr
->mode
,
201 &iattr
->uid
, &iattr
->gid
, &iattr
->size
,
202 &iattr
->atime_sec
, &iattr
->atime_nsec
,
203 &iattr
->mtime_sec
, &iattr
->mtime_nsec
);
217 return offset
- old_offset
;
220 ssize_t
v9fs_marshal(struct iovec
*in_sg
, int in_num
, size_t offset
,
221 int bswap
, const char *fmt
, ...)
226 size_t old_offset
= offset
;
229 for (i
= 0; fmt
[i
]; i
++) {
232 uint8_t val
= va_arg(ap
, int);
233 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
239 cpu_to_le16w(&val
, va_arg(ap
, int));
241 val
= va_arg(ap
, int);
243 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
249 cpu_to_le32w(&val
, va_arg(ap
, uint32_t));
251 val
= va_arg(ap
, uint32_t);
253 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
259 cpu_to_le64w(&val
, va_arg(ap
, uint64_t));
261 val
= va_arg(ap
, uint64_t);
263 copied
= v9fs_pack(in_sg
, in_num
, offset
, &val
, sizeof(val
));
267 V9fsString
*str
= va_arg(ap
, V9fsString
*);
268 copied
= v9fs_marshal(in_sg
, in_num
, offset
, bswap
,
272 copied
= v9fs_pack(in_sg
, in_num
, offset
, str
->data
, str
->size
);
277 V9fsQID
*qidp
= va_arg(ap
, V9fsQID
*);
278 copied
= v9fs_marshal(in_sg
, in_num
, offset
, bswap
, "bdq",
279 qidp
->type
, qidp
->version
, qidp
->path
);
283 V9fsStat
*statp
= va_arg(ap
, V9fsStat
*);
284 copied
= v9fs_marshal(in_sg
, in_num
, offset
, bswap
,
286 statp
->size
, statp
->type
, statp
->dev
,
287 &statp
->qid
, statp
->mode
, statp
->atime
,
288 statp
->mtime
, statp
->length
, &statp
->name
,
289 &statp
->uid
, &statp
->gid
, &statp
->muid
,
290 &statp
->extension
, statp
->n_uid
,
291 statp
->n_gid
, statp
->n_muid
);
295 V9fsStatDotl
*statp
= va_arg(ap
, V9fsStatDotl
*);
296 copied
= v9fs_marshal(in_sg
, in_num
, offset
, bswap
,
297 "qQdddqqqqqqqqqqqqqqq",
298 statp
->st_result_mask
,
299 &statp
->qid
, statp
->st_mode
,
300 statp
->st_uid
, statp
->st_gid
,
301 statp
->st_nlink
, statp
->st_rdev
,
302 statp
->st_size
, statp
->st_blksize
,
303 statp
->st_blocks
, statp
->st_atime_sec
,
304 statp
->st_atime_nsec
, statp
->st_mtime_sec
,
305 statp
->st_mtime_nsec
, statp
->st_ctime_sec
,
306 statp
->st_ctime_nsec
, statp
->st_btime_sec
,
307 statp
->st_btime_nsec
, statp
->st_gen
,
308 statp
->st_data_version
);
322 return offset
- old_offset
;