blob: 20f308b76025d0f62d0143a5614f4b3797e073d6 [file] [log] [blame]
M. Mohan Kumar10925bf2011-12-14 13:49:06 +05301/*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#include <glib.h>
15#include <glib/gprintf.h>
16#include <sys/types.h>
17#include <dirent.h>
18#include <sys/time.h>
19#include <utime.h>
20#include <sys/uio.h>
21#include <string.h>
22#include <stdint.h>
M. Mohan Kumarddca7f82011-12-14 13:49:13 +053023#include <errno.h>
M. Mohan Kumar10925bf2011-12-14 13:49:06 +053024
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/compiler.h"
M. Mohan Kumar10925bf2011-12-14 13:49:06 +053026#include "virtio-9p-marshal.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/bswap.h"
M. Mohan Kumar10925bf2011-12-14 13:49:06 +053028
M. Mohan Kumar10925bf2011-12-14 13:49:06 +053029void v9fs_string_free(V9fsString *str)
30{
31 g_free(str->data);
32 str->data = NULL;
33 str->size = 0;
34}
35
36void v9fs_string_null(V9fsString *str)
37{
38 v9fs_string_free(str);
39}
40
41void GCC_FMT_ATTR(2, 3)
42v9fs_string_sprintf(V9fsString *str, const char *fmt, ...)
43{
44 va_list ap;
45
46 v9fs_string_free(str);
47
48 va_start(ap, fmt);
49 str->size = g_vasprintf(&str->data, fmt, ap);
50 va_end(ap);
51}
52
53void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs)
54{
55 v9fs_string_free(lhs);
56 v9fs_string_sprintf(lhs, "%s", rhs->data);
57}
58
59
M. Mohan Kumarddca7f82011-12-14 13:49:13 +053060static ssize_t v9fs_packunpack(void *addr, struct iovec *sg, int sg_count,
61 size_t offset, size_t size, int pack)
M. Mohan Kumar10925bf2011-12-14 13:49:06 +053062{
63 int i = 0;
64 size_t copied = 0;
M. Mohan Kumarddca7f82011-12-14 13:49:13 +053065 size_t req_size = size;
66
M. Mohan Kumar10925bf2011-12-14 13:49:06 +053067
68 for (i = 0; size && i < sg_count; i++) {
69 size_t len;
70 if (offset >= sg[i].iov_len) {
71 /* skip this sg */
72 offset -= sg[i].iov_len;
73 continue;
74 } else {
75 len = MIN(sg[i].iov_len - offset, size);
76 if (pack) {
77 memcpy(sg[i].iov_base + offset, addr, len);
78 } else {
79 memcpy(addr, sg[i].iov_base + offset, len);
80 }
81 size -= len;
82 copied += len;
83 addr += len;
84 if (size) {
85 offset = 0;
86 continue;
87 }
88 }
89 }
M. Mohan Kumarddca7f82011-12-14 13:49:13 +053090 if (copied < req_size) {
91 /*
92 * We copied less that requested size. error out
93 */
94 return -ENOBUFS;
95 }
M. Mohan Kumar10925bf2011-12-14 13:49:06 +053096 return copied;
97}
98
M. Mohan Kumarddca7f82011-12-14 13:49:13 +053099static ssize_t v9fs_unpack(void *dst, struct iovec *out_sg, int out_num,
100 size_t offset, size_t size)
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530101{
102 return v9fs_packunpack(dst, out_sg, out_num, offset, size, 0);
103}
104
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530105ssize_t v9fs_pack(struct iovec *in_sg, int in_num, size_t offset,
106 const void *src, size_t size)
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530107{
108 return v9fs_packunpack((void *)src, in_sg, in_num, offset, size, 1);
109}
110
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530111ssize_t v9fs_unmarshal(struct iovec *out_sg, int out_num, size_t offset,
112 int bswap, const char *fmt, ...)
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530113{
114 int i;
115 va_list ap;
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530116 ssize_t copied = 0;
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530117 size_t old_offset = offset;
118
119 va_start(ap, fmt);
120 for (i = 0; fmt[i]; i++) {
121 switch (fmt[i]) {
122 case 'b': {
123 uint8_t *valp = va_arg(ap, uint8_t *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530124 copied = v9fs_unpack(valp, out_sg, out_num, offset, sizeof(*valp));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530125 break;
126 }
127 case 'w': {
128 uint16_t val, *valp;
129 valp = va_arg(ap, uint16_t *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530130 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530131 if (bswap) {
132 *valp = le16_to_cpu(val);
133 } else {
134 *valp = val;
135 }
136 break;
137 }
138 case 'd': {
139 uint32_t val, *valp;
140 valp = va_arg(ap, uint32_t *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530141 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530142 if (bswap) {
143 *valp = le32_to_cpu(val);
144 } else {
145 *valp = val;
146 }
147 break;
148 }
149 case 'q': {
150 uint64_t val, *valp;
151 valp = va_arg(ap, uint64_t *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530152 copied = v9fs_unpack(&val, out_sg, out_num, offset, sizeof(val));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530153 if (bswap) {
154 *valp = le64_to_cpu(val);
155 } else {
156 *valp = val;
157 }
158 break;
159 }
160 case 's': {
161 V9fsString *str = va_arg(ap, V9fsString *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530162 copied = v9fs_unmarshal(out_sg, out_num, offset, bswap,
163 "w", &str->size);
164 if (copied > 0) {
165 offset += copied;
166 str->data = g_malloc(str->size + 1);
167 copied = v9fs_unpack(str->data, out_sg, out_num, offset,
168 str->size);
169 if (copied > 0) {
170 str->data[str->size] = 0;
171 } else {
172 v9fs_string_free(str);
173 }
174 }
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530175 break;
176 }
177 case 'Q': {
178 V9fsQID *qidp = va_arg(ap, V9fsQID *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530179 copied = v9fs_unmarshal(out_sg, out_num, offset, bswap, "bdq",
180 &qidp->type, &qidp->version, &qidp->path);
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530181 break;
182 }
183 case 'S': {
184 V9fsStat *statp = va_arg(ap, V9fsStat *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530185 copied = v9fs_unmarshal(out_sg, out_num, offset, bswap,
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530186 "wwdQdddqsssssddd",
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530187 &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,
193 &statp->n_muid);
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530194 break;
195 }
196 case 'I': {
197 V9fsIattr *iattr = va_arg(ap, V9fsIattr *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530198 copied = v9fs_unmarshal(out_sg, out_num, offset, bswap,
199 "ddddqqqqq",
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);
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530204 break;
205 }
206 default:
207 break;
208 }
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530209 if (copied < 0) {
210 va_end(ap);
211 return copied;
212 }
213 offset += copied;
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530214 }
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530215 va_end(ap);
216
217 return offset - old_offset;
218}
219
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530220ssize_t v9fs_marshal(struct iovec *in_sg, int in_num, size_t offset,
221 int bswap, const char *fmt, ...)
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530222{
223 int i;
224 va_list ap;
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530225 ssize_t copied = 0;
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530226 size_t old_offset = offset;
227
228 va_start(ap, fmt);
229 for (i = 0; fmt[i]; i++) {
230 switch (fmt[i]) {
231 case 'b': {
232 uint8_t val = va_arg(ap, int);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530233 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530234 break;
235 }
236 case 'w': {
237 uint16_t val;
238 if (bswap) {
239 cpu_to_le16w(&val, va_arg(ap, int));
240 } else {
241 val = va_arg(ap, int);
242 }
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530243 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530244 break;
245 }
246 case 'd': {
247 uint32_t val;
248 if (bswap) {
249 cpu_to_le32w(&val, va_arg(ap, uint32_t));
250 } else {
251 val = va_arg(ap, uint32_t);
252 }
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530253 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530254 break;
255 }
256 case 'q': {
257 uint64_t val;
258 if (bswap) {
259 cpu_to_le64w(&val, va_arg(ap, uint64_t));
260 } else {
261 val = va_arg(ap, uint64_t);
262 }
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530263 copied = v9fs_pack(in_sg, in_num, offset, &val, sizeof(val));
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530264 break;
265 }
266 case 's': {
267 V9fsString *str = va_arg(ap, V9fsString *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530268 copied = v9fs_marshal(in_sg, in_num, offset, bswap,
269 "w", str->size);
270 if (copied > 0) {
271 offset += copied;
272 copied = v9fs_pack(in_sg, in_num, offset, str->data, str->size);
273 }
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530274 break;
275 }
276 case 'Q': {
277 V9fsQID *qidp = va_arg(ap, V9fsQID *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530278 copied = v9fs_marshal(in_sg, in_num, offset, bswap, "bdq",
279 qidp->type, qidp->version, qidp->path);
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530280 break;
281 }
282 case 'S': {
283 V9fsStat *statp = va_arg(ap, V9fsStat *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530284 copied = v9fs_marshal(in_sg, in_num, offset, bswap,
285 "wwdQdddqsssssddd",
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);
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530292 break;
293 }
294 case 'A': {
295 V9fsStatDotl *statp = va_arg(ap, V9fsStatDotl *);
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530296 copied = v9fs_marshal(in_sg, in_num, offset, bswap,
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530297 "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);
309 break;
310 }
311 default:
312 break;
313 }
M. Mohan Kumarddca7f82011-12-14 13:49:13 +0530314 if (copied < 0) {
315 va_end(ap);
316 return copied;
317 }
318 offset += copied;
M. Mohan Kumar10925bf2011-12-14 13:49:06 +0530319 }
320 va_end(ap);
321
322 return offset - old_offset;
323}