4 * 9P protocol conversion functions
6 * Copyright (C) 2004, 2005 by Latchesar Ionkov <lucho@ionkov.net>
7 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
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.
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.
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
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/errno.h>
32 #include <linux/idr.h>
40 * Buffer to help with string parsing
48 static inline void buf_init(struct cbuf *buf, void *data, int datalen)
50 buf->sp = buf->p = data;
51 buf->ep = data + datalen;
54 static inline int buf_check_overflow(struct cbuf *buf)
56 return buf->p > buf->ep;
59 static inline int buf_check_size(struct cbuf *buf, int len)
61 if (buf->p+len > buf->ep) {
62 if (buf->p < buf->ep) {
63 eprintk(KERN_ERR, "buffer overflow\n");
72 static inline void *buf_alloc(struct cbuf *buf, int len)
76 if (buf_check_size(buf, len)) {
84 static inline void buf_put_int8(struct cbuf *buf, u8 val)
86 if (buf_check_size(buf, 1)) {
92 static inline void buf_put_int16(struct cbuf *buf, u16 val)
94 if (buf_check_size(buf, 2)) {
95 *(__le16 *) buf->p = cpu_to_le16(val);
100 static inline void buf_put_int32(struct cbuf *buf, u32 val)
102 if (buf_check_size(buf, 4)) {
103 *(__le32 *)buf->p = cpu_to_le32(val);
108 static inline void buf_put_int64(struct cbuf *buf, u64 val)
110 if (buf_check_size(buf, 8)) {
111 *(__le64 *)buf->p = cpu_to_le64(val);
116 static inline void buf_put_stringn(struct cbuf *buf, const char *s, u16 slen)
118 if (buf_check_size(buf, slen + 2)) {
119 buf_put_int16(buf, slen);
120 memcpy(buf->p, s, slen);
125 static inline void buf_put_string(struct cbuf *buf, const char *s)
127 buf_put_stringn(buf, s, strlen(s));
130 static inline void buf_put_data(struct cbuf *buf, void *data, u32 datalen)
132 if (buf_check_size(buf, datalen)) {
133 memcpy(buf->p, data, datalen);
138 static inline u8 buf_get_int8(struct cbuf *buf)
142 if (buf_check_size(buf, 1)) {
150 static inline u16 buf_get_int16(struct cbuf *buf)
154 if (buf_check_size(buf, 2)) {
155 ret = le16_to_cpu(*(__le16 *)buf->p);
162 static inline u32 buf_get_int32(struct cbuf *buf)
166 if (buf_check_size(buf, 4)) {
167 ret = le32_to_cpu(*(__le32 *)buf->p);
174 static inline u64 buf_get_int64(struct cbuf *buf)
178 if (buf_check_size(buf, 8)) {
179 ret = le64_to_cpu(*(__le64 *)buf->p);
187 buf_get_string(struct cbuf *buf, char *data, unsigned int datalen)
191 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);
202 static inline char *buf_get_stringb(struct cbuf *buf, struct cbuf *sbuf)
208 len = buf_get_int16(buf);
210 if (!buf_check_overflow(buf) && buf_check_size(buf, len) &&
211 buf_check_size(sbuf, len + 1)) {
213 memcpy(sbuf->p, buf->p, len);
223 static inline int buf_get_data(struct cbuf *buf, void *data, int datalen)
227 if (buf_check_size(buf, datalen)) {
228 memcpy(data, buf->p, datalen);
236 static inline void *buf_get_datab(struct cbuf *buf, struct cbuf *dbuf,
242 if (buf_check_size(dbuf, datalen)) {
243 n = buf_get_data(buf, dbuf->p, datalen);
254 * v9fs_size_stat - calculate the size of a variable length stat struct
255 * @stat: metadata (stat) structure
256 * @extended: non-zero if 9P2000.u
260 static int v9fs_size_stat(struct v9fs_stat *stat, int extended)
265 eprintk(KERN_ERR, "v9fs_size_stat: got a NULL stat pointer\n");
269 size = /* 2 + *//* size[2] */
272 1 + /* qid.type[1] */
273 4 + /* qid.vers[4] */
274 8 + /* qid.path[8] */
279 8; /* minimum sum of string lengths */
282 size += strlen(stat->name);
284 size += strlen(stat->uid);
286 size += strlen(stat->gid);
288 size += strlen(stat->muid);
291 size += 4 + /* n_uid[4] */
294 2; /* string length of extension[4] */
296 size += strlen(stat->extension);
303 * serialize_stat - safely format a stat structure for transmission
304 * @stat: metadata (stat) structure
305 * @bufp: buffer to serialize structure into
306 * @extended: non-zero if 9P2000.u
311 serialize_stat(struct v9fs_stat *stat, struct cbuf *bufp, int extended)
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);
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);
330 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);
336 if (buf_check_overflow(bufp))
343 * deserialize_stat - safely decode a recieved metadata (stat) structure
344 * @bufp: buffer to deserialize
345 * @stat: metadata (stat) structure
346 * @dbufp: buffer to deserialize variable strings into
347 * @extended: non-zero if 9P2000.u
352 deserialize_stat(struct cbuf *bufp, struct v9fs_stat *stat,
353 struct cbuf *dbufp, int extended)
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);
372 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);
378 if (buf_check_overflow(bufp) || buf_check_overflow(dbufp))
381 return stat->size + 2;
385 * deserialize_statb - wrapper for decoding a received metadata structure
386 * @bufp: buffer to deserialize
387 * @dbufp: buffer to deserialize variable strings into
388 * @extended: non-zero if 9P2000.u
392 static inline struct v9fs_stat *deserialize_statb(struct cbuf *bufp,
396 struct v9fs_stat *ret = buf_alloc(dbufp, sizeof(struct v9fs_stat));
399 int n = deserialize_stat(bufp, ret, dbufp, extended);
408 * v9fs_deserialize_stat - decode a received metadata structure
409 * @buf: buffer to deserialize
410 * @buflen: length of received buffer
411 * @stat: metadata structure to decode into
412 * @statlen: length of destination metadata structure
413 * @extended: non-zero if 9P2000.u
417 int v9fs_deserialize_stat(void *buf, u32 buflen, struct v9fs_stat *stat,
418 u32 statlen, int extended)
421 struct cbuf *bufp = &buffer;
423 struct cbuf *dbufp = &dbuffer;
425 buf_init(bufp, buf, buflen);
426 buf_init(dbufp, (char *)stat + sizeof(struct v9fs_stat),
427 statlen - sizeof(struct v9fs_stat));
429 return deserialize_stat(bufp, stat, dbufp, extended);
432 static inline int v9fs_size_fcall(struct v9fs_fcall *fcall, int extended)
434 int size = 4 + 1 + 2; /* size[4] msg[1] tag[2] */
439 eprintk(KERN_ERR, "bad msg type %d\n", fcall->id);
441 case TVERSION: /* msize[4] version[s] */
442 size += 4 + 2 + strlen(fcall->params.tversion.version);
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);
448 case TFLUSH: /* oldtag[2] */
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);
455 case TWALK: /* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
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]);
461 case TOPEN: /* fid[4] mode[1] */
464 case TCREATE: /* fid[4] name[s] perm[4] mode[1] */
465 size += 4 + 2 + strlen(fcall->params.tcreate.name) + 4 + 1;
467 case TREAD: /* fid[4] offset[8] count[4] */
470 case TWRITE: /* fid[4] offset[8] count[4] data[count] */
471 size += 4 + 8 + 4 + fcall->params.twrite.count;
473 case TCLUNK: /* fid[4] */
476 case TREMOVE: /* fid[4] */
479 case TSTAT: /* fid[4] */
482 case TWSTAT: /* fid[4] stat[n] */
483 fcall->params.twstat.stat->size =
484 v9fs_size_stat(fcall->params.twstat.stat, extended);
485 size += 4 + 2 + 2 + fcall->params.twstat.stat->size;
491 * v9fs_serialize_fcall - marshall fcall struct into a packet
492 * @fcall: structure to convert
493 * @data: buffer to serialize fcall into
494 * @datalen: length of buffer to serialize fcall into
495 * @extended: non-zero if 9P2000.u
500 v9fs_serialize_fcall(struct v9fs_fcall *fcall, void *data, u32 datalen,
504 struct v9fs_stat *stat = NULL;
506 struct cbuf *bufp = &buffer;
508 buf_init(bufp, data, datalen);
511 eprintk(KERN_ERR, "no fcall\n");
515 fcall->size = v9fs_size_fcall(fcall, extended);
517 buf_put_int32(bufp, fcall->size);
518 buf_put_int8(bufp, fcall->id);
519 buf_put_int16(bufp, fcall->tag);
521 dprintk(DEBUG_CONV, "size %d id %d tag %d\n", fcall->size, fcall->id,
527 eprintk(KERN_ERR, "bad msg type: %d\n", fcall->id);
530 buf_put_int32(bufp, fcall->params.tversion.msize);
531 buf_put_string(bufp, fcall->params.tversion.version);
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);
539 buf_put_int16(bufp, fcall->params.tflush.oldtag);
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);
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]);
555 buf_put_int32(bufp, fcall->params.topen.fid);
556 buf_put_int8(bufp, fcall->params.topen.mode);
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);
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);
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);
577 buf_put_int32(bufp, fcall->params.tclunk.fid);
580 buf_put_int32(bufp, fcall->params.tremove.fid);
583 buf_put_int32(bufp, fcall->params.tstat.fid);
586 buf_put_int32(bufp, fcall->params.twstat.fid);
587 stat = fcall->params.twstat.stat;
589 buf_put_int16(bufp, stat->size + 2);
590 serialize_stat(stat, bufp, extended);
594 if (buf_check_overflow(bufp)) {
595 dprintk(DEBUG_ERROR, "buffer overflow\n");
603 * deserialize_fcall - unmarshal a response
604 * @buf: recieved buffer
605 * @buflen: length of received buffer
606 * @rcall: fcall structure to populate
607 * @rcalllen: length of fcall structure to populate
608 * @extended: non-zero if 9P2000.u
613 v9fs_deserialize_fcall(void *buf, u32 buflen, struct v9fs_fcall *rcall,
614 int rcalllen, int extended)
618 struct cbuf *bufp = &buffer;
620 struct cbuf *dbufp = &dbuffer;
623 buf_init(bufp, buf, buflen);
624 buf_init(dbufp, (char *)rcall + sizeof(struct v9fs_fcall),
625 rcalllen - sizeof(struct v9fs_fcall));
627 rcall->size = buf_get_int32(bufp);
628 rcall->id = buf_get_int8(bufp);
629 rcall->tag = buf_get_int16(bufp);
631 dprintk(DEBUG_CONV, "size %d id %d tag %d\n", rcall->size, rcall->id,
635 eprintk(KERN_ERR, "unknown message type: %d\n", rcall->id);
638 rcall->params.rversion.msize = buf_get_int32(bufp);
639 rcall->params.rversion.version = buf_get_stringb(bufp, dbufp);
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);
649 rcall->params.rwalk.nwqid = buf_get_int16(bufp);
650 if (rcall->params.rwalk.nwqid > 16) {
651 eprintk(KERN_ERR, "Rwalk with more than 16 qids: %d\n",
652 rcall->params.rwalk.nwqid);
656 rcall->params.rwalk.wqids = buf_alloc(dbufp,
657 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 =
662 rcall->params.rwalk.wqids[i].version =
664 rcall->params.rwalk.wqids[i].path =
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);
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);
681 rcall->params.rread.count = buf_get_int32(bufp);
682 rcall->params.rread.data = buf_get_datab(bufp, dbufp,
683 rcall->params.rread.count);
686 rcall->params.rwrite.count = buf_get_int32(bufp);
694 rcall->params.rstat.stat =
695 deserialize_statb(bufp, dbufp, extended);
700 rcall->params.rerror.error = buf_get_stringb(bufp, dbufp);
702 rcall->params.rerror.errno = buf_get_int16(bufp);
706 if (buf_check_overflow(bufp) || buf_check_overflow(dbufp)) {
707 dprintk(DEBUG_ERROR, "buffer overflow\n");