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/config.h>
28 #include <linux/module.h>
29 #include <linux/errno.h>
31 #include <linux/idr.h>
32 #include <asm/uaccess.h>
39 * Buffer to help with string parsing
47 static inline void buf_init(struct cbuf
*buf
, void *data
, int datalen
)
49 buf
->sp
= buf
->p
= data
;
50 buf
->ep
= data
+ datalen
;
53 static inline int buf_check_overflow(struct cbuf
*buf
)
55 return buf
->p
> buf
->ep
;
58 static int buf_check_size(struct cbuf
*buf
, int len
)
60 if (buf
->p
+ len
> buf
->ep
) {
61 if (buf
->p
< buf
->ep
) {
62 eprintk(KERN_ERR
, "buffer overflow: want %d has %d\n",
63 len
, (int)(buf
->ep
- buf
->p
));
74 static void *buf_alloc(struct cbuf
*buf
, int len
)
78 if (buf_check_size(buf
, len
)) {
86 static void buf_put_int8(struct cbuf
*buf
, u8 val
)
88 if (buf_check_size(buf
, 1)) {
94 static void buf_put_int16(struct cbuf
*buf
, u16 val
)
96 if (buf_check_size(buf
, 2)) {
97 *(__le16
*) buf
->p
= cpu_to_le16(val
);
102 static void buf_put_int32(struct cbuf
*buf
, u32 val
)
104 if (buf_check_size(buf
, 4)) {
105 *(__le32
*)buf
->p
= cpu_to_le32(val
);
110 static void buf_put_int64(struct cbuf
*buf
, u64 val
)
112 if (buf_check_size(buf
, 8)) {
113 *(__le64
*)buf
->p
= cpu_to_le64(val
);
118 static char *buf_put_stringn(struct cbuf
*buf
, const char *s
, u16 slen
)
123 if (buf_check_size(buf
, slen
+ 2)) {
124 buf_put_int16(buf
, slen
);
126 memcpy(buf
->p
, s
, slen
);
133 static inline void buf_put_string(struct cbuf
*buf
, const char *s
)
135 buf_put_stringn(buf
, s
, strlen(s
));
138 static u8
buf_get_int8(struct cbuf
*buf
)
142 if (buf_check_size(buf
, 1)) {
150 static u16
buf_get_int16(struct cbuf
*buf
)
154 if (buf_check_size(buf
, 2)) {
155 ret
= le16_to_cpu(*(__le16
*)buf
->p
);
162 static u32
buf_get_int32(struct cbuf
*buf
)
166 if (buf_check_size(buf
, 4)) {
167 ret
= le32_to_cpu(*(__le32
*)buf
->p
);
174 static u64
buf_get_int64(struct cbuf
*buf
)
178 if (buf_check_size(buf
, 8)) {
179 ret
= le64_to_cpu(*(__le64
*)buf
->p
);
186 static void buf_get_str(struct cbuf
*buf
, struct v9fs_str
*vstr
)
188 vstr
->len
= buf_get_int16(buf
);
189 if (!buf_check_overflow(buf
) && buf_check_size(buf
, vstr
->len
)) {
198 static void buf_get_qid(struct cbuf
*bufp
, struct v9fs_qid
*qid
)
200 qid
->type
= buf_get_int8(bufp
);
201 qid
->version
= buf_get_int32(bufp
);
202 qid
->path
= buf_get_int64(bufp
);
206 * v9fs_size_wstat - calculate the size of a variable length stat struct
207 * @stat: metadata (stat) structure
208 * @extended: non-zero if 9P2000.u
212 static int v9fs_size_wstat(struct v9fs_wstat
*wstat
, int extended
)
217 eprintk(KERN_ERR
, "v9fs_size_stat: got a NULL stat pointer\n");
221 size
= /* 2 + *//* size[2] */
224 1 + /* qid.type[1] */
225 4 + /* qid.vers[4] */
226 8 + /* qid.path[8] */
231 8; /* minimum sum of string lengths */
234 size
+= strlen(wstat
->name
);
236 size
+= strlen(wstat
->uid
);
238 size
+= strlen(wstat
->gid
);
240 size
+= strlen(wstat
->muid
);
243 size
+= 4 + /* n_uid[4] */
246 2; /* string length of extension[4] */
247 if (wstat
->extension
)
248 size
+= strlen(wstat
->extension
);
255 * buf_get_stat - safely decode a recieved metadata (stat) structure
256 * @bufp: buffer to deserialize
257 * @stat: metadata (stat) structure
258 * @extended: non-zero if 9P2000.u
263 buf_get_stat(struct cbuf
*bufp
, struct v9fs_stat
*stat
, int extended
)
265 stat
->size
= buf_get_int16(bufp
);
266 stat
->type
= buf_get_int16(bufp
);
267 stat
->dev
= buf_get_int32(bufp
);
268 stat
->qid
.type
= buf_get_int8(bufp
);
269 stat
->qid
.version
= buf_get_int32(bufp
);
270 stat
->qid
.path
= buf_get_int64(bufp
);
271 stat
->mode
= buf_get_int32(bufp
);
272 stat
->atime
= buf_get_int32(bufp
);
273 stat
->mtime
= buf_get_int32(bufp
);
274 stat
->length
= buf_get_int64(bufp
);
275 buf_get_str(bufp
, &stat
->name
);
276 buf_get_str(bufp
, &stat
->uid
);
277 buf_get_str(bufp
, &stat
->gid
);
278 buf_get_str(bufp
, &stat
->muid
);
281 buf_get_str(bufp
, &stat
->extension
);
282 stat
->n_uid
= buf_get_int32(bufp
);
283 stat
->n_gid
= buf_get_int32(bufp
);
284 stat
->n_muid
= buf_get_int32(bufp
);
289 * v9fs_deserialize_stat - decode a received metadata structure
290 * @buf: buffer to deserialize
291 * @buflen: length of received buffer
292 * @stat: metadata structure to decode into
293 * @extended: non-zero if 9P2000.u
295 * Note: stat will point to the buf region.
299 v9fs_deserialize_stat(void *buf
, u32 buflen
, struct v9fs_stat
*stat
,
303 struct cbuf
*bufp
= &buffer
;
306 buf_init(bufp
, buf
, buflen
);
308 buf_get_stat(bufp
, stat
, extended
);
310 if (buf_check_overflow(bufp
))
317 * deserialize_fcall - unmarshal a response
318 * @buf: recieved buffer
319 * @buflen: length of received buffer
320 * @rcall: fcall structure to populate
321 * @rcalllen: length of fcall structure to populate
322 * @extended: non-zero if 9P2000.u
327 v9fs_deserialize_fcall(void *buf
, u32 buflen
, struct v9fs_fcall
*rcall
,
332 struct cbuf
*bufp
= &buffer
;
335 buf_init(bufp
, buf
, buflen
);
337 rcall
->size
= buf_get_int32(bufp
);
338 rcall
->id
= buf_get_int8(bufp
);
339 rcall
->tag
= buf_get_int16(bufp
);
341 dprintk(DEBUG_CONV
, "size %d id %d tag %d\n", rcall
->size
, rcall
->id
,
346 eprintk(KERN_ERR
, "unknown message type: %d\n", rcall
->id
);
349 rcall
->params
.rversion
.msize
= buf_get_int32(bufp
);
350 buf_get_str(bufp
, &rcall
->params
.rversion
.version
);
355 rcall
->params
.rattach
.qid
.type
= buf_get_int8(bufp
);
356 rcall
->params
.rattach
.qid
.version
= buf_get_int32(bufp
);
357 rcall
->params
.rattach
.qid
.path
= buf_get_int64(bufp
);
360 rcall
->params
.rwalk
.nwqid
= buf_get_int16(bufp
);
361 if (rcall
->params
.rwalk
.nwqid
> V9FS_MAXWELEM
) {
362 eprintk(KERN_ERR
, "Rwalk with more than %d qids: %d\n",
363 V9FS_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
, extended
);
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 dprintk(DEBUG_ERROR
, "buffer overflow\n");
408 return bufp
->p
- bufp
->sp
;
411 static inline void v9fs_put_int8(struct cbuf
*bufp
, u8 val
, u8
* p
)
414 buf_put_int8(bufp
, val
);
417 static inline void v9fs_put_int16(struct cbuf
*bufp
, u16 val
, u16
* p
)
420 buf_put_int16(bufp
, val
);
423 static inline void v9fs_put_int32(struct cbuf
*bufp
, u32 val
, u32
* p
)
426 buf_put_int32(bufp
, val
);
429 static inline void v9fs_put_int64(struct cbuf
*bufp
, u64 val
, u64
* p
)
432 buf_put_int64(bufp
, val
);
436 v9fs_put_str(struct cbuf
*bufp
, char *data
, struct v9fs_str
*str
)
446 s
= buf_put_stringn(bufp
, data
, len
);
454 v9fs_put_user_data(struct cbuf
*bufp
, const char __user
* data
, int count
,
455 unsigned char **pdata
)
457 *pdata
= buf_alloc(bufp
, count
);
458 return copy_from_user(*pdata
, data
, count
);
462 v9fs_put_wstat(struct cbuf
*bufp
, struct v9fs_wstat
*wstat
,
463 struct v9fs_stat
*stat
, int statsz
, int extended
)
465 v9fs_put_int16(bufp
, statsz
, &stat
->size
);
466 v9fs_put_int16(bufp
, wstat
->type
, &stat
->type
);
467 v9fs_put_int32(bufp
, wstat
->dev
, &stat
->dev
);
468 v9fs_put_int8(bufp
, wstat
->qid
.type
, &stat
->qid
.type
);
469 v9fs_put_int32(bufp
, wstat
->qid
.version
, &stat
->qid
.version
);
470 v9fs_put_int64(bufp
, wstat
->qid
.path
, &stat
->qid
.path
);
471 v9fs_put_int32(bufp
, wstat
->mode
, &stat
->mode
);
472 v9fs_put_int32(bufp
, wstat
->atime
, &stat
->atime
);
473 v9fs_put_int32(bufp
, wstat
->mtime
, &stat
->mtime
);
474 v9fs_put_int64(bufp
, wstat
->length
, &stat
->length
);
476 v9fs_put_str(bufp
, wstat
->name
, &stat
->name
);
477 v9fs_put_str(bufp
, wstat
->uid
, &stat
->uid
);
478 v9fs_put_str(bufp
, wstat
->gid
, &stat
->gid
);
479 v9fs_put_str(bufp
, wstat
->muid
, &stat
->muid
);
482 v9fs_put_str(bufp
, wstat
->extension
, &stat
->extension
);
483 v9fs_put_int32(bufp
, wstat
->n_uid
, &stat
->n_uid
);
484 v9fs_put_int32(bufp
, wstat
->n_gid
, &stat
->n_gid
);
485 v9fs_put_int32(bufp
, wstat
->n_muid
, &stat
->n_muid
);
489 static struct v9fs_fcall
*
490 v9fs_create_common(struct cbuf
*bufp
, u32 size
, u8 id
)
492 struct v9fs_fcall
*fc
;
494 size
+= 4 + 1 + 2; /* size[4] id[1] tag[2] */
495 fc
= kmalloc(sizeof(struct v9fs_fcall
) + size
, GFP_KERNEL
);
497 return ERR_PTR(-ENOMEM
);
499 fc
->sdata
= (char *)fc
+ sizeof(*fc
);
501 buf_init(bufp
, (char *)fc
->sdata
, size
);
502 v9fs_put_int32(bufp
, size
, &fc
->size
);
503 v9fs_put_int8(bufp
, id
, &fc
->id
);
504 v9fs_put_int16(bufp
, V9FS_NOTAG
, &fc
->tag
);
509 void v9fs_set_tag(struct v9fs_fcall
*fc
, u16 tag
)
512 *(__le16
*) (fc
->sdata
+ 5) = cpu_to_le16(tag
);
515 struct v9fs_fcall
*v9fs_create_tversion(u32 msize
, char *version
)
518 struct v9fs_fcall
*fc
;
520 struct cbuf
*bufp
= &buffer
;
522 size
= 4 + 2 + strlen(version
); /* msize[4] version[s] */
523 fc
= v9fs_create_common(bufp
, size
, TVERSION
);
527 v9fs_put_int32(bufp
, msize
, &fc
->params
.tversion
.msize
);
528 v9fs_put_str(bufp
, version
, &fc
->params
.tversion
.version
);
530 if (buf_check_overflow(bufp
)) {
532 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
);
565 v9fs_create_tattach(u32 fid
, u32 afid
, char *uname
, char *aname
)
568 struct v9fs_fcall
*fc
;
570 struct cbuf
*bufp
= &buffer
;
572 size
= 4 + 4 + 2 + strlen(uname
) + 2 + strlen(aname
); /* fid[4] afid[4] uname[s] aname[s] */
573 fc
= v9fs_create_common(bufp
, size
, TATTACH
);
577 v9fs_put_int32(bufp
, fid
, &fc
->params
.tattach
.fid
);
578 v9fs_put_int32(bufp
, afid
, &fc
->params
.tattach
.afid
);
579 v9fs_put_str(bufp
, uname
, &fc
->params
.tattach
.uname
);
580 v9fs_put_str(bufp
, aname
, &fc
->params
.tattach
.aname
);
586 struct v9fs_fcall
*v9fs_create_tflush(u16 oldtag
)
589 struct v9fs_fcall
*fc
;
591 struct cbuf
*bufp
= &buffer
;
593 size
= 2; /* oldtag[2] */
594 fc
= v9fs_create_common(bufp
, size
, TFLUSH
);
598 v9fs_put_int16(bufp
, oldtag
, &fc
->params
.tflush
.oldtag
);
600 if (buf_check_overflow(bufp
)) {
602 fc
= ERR_PTR(-ENOMEM
);
608 struct v9fs_fcall
*v9fs_create_twalk(u32 fid
, u32 newfid
, u16 nwname
,
612 struct v9fs_fcall
*fc
;
614 struct cbuf
*bufp
= &buffer
;
616 if (nwname
> V9FS_MAXWELEM
) {
617 dprintk(DEBUG_ERROR
, "nwname > %d\n", V9FS_MAXWELEM
);
621 size
= 4 + 4 + 2; /* fid[4] newfid[4] nwname[2] ... */
622 for (i
= 0; i
< nwname
; i
++) {
623 size
+= 2 + strlen(wnames
[i
]); /* wname[s] */
626 fc
= v9fs_create_common(bufp
, size
, TWALK
);
630 v9fs_put_int32(bufp
, fid
, &fc
->params
.twalk
.fid
);
631 v9fs_put_int32(bufp
, newfid
, &fc
->params
.twalk
.newfid
);
632 v9fs_put_int16(bufp
, nwname
, &fc
->params
.twalk
.nwname
);
633 for (i
= 0; i
< nwname
; i
++) {
634 v9fs_put_str(bufp
, wnames
[i
], &fc
->params
.twalk
.wnames
[i
]);
637 if (buf_check_overflow(bufp
)) {
639 fc
= ERR_PTR(-ENOMEM
);
645 struct v9fs_fcall
*v9fs_create_topen(u32 fid
, u8 mode
)
648 struct v9fs_fcall
*fc
;
650 struct cbuf
*bufp
= &buffer
;
652 size
= 4 + 1; /* fid[4] mode[1] */
653 fc
= v9fs_create_common(bufp
, size
, TOPEN
);
657 v9fs_put_int32(bufp
, fid
, &fc
->params
.topen
.fid
);
658 v9fs_put_int8(bufp
, mode
, &fc
->params
.topen
.mode
);
660 if (buf_check_overflow(bufp
)) {
662 fc
= ERR_PTR(-ENOMEM
);
668 struct v9fs_fcall
*v9fs_create_tcreate(u32 fid
, char *name
, u32 perm
, u8 mode
,
669 char *extension
, int extended
)
672 struct v9fs_fcall
*fc
;
674 struct cbuf
*bufp
= &buffer
;
676 size
= 4 + 2 + strlen(name
) + 4 + 1; /* fid[4] name[s] perm[4] mode[1] */
677 if (extended
&& extension
!=NULL
)
678 size
+= 2 + strlen(extension
); /* extension[s] */
680 fc
= v9fs_create_common(bufp
, size
, TCREATE
);
684 v9fs_put_int32(bufp
, fid
, &fc
->params
.tcreate
.fid
);
685 v9fs_put_str(bufp
, name
, &fc
->params
.tcreate
.name
);
686 v9fs_put_int32(bufp
, perm
, &fc
->params
.tcreate
.perm
);
687 v9fs_put_int8(bufp
, mode
, &fc
->params
.tcreate
.mode
);
689 v9fs_put_str(bufp
, extension
, &fc
->params
.tcreate
.extension
);
691 if (buf_check_overflow(bufp
)) {
693 fc
= ERR_PTR(-ENOMEM
);
699 struct v9fs_fcall
*v9fs_create_tread(u32 fid
, u64 offset
, u32 count
)
702 struct v9fs_fcall
*fc
;
704 struct cbuf
*bufp
= &buffer
;
706 size
= 4 + 8 + 4; /* fid[4] offset[8] count[4] */
707 fc
= v9fs_create_common(bufp
, size
, TREAD
);
711 v9fs_put_int32(bufp
, fid
, &fc
->params
.tread
.fid
);
712 v9fs_put_int64(bufp
, offset
, &fc
->params
.tread
.offset
);
713 v9fs_put_int32(bufp
, count
, &fc
->params
.tread
.count
);
715 if (buf_check_overflow(bufp
)) {
717 fc
= ERR_PTR(-ENOMEM
);
723 struct v9fs_fcall
*v9fs_create_twrite(u32 fid
, u64 offset
, u32 count
,
724 const char __user
* data
)
727 struct v9fs_fcall
*fc
;
729 struct cbuf
*bufp
= &buffer
;
731 size
= 4 + 8 + 4 + count
; /* fid[4] offset[8] count[4] data[count] */
732 fc
= v9fs_create_common(bufp
, size
, TWRITE
);
736 v9fs_put_int32(bufp
, fid
, &fc
->params
.twrite
.fid
);
737 v9fs_put_int64(bufp
, offset
, &fc
->params
.twrite
.offset
);
738 v9fs_put_int32(bufp
, count
, &fc
->params
.twrite
.count
);
739 err
= v9fs_put_user_data(bufp
, data
, count
, &fc
->params
.twrite
.data
);
745 if (buf_check_overflow(bufp
)) {
747 fc
= ERR_PTR(-ENOMEM
);
753 struct v9fs_fcall
*v9fs_create_tclunk(u32 fid
)
756 struct v9fs_fcall
*fc
;
758 struct cbuf
*bufp
= &buffer
;
760 size
= 4; /* fid[4] */
761 fc
= v9fs_create_common(bufp
, size
, TCLUNK
);
765 v9fs_put_int32(bufp
, fid
, &fc
->params
.tclunk
.fid
);
767 if (buf_check_overflow(bufp
)) {
769 fc
= ERR_PTR(-ENOMEM
);
775 struct v9fs_fcall
*v9fs_create_tremove(u32 fid
)
778 struct v9fs_fcall
*fc
;
780 struct cbuf
*bufp
= &buffer
;
782 size
= 4; /* fid[4] */
783 fc
= v9fs_create_common(bufp
, size
, TREMOVE
);
787 v9fs_put_int32(bufp
, fid
, &fc
->params
.tremove
.fid
);
789 if (buf_check_overflow(bufp
)) {
791 fc
= ERR_PTR(-ENOMEM
);
797 struct v9fs_fcall
*v9fs_create_tstat(u32 fid
)
800 struct v9fs_fcall
*fc
;
802 struct cbuf
*bufp
= &buffer
;
804 size
= 4; /* fid[4] */
805 fc
= v9fs_create_common(bufp
, size
, TSTAT
);
809 v9fs_put_int32(bufp
, fid
, &fc
->params
.tstat
.fid
);
811 if (buf_check_overflow(bufp
)) {
813 fc
= ERR_PTR(-ENOMEM
);
819 struct v9fs_fcall
*v9fs_create_twstat(u32 fid
, struct v9fs_wstat
*wstat
,
823 struct v9fs_fcall
*fc
;
825 struct cbuf
*bufp
= &buffer
;
827 statsz
= v9fs_size_wstat(wstat
, extended
);
828 size
= 4 + 2 + 2 + statsz
; /* fid[4] stat[n] */
829 fc
= v9fs_create_common(bufp
, size
, TWSTAT
);
833 v9fs_put_int32(bufp
, fid
, &fc
->params
.twstat
.fid
);
834 buf_put_int16(bufp
, statsz
+ 2);
835 v9fs_put_wstat(bufp
, wstat
, &fc
->params
.twstat
.stat
, statsz
, extended
);
837 if (buf_check_overflow(bufp
)) {
839 fc
= ERR_PTR(-ENOMEM
);