4 * 9P Protocol Support Code
6 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Base on code from Anthony Liguori <aliguori@us.ibm.com>
9 * Copyright (C) 2008 by IBM, Corp.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
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/module.h>
29 #include <linux/errno.h>
30 #include <linux/uaccess.h>
31 #include <linux/sched.h>
32 #include <linux/types.h>
33 #include <net/9p/9p.h>
34 #include <net/9p/client.h>
38 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
42 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
46 #define offset_of(type, memb) \
47 ((unsigned long)(&((type *)0)->memb))
50 #define container_of(obj, type, memb) \
51 ((type *)(((char *)obj) - offset_of(type, memb)))
55 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...);
57 #ifdef CONFIG_NET_9P_DEBUG
59 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
62 u8
*data
= pdu
->sdata
;
63 int datalen
= pdu
->size
;
68 if (datalen
> (buflen
-16))
71 n
+= scnprintf(buf
+ n
, buflen
- n
, "%02x ", data
[i
]);
73 n
+= scnprintf(buf
+ n
, buflen
- n
, " ");
75 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
79 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
82 P9_DPRINTK(P9_DEBUG_PKT
, "[[[(%d) %s\n", datalen
, buf
);
84 P9_DPRINTK(P9_DEBUG_PKT
, "]]](%d) %s\n", datalen
, buf
);
88 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
92 EXPORT_SYMBOL(p9pdu_dump
);
94 void p9stat_free(struct p9_wstat
*stbuf
)
100 kfree(stbuf
->extension
);
102 EXPORT_SYMBOL(p9stat_free
);
104 static size_t pdu_read(struct p9_fcall
*pdu
, void *data
, size_t size
)
106 size_t len
= MIN(pdu
->size
- pdu
->offset
, size
);
107 memcpy(data
, &pdu
->sdata
[pdu
->offset
], len
);
112 static size_t pdu_write(struct p9_fcall
*pdu
, const void *data
, size_t size
)
114 size_t len
= MIN(pdu
->capacity
- pdu
->size
, size
);
115 memcpy(&pdu
->sdata
[pdu
->size
], data
, len
);
121 pdu_write_u(struct p9_fcall
*pdu
, const char __user
*udata
, size_t size
)
123 size_t len
= MIN(pdu
->capacity
- pdu
->size
, size
);
124 int err
= copy_from_user(&pdu
->sdata
[pdu
->size
], udata
, len
);
126 printk(KERN_WARNING
"pdu_write_u returning: %d\n", err
);
140 D - data blob (int32_t size followed by void *, results are not freed)
141 T - array of strings (int16_t count, followed by strings)
142 R - array of qids (int16_t count, followed by qids)
143 ? - if optional = 1, continue parsing
147 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
153 for (ptr
= fmt
; *ptr
; ptr
++) {
156 int8_t *val
= va_arg(ap
, int8_t *);
157 if (pdu_read(pdu
, val
, sizeof(*val
))) {
164 int16_t *val
= va_arg(ap
, int16_t *);
166 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
170 *val
= le16_to_cpu(le_val
);
174 int32_t *val
= va_arg(ap
, int32_t *);
176 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
180 *val
= le32_to_cpu(le_val
);
184 int64_t *val
= va_arg(ap
, int64_t *);
186 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
190 *val
= le64_to_cpu(le_val
);
194 char **sptr
= va_arg(ap
, char **);
198 errcode
= p9pdu_readf(pdu
, proto_version
,
205 *sptr
= kmalloc(size
+ 1, GFP_KERNEL
);
210 if (pdu_read(pdu
, *sptr
, size
)) {
220 va_arg(ap
, struct p9_qid
*);
222 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
223 &qid
->type
, &qid
->version
,
228 struct p9_wstat
*stbuf
=
229 va_arg(ap
, struct p9_wstat
*);
231 memset(stbuf
, 0, sizeof(struct p9_wstat
));
232 stbuf
->n_uid
= stbuf
->n_gid
= stbuf
->n_muid
=
235 p9pdu_readf(pdu
, proto_version
,
237 &stbuf
->size
, &stbuf
->type
,
238 &stbuf
->dev
, &stbuf
->qid
,
239 &stbuf
->mode
, &stbuf
->atime
,
240 &stbuf
->mtime
, &stbuf
->length
,
241 &stbuf
->name
, &stbuf
->uid
,
242 &stbuf
->gid
, &stbuf
->muid
,
244 &stbuf
->n_uid
, &stbuf
->n_gid
,
251 int32_t *count
= va_arg(ap
, int32_t *);
252 void **data
= va_arg(ap
, void **);
255 p9pdu_readf(pdu
, proto_version
, "d", count
);
259 pdu
->size
- pdu
->offset
);
260 *data
= &pdu
->sdata
[pdu
->offset
];
265 int16_t *nwname
= va_arg(ap
, int16_t *);
266 char ***wnames
= va_arg(ap
, char ***);
268 errcode
= p9pdu_readf(pdu
, proto_version
,
272 kmalloc(sizeof(char *) * *nwname
,
281 for (i
= 0; i
< *nwname
; i
++) {
296 for (i
= 0; i
< *nwname
; i
++)
305 int16_t *nwqid
= va_arg(ap
, int16_t *);
306 struct p9_qid
**wqids
=
307 va_arg(ap
, struct p9_qid
**);
312 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
316 sizeof(struct p9_qid
),
325 for (i
= 0; i
< *nwqid
; i
++) {
343 if (proto_version
!= p9_proto_2000u
)
359 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
365 for (ptr
= fmt
; *ptr
; ptr
++) {
368 int8_t val
= va_arg(ap
, int);
369 if (pdu_write(pdu
, &val
, sizeof(val
)))
374 __le16 val
= cpu_to_le16(va_arg(ap
, int));
375 if (pdu_write(pdu
, &val
, sizeof(val
)))
380 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
381 if (pdu_write(pdu
, &val
, sizeof(val
)))
386 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
387 if (pdu_write(pdu
, &val
, sizeof(val
)))
392 const char *sptr
= va_arg(ap
, const char *);
395 len
= MIN(strlen(sptr
), USHORT_MAX
);
397 errcode
= p9pdu_writef(pdu
, proto_version
,
399 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
404 const struct p9_qid
*qid
=
405 va_arg(ap
, const struct p9_qid
*);
407 p9pdu_writef(pdu
, proto_version
, "bdq",
408 qid
->type
, qid
->version
,
412 const struct p9_wstat
*stbuf
=
413 va_arg(ap
, const struct p9_wstat
*);
415 p9pdu_writef(pdu
, proto_version
,
417 stbuf
->size
, stbuf
->type
,
418 stbuf
->dev
, &stbuf
->qid
,
419 stbuf
->mode
, stbuf
->atime
,
420 stbuf
->mtime
, stbuf
->length
,
421 stbuf
->name
, stbuf
->uid
,
422 stbuf
->gid
, stbuf
->muid
,
423 stbuf
->extension
, stbuf
->n_uid
,
424 stbuf
->n_gid
, stbuf
->n_muid
);
427 int32_t count
= va_arg(ap
, int32_t);
428 const void *data
= va_arg(ap
, const void *);
430 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
432 if (!errcode
&& pdu_write(pdu
, data
, count
))
437 int32_t count
= va_arg(ap
, int32_t);
438 const char __user
*udata
=
439 va_arg(ap
, const void __user
*);
440 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
442 if (!errcode
&& pdu_write_u(pdu
, udata
, count
))
447 int16_t nwname
= va_arg(ap
, int);
448 const char **wnames
= va_arg(ap
, const char **);
450 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
455 for (i
= 0; i
< nwname
; i
++) {
468 int16_t nwqid
= va_arg(ap
, int);
469 struct p9_qid
*wqids
=
470 va_arg(ap
, struct p9_qid
*);
472 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
477 for (i
= 0; i
< nwqid
; i
++) {
490 if (proto_version
!= p9_proto_2000u
)
505 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
511 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
518 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
524 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
530 int p9stat_read(char *buf
, int len
, struct p9_wstat
*st
, int proto_version
)
532 struct p9_fcall fake_pdu
;
536 fake_pdu
.capacity
= len
;
537 fake_pdu
.sdata
= buf
;
540 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "S", st
);
542 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
543 p9pdu_dump(1, &fake_pdu
);
548 EXPORT_SYMBOL(p9stat_read
);
550 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
552 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
555 int p9pdu_finalize(struct p9_fcall
*pdu
)
557 int size
= pdu
->size
;
561 err
= p9pdu_writef(pdu
, 0, "d", size
);
564 #ifdef CONFIG_NET_9P_DEBUG
565 if ((p9_debug_level
& P9_DEBUG_PKT
) == P9_DEBUG_PKT
)
569 P9_DPRINTK(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n", pdu
->size
,
575 void p9pdu_reset(struct p9_fcall
*pdu
)