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 <net/9p/9p.h>
33 #include <net/9p/client.h>
37 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
41 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
45 #define offset_of(type, memb) \
46 ((unsigned long)(&((type *)0)->memb))
49 #define container_of(obj, type, memb) \
50 ((type *)(((char *)obj) - offset_of(type, memb)))
54 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...);
57 p9pdu_dump(int way, struct p9_fcall *pdu)
60 u8 *data = pdu->sdata;
61 int datalen = pdu->size;
66 if (datalen > (buflen-16))
69 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
71 n += scnprintf(buf + n, buflen - n, " ");
73 n += scnprintf(buf + n, buflen - n, "\n");
77 n += scnprintf(buf + n, buflen - n, "\n");
80 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
82 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
84 EXPORT_SYMBOL(p9pdu_dump);
86 void p9stat_free(struct p9_wstat *stbuf)
92 kfree(stbuf->extension);
94 EXPORT_SYMBOL(p9stat_free);
96 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
98 size_t len = MIN(pdu->size - pdu->offset, size);
99 memcpy(data, &pdu->sdata[pdu->offset], len);
104 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
106 size_t len = MIN(pdu->capacity - pdu->size, size);
107 memcpy(&pdu->sdata[pdu->size], data, len);
113 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
115 size_t len = MIN(pdu->capacity - pdu->size, size);
116 int err = copy_from_user(&pdu->sdata[pdu->size], udata, len);
118 printk(KERN_WARNING "pdu_write_u returning: %d\n", err);
132 D - data blob (int32_t size followed by void *, results are not freed)
133 T - array of strings (int16_t count, followed by strings)
134 R - array of qids (int16_t count, followed by qids)
135 ? - if optional = 1, continue parsing
139 p9pdu_vreadf(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
144 for (ptr = fmt; *ptr; ptr++) {
147 int8_t *val = va_arg(ap, int8_t *);
148 if (pdu_read(pdu, val, sizeof(*val))) {
155 int16_t *val = va_arg(ap, int16_t *);
156 if (pdu_read(pdu, val, sizeof(*val))) {
160 *val = cpu_to_le16(*val);
164 int32_t *val = va_arg(ap, int32_t *);
165 if (pdu_read(pdu, val, sizeof(*val))) {
169 *val = cpu_to_le32(*val);
173 int64_t *val = va_arg(ap, int64_t *);
174 if (pdu_read(pdu, val, sizeof(*val))) {
178 *val = cpu_to_le64(*val);
182 char **ptr = va_arg(ap, char **);
186 errcode = p9pdu_readf(pdu, optional, "w", &len);
192 *ptr = kmalloc(size + 1, GFP_KERNEL);
197 if (pdu_read(pdu, *ptr, size)) {
207 va_arg(ap, struct p9_qid *);
209 errcode = p9pdu_readf(pdu, optional, "bdq",
210 &qid->type, &qid->version,
215 struct p9_wstat *stbuf =
216 va_arg(ap, struct p9_wstat *);
218 memset(stbuf, 0, sizeof(struct p9_wstat));
219 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
222 p9pdu_readf(pdu, optional,
224 &stbuf->size, &stbuf->type,
225 &stbuf->dev, &stbuf->qid,
226 &stbuf->mode, &stbuf->atime,
227 &stbuf->mtime, &stbuf->length,
228 &stbuf->name, &stbuf->uid,
229 &stbuf->gid, &stbuf->muid,
231 &stbuf->n_uid, &stbuf->n_gid,
238 int32_t *count = va_arg(ap, int32_t *);
239 void **data = va_arg(ap, void **);
242 p9pdu_readf(pdu, optional, "d", count);
246 pdu->size - pdu->offset);
247 *data = &pdu->sdata[pdu->offset];
252 int16_t *nwname = va_arg(ap, int16_t *);
253 char ***wnames = va_arg(ap, char ***);
256 p9pdu_readf(pdu, optional, "w", nwname);
259 kmalloc(sizeof(char *) * *nwname,
268 for (i = 0; i < *nwname; i++) {
270 p9pdu_readf(pdu, optional,
282 for (i = 0; i < *nwname; i++)
291 int16_t *nwqid = va_arg(ap, int16_t *);
292 struct p9_qid **wqids =
293 va_arg(ap, struct p9_qid **);
298 p9pdu_readf(pdu, optional, "w", nwqid);
302 sizeof(struct p9_qid),
311 for (i = 0; i < *nwqid; i++) {
313 p9pdu_readf(pdu, optional,
344 p9pdu_vwritef(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
349 for (ptr = fmt; *ptr; ptr++) {
352 int8_t val = va_arg(ap, int);
353 if (pdu_write(pdu, &val, sizeof(val)))
358 int16_t val = va_arg(ap, int);
359 if (pdu_write(pdu, &val, sizeof(val)))
364 int32_t val = va_arg(ap, int32_t);
365 if (pdu_write(pdu, &val, sizeof(val)))
370 int64_t val = va_arg(ap, int64_t);
371 if (pdu_write(pdu, &val, sizeof(val)))
376 const char *ptr = va_arg(ap, const char *);
379 len = MIN(strlen(ptr), USHORT_MAX);
381 errcode = p9pdu_writef(pdu, optional, "w", len);
382 if (!errcode && pdu_write(pdu, ptr, len))
387 const struct p9_qid *qid =
388 va_arg(ap, const struct p9_qid *);
390 p9pdu_writef(pdu, optional, "bdq",
391 qid->type, qid->version,
395 const struct p9_wstat *stbuf =
396 va_arg(ap, const struct p9_wstat *);
398 p9pdu_writef(pdu, optional,
400 stbuf->size, stbuf->type,
401 stbuf->dev, &stbuf->qid,
402 stbuf->mode, stbuf->atime,
403 stbuf->mtime, stbuf->length,
404 stbuf->name, stbuf->uid,
405 stbuf->gid, stbuf->muid,
406 stbuf->extension, stbuf->n_uid,
407 stbuf->n_gid, stbuf->n_muid);
410 int32_t count = va_arg(ap, int32_t);
411 const void *data = va_arg(ap, const void *);
414 p9pdu_writef(pdu, optional, "d", count);
415 if (!errcode && pdu_write(pdu, data, count))
420 int32_t count = va_arg(ap, int32_t);
421 const char __user *udata =
422 va_arg(ap, const void *);
424 p9pdu_writef(pdu, optional, "d", count);
425 if (!errcode && pdu_write_u(pdu, udata, count))
430 int16_t nwname = va_arg(ap, int);
431 const char **wnames = va_arg(ap, const char **);
434 p9pdu_writef(pdu, optional, "w", nwname);
438 for (i = 0; i < nwname; i++) {
440 p9pdu_writef(pdu, optional,
450 int16_t nwqid = va_arg(ap, int);
451 struct p9_qid *wqids =
452 va_arg(ap, struct p9_qid *);
455 p9pdu_writef(pdu, optional, "w", nwqid);
459 for (i = 0; i < nwqid; i++) {
461 p9pdu_writef(pdu, optional,
486 int p9pdu_readf(struct p9_fcall *pdu, int optional, const char *fmt, ...)
492 ret = p9pdu_vreadf(pdu, optional, fmt, ap);
499 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...)
505 ret = p9pdu_vwritef(pdu, optional, fmt, ap);
511 int p9stat_read(char *buf, int len, struct p9_wstat *st, int dotu)
513 struct p9_fcall fake_pdu;
517 fake_pdu.capacity = len;
518 fake_pdu.sdata = buf;
521 ret = p9pdu_readf(&fake_pdu, dotu, "S", st);
523 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
524 p9pdu_dump(1, &fake_pdu);
529 EXPORT_SYMBOL(p9stat_read);
531 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
533 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
536 int p9pdu_finalize(struct p9_fcall *pdu)
538 int size = pdu->size;
542 err = p9pdu_writef(pdu, 0, "d", size);
545 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
548 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
554 void p9pdu_reset(struct p9_fcall *pdu)