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 optional, 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 optional, const char *fmt, va_list ap)
152 for (ptr = fmt; *ptr; ptr++) {
155 int8_t *val = va_arg(ap, int8_t *);
156 if (pdu_read(pdu, val, sizeof(*val))) {
163 int16_t *val = va_arg(ap, int16_t *);
165 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
169 *val = le16_to_cpu(le_val);
173 int32_t *val = va_arg(ap, int32_t *);
175 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
179 *val = le32_to_cpu(le_val);
183 int64_t *val = va_arg(ap, int64_t *);
185 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
189 *val = le64_to_cpu(le_val);
193 char **sptr = va_arg(ap, char **);
197 errcode = p9pdu_readf(pdu, optional, "w", &len);
203 *sptr = kmalloc(size + 1, GFP_KERNEL);
208 if (pdu_read(pdu, *sptr, size)) {
218 va_arg(ap, struct p9_qid *);
220 errcode = p9pdu_readf(pdu, optional, "bdq",
221 &qid->type, &qid->version,
226 struct p9_wstat *stbuf =
227 va_arg(ap, struct p9_wstat *);
229 memset(stbuf, 0, sizeof(struct p9_wstat));
230 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
233 p9pdu_readf(pdu, optional,
235 &stbuf->size, &stbuf->type,
236 &stbuf->dev, &stbuf->qid,
237 &stbuf->mode, &stbuf->atime,
238 &stbuf->mtime, &stbuf->length,
239 &stbuf->name, &stbuf->uid,
240 &stbuf->gid, &stbuf->muid,
242 &stbuf->n_uid, &stbuf->n_gid,
249 int32_t *count = va_arg(ap, int32_t *);
250 void **data = va_arg(ap, void **);
253 p9pdu_readf(pdu, optional, "d", count);
257 pdu->size - pdu->offset);
258 *data = &pdu->sdata[pdu->offset];
263 int16_t *nwname = va_arg(ap, int16_t *);
264 char ***wnames = va_arg(ap, char ***);
267 p9pdu_readf(pdu, optional, "w", nwname);
270 kmalloc(sizeof(char *) * *nwname,
279 for (i = 0; i < *nwname; i++) {
281 p9pdu_readf(pdu, optional,
293 for (i = 0; i < *nwname; i++)
302 int16_t *nwqid = va_arg(ap, int16_t *);
303 struct p9_qid **wqids =
304 va_arg(ap, struct p9_qid **);
309 p9pdu_readf(pdu, optional, "w", nwqid);
313 sizeof(struct p9_qid),
322 for (i = 0; i < *nwqid; i++) {
324 p9pdu_readf(pdu, optional,
355 p9pdu_vwritef(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
360 for (ptr = fmt; *ptr; ptr++) {
363 int8_t val = va_arg(ap, int);
364 if (pdu_write(pdu, &val, sizeof(val)))
369 __le16 val = cpu_to_le16(va_arg(ap, int));
370 if (pdu_write(pdu, &val, sizeof(val)))
375 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
376 if (pdu_write(pdu, &val, sizeof(val)))
381 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
382 if (pdu_write(pdu, &val, sizeof(val)))
387 const char *sptr = va_arg(ap, const char *);
390 len = MIN(strlen(sptr), USHORT_MAX);
392 errcode = p9pdu_writef(pdu, optional, "w", len);
393 if (!errcode && pdu_write(pdu, sptr, len))
398 const struct p9_qid *qid =
399 va_arg(ap, const struct p9_qid *);
401 p9pdu_writef(pdu, optional, "bdq",
402 qid->type, qid->version,
406 const struct p9_wstat *stbuf =
407 va_arg(ap, const struct p9_wstat *);
409 p9pdu_writef(pdu, optional,
411 stbuf->size, stbuf->type,
412 stbuf->dev, &stbuf->qid,
413 stbuf->mode, stbuf->atime,
414 stbuf->mtime, stbuf->length,
415 stbuf->name, stbuf->uid,
416 stbuf->gid, stbuf->muid,
417 stbuf->extension, stbuf->n_uid,
418 stbuf->n_gid, stbuf->n_muid);
421 int32_t count = va_arg(ap, int32_t);
422 const void *data = va_arg(ap, const void *);
425 p9pdu_writef(pdu, optional, "d", count);
426 if (!errcode && pdu_write(pdu, data, count))
431 int32_t count = va_arg(ap, int32_t);
432 const char __user *udata =
433 va_arg(ap, const void __user *);
435 p9pdu_writef(pdu, optional, "d", count);
436 if (!errcode && pdu_write_u(pdu, udata, count))
441 int16_t nwname = va_arg(ap, int);
442 const char **wnames = va_arg(ap, const char **);
445 p9pdu_writef(pdu, optional, "w", nwname);
449 for (i = 0; i < nwname; i++) {
451 p9pdu_writef(pdu, optional,
461 int16_t nwqid = va_arg(ap, int);
462 struct p9_qid *wqids =
463 va_arg(ap, struct p9_qid *);
466 p9pdu_writef(pdu, optional, "w", nwqid);
470 for (i = 0; i < nwqid; i++) {
472 p9pdu_writef(pdu, optional,
497 int p9pdu_readf(struct p9_fcall *pdu, int optional, const char *fmt, ...)
503 ret = p9pdu_vreadf(pdu, optional, fmt, ap);
510 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...)
516 ret = p9pdu_vwritef(pdu, optional, fmt, ap);
522 int p9stat_read(char *buf, int len, struct p9_wstat *st, int dotu)
524 struct p9_fcall fake_pdu;
528 fake_pdu.capacity = len;
529 fake_pdu.sdata = buf;
532 ret = p9pdu_readf(&fake_pdu, dotu, "S", st);
534 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
535 p9pdu_dump(1, &fake_pdu);
540 EXPORT_SYMBOL(p9stat_read);
542 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
544 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
547 int p9pdu_finalize(struct p9_fcall *pdu)
549 int size = pdu->size;
553 err = p9pdu_writef(pdu, 0, "d", size);
556 #ifdef CONFIG_NET_9P_DEBUG
557 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
561 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
567 void p9pdu_reset(struct p9_fcall *pdu)