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 <net/9p/9p.h>
32 #include <net/9p/client.h>
36 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
40 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
44 #define offset_of(type, memb) \
45 ((unsigned long)(&((type *)0)->memb))
48 #define container_of(obj, type, memb) \
49 ((type *)(((char *)obj) - offset_of(type, memb)))
53 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...);
55 #define PACKET_DEBUG 0
58 p9pdu_dump(int way, struct p9_fcall *pdu)
61 u8 *data = pdu->sdata;
62 int datalen = pdu->size;
67 if (datalen > (buflen-16))
70 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
72 n += scnprintf(buf + n, buflen - n, " ");
74 n += scnprintf(buf + n, buflen - n, "\n");
78 n += scnprintf(buf + n, buflen - n, "\n");
81 printk(KERN_NOTICE "[[(%d)[ %s\n", datalen, buf);
83 printk(KERN_NOTICE "]](%d)] %s\n", datalen, buf);
85 EXPORT_SYMBOL(p9pdu_dump);
87 void p9stat_free(struct p9_wstat *stbuf)
93 kfree(stbuf->extension);
95 EXPORT_SYMBOL(p9stat_free);
97 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
99 size_t len = MIN(pdu->size - pdu->offset, size);
100 memcpy(data, &pdu->sdata[pdu->offset], len);
105 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
107 size_t len = MIN(pdu->capacity - pdu->size, size);
108 memcpy(&pdu->sdata[pdu->size], data, len);
114 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
116 size_t len = MIN(pdu->capacity - pdu->size, size);
117 int err = copy_from_user(&pdu->sdata[pdu->size], udata, len);
119 printk(KERN_WARNING "pdu_write_u returning: %d\n", err);
133 D - data blob (int32_t size followed by void *, results are not freed)
134 T - array of strings (int16_t count, followed by strings)
135 R - array of qids (int16_t count, followed by qids)
136 ? - if optional = 1, continue parsing
140 p9pdu_vreadf(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
145 for (ptr = fmt; *ptr; ptr++) {
148 int8_t *val = va_arg(ap, int8_t *);
149 if (pdu_read(pdu, val, sizeof(*val))) {
156 int16_t *val = va_arg(ap, int16_t *);
157 if (pdu_read(pdu, val, sizeof(*val))) {
161 *val = cpu_to_le16(*val);
165 int32_t *val = va_arg(ap, int32_t *);
166 if (pdu_read(pdu, val, sizeof(*val))) {
170 *val = cpu_to_le32(*val);
174 int64_t *val = va_arg(ap, int64_t *);
175 if (pdu_read(pdu, val, sizeof(*val))) {
179 *val = cpu_to_le64(*val);
183 char **ptr = va_arg(ap, char **);
187 errcode = p9pdu_readf(pdu, optional, "w", &len);
193 *ptr = kmalloc(size + 1, GFP_KERNEL);
198 if (pdu_read(pdu, *ptr, size)) {
208 va_arg(ap, struct p9_qid *);
210 errcode = p9pdu_readf(pdu, optional, "bdq",
211 &qid->type, &qid->version,
216 struct p9_wstat *stbuf =
217 va_arg(ap, struct p9_wstat *);
219 stbuf->extension = NULL;
220 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
223 p9pdu_readf(pdu, optional,
225 &stbuf->size, &stbuf->type,
226 &stbuf->dev, &stbuf->qid,
227 &stbuf->mode, &stbuf->atime,
228 &stbuf->mtime, &stbuf->length,
229 &stbuf->name, &stbuf->uid,
230 &stbuf->gid, &stbuf->muid,
232 &stbuf->n_uid, &stbuf->n_gid,
239 int32_t *count = va_arg(ap, int32_t *);
240 void **data = va_arg(ap, void **);
243 p9pdu_readf(pdu, optional, "d", count);
247 pdu->size - pdu->offset);
248 *data = &pdu->sdata[pdu->offset];
253 int16_t *nwname = va_arg(ap, int16_t *);
254 char ***wnames = va_arg(ap, char ***);
257 p9pdu_readf(pdu, optional, "w", nwname);
260 kmalloc(sizeof(char *) * *nwname,
269 for (i = 0; i < *nwname; i++) {
271 p9pdu_readf(pdu, optional,
283 for (i = 0; i < *nwname; i++)
292 int16_t *nwqid = va_arg(ap, int16_t *);
293 struct p9_qid **wqids =
294 va_arg(ap, struct p9_qid **);
299 p9pdu_readf(pdu, optional, "w", nwqid);
303 sizeof(struct p9_qid),
312 for (i = 0; i < *nwqid; i++) {
314 p9pdu_readf(pdu, optional,
345 p9pdu_vwritef(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
350 for (ptr = fmt; *ptr; ptr++) {
353 int8_t val = va_arg(ap, int);
354 if (pdu_write(pdu, &val, sizeof(val)))
359 int16_t val = va_arg(ap, int);
360 if (pdu_write(pdu, &val, sizeof(val)))
365 int32_t val = va_arg(ap, int32_t);
366 if (pdu_write(pdu, &val, sizeof(val)))
371 int64_t val = va_arg(ap, int64_t);
372 if (pdu_write(pdu, &val, sizeof(val)))
377 const char *ptr = va_arg(ap, const char *);
380 len = MIN(strlen(ptr), USHORT_MAX);
382 errcode = p9pdu_writef(pdu, optional, "w", len);
383 if (!errcode && pdu_write(pdu, ptr, len))
388 const struct p9_qid *qid =
389 va_arg(ap, const struct p9_qid *);
391 p9pdu_writef(pdu, optional, "bdq",
392 qid->type, qid->version,
396 const struct p9_wstat *stbuf =
397 va_arg(ap, const struct p9_wstat *);
399 p9pdu_writef(pdu, optional,
401 stbuf->size, stbuf->type,
402 stbuf->dev, &stbuf->qid,
403 stbuf->mode, stbuf->atime,
404 stbuf->mtime, stbuf->length,
405 stbuf->name, stbuf->uid,
406 stbuf->gid, stbuf->muid,
407 stbuf->extension, stbuf->n_uid,
408 stbuf->n_gid, stbuf->n_muid);
411 int32_t count = va_arg(ap, int32_t);
412 const void *data = va_arg(ap, const void *);
415 p9pdu_writef(pdu, optional, "d", count);
416 if (!errcode && pdu_write(pdu, data, count))
421 int32_t count = va_arg(ap, int32_t);
422 const char __user *udata =
423 va_arg(ap, const void *);
425 p9pdu_writef(pdu, optional, "d", count);
426 if (!errcode && pdu_write_u(pdu, udata, count))
431 int16_t nwname = va_arg(ap, int);
432 const char **wnames = va_arg(ap, const char **);
435 p9pdu_writef(pdu, optional, "w", nwname);
439 for (i = 0; i < nwname; i++) {
441 p9pdu_writef(pdu, optional,
451 int16_t nwqid = va_arg(ap, int);
452 struct p9_qid *wqids =
453 va_arg(ap, struct p9_qid *);
456 p9pdu_writef(pdu, optional, "w", nwqid);
460 for (i = 0; i < nwqid; i++) {
462 p9pdu_writef(pdu, optional,
487 int p9pdu_readf(struct p9_fcall *pdu, int optional, const char *fmt, ...)
493 ret = p9pdu_vreadf(pdu, optional, fmt, ap);
500 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...)
506 ret = p9pdu_vwritef(pdu, optional, fmt, ap);
512 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
514 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
517 int p9pdu_finalize(struct p9_fcall *pdu)
519 int size = pdu->size;
523 err = p9pdu_writef(pdu, 0, "d", size);
532 void p9pdu_reset(struct p9_fcall *pdu)