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/kernel.h>
31 #include <linux/uaccess.h>
32 #include <linux/slab.h>
33 #include <linux/sched.h>
34 #include <linux/stddef.h>
35 #include <linux/types.h>
36 #include <net/9p/9p.h>
37 #include <net/9p/client.h>
41 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
43 #ifdef CONFIG_NET_9P_DEBUG
45 p9pdu_dump(int way, struct p9_fcall *pdu)
48 u8 *data = pdu->sdata;
49 int datalen = pdu->size;
54 if (datalen > (buflen-16))
57 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
59 n += scnprintf(buf + n, buflen - n, " ");
61 n += scnprintf(buf + n, buflen - n, "\n");
65 n += scnprintf(buf + n, buflen - n, "\n");
68 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
70 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
74 p9pdu_dump(int way, struct p9_fcall *pdu)
78 EXPORT_SYMBOL(p9pdu_dump);
80 void p9stat_free(struct p9_wstat *stbuf)
86 kfree(stbuf->extension);
88 EXPORT_SYMBOL(p9stat_free);
90 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
92 size_t len = min(pdu->size - pdu->offset, size);
93 memcpy(data, &pdu->sdata[pdu->offset], len);
98 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
100 size_t len = min(pdu->capacity - pdu->size, size);
101 memcpy(&pdu->sdata[pdu->size], data, len);
107 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
109 size_t len = min(pdu->capacity - pdu->size, size);
110 if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
118 pdu_write_urw(struct p9_fcall *pdu, const char *kdata, const char __user *udata,
121 BUG_ON(pdu->size > P9_IOHDRSZ);
122 pdu->pubuf = (char __user *)udata;
123 pdu->pkbuf = (char *)kdata;
124 pdu->pbuf_size = size;
129 pdu_write_readdir(struct p9_fcall *pdu, const char *kdata, size_t size)
131 BUG_ON(pdu->size > P9_READDIRHDRSZ);
132 pdu->pkbuf = (char *)kdata;
133 pdu->pbuf_size = size;
145 D - data blob (int32_t size followed by void *, results are not freed)
146 T - array of strings (int16_t count, followed by strings)
147 R - array of qids (int16_t count, followed by qids)
148 A - stat for 9p2000.L (p9_stat_dotl)
149 ? - if optional = 1, continue parsing
153 p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
159 for (ptr = fmt; *ptr; ptr++) {
162 int8_t *val = va_arg(ap, int8_t *);
163 if (pdu_read(pdu, val, sizeof(*val))) {
170 int16_t *val = va_arg(ap, int16_t *);
172 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
176 *val = le16_to_cpu(le_val);
180 int32_t *val = va_arg(ap, int32_t *);
182 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
186 *val = le32_to_cpu(le_val);
190 int64_t *val = va_arg(ap, int64_t *);
192 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
196 *val = le64_to_cpu(le_val);
200 char **sptr = va_arg(ap, char **);
203 errcode = p9pdu_readf(pdu, proto_version,
208 *sptr = kmalloc(len + 1, GFP_NOFS);
213 if (pdu_read(pdu, *sptr, len)) {
223 va_arg(ap, struct p9_qid *);
225 errcode = p9pdu_readf(pdu, proto_version, "bdq",
226 &qid->type, &qid->version,
231 struct p9_wstat *stbuf =
232 va_arg(ap, struct p9_wstat *);
234 memset(stbuf, 0, sizeof(struct p9_wstat));
235 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
238 p9pdu_readf(pdu, proto_version,
240 &stbuf->size, &stbuf->type,
241 &stbuf->dev, &stbuf->qid,
242 &stbuf->mode, &stbuf->atime,
243 &stbuf->mtime, &stbuf->length,
244 &stbuf->name, &stbuf->uid,
245 &stbuf->gid, &stbuf->muid,
247 &stbuf->n_uid, &stbuf->n_gid,
254 uint32_t *count = va_arg(ap, uint32_t *);
255 void **data = va_arg(ap, void **);
258 p9pdu_readf(pdu, proto_version, "d", count);
261 min_t(uint32_t, *count,
262 pdu->size - pdu->offset);
263 *data = &pdu->sdata[pdu->offset];
268 int16_t *nwname = va_arg(ap, int16_t *);
269 char ***wnames = va_arg(ap, char ***);
271 errcode = p9pdu_readf(pdu, proto_version,
275 kmalloc(sizeof(char *) * *nwname,
284 for (i = 0; i < *nwname; i++) {
299 for (i = 0; i < *nwname; i++)
308 int16_t *nwqid = va_arg(ap, int16_t *);
309 struct p9_qid **wqids =
310 va_arg(ap, struct p9_qid **);
315 p9pdu_readf(pdu, proto_version, "w", nwqid);
319 sizeof(struct p9_qid),
328 for (i = 0; i < *nwqid; i++) {
346 struct p9_stat_dotl *stbuf =
347 va_arg(ap, struct p9_stat_dotl *);
349 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
351 p9pdu_readf(pdu, proto_version,
352 "qQdddqqqqqqqqqqqqqqq",
353 &stbuf->st_result_mask,
356 &stbuf->st_uid, &stbuf->st_gid,
358 &stbuf->st_rdev, &stbuf->st_size,
359 &stbuf->st_blksize, &stbuf->st_blocks,
360 &stbuf->st_atime_sec,
361 &stbuf->st_atime_nsec,
362 &stbuf->st_mtime_sec,
363 &stbuf->st_mtime_nsec,
364 &stbuf->st_ctime_sec,
365 &stbuf->st_ctime_nsec,
366 &stbuf->st_btime_sec,
367 &stbuf->st_btime_nsec,
369 &stbuf->st_data_version);
373 if ((proto_version != p9_proto_2000u) &&
374 (proto_version != p9_proto_2000L))
390 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
396 for (ptr = fmt; *ptr; ptr++) {
399 int8_t val = va_arg(ap, int);
400 if (pdu_write(pdu, &val, sizeof(val)))
405 __le16 val = cpu_to_le16(va_arg(ap, int));
406 if (pdu_write(pdu, &val, sizeof(val)))
411 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
412 if (pdu_write(pdu, &val, sizeof(val)))
417 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
418 if (pdu_write(pdu, &val, sizeof(val)))
423 const char *sptr = va_arg(ap, const char *);
426 len = min_t(uint16_t, strlen(sptr),
429 errcode = p9pdu_writef(pdu, proto_version,
431 if (!errcode && pdu_write(pdu, sptr, len))
436 const struct p9_qid *qid =
437 va_arg(ap, const struct p9_qid *);
439 p9pdu_writef(pdu, proto_version, "bdq",
440 qid->type, qid->version,
444 const struct p9_wstat *stbuf =
445 va_arg(ap, const struct p9_wstat *);
447 p9pdu_writef(pdu, proto_version,
449 stbuf->size, stbuf->type,
450 stbuf->dev, &stbuf->qid,
451 stbuf->mode, stbuf->atime,
452 stbuf->mtime, stbuf->length,
453 stbuf->name, stbuf->uid,
454 stbuf->gid, stbuf->muid,
455 stbuf->extension, stbuf->n_uid,
456 stbuf->n_gid, stbuf->n_muid);
459 uint32_t count = va_arg(ap, uint32_t);
460 const void *data = va_arg(ap, const void *);
462 errcode = p9pdu_writef(pdu, proto_version, "d",
464 if (!errcode && pdu_write(pdu, data, count))
469 int32_t cnt = va_arg(ap, int32_t);
470 const char *k = va_arg(ap, const void *);
471 const char __user *u = va_arg(ap,
472 const void __user *);
473 errcode = p9pdu_writef(pdu, proto_version, "d",
475 if (!errcode && pdu_write_urw(pdu, k, u, cnt))
480 int32_t cnt = va_arg(ap, int32_t);
481 const char *k = va_arg(ap, const void *);
482 errcode = p9pdu_writef(pdu, proto_version, "d",
484 if (!errcode && pdu_write_readdir(pdu, k, cnt))
489 int32_t count = va_arg(ap, int32_t);
490 const char __user *udata =
491 va_arg(ap, const void __user *);
492 errcode = p9pdu_writef(pdu, proto_version, "d",
494 if (!errcode && pdu_write_u(pdu, udata, count))
499 int16_t nwname = va_arg(ap, int);
500 const char **wnames = va_arg(ap, const char **);
502 errcode = p9pdu_writef(pdu, proto_version, "w",
507 for (i = 0; i < nwname; i++) {
520 int16_t nwqid = va_arg(ap, int);
521 struct p9_qid *wqids =
522 va_arg(ap, struct p9_qid *);
524 errcode = p9pdu_writef(pdu, proto_version, "w",
529 for (i = 0; i < nwqid; i++) {
542 struct p9_iattr_dotl *p9attr = va_arg(ap,
543 struct p9_iattr_dotl *);
545 errcode = p9pdu_writef(pdu, proto_version,
559 if ((proto_version != p9_proto_2000u) &&
560 (proto_version != p9_proto_2000L))
575 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
581 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
588 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
594 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
600 int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
602 struct p9_fcall fake_pdu;
606 fake_pdu.capacity = len;
607 fake_pdu.sdata = buf;
610 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
612 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
613 p9pdu_dump(1, &fake_pdu);
618 EXPORT_SYMBOL(p9stat_read);
620 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
623 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
626 int p9pdu_finalize(struct p9_fcall *pdu)
628 int size = pdu->size;
632 err = p9pdu_writef(pdu, 0, "d", size);
635 #ifdef CONFIG_NET_9P_DEBUG
636 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
640 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
646 void p9pdu_reset(struct p9_fcall *pdu)
656 int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
659 struct p9_fcall fake_pdu;
664 fake_pdu.capacity = len;
665 fake_pdu.sdata = buf;
668 ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
669 &dirent->d_off, &dirent->d_type, &nameptr);
671 P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
672 p9pdu_dump(1, &fake_pdu);
676 strcpy(dirent->d_name, nameptr);
679 return fake_pdu.offset;
681 EXPORT_SYMBOL(p9dirent_read);