blob: 1b9b15dfeaf01ed078ee2b16fe796ae028336c46 [file] [log] [blame]
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -07001/*
2 * linux/fs/9p/conv.c
3 *
4 * 9P protocol conversion functions
5 *
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -07006 * Copyright (C) 2004, 2005 by Latchesar Ionkov <lucho@ionkov.net>
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -07007 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to:
22 * Free Software Foundation
23 * 51 Franklin Street, Fifth Floor
24 * Boston, MA 02111-1301 USA
25 *
26 */
27
28#include <linux/config.h>
29#include <linux/module.h>
30#include <linux/errno.h>
31#include <linux/fs.h>
32#include <linux/idr.h>
33
34#include "debug.h"
35#include "v9fs.h"
36#include "9p.h"
37#include "conv.h"
38
39/*
40 * Buffer to help with string parsing
41 */
42struct cbuf {
43 unsigned char *sp;
44 unsigned char *p;
45 unsigned char *ep;
46};
47
48static inline void buf_init(struct cbuf *buf, void *data, int datalen)
49{
50 buf->sp = buf->p = data;
51 buf->ep = data + datalen;
52}
53
54static inline int buf_check_overflow(struct cbuf *buf)
55{
56 return buf->p > buf->ep;
57}
58
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -070059static inline int buf_check_size(struct cbuf *buf, int len)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070060{
61 if (buf->p+len > buf->ep) {
62 if (buf->p < buf->ep) {
63 eprintk(KERN_ERR, "buffer overflow\n");
64 buf->p = buf->ep + 1;
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -070065 return 0;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070066 }
67 }
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -070068
69 return 1;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070070}
71
72static inline void *buf_alloc(struct cbuf *buf, int len)
73{
74 void *ret = NULL;
75
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -070076 if (buf_check_size(buf, len)) {
77 ret = buf->p;
78 buf->p += len;
79 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070080
81 return ret;
82}
83
84static inline void buf_put_int8(struct cbuf *buf, u8 val)
85{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -070086 if (buf_check_size(buf, 1)) {
87 buf->p[0] = val;
88 buf->p++;
89 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070090}
91
92static inline void buf_put_int16(struct cbuf *buf, u16 val)
93{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -070094 if (buf_check_size(buf, 2)) {
95 *(__le16 *) buf->p = cpu_to_le16(val);
96 buf->p += 2;
97 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -070098}
99
100static inline void buf_put_int32(struct cbuf *buf, u32 val)
101{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700102 if (buf_check_size(buf, 4)) {
103 *(__le32 *)buf->p = cpu_to_le32(val);
104 buf->p += 4;
105 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700106}
107
108static inline void buf_put_int64(struct cbuf *buf, u64 val)
109{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700110 if (buf_check_size(buf, 8)) {
111 *(__le64 *)buf->p = cpu_to_le64(val);
112 buf->p += 8;
113 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700114}
115
116static inline void buf_put_stringn(struct cbuf *buf, const char *s, u16 slen)
117{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700118 if (buf_check_size(buf, slen + 2)) {
119 buf_put_int16(buf, slen);
120 memcpy(buf->p, s, slen);
121 buf->p += slen;
122 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700123}
124
125static inline void buf_put_string(struct cbuf *buf, const char *s)
126{
127 buf_put_stringn(buf, s, strlen(s));
128}
129
130static inline void buf_put_data(struct cbuf *buf, void *data, u32 datalen)
131{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700132 if (buf_check_size(buf, datalen)) {
133 memcpy(buf->p, data, datalen);
134 buf->p += datalen;
135 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700136}
137
138static inline u8 buf_get_int8(struct cbuf *buf)
139{
140 u8 ret = 0;
141
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700142 if (buf_check_size(buf, 1)) {
143 ret = buf->p[0];
144 buf->p++;
145 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700146
147 return ret;
148}
149
150static inline u16 buf_get_int16(struct cbuf *buf)
151{
152 u16 ret = 0;
153
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700154 if (buf_check_size(buf, 2)) {
155 ret = le16_to_cpu(*(__le16 *)buf->p);
156 buf->p += 2;
157 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700158
159 return ret;
160}
161
162static inline u32 buf_get_int32(struct cbuf *buf)
163{
164 u32 ret = 0;
165
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700166 if (buf_check_size(buf, 4)) {
167 ret = le32_to_cpu(*(__le32 *)buf->p);
168 buf->p += 4;
169 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700170
171 return ret;
172}
173
174static inline u64 buf_get_int64(struct cbuf *buf)
175{
176 u64 ret = 0;
177
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700178 if (buf_check_size(buf, 8)) {
179 ret = le64_to_cpu(*(__le64 *)buf->p);
180 buf->p += 8;
181 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700182
183 return ret;
184}
185
186static inline int
187buf_get_string(struct cbuf *buf, char *data, unsigned int datalen)
188{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700189 u16 len = 0;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700190
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700191 len = buf_get_int16(buf);
192 if (!buf_check_overflow(buf) && buf_check_size(buf, len) && len+1>datalen) {
193 memcpy(data, buf->p, len);
194 data[len] = 0;
195 buf->p += len;
196 len++;
197 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700198
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700199 return len;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700200}
201
202static inline char *buf_get_stringb(struct cbuf *buf, struct cbuf *sbuf)
203{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700204 char *ret;
205 u16 len;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700206
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700207 ret = NULL;
208 len = buf_get_int16(buf);
209
210 if (!buf_check_overflow(buf) && buf_check_size(buf, len) &&
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800211 buf_check_size(sbuf, len + 1)) {
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700212
213 memcpy(sbuf->p, buf->p, len);
214 sbuf->p[len] = 0;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700215 ret = sbuf->p;
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700216 buf->p += len;
217 sbuf->p += len + 1;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700218 }
219
220 return ret;
221}
222
223static inline int buf_get_data(struct cbuf *buf, void *data, int datalen)
224{
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700225 int ret = 0;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700226
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700227 if (buf_check_size(buf, datalen)) {
228 memcpy(data, buf->p, datalen);
229 buf->p += datalen;
230 ret = datalen;
231 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700232
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700233 return ret;
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700234}
235
236static inline void *buf_get_datab(struct cbuf *buf, struct cbuf *dbuf,
237 int datalen)
238{
239 char *ret = NULL;
240 int n = 0;
241
Latchesar Ionkovd06a8fb2005-09-22 21:43:48 -0700242 if (buf_check_size(dbuf, datalen)) {
243 n = buf_get_data(buf, dbuf->p, datalen);
244 if (n > 0) {
245 ret = dbuf->p;
246 dbuf->p += n;
247 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700248 }
249
250 return ret;
251}
252
253/**
254 * v9fs_size_stat - calculate the size of a variable length stat struct
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700255 * @stat: metadata (stat) structure
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800256 * @extended: non-zero if 9P2000.u
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700257 *
258 */
259
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800260static int v9fs_size_stat(struct v9fs_stat *stat, int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700261{
262 int size = 0;
263
264 if (stat == NULL) {
265 eprintk(KERN_ERR, "v9fs_size_stat: got a NULL stat pointer\n");
266 return 0;
267 }
268
269 size = /* 2 + *//* size[2] */
270 2 + /* type[2] */
271 4 + /* dev[4] */
272 1 + /* qid.type[1] */
273 4 + /* qid.vers[4] */
274 8 + /* qid.path[8] */
275 4 + /* mode[4] */
276 4 + /* atime[4] */
277 4 + /* mtime[4] */
278 8 + /* length[8] */
279 8; /* minimum sum of string lengths */
280
281 if (stat->name)
282 size += strlen(stat->name);
283 if (stat->uid)
284 size += strlen(stat->uid);
285 if (stat->gid)
286 size += strlen(stat->gid);
287 if (stat->muid)
288 size += strlen(stat->muid);
289
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800290 if (extended) {
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700291 size += 4 + /* n_uid[4] */
292 4 + /* n_gid[4] */
293 4 + /* n_muid[4] */
294 2; /* string length of extension[4] */
295 if (stat->extension)
296 size += strlen(stat->extension);
297 }
298
299 return size;
300}
301
302/**
303 * serialize_stat - safely format a stat structure for transmission
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700304 * @stat: metadata (stat) structure
305 * @bufp: buffer to serialize structure into
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800306 * @extended: non-zero if 9P2000.u
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700307 *
308 */
309
310static int
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800311serialize_stat(struct v9fs_stat *stat, struct cbuf *bufp, int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700312{
313 buf_put_int16(bufp, stat->size);
314 buf_put_int16(bufp, stat->type);
315 buf_put_int32(bufp, stat->dev);
316 buf_put_int8(bufp, stat->qid.type);
317 buf_put_int32(bufp, stat->qid.version);
318 buf_put_int64(bufp, stat->qid.path);
319 buf_put_int32(bufp, stat->mode);
320 buf_put_int32(bufp, stat->atime);
321 buf_put_int32(bufp, stat->mtime);
322 buf_put_int64(bufp, stat->length);
323
324 buf_put_string(bufp, stat->name);
325 buf_put_string(bufp, stat->uid);
326 buf_put_string(bufp, stat->gid);
327 buf_put_string(bufp, stat->muid);
328
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800329 if (extended) {
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700330 buf_put_string(bufp, stat->extension);
331 buf_put_int32(bufp, stat->n_uid);
332 buf_put_int32(bufp, stat->n_gid);
333 buf_put_int32(bufp, stat->n_muid);
334 }
335
336 if (buf_check_overflow(bufp))
337 return 0;
338
339 return stat->size;
340}
341
342/**
343 * deserialize_stat - safely decode a recieved metadata (stat) structure
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700344 * @bufp: buffer to deserialize
345 * @stat: metadata (stat) structure
346 * @dbufp: buffer to deserialize variable strings into
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800347 * @extended: non-zero if 9P2000.u
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700348 *
349 */
350
351static inline int
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800352deserialize_stat(struct cbuf *bufp, struct v9fs_stat *stat,
353 struct cbuf *dbufp, int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700354{
355
356 stat->size = buf_get_int16(bufp);
357 stat->type = buf_get_int16(bufp);
358 stat->dev = buf_get_int32(bufp);
359 stat->qid.type = buf_get_int8(bufp);
360 stat->qid.version = buf_get_int32(bufp);
361 stat->qid.path = buf_get_int64(bufp);
362 stat->mode = buf_get_int32(bufp);
363 stat->atime = buf_get_int32(bufp);
364 stat->mtime = buf_get_int32(bufp);
365 stat->length = buf_get_int64(bufp);
366 stat->name = buf_get_stringb(bufp, dbufp);
367 stat->uid = buf_get_stringb(bufp, dbufp);
368 stat->gid = buf_get_stringb(bufp, dbufp);
369 stat->muid = buf_get_stringb(bufp, dbufp);
370
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800371 if (extended) {
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700372 stat->extension = buf_get_stringb(bufp, dbufp);
373 stat->n_uid = buf_get_int32(bufp);
374 stat->n_gid = buf_get_int32(bufp);
375 stat->n_muid = buf_get_int32(bufp);
376 }
377
378 if (buf_check_overflow(bufp) || buf_check_overflow(dbufp))
379 return 0;
380
381 return stat->size + 2;
382}
383
384/**
385 * deserialize_statb - wrapper for decoding a received metadata structure
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700386 * @bufp: buffer to deserialize
387 * @dbufp: buffer to deserialize variable strings into
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800388 * @extended: non-zero if 9P2000.u
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700389 *
390 */
391
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800392static inline struct v9fs_stat *deserialize_statb(struct cbuf *bufp,
393 struct cbuf *dbufp,
394 int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700395{
396 struct v9fs_stat *ret = buf_alloc(dbufp, sizeof(struct v9fs_stat));
397
398 if (ret) {
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800399 int n = deserialize_stat(bufp, ret, dbufp, extended);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700400 if (n <= 0)
401 return NULL;
402 }
403
404 return ret;
405}
406
407/**
408 * v9fs_deserialize_stat - decode a received metadata structure
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700409 * @buf: buffer to deserialize
410 * @buflen: length of received buffer
411 * @stat: metadata structure to decode into
412 * @statlen: length of destination metadata structure
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800413 * @extended: non-zero if 9P2000.u
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700414 *
415 */
416
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800417int v9fs_deserialize_stat(void *buf, u32 buflen, struct v9fs_stat *stat,
418 u32 statlen, int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700419{
420 struct cbuf buffer;
421 struct cbuf *bufp = &buffer;
422 struct cbuf dbuffer;
423 struct cbuf *dbufp = &dbuffer;
424
425 buf_init(bufp, buf, buflen);
426 buf_init(dbufp, (char *)stat + sizeof(struct v9fs_stat),
427 statlen - sizeof(struct v9fs_stat));
428
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800429 return deserialize_stat(bufp, stat, dbufp, extended);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700430}
431
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800432static inline int v9fs_size_fcall(struct v9fs_fcall *fcall, int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700433{
434 int size = 4 + 1 + 2; /* size[4] msg[1] tag[2] */
435 int i = 0;
436
437 switch (fcall->id) {
438 default:
439 eprintk(KERN_ERR, "bad msg type %d\n", fcall->id);
440 return 0;
441 case TVERSION: /* msize[4] version[s] */
442 size += 4 + 2 + strlen(fcall->params.tversion.version);
443 break;
444 case TAUTH: /* afid[4] uname[s] aname[s] */
445 size += 4 + 2 + strlen(fcall->params.tauth.uname) +
446 2 + strlen(fcall->params.tauth.aname);
447 break;
448 case TFLUSH: /* oldtag[2] */
449 size += 2;
450 break;
451 case TATTACH: /* fid[4] afid[4] uname[s] aname[s] */
452 size += 4 + 4 + 2 + strlen(fcall->params.tattach.uname) +
453 2 + strlen(fcall->params.tattach.aname);
454 break;
455 case TWALK: /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
456 size += 4 + 4 + 2;
457 /* now compute total for the array of names */
458 for (i = 0; i < fcall->params.twalk.nwname; i++)
459 size += 2 + strlen(fcall->params.twalk.wnames[i]);
460 break;
461 case TOPEN: /* fid[4] mode[1] */
462 size += 4 + 1;
463 break;
464 case TCREATE: /* fid[4] name[s] perm[4] mode[1] */
465 size += 4 + 2 + strlen(fcall->params.tcreate.name) + 4 + 1;
466 break;
467 case TREAD: /* fid[4] offset[8] count[4] */
468 size += 4 + 8 + 4;
469 break;
470 case TWRITE: /* fid[4] offset[8] count[4] data[count] */
471 size += 4 + 8 + 4 + fcall->params.twrite.count;
472 break;
473 case TCLUNK: /* fid[4] */
474 size += 4;
475 break;
476 case TREMOVE: /* fid[4] */
477 size += 4;
478 break;
479 case TSTAT: /* fid[4] */
480 size += 4;
481 break;
482 case TWSTAT: /* fid[4] stat[n] */
483 fcall->params.twstat.stat->size =
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800484 v9fs_size_stat(fcall->params.twstat.stat, extended);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700485 size += 4 + 2 + 2 + fcall->params.twstat.stat->size;
486 }
487 return size;
488}
489
490/*
491 * v9fs_serialize_fcall - marshall fcall struct into a packet
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700492 * @fcall: structure to convert
493 * @data: buffer to serialize fcall into
494 * @datalen: length of buffer to serialize fcall into
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800495 * @extended: non-zero if 9P2000.u
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700496 *
497 */
498
499int
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800500v9fs_serialize_fcall(struct v9fs_fcall *fcall, void *data, u32 datalen,
501 int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700502{
503 int i = 0;
504 struct v9fs_stat *stat = NULL;
505 struct cbuf buffer;
506 struct cbuf *bufp = &buffer;
507
508 buf_init(bufp, data, datalen);
509
510 if (!fcall) {
511 eprintk(KERN_ERR, "no fcall\n");
512 return -EINVAL;
513 }
514
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800515 fcall->size = v9fs_size_fcall(fcall, extended);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700516
517 buf_put_int32(bufp, fcall->size);
518 buf_put_int8(bufp, fcall->id);
519 buf_put_int16(bufp, fcall->tag);
520
521 dprintk(DEBUG_CONV, "size %d id %d tag %d\n", fcall->size, fcall->id,
522 fcall->tag);
523
524 /* now encode it */
525 switch (fcall->id) {
526 default:
527 eprintk(KERN_ERR, "bad msg type: %d\n", fcall->id);
528 return -EPROTO;
529 case TVERSION:
530 buf_put_int32(bufp, fcall->params.tversion.msize);
531 buf_put_string(bufp, fcall->params.tversion.version);
532 break;
533 case TAUTH:
534 buf_put_int32(bufp, fcall->params.tauth.afid);
535 buf_put_string(bufp, fcall->params.tauth.uname);
536 buf_put_string(bufp, fcall->params.tauth.aname);
537 break;
538 case TFLUSH:
539 buf_put_int16(bufp, fcall->params.tflush.oldtag);
540 break;
541 case TATTACH:
542 buf_put_int32(bufp, fcall->params.tattach.fid);
543 buf_put_int32(bufp, fcall->params.tattach.afid);
544 buf_put_string(bufp, fcall->params.tattach.uname);
545 buf_put_string(bufp, fcall->params.tattach.aname);
546 break;
547 case TWALK:
548 buf_put_int32(bufp, fcall->params.twalk.fid);
549 buf_put_int32(bufp, fcall->params.twalk.newfid);
550 buf_put_int16(bufp, fcall->params.twalk.nwname);
551 for (i = 0; i < fcall->params.twalk.nwname; i++)
552 buf_put_string(bufp, fcall->params.twalk.wnames[i]);
553 break;
554 case TOPEN:
555 buf_put_int32(bufp, fcall->params.topen.fid);
556 buf_put_int8(bufp, fcall->params.topen.mode);
557 break;
558 case TCREATE:
559 buf_put_int32(bufp, fcall->params.tcreate.fid);
560 buf_put_string(bufp, fcall->params.tcreate.name);
561 buf_put_int32(bufp, fcall->params.tcreate.perm);
562 buf_put_int8(bufp, fcall->params.tcreate.mode);
563 break;
564 case TREAD:
565 buf_put_int32(bufp, fcall->params.tread.fid);
566 buf_put_int64(bufp, fcall->params.tread.offset);
567 buf_put_int32(bufp, fcall->params.tread.count);
568 break;
569 case TWRITE:
570 buf_put_int32(bufp, fcall->params.twrite.fid);
571 buf_put_int64(bufp, fcall->params.twrite.offset);
572 buf_put_int32(bufp, fcall->params.twrite.count);
573 buf_put_data(bufp, fcall->params.twrite.data,
574 fcall->params.twrite.count);
575 break;
576 case TCLUNK:
577 buf_put_int32(bufp, fcall->params.tclunk.fid);
578 break;
579 case TREMOVE:
580 buf_put_int32(bufp, fcall->params.tremove.fid);
581 break;
582 case TSTAT:
583 buf_put_int32(bufp, fcall->params.tstat.fid);
584 break;
585 case TWSTAT:
586 buf_put_int32(bufp, fcall->params.twstat.fid);
587 stat = fcall->params.twstat.stat;
588
589 buf_put_int16(bufp, stat->size + 2);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800590 serialize_stat(stat, bufp, extended);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700591 break;
592 }
593
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800594 if (buf_check_overflow(bufp)) {
595 dprintk(DEBUG_ERROR, "buffer overflow\n");
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700596 return -EIO;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800597 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700598
599 return fcall->size;
600}
601
602/**
603 * deserialize_fcall - unmarshal a response
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700604 * @buf: recieved buffer
605 * @buflen: length of received buffer
606 * @rcall: fcall structure to populate
607 * @rcalllen: length of fcall structure to populate
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800608 * @extended: non-zero if 9P2000.u
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700609 *
610 */
611
612int
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800613v9fs_deserialize_fcall(void *buf, u32 buflen, struct v9fs_fcall *rcall,
614 int rcalllen, int extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700615{
616
617 struct cbuf buffer;
618 struct cbuf *bufp = &buffer;
619 struct cbuf dbuffer;
620 struct cbuf *dbufp = &dbuffer;
621 int i = 0;
622
623 buf_init(bufp, buf, buflen);
624 buf_init(dbufp, (char *)rcall + sizeof(struct v9fs_fcall),
625 rcalllen - sizeof(struct v9fs_fcall));
626
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800627 rcall->size = buf_get_int32(bufp);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700628 rcall->id = buf_get_int8(bufp);
629 rcall->tag = buf_get_int16(bufp);
630
631 dprintk(DEBUG_CONV, "size %d id %d tag %d\n", rcall->size, rcall->id,
632 rcall->tag);
633 switch (rcall->id) {
634 default:
635 eprintk(KERN_ERR, "unknown message type: %d\n", rcall->id);
636 return -EPROTO;
637 case RVERSION:
638 rcall->params.rversion.msize = buf_get_int32(bufp);
639 rcall->params.rversion.version = buf_get_stringb(bufp, dbufp);
640 break;
641 case RFLUSH:
642 break;
643 case RATTACH:
644 rcall->params.rattach.qid.type = buf_get_int8(bufp);
645 rcall->params.rattach.qid.version = buf_get_int32(bufp);
646 rcall->params.rattach.qid.path = buf_get_int64(bufp);
647 break;
648 case RWALK:
649 rcall->params.rwalk.nwqid = buf_get_int16(bufp);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800650 if (rcall->params.rwalk.nwqid > 16) {
651 eprintk(KERN_ERR, "Rwalk with more than 16 qids: %d\n",
652 rcall->params.rwalk.nwqid);
653 return -EPROTO;
654 }
655
Latchesar Ionkov5b067672005-09-22 21:43:50 -0700656 rcall->params.rwalk.wqids = buf_alloc(dbufp,
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700657 rcall->params.rwalk.nwqid * sizeof(struct v9fs_qid));
658 if (rcall->params.rwalk.wqids)
659 for (i = 0; i < rcall->params.rwalk.nwqid; i++) {
660 rcall->params.rwalk.wqids[i].type =
661 buf_get_int8(bufp);
662 rcall->params.rwalk.wqids[i].version =
663 buf_get_int16(bufp);
664 rcall->params.rwalk.wqids[i].path =
665 buf_get_int64(bufp);
666 }
667 break;
668 case ROPEN:
669 rcall->params.ropen.qid.type = buf_get_int8(bufp);
670 rcall->params.ropen.qid.version = buf_get_int32(bufp);
671 rcall->params.ropen.qid.path = buf_get_int64(bufp);
672 rcall->params.ropen.iounit = buf_get_int32(bufp);
673 break;
674 case RCREATE:
675 rcall->params.rcreate.qid.type = buf_get_int8(bufp);
676 rcall->params.rcreate.qid.version = buf_get_int32(bufp);
677 rcall->params.rcreate.qid.path = buf_get_int64(bufp);
678 rcall->params.rcreate.iounit = buf_get_int32(bufp);
679 break;
680 case RREAD:
681 rcall->params.rread.count = buf_get_int32(bufp);
682 rcall->params.rread.data = buf_get_datab(bufp, dbufp,
683 rcall->params.rread.count);
684 break;
685 case RWRITE:
686 rcall->params.rwrite.count = buf_get_int32(bufp);
687 break;
688 case RCLUNK:
689 break;
690 case RREMOVE:
691 break;
692 case RSTAT:
693 buf_get_int16(bufp);
694 rcall->params.rstat.stat =
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800695 deserialize_statb(bufp, dbufp, extended);
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700696 break;
697 case RWSTAT:
698 break;
699 case RERROR:
700 rcall->params.rerror.error = buf_get_stringb(bufp, dbufp);
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800701 if (extended)
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700702 rcall->params.rerror.errno = buf_get_int16(bufp);
703 break;
704 }
705
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800706 if (buf_check_overflow(bufp) || buf_check_overflow(dbufp)) {
707 dprintk(DEBUG_ERROR, "buffer overflow\n");
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700708 return -EIO;
Latchesar Ionkov3cf64292006-01-08 01:04:58 -0800709 }
Eric Van Hensbergenb8cf9452005-09-09 13:04:21 -0700710
711 return rcall->size;
712}