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 version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
27 #include <linux/module.h>
28 #include <linux/errno.h>
30 #include <linux/sched.h>
31 #include <linux/idr.h>
32 #include <linux/uaccess.h>
33 #include <net/9p/9p.h>
36 * Buffer to help with string parsing
44 static inline void buf_init(struct cbuf *buf, void *data, int datalen)
46 buf->sp = buf->p = data;
47 buf->ep = data + datalen;
50 static inline int buf_check_overflow(struct cbuf *buf)
52 return buf->p > buf->ep;
55 static int buf_check_size(struct cbuf *buf, int len)
57 if (buf->p + len > buf->ep) {
58 if (buf->p < buf->ep) {
60 "buffer overflow: want %d has %d\n", len,
61 (int)(buf->ep - buf->p));
72 static void *buf_alloc(struct cbuf *buf, int len)
76 if (buf_check_size(buf, len)) {
84 static void buf_put_int8(struct cbuf *buf, u8 val)
86 if (buf_check_size(buf, 1)) {
92 static 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 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 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 char *buf_put_stringn(struct cbuf *buf, const char *s, u16 slen)
121 if (buf_check_size(buf, slen + 2)) {
122 buf_put_int16(buf, slen);
124 memcpy(buf->p, s, slen);
131 static inline void buf_put_string(struct cbuf *buf, const char *s)
133 buf_put_stringn(buf, s, strlen(s));
136 static u8 buf_get_int8(struct cbuf *buf)
140 if (buf_check_size(buf, 1)) {
148 static u16 buf_get_int16(struct cbuf *buf)
152 if (buf_check_size(buf, 2)) {
153 ret = le16_to_cpu(*(__le16 *)buf->p);
160 static u32 buf_get_int32(struct cbuf *buf)
164 if (buf_check_size(buf, 4)) {
165 ret = le32_to_cpu(*(__le32 *)buf->p);
172 static u64 buf_get_int64(struct cbuf *buf)
176 if (buf_check_size(buf, 8)) {
177 ret = le64_to_cpu(*(__le64 *)buf->p);
184 static void buf_get_str(struct cbuf *buf, struct p9_str *vstr)
186 vstr->len = buf_get_int16(buf);
187 if (!buf_check_overflow(buf) && buf_check_size(buf, vstr->len)) {
196 static void buf_get_qid(struct cbuf *bufp, struct p9_qid *qid)
198 qid->type = buf_get_int8(bufp);
199 qid->version = buf_get_int32(bufp);
200 qid->path = buf_get_int64(bufp);
204 * p9_size_wstat - calculate the size of a variable length stat struct
205 * @stat: metadata (stat) structure
206 * @dotu: non-zero if 9P2000.u
210 static int p9_size_wstat(struct p9_wstat *wstat, int dotu)
215 P9_EPRINTK(KERN_ERR, "p9_size_stat: got a NULL stat pointer\n");
219 size = /* 2 + *//* size[2] */
222 1 + /* qid.type[1] */
223 4 + /* qid.vers[4] */
224 8 + /* qid.path[8] */
229 8; /* minimum sum of string lengths */
232 size += strlen(wstat->name);
234 size += strlen(wstat->uid);
236 size += strlen(wstat->gid);
238 size += strlen(wstat->muid);
241 size += 4 + /* n_uid[4] */
244 2; /* string length of extension[4] */
245 if (wstat->extension)
246 size += strlen(wstat->extension);
253 * buf_get_stat - safely decode a recieved metadata (stat) structure
254 * @bufp: buffer to deserialize
255 * @stat: metadata (stat) structure
256 * @dotu: non-zero if 9P2000.u
261 buf_get_stat(struct cbuf *bufp, struct p9_stat *stat, int dotu)
263 stat->size = buf_get_int16(bufp);
264 stat->type = buf_get_int16(bufp);
265 stat->dev = buf_get_int32(bufp);
266 stat->qid.type = buf_get_int8(bufp);
267 stat->qid.version = buf_get_int32(bufp);
268 stat->qid.path = buf_get_int64(bufp);
269 stat->mode = buf_get_int32(bufp);
270 stat->atime = buf_get_int32(bufp);
271 stat->mtime = buf_get_int32(bufp);
272 stat->length = buf_get_int64(bufp);
273 buf_get_str(bufp, &stat->name);
274 buf_get_str(bufp, &stat->uid);
275 buf_get_str(bufp, &stat->gid);
276 buf_get_str(bufp, &stat->muid);
279 buf_get_str(bufp, &stat->extension);
280 stat->n_uid = buf_get_int32(bufp);
281 stat->n_gid = buf_get_int32(bufp);
282 stat->n_muid = buf_get_int32(bufp);
287 * p9_deserialize_stat - decode a received metadata structure
288 * @buf: buffer to deserialize
289 * @buflen: length of received buffer
290 * @stat: metadata structure to decode into
291 * @dotu: non-zero if 9P2000.u
293 * Note: stat will point to the buf region.
297 p9_deserialize_stat(void *buf, u32 buflen, struct p9_stat *stat,
301 struct cbuf *bufp = &buffer;
304 buf_init(bufp, buf, buflen);
306 buf_get_stat(bufp, stat, dotu);
308 if (buf_check_overflow(bufp))
313 EXPORT_SYMBOL(p9_deserialize_stat);
316 * deserialize_fcall - unmarshal a response
317 * @buf: recieved buffer
318 * @buflen: length of received buffer
319 * @rcall: fcall structure to populate
320 * @rcalllen: length of fcall structure to populate
321 * @dotu: non-zero if 9P2000.u
326 p9_deserialize_fcall(void *buf, u32 buflen, struct p9_fcall *rcall,
331 struct cbuf *bufp = &buffer;
334 buf_init(bufp, buf, buflen);
336 rcall->size = buf_get_int32(bufp);
337 rcall->id = buf_get_int8(bufp);
338 rcall->tag = buf_get_int16(bufp);
340 P9_DPRINTK(P9_DEBUG_CONV, "size %d id %d tag %d\n", rcall->size,
341 rcall->id, rcall->tag);
345 P9_EPRINTK(KERN_ERR, "unknown message type: %d\n", rcall->id);
348 rcall->params.rversion.msize = buf_get_int32(bufp);
349 buf_get_str(bufp, &rcall->params.rversion.version);
354 rcall->params.rattach.qid.type = buf_get_int8(bufp);
355 rcall->params.rattach.qid.version = buf_get_int32(bufp);
356 rcall->params.rattach.qid.path = buf_get_int64(bufp);
359 rcall->params.rwalk.nwqid = buf_get_int16(bufp);
360 if (rcall->params.rwalk.nwqid > P9_MAXWELEM) {
362 "Rwalk with more than %d qids: %d\n",
363 P9_MAXWELEM, rcall->params.rwalk.nwqid);
367 for (i = 0; i < rcall->params.rwalk.nwqid; i++)
368 buf_get_qid(bufp, &rcall->params.rwalk.wqids[i]);
371 buf_get_qid(bufp, &rcall->params.ropen.qid);
372 rcall->params.ropen.iounit = buf_get_int32(bufp);
375 buf_get_qid(bufp, &rcall->params.rcreate.qid);
376 rcall->params.rcreate.iounit = buf_get_int32(bufp);
379 rcall->params.rread.count = buf_get_int32(bufp);
380 rcall->params.rread.data = bufp->p;
381 buf_check_size(bufp, rcall->params.rread.count);
384 rcall->params.rwrite.count = buf_get_int32(bufp);
392 buf_get_stat(bufp, &rcall->params.rstat.stat, dotu);
397 buf_get_str(bufp, &rcall->params.rerror.error);
399 rcall->params.rerror.errno = buf_get_int16(bufp);
403 if (buf_check_overflow(bufp)) {
404 P9_DPRINTK(P9_DEBUG_ERROR, "buffer overflow\n");
408 return bufp->p - bufp->sp;
410 EXPORT_SYMBOL(p9_deserialize_fcall);
412 static inline void p9_put_int8(struct cbuf *bufp, u8 val, u8 * p)
415 buf_put_int8(bufp, val);
418 static inline void p9_put_int16(struct cbuf *bufp, u16 val, u16 * p)
421 buf_put_int16(bufp, val);
424 static inline void p9_put_int32(struct cbuf *bufp, u32 val, u32 * p)
427 buf_put_int32(bufp, val);
430 static inline void p9_put_int64(struct cbuf *bufp, u64 val, u64 * p)
433 buf_put_int64(bufp, val);
437 p9_put_str(struct cbuf *bufp, char *data, struct p9_str *str)
447 s = buf_put_stringn(bufp, data, len);
455 p9_put_data(struct cbuf *bufp, const char *data, int count,
456 unsigned char **pdata)
458 *pdata = buf_alloc(bufp, count);
459 memmove(*pdata, data, count);
464 p9_put_user_data(struct cbuf *bufp, const char __user *data, int count,
465 unsigned char **pdata)
467 *pdata = buf_alloc(bufp, count);
468 return copy_from_user(*pdata, data, count);
472 p9_put_wstat(struct cbuf *bufp, struct p9_wstat *wstat,
473 struct p9_stat *stat, int statsz, int dotu)
475 p9_put_int16(bufp, statsz, &stat->size);
476 p9_put_int16(bufp, wstat->type, &stat->type);
477 p9_put_int32(bufp, wstat->dev, &stat->dev);
478 p9_put_int8(bufp, wstat->qid.type, &stat->qid.type);
479 p9_put_int32(bufp, wstat->qid.version, &stat->qid.version);
480 p9_put_int64(bufp, wstat->qid.path, &stat->qid.path);
481 p9_put_int32(bufp, wstat->mode, &stat->mode);
482 p9_put_int32(bufp, wstat->atime, &stat->atime);
483 p9_put_int32(bufp, wstat->mtime, &stat->mtime);
484 p9_put_int64(bufp, wstat->length, &stat->length);
486 p9_put_str(bufp, wstat->name, &stat->name);
487 p9_put_str(bufp, wstat->uid, &stat->uid);
488 p9_put_str(bufp, wstat->gid, &stat->gid);
489 p9_put_str(bufp, wstat->muid, &stat->muid);
492 p9_put_str(bufp, wstat->extension, &stat->extension);
493 p9_put_int32(bufp, wstat->n_uid, &stat->n_uid);
494 p9_put_int32(bufp, wstat->n_gid, &stat->n_gid);
495 p9_put_int32(bufp, wstat->n_muid, &stat->n_muid);
499 static struct p9_fcall *
500 p9_create_common(struct cbuf *bufp, u32 size, u8 id)
504 size += 4 + 1 + 2; /* size[4] id[1] tag[2] */
505 fc = kmalloc(sizeof(struct p9_fcall) + size, GFP_KERNEL);
507 return ERR_PTR(-ENOMEM);
509 fc->sdata = (char *)fc + sizeof(*fc);
511 buf_init(bufp, (char *)fc->sdata, size);
512 p9_put_int32(bufp, size, &fc->size);
513 p9_put_int8(bufp, id, &fc->id);
514 p9_put_int16(bufp, P9_NOTAG, &fc->tag);
519 void p9_set_tag(struct p9_fcall *fc, u16 tag)
522 *(__le16 *) (fc->sdata + 5) = cpu_to_le16(tag);
524 EXPORT_SYMBOL(p9_set_tag);
526 struct p9_fcall *p9_create_tversion(u32 msize, char *version)
531 struct cbuf *bufp = &buffer;
533 size = 4 + 2 + strlen(version); /* msize[4] version[s] */
534 fc = p9_create_common(bufp, size, P9_TVERSION);
538 p9_put_int32(bufp, msize, &fc->params.tversion.msize);
539 p9_put_str(bufp, version, &fc->params.tversion.version);
541 if (buf_check_overflow(bufp)) {
543 fc = ERR_PTR(-ENOMEM);
548 EXPORT_SYMBOL(p9_create_tversion);
550 struct p9_fcall *p9_create_tauth(u32 afid, char *uname, char *aname)
555 struct cbuf *bufp = &buffer;
557 /* afid[4] uname[s] aname[s] */
558 size = 4 + 2 + strlen(uname) + 2 + strlen(aname);
559 fc = p9_create_common(bufp, size, P9_TAUTH);
563 p9_put_int32(bufp, afid, &fc->params.tauth.afid);
564 p9_put_str(bufp, uname, &fc->params.tauth.uname);
565 p9_put_str(bufp, aname, &fc->params.tauth.aname);
567 if (buf_check_overflow(bufp)) {
569 fc = ERR_PTR(-ENOMEM);
574 EXPORT_SYMBOL(p9_create_tauth);
577 p9_create_tattach(u32 fid, u32 afid, char *uname, char *aname)
582 struct cbuf *bufp = &buffer;
584 /* fid[4] afid[4] uname[s] aname[s] */
585 size = 4 + 4 + 2 + strlen(uname) + 2 + strlen(aname);
586 fc = p9_create_common(bufp, size, P9_TATTACH);
590 p9_put_int32(bufp, fid, &fc->params.tattach.fid);
591 p9_put_int32(bufp, afid, &fc->params.tattach.afid);
592 p9_put_str(bufp, uname, &fc->params.tattach.uname);
593 p9_put_str(bufp, aname, &fc->params.tattach.aname);
598 EXPORT_SYMBOL(p9_create_tattach);
600 struct p9_fcall *p9_create_tflush(u16 oldtag)
605 struct cbuf *bufp = &buffer;
607 size = 2; /* oldtag[2] */
608 fc = p9_create_common(bufp, size, P9_TFLUSH);
612 p9_put_int16(bufp, oldtag, &fc->params.tflush.oldtag);
614 if (buf_check_overflow(bufp)) {
616 fc = ERR_PTR(-ENOMEM);
621 EXPORT_SYMBOL(p9_create_tflush);
623 struct p9_fcall *p9_create_twalk(u32 fid, u32 newfid, u16 nwname,
629 struct cbuf *bufp = &buffer;
631 if (nwname > P9_MAXWELEM) {
632 P9_DPRINTK(P9_DEBUG_ERROR, "nwname > %d\n", P9_MAXWELEM);
636 size = 4 + 4 + 2; /* fid[4] newfid[4] nwname[2] ... */
637 for (i = 0; i < nwname; i++) {
638 size += 2 + strlen(wnames[i]); /* wname[s] */
641 fc = p9_create_common(bufp, size, P9_TWALK);
645 p9_put_int32(bufp, fid, &fc->params.twalk.fid);
646 p9_put_int32(bufp, newfid, &fc->params.twalk.newfid);
647 p9_put_int16(bufp, nwname, &fc->params.twalk.nwname);
648 for (i = 0; i < nwname; i++) {
649 p9_put_str(bufp, wnames[i], &fc->params.twalk.wnames[i]);
652 if (buf_check_overflow(bufp)) {
654 fc = ERR_PTR(-ENOMEM);
659 EXPORT_SYMBOL(p9_create_twalk);
661 struct p9_fcall *p9_create_topen(u32 fid, u8 mode)
666 struct cbuf *bufp = &buffer;
668 size = 4 + 1; /* fid[4] mode[1] */
669 fc = p9_create_common(bufp, size, P9_TOPEN);
673 p9_put_int32(bufp, fid, &fc->params.topen.fid);
674 p9_put_int8(bufp, mode, &fc->params.topen.mode);
676 if (buf_check_overflow(bufp)) {
678 fc = ERR_PTR(-ENOMEM);
683 EXPORT_SYMBOL(p9_create_topen);
685 struct p9_fcall *p9_create_tcreate(u32 fid, char *name, u32 perm, u8 mode,
686 char *extension, int dotu)
691 struct cbuf *bufp = &buffer;
693 /* fid[4] name[s] perm[4] mode[1] */
694 size = 4 + 2 + strlen(name) + 4 + 1;
696 size += 2 + /* extension[s] */
697 (extension == NULL ? 0 : strlen(extension));
700 fc = p9_create_common(bufp, size, P9_TCREATE);
704 p9_put_int32(bufp, fid, &fc->params.tcreate.fid);
705 p9_put_str(bufp, name, &fc->params.tcreate.name);
706 p9_put_int32(bufp, perm, &fc->params.tcreate.perm);
707 p9_put_int8(bufp, mode, &fc->params.tcreate.mode);
709 p9_put_str(bufp, extension, &fc->params.tcreate.extension);
711 if (buf_check_overflow(bufp)) {
713 fc = ERR_PTR(-ENOMEM);
718 EXPORT_SYMBOL(p9_create_tcreate);
720 struct p9_fcall *p9_create_tread(u32 fid, u64 offset, u32 count)
725 struct cbuf *bufp = &buffer;
727 size = 4 + 8 + 4; /* fid[4] offset[8] count[4] */
728 fc = p9_create_common(bufp, size, P9_TREAD);
732 p9_put_int32(bufp, fid, &fc->params.tread.fid);
733 p9_put_int64(bufp, offset, &fc->params.tread.offset);
734 p9_put_int32(bufp, count, &fc->params.tread.count);
736 if (buf_check_overflow(bufp)) {
738 fc = ERR_PTR(-ENOMEM);
743 EXPORT_SYMBOL(p9_create_tread);
745 struct p9_fcall *p9_create_twrite(u32 fid, u64 offset, u32 count,
751 struct cbuf *bufp = &buffer;
753 /* fid[4] offset[8] count[4] data[count] */
754 size = 4 + 8 + 4 + count;
755 fc = p9_create_common(bufp, size, P9_TWRITE);
759 p9_put_int32(bufp, fid, &fc->params.twrite.fid);
760 p9_put_int64(bufp, offset, &fc->params.twrite.offset);
761 p9_put_int32(bufp, count, &fc->params.twrite.count);
762 err = p9_put_data(bufp, data, count, &fc->params.twrite.data);
769 if (buf_check_overflow(bufp)) {
771 fc = ERR_PTR(-ENOMEM);
776 EXPORT_SYMBOL(p9_create_twrite);
778 struct p9_fcall *p9_create_twrite_u(u32 fid, u64 offset, u32 count,
779 const char __user *data)
784 struct cbuf *bufp = &buffer;
786 /* fid[4] offset[8] count[4] data[count] */
787 size = 4 + 8 + 4 + count;
788 fc = p9_create_common(bufp, size, P9_TWRITE);
792 p9_put_int32(bufp, fid, &fc->params.twrite.fid);
793 p9_put_int64(bufp, offset, &fc->params.twrite.offset);
794 p9_put_int32(bufp, count, &fc->params.twrite.count);
795 err = p9_put_user_data(bufp, data, count, &fc->params.twrite.data);
802 if (buf_check_overflow(bufp)) {
804 fc = ERR_PTR(-ENOMEM);
809 EXPORT_SYMBOL(p9_create_twrite_u);
811 struct p9_fcall *p9_create_tclunk(u32 fid)
816 struct cbuf *bufp = &buffer;
818 size = 4; /* fid[4] */
819 fc = p9_create_common(bufp, size, P9_TCLUNK);
823 p9_put_int32(bufp, fid, &fc->params.tclunk.fid);
825 if (buf_check_overflow(bufp)) {
827 fc = ERR_PTR(-ENOMEM);
832 EXPORT_SYMBOL(p9_create_tclunk);
834 struct p9_fcall *p9_create_tremove(u32 fid)
839 struct cbuf *bufp = &buffer;
841 size = 4; /* fid[4] */
842 fc = p9_create_common(bufp, size, P9_TREMOVE);
846 p9_put_int32(bufp, fid, &fc->params.tremove.fid);
848 if (buf_check_overflow(bufp)) {
850 fc = ERR_PTR(-ENOMEM);
855 EXPORT_SYMBOL(p9_create_tremove);
857 struct p9_fcall *p9_create_tstat(u32 fid)
862 struct cbuf *bufp = &buffer;
864 size = 4; /* fid[4] */
865 fc = p9_create_common(bufp, size, P9_TSTAT);
869 p9_put_int32(bufp, fid, &fc->params.tstat.fid);
871 if (buf_check_overflow(bufp)) {
873 fc = ERR_PTR(-ENOMEM);
878 EXPORT_SYMBOL(p9_create_tstat);
880 struct p9_fcall *p9_create_twstat(u32 fid, struct p9_wstat *wstat,
886 struct cbuf *bufp = &buffer;
888 statsz = p9_size_wstat(wstat, dotu);
889 size = 4 + 2 + 2 + statsz; /* fid[4] stat[n] */
890 fc = p9_create_common(bufp, size, P9_TWSTAT);
894 p9_put_int32(bufp, fid, &fc->params.twstat.fid);
895 buf_put_int16(bufp, statsz + 2);
896 p9_put_wstat(bufp, wstat, &fc->params.twstat.stat, statsz, dotu);
898 if (buf_check_overflow(bufp)) {
900 fc = ERR_PTR(-ENOMEM);
905 EXPORT_SYMBOL(p9_create_twstat);