Add linux-next specific files for 20110831
[linux-2.6/next.git] / tools / kvm / virtio / 9p-pdu.c
blobb9ce8ce60f2f24a8bc410c72d0f276f4859087df
1 #include "kvm/util.h"
2 #include "kvm/virtio-9p.h"
4 #include <endian.h>
5 #include <stdint.h>
7 #include <linux/compiler.h>
8 #include <net/9p/9p.h>
10 static void virtio_p9_pdu_read(struct p9_pdu *pdu, void *data, size_t size)
12 size_t len;
13 int i, copied = 0;
14 u16 iov_cnt = pdu->out_iov_cnt;
15 size_t offset = pdu->read_offset;
16 struct iovec *iov = pdu->out_iov;
18 for (i = 0; i < iov_cnt && size; i++) {
19 if (offset >= iov[i].iov_len) {
20 offset -= iov[i].iov_len;
21 continue;
22 } else {
23 len = MIN(iov[i].iov_len - offset, size);
24 memcpy(data, iov[i].iov_base + offset, len);
25 size -= len;
26 data += len;
27 offset = 0;
28 copied += len;
31 pdu->read_offset += copied;
34 static void virtio_p9_pdu_write(struct p9_pdu *pdu,
35 const void *data, size_t size)
37 size_t len;
38 int i, copied = 0;
39 u16 iov_cnt = pdu->in_iov_cnt;
40 size_t offset = pdu->write_offset;
41 struct iovec *iov = pdu->in_iov;
43 for (i = 0; i < iov_cnt && size; i++) {
44 if (offset >= iov[i].iov_len) {
45 offset -= iov[i].iov_len;
46 continue;
47 } else {
48 len = MIN(iov[i].iov_len - offset, size);
49 memcpy(iov[i].iov_base + offset, data, len);
50 size -= len;
51 data += len;
52 offset = 0;
53 copied += len;
56 pdu->write_offset += copied;
59 static void virtio_p9_wstat_free(struct p9_wstat *stbuf)
61 free(stbuf->name);
62 free(stbuf->uid);
63 free(stbuf->gid);
64 free(stbuf->muid);
67 static int virtio_p9_decode(struct p9_pdu *pdu, const char *fmt, va_list ap)
69 int retval = 0;
70 const char *ptr;
72 for (ptr = fmt; *ptr; ptr++) {
73 switch (*ptr) {
74 case 'b':
76 int8_t *val = va_arg(ap, int8_t *);
77 virtio_p9_pdu_read(pdu, val, sizeof(*val));
79 break;
80 case 'w':
82 int16_t le_val;
83 int16_t *val = va_arg(ap, int16_t *);
84 virtio_p9_pdu_read(pdu, &le_val, sizeof(le_val));
85 *val = le16toh(le_val);
87 break;
88 case 'd':
90 int32_t le_val;
91 int32_t *val = va_arg(ap, int32_t *);
92 virtio_p9_pdu_read(pdu, &le_val, sizeof(le_val));
93 *val = le32toh(le_val);
95 break;
96 case 'q':
98 int64_t le_val;
99 int64_t *val = va_arg(ap, int64_t *);
100 virtio_p9_pdu_read(pdu, &le_val, sizeof(le_val));
101 *val = le64toh(le_val);
103 break;
104 case 's':
106 int16_t len;
107 char **str = va_arg(ap, char **);
109 virtio_p9_pdu_readf(pdu, "w", &len);
110 *str = malloc(len + 1);
111 if (*str == NULL) {
112 retval = ENOMEM;
113 break;
115 virtio_p9_pdu_read(pdu, *str, len);
116 (*str)[len] = 0;
118 break;
119 case 'Q':
121 struct p9_qid *qid = va_arg(ap, struct p9_qid *);
122 retval = virtio_p9_pdu_readf(pdu, "bdq",
123 &qid->type, &qid->version,
124 &qid->path);
126 break;
127 case 'S':
129 struct p9_wstat *stbuf = va_arg(ap, struct p9_wstat *);
130 memset(stbuf, 0, sizeof(struct p9_wstat));
131 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid = -1;
132 retval = virtio_p9_pdu_readf(pdu, "wwdQdddqssss",
133 &stbuf->size, &stbuf->type,
134 &stbuf->dev, &stbuf->qid,
135 &stbuf->mode, &stbuf->atime,
136 &stbuf->mtime, &stbuf->length,
137 &stbuf->name, &stbuf->uid,
138 &stbuf->gid, &stbuf->muid);
139 if (retval)
140 virtio_p9_wstat_free(stbuf);
142 break;
143 case 'I':
145 struct p9_iattr_dotl *p9attr = va_arg(ap,
146 struct p9_iattr_dotl *);
148 retval = virtio_p9_pdu_readf(pdu, "ddddqqqqq",
149 &p9attr->valid,
150 &p9attr->mode,
151 &p9attr->uid,
152 &p9attr->gid,
153 &p9attr->size,
154 &p9attr->atime_sec,
155 &p9attr->atime_nsec,
156 &p9attr->mtime_sec,
157 &p9attr->mtime_nsec);
159 break;
160 default:
161 retval = EINVAL;
162 break;
165 return retval;
168 static int virtio_p9_pdu_encode(struct p9_pdu *pdu, const char *fmt, va_list ap)
170 int retval = 0;
171 const char *ptr;
173 for (ptr = fmt; *ptr; ptr++) {
174 switch (*ptr) {
175 case 'b':
177 int8_t val = va_arg(ap, int);
178 virtio_p9_pdu_write(pdu, &val, sizeof(val));
180 break;
181 case 'w':
183 int16_t val = htole16(va_arg(ap, int));
184 virtio_p9_pdu_write(pdu, &val, sizeof(val));
186 break;
187 case 'd':
189 int32_t val = htole32(va_arg(ap, int32_t));
190 virtio_p9_pdu_write(pdu, &val, sizeof(val));
192 break;
193 case 'q':
195 int64_t val = htole64(va_arg(ap, int64_t));
196 virtio_p9_pdu_write(pdu, &val, sizeof(val));
198 break;
199 case 's':
201 uint16_t len = 0;
202 const char *s = va_arg(ap, char *);
203 if (s)
204 len = MIN(strlen(s), USHRT_MAX);
205 virtio_p9_pdu_writef(pdu, "w", len);
206 virtio_p9_pdu_write(pdu, s, len);
208 break;
209 case 'Q':
211 struct p9_qid *qid = va_arg(ap, struct p9_qid *);
212 retval = virtio_p9_pdu_writef(pdu, "bdq",
213 qid->type, qid->version,
214 qid->path);
216 break;
217 case 'S':
219 struct p9_wstat *stbuf = va_arg(ap, struct p9_wstat *);
220 retval = virtio_p9_pdu_writef(pdu, "wwdQdddqssss",
221 stbuf->size, stbuf->type,
222 stbuf->dev, &stbuf->qid,
223 stbuf->mode, stbuf->atime,
224 stbuf->mtime, stbuf->length,
225 stbuf->name, stbuf->uid,
226 stbuf->gid, stbuf->muid);
228 break;
229 case 'A':
231 struct p9_stat_dotl *stbuf = va_arg(ap,
232 struct p9_stat_dotl *);
233 retval = virtio_p9_pdu_writef(pdu,
234 "qQdddqqqqqqqqqqqqqqq",
235 stbuf->st_result_mask,
236 &stbuf->qid,
237 stbuf->st_mode,
238 stbuf->st_uid,
239 stbuf->st_gid,
240 stbuf->st_nlink,
241 stbuf->st_rdev,
242 stbuf->st_size,
243 stbuf->st_blksize,
244 stbuf->st_blocks,
245 stbuf->st_atime_sec,
246 stbuf->st_atime_nsec,
247 stbuf->st_mtime_sec,
248 stbuf->st_mtime_nsec,
249 stbuf->st_ctime_sec,
250 stbuf->st_ctime_nsec,
251 stbuf->st_btime_sec,
252 stbuf->st_btime_nsec,
253 stbuf->st_gen,
254 stbuf->st_data_version);
256 break;
257 default:
258 retval = EINVAL;
259 break;
262 return retval;
265 int virtio_p9_pdu_readf(struct p9_pdu *pdu, const char *fmt, ...)
267 int ret;
268 va_list ap;
270 va_start(ap, fmt);
271 ret = virtio_p9_decode(pdu, fmt, ap);
272 va_end(ap);
274 return ret;
277 int virtio_p9_pdu_writef(struct p9_pdu *pdu, const char *fmt, ...)
279 int ret;
280 va_list ap;
282 va_start(ap, fmt);
283 ret = virtio_p9_pdu_encode(pdu, fmt, ap);
284 va_end(ap);
286 return ret;