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>
33 #include <asm/uaccess.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 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: want %d has %d\n",
64 len, (int)(buf->ep - buf->p));
75 static void *buf_alloc(struct cbuf *buf, int len)
79 if (buf_check_size(buf, len)) {
87 static void buf_put_int8(struct cbuf *buf, u8 val)
89 if (buf_check_size(buf, 1)) {
95 static void buf_put_int16(struct cbuf *buf, u16 val)
97 if (buf_check_size(buf, 2)) {
98 *(__le16 *) buf->p = cpu_to_le16(val);
103 static void buf_put_int32(struct cbuf *buf, u32 val)
105 if (buf_check_size(buf, 4)) {
106 *(__le32 *)buf->p = cpu_to_le32(val);
111 static void buf_put_int64(struct cbuf *buf, u64 val)
113 if (buf_check_size(buf, 8)) {
114 *(__le64 *)buf->p = cpu_to_le64(val);
119 static char *buf_put_stringn(struct cbuf *buf, const char *s, u16 slen)
124 if (buf_check_size(buf, slen + 2)) {
125 buf_put_int16(buf, slen);
127 memcpy(buf->p, s, slen);
134 static inline void buf_put_string(struct cbuf *buf, const char *s)
136 buf_put_stringn(buf, s, strlen(s));
139 static u8 buf_get_int8(struct cbuf *buf)
143 if (buf_check_size(buf, 1)) {
151 static u16 buf_get_int16(struct cbuf *buf)
155 if (buf_check_size(buf, 2)) {
156 ret = le16_to_cpu(*(__le16 *)buf->p);
163 static u32 buf_get_int32(struct cbuf *buf)
167 if (buf_check_size(buf, 4)) {
168 ret = le32_to_cpu(*(__le32 *)buf->p);
175 static u64 buf_get_int64(struct cbuf *buf)
179 if (buf_check_size(buf, 8)) {
180 ret = le64_to_cpu(*(__le64 *)buf->p);
187 static void buf_get_str(struct cbuf *buf, struct v9fs_str *vstr)
189 vstr->len = buf_get_int16(buf);
190 if (!buf_check_overflow(buf) && buf_check_size(buf, vstr->len)) {
199 static void buf_get_qid(struct cbuf *bufp, struct v9fs_qid *qid)
201 qid->type = buf_get_int8(bufp);
202 qid->version = buf_get_int32(bufp);
203 qid->path = buf_get_int64(bufp);
207 * v9fs_size_wstat - calculate the size of a variable length stat struct
208 * @stat: metadata (stat) structure
209 * @extended: non-zero if 9P2000.u
213 static int v9fs_size_wstat(struct v9fs_wstat *wstat, int extended)
218 eprintk(KERN_ERR, "v9fs_size_stat: got a NULL stat pointer\n");
222 size = /* 2 + *//* size[2] */
225 1 + /* qid.type[1] */
226 4 + /* qid.vers[4] */
227 8 + /* qid.path[8] */
232 8; /* minimum sum of string lengths */
235 size += strlen(wstat->name);
237 size += strlen(wstat->uid);
239 size += strlen(wstat->gid);
241 size += strlen(wstat->muid);
244 size += 4 + /* n_uid[4] */
247 2; /* string length of extension[4] */
248 if (wstat->extension)
249 size += strlen(wstat->extension);
256 * buf_get_stat - safely decode a recieved metadata (stat) structure
257 * @bufp: buffer to deserialize
258 * @stat: metadata (stat) structure
259 * @extended: non-zero if 9P2000.u
264 buf_get_stat(struct cbuf *bufp, struct v9fs_stat *stat, int extended)
266 stat->size = buf_get_int16(bufp);
267 stat->type = buf_get_int16(bufp);
268 stat->dev = buf_get_int32(bufp);
269 stat->qid.type = buf_get_int8(bufp);
270 stat->qid.version = buf_get_int32(bufp);
271 stat->qid.path = buf_get_int64(bufp);
272 stat->mode = buf_get_int32(bufp);
273 stat->atime = buf_get_int32(bufp);
274 stat->mtime = buf_get_int32(bufp);
275 stat->length = buf_get_int64(bufp);
276 buf_get_str(bufp, &stat->name);
277 buf_get_str(bufp, &stat->uid);
278 buf_get_str(bufp, &stat->gid);
279 buf_get_str(bufp, &stat->muid);
282 buf_get_str(bufp, &stat->extension);
283 stat->n_uid = buf_get_int32(bufp);
284 stat->n_gid = buf_get_int32(bufp);
285 stat->n_muid = buf_get_int32(bufp);
290 * v9fs_deserialize_stat - decode a received metadata structure
291 * @buf: buffer to deserialize
292 * @buflen: length of received buffer
293 * @stat: metadata structure to decode into
294 * @extended: non-zero if 9P2000.u
296 * Note: stat will point to the buf region.
300 v9fs_deserialize_stat(void *buf, u32 buflen, struct v9fs_stat *stat,
304 struct cbuf *bufp = &buffer;
307 buf_init(bufp, buf, buflen);
309 buf_get_stat(bufp, stat, extended);
311 if (buf_check_overflow(bufp))
318 * deserialize_fcall - unmarshal a response
319 * @buf: recieved buffer
320 * @buflen: length of received buffer
321 * @rcall: fcall structure to populate
322 * @rcalllen: length of fcall structure to populate
323 * @extended: non-zero if 9P2000.u
328 v9fs_deserialize_fcall(void *buf, u32 buflen, struct v9fs_fcall *rcall,
333 struct cbuf *bufp = &buffer;
336 buf_init(bufp, buf, buflen);
338 rcall->size = buf_get_int32(bufp);
339 rcall->id = buf_get_int8(bufp);
340 rcall->tag = buf_get_int16(bufp);
342 dprintk(DEBUG_CONV, "size %d id %d tag %d\n", rcall->size, rcall->id,
347 eprintk(KERN_ERR, "unknown message type: %d\n", rcall->id);
350 rcall->params.rversion.msize = buf_get_int32(bufp);
351 buf_get_str(bufp, &rcall->params.rversion.version);
356 rcall->params.rattach.qid.type = buf_get_int8(bufp);
357 rcall->params.rattach.qid.version = buf_get_int32(bufp);
358 rcall->params.rattach.qid.path = buf_get_int64(bufp);
361 rcall->params.rwalk.nwqid = buf_get_int16(bufp);
362 if (rcall->params.rwalk.nwqid > V9FS_MAXWELEM) {
363 eprintk(KERN_ERR, "Rwalk with more than %d qids: %d\n",
364 V9FS_MAXWELEM, rcall->params.rwalk.nwqid);
368 for (i = 0; i < rcall->params.rwalk.nwqid; i++)
369 buf_get_qid(bufp, &rcall->params.rwalk.wqids[i]);
372 buf_get_qid(bufp, &rcall->params.ropen.qid);
373 rcall->params.ropen.iounit = buf_get_int32(bufp);
376 buf_get_qid(bufp, &rcall->params.rcreate.qid);
377 rcall->params.rcreate.iounit = buf_get_int32(bufp);
380 rcall->params.rread.count = buf_get_int32(bufp);
381 rcall->params.rread.data = bufp->p;
382 buf_check_size(bufp, rcall->params.rread.count);
385 rcall->params.rwrite.count = buf_get_int32(bufp);
393 buf_get_stat(bufp, &rcall->params.rstat.stat, extended);
398 buf_get_str(bufp, &rcall->params.rerror.error);
400 rcall->params.rerror.errno = buf_get_int16(bufp);
404 if (buf_check_overflow(bufp)) {
405 dprintk(DEBUG_ERROR, "buffer overflow\n");
409 return bufp->p - bufp->sp;
412 static inline void v9fs_put_int8(struct cbuf *bufp, u8 val, u8 * p)
415 buf_put_int8(bufp, val);
418 static inline void v9fs_put_int16(struct cbuf *bufp, u16 val, u16 * p)
421 buf_put_int16(bufp, val);
424 static inline void v9fs_put_int32(struct cbuf *bufp, u32 val, u32 * p)
427 buf_put_int32(bufp, val);
430 static inline void v9fs_put_int64(struct cbuf *bufp, u64 val, u64 * p)
433 buf_put_int64(bufp, val);
437 v9fs_put_str(struct cbuf *bufp, char *data, struct v9fs_str *str)
447 s = buf_put_stringn(bufp, data, len);
455 v9fs_put_user_data(struct cbuf *bufp, const char __user * data, int count,
456 unsigned char **pdata)
458 *pdata = buf_alloc(bufp, count);
459 return copy_from_user(*pdata, data, count);
463 v9fs_put_wstat(struct cbuf *bufp, struct v9fs_wstat *wstat,
464 struct v9fs_stat *stat, int statsz, int extended)
466 v9fs_put_int16(bufp, statsz, &stat->size);
467 v9fs_put_int16(bufp, wstat->type, &stat->type);
468 v9fs_put_int32(bufp, wstat->dev, &stat->dev);
469 v9fs_put_int8(bufp, wstat->qid.type, &stat->qid.type);
470 v9fs_put_int32(bufp, wstat->qid.version, &stat->qid.version);
471 v9fs_put_int64(bufp, wstat->qid.path, &stat->qid.path);
472 v9fs_put_int32(bufp, wstat->mode, &stat->mode);
473 v9fs_put_int32(bufp, wstat->atime, &stat->atime);
474 v9fs_put_int32(bufp, wstat->mtime, &stat->mtime);
475 v9fs_put_int64(bufp, wstat->length, &stat->length);
477 v9fs_put_str(bufp, wstat->name, &stat->name);
478 v9fs_put_str(bufp, wstat->uid, &stat->uid);
479 v9fs_put_str(bufp, wstat->gid, &stat->gid);
480 v9fs_put_str(bufp, wstat->muid, &stat->muid);
483 v9fs_put_str(bufp, wstat->extension, &stat->extension);
484 v9fs_put_int32(bufp, wstat->n_uid, &stat->n_uid);
485 v9fs_put_int32(bufp, wstat->n_gid, &stat->n_gid);
486 v9fs_put_int32(bufp, wstat->n_muid, &stat->n_muid);
490 static struct v9fs_fcall *
491 v9fs_create_common(struct cbuf *bufp, u32 size, u8 id)
493 struct v9fs_fcall *fc;
495 size += 4 + 1 + 2; /* size[4] id[1] tag[2] */
496 fc = kmalloc(sizeof(struct v9fs_fcall) + size, GFP_KERNEL);
498 return ERR_PTR(-ENOMEM);
500 fc->sdata = (char *)fc + sizeof(*fc);
502 buf_init(bufp, (char *)fc->sdata, size);
503 v9fs_put_int32(bufp, size, &fc->size);
504 v9fs_put_int8(bufp, id, &fc->id);
505 v9fs_put_int16(bufp, V9FS_NOTAG, &fc->tag);
510 void v9fs_set_tag(struct v9fs_fcall *fc, u16 tag)
513 *(__le16 *) (fc->sdata + 5) = cpu_to_le16(tag);
516 struct v9fs_fcall *v9fs_create_tversion(u32 msize, char *version)
519 struct v9fs_fcall *fc;
521 struct cbuf *bufp = &buffer;
523 size = 4 + 2 + strlen(version); /* msize[4] version[s] */
524 fc = v9fs_create_common(bufp, size, TVERSION);
528 v9fs_put_int32(bufp, msize, &fc->params.tversion.msize);
529 v9fs_put_str(bufp, version, &fc->params.tversion.version);
531 if (buf_check_overflow(bufp)) {
533 fc = ERR_PTR(-ENOMEM);
539 struct v9fs_fcall *v9fs_create_tauth(u32 afid, char *uname, char *aname)
542 struct v9fs_fcall *fc;
544 struct cbuf *bufp = &buffer;
546 size = 4 + 2 + strlen(uname) + 2 + strlen(aname); /* afid[4] uname[s] aname[s] */
547 fc = v9fs_create_common(bufp, size, TAUTH);
551 v9fs_put_int32(bufp, afid, &fc->params.tauth.afid);
552 v9fs_put_str(bufp, uname, &fc->params.tauth.uname);
553 v9fs_put_str(bufp, aname, &fc->params.tauth.aname);
555 if (buf_check_overflow(bufp)) {
557 fc = ERR_PTR(-ENOMEM);
564 v9fs_create_tattach(u32 fid, u32 afid, char *uname, char *aname)
567 struct v9fs_fcall *fc;
569 struct cbuf *bufp = &buffer;
571 size = 4 + 4 + 2 + strlen(uname) + 2 + strlen(aname); /* fid[4] afid[4] uname[s] aname[s] */
572 fc = v9fs_create_common(bufp, size, TATTACH);
576 v9fs_put_int32(bufp, fid, &fc->params.tattach.fid);
577 v9fs_put_int32(bufp, afid, &fc->params.tattach.afid);
578 v9fs_put_str(bufp, uname, &fc->params.tattach.uname);
579 v9fs_put_str(bufp, aname, &fc->params.tattach.aname);
585 struct v9fs_fcall *v9fs_create_tflush(u16 oldtag)
588 struct v9fs_fcall *fc;
590 struct cbuf *bufp = &buffer;
592 size = 2; /* oldtag[2] */
593 fc = v9fs_create_common(bufp, size, TFLUSH);
597 v9fs_put_int16(bufp, oldtag, &fc->params.tflush.oldtag);
599 if (buf_check_overflow(bufp)) {
601 fc = ERR_PTR(-ENOMEM);
607 struct v9fs_fcall *v9fs_create_twalk(u32 fid, u32 newfid, u16 nwname,
611 struct v9fs_fcall *fc;
613 struct cbuf *bufp = &buffer;
615 if (nwname > V9FS_MAXWELEM) {
616 dprintk(DEBUG_ERROR, "nwname > %d\n", V9FS_MAXWELEM);
620 size = 4 + 4 + 2; /* fid[4] newfid[4] nwname[2] ... */
621 for (i = 0; i < nwname; i++) {
622 size += 2 + strlen(wnames[i]); /* wname[s] */
625 fc = v9fs_create_common(bufp, size, TWALK);
629 v9fs_put_int32(bufp, fid, &fc->params.twalk.fid);
630 v9fs_put_int32(bufp, newfid, &fc->params.twalk.newfid);
631 v9fs_put_int16(bufp, nwname, &fc->params.twalk.nwname);
632 for (i = 0; i < nwname; i++) {
633 v9fs_put_str(bufp, wnames[i], &fc->params.twalk.wnames[i]);
636 if (buf_check_overflow(bufp)) {
638 fc = ERR_PTR(-ENOMEM);
644 struct v9fs_fcall *v9fs_create_topen(u32 fid, u8 mode)
647 struct v9fs_fcall *fc;
649 struct cbuf *bufp = &buffer;
651 size = 4 + 1; /* fid[4] mode[1] */
652 fc = v9fs_create_common(bufp, size, TOPEN);
656 v9fs_put_int32(bufp, fid, &fc->params.topen.fid);
657 v9fs_put_int8(bufp, mode, &fc->params.topen.mode);
659 if (buf_check_overflow(bufp)) {
661 fc = ERR_PTR(-ENOMEM);
667 struct v9fs_fcall *v9fs_create_tcreate(u32 fid, char *name, u32 perm, u8 mode)
670 struct v9fs_fcall *fc;
672 struct cbuf *bufp = &buffer;
674 size = 4 + 2 + strlen(name) + 4 + 1; /* fid[4] name[s] perm[4] mode[1] */
675 fc = v9fs_create_common(bufp, size, TCREATE);
679 v9fs_put_int32(bufp, fid, &fc->params.tcreate.fid);
680 v9fs_put_str(bufp, name, &fc->params.tcreate.name);
681 v9fs_put_int32(bufp, perm, &fc->params.tcreate.perm);
682 v9fs_put_int8(bufp, mode, &fc->params.tcreate.mode);
684 if (buf_check_overflow(bufp)) {
686 fc = ERR_PTR(-ENOMEM);
692 struct v9fs_fcall *v9fs_create_tread(u32 fid, u64 offset, u32 count)
695 struct v9fs_fcall *fc;
697 struct cbuf *bufp = &buffer;
699 size = 4 + 8 + 4; /* fid[4] offset[8] count[4] */
700 fc = v9fs_create_common(bufp, size, TREAD);
704 v9fs_put_int32(bufp, fid, &fc->params.tread.fid);
705 v9fs_put_int64(bufp, offset, &fc->params.tread.offset);
706 v9fs_put_int32(bufp, count, &fc->params.tread.count);
708 if (buf_check_overflow(bufp)) {
710 fc = ERR_PTR(-ENOMEM);
716 struct v9fs_fcall *v9fs_create_twrite(u32 fid, u64 offset, u32 count,
717 const char __user * data)
720 struct v9fs_fcall *fc;
722 struct cbuf *bufp = &buffer;
724 size = 4 + 8 + 4 + count; /* fid[4] offset[8] count[4] data[count] */
725 fc = v9fs_create_common(bufp, size, TWRITE);
729 v9fs_put_int32(bufp, fid, &fc->params.twrite.fid);
730 v9fs_put_int64(bufp, offset, &fc->params.twrite.offset);
731 v9fs_put_int32(bufp, count, &fc->params.twrite.count);
732 err = v9fs_put_user_data(bufp, data, count, &fc->params.twrite.data);
738 if (buf_check_overflow(bufp)) {
740 fc = ERR_PTR(-ENOMEM);
746 struct v9fs_fcall *v9fs_create_tclunk(u32 fid)
749 struct v9fs_fcall *fc;
751 struct cbuf *bufp = &buffer;
753 size = 4; /* fid[4] */
754 fc = v9fs_create_common(bufp, size, TCLUNK);
758 v9fs_put_int32(bufp, fid, &fc->params.tclunk.fid);
760 if (buf_check_overflow(bufp)) {
762 fc = ERR_PTR(-ENOMEM);
768 struct v9fs_fcall *v9fs_create_tremove(u32 fid)
771 struct v9fs_fcall *fc;
773 struct cbuf *bufp = &buffer;
775 size = 4; /* fid[4] */
776 fc = v9fs_create_common(bufp, size, TREMOVE);
780 v9fs_put_int32(bufp, fid, &fc->params.tremove.fid);
782 if (buf_check_overflow(bufp)) {
784 fc = ERR_PTR(-ENOMEM);
790 struct v9fs_fcall *v9fs_create_tstat(u32 fid)
793 struct v9fs_fcall *fc;
795 struct cbuf *bufp = &buffer;
797 size = 4; /* fid[4] */
798 fc = v9fs_create_common(bufp, size, TSTAT);
802 v9fs_put_int32(bufp, fid, &fc->params.tstat.fid);
804 if (buf_check_overflow(bufp)) {
806 fc = ERR_PTR(-ENOMEM);
812 struct v9fs_fcall *v9fs_create_twstat(u32 fid, struct v9fs_wstat *wstat,
816 struct v9fs_fcall *fc;
818 struct cbuf *bufp = &buffer;
820 statsz = v9fs_size_wstat(wstat, extended);
821 size = 4 + 2 + 2 + statsz; /* fid[4] stat[n] */
822 fc = v9fs_create_common(bufp, size, TWSTAT);
826 v9fs_put_int32(bufp, fid, &fc->params.twstat.fid);
827 buf_put_int16(bufp, statsz + 2);
828 v9fs_put_wstat(bufp, wstat, &fc->params.twstat.stat, statsz, extended);
830 if (buf_check_overflow(bufp)) {
832 fc = ERR_PTR(-ENOMEM);