4 * 9P Protocol Support Code
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)
49 if ((p9_debug_level & P9_DEBUG_VPKT) != P9_DEBUG_VPKT) {
50 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT) {
54 /* shouldn't happen */
60 print_hex_dump_bytes("[9P] ", DUMP_PREFIX_OFFSET, pdu->sdata,
63 print_hex_dump_bytes("]9P[ ", DUMP_PREFIX_OFFSET, pdu->sdata,
68 p9pdu_dump(int way, struct p9_fcall *pdu)
72 EXPORT_SYMBOL(p9pdu_dump);
74 void p9stat_free(struct p9_wstat *stbuf)
80 kfree(stbuf->extension);
82 EXPORT_SYMBOL(p9stat_free);
84 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
86 size_t len = min(pdu->size - pdu->offset, size);
87 memcpy(data, &pdu->sdata[pdu->offset], len);
92 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
94 size_t len = min(pdu->capacity - pdu->size, size);
95 memcpy(&pdu->sdata[pdu->size], data, len);
101 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
103 size_t len = min(pdu->capacity - pdu->size, size);
104 if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
112 pdu_write_urw(struct p9_fcall *pdu, const char *kdata, const char __user *udata,
115 BUG_ON(pdu->size > P9_IOHDRSZ);
116 pdu->pubuf = (char __user *)udata;
117 pdu->pkbuf = (char *)kdata;
118 pdu->pbuf_size = size;
123 pdu_write_readdir(struct p9_fcall *pdu, const char *kdata, size_t size)
125 BUG_ON(pdu->size > P9_READDIRHDRSZ);
126 pdu->pkbuf = (char *)kdata;
127 pdu->pbuf_size = size;
139 D - data blob (int32_t size followed by void *, results are not freed)
140 T - array of strings (int16_t count, followed by strings)
141 R - array of qids (int16_t count, followed by qids)
142 A - stat for 9p2000.L (p9_stat_dotl)
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 **);
197 errcode = p9pdu_readf(pdu, proto_version,
202 *sptr = kmalloc(len + 1, GFP_NOFS);
207 if (pdu_read(pdu, *sptr, len)) {
217 va_arg(ap, struct p9_qid *);
219 errcode = p9pdu_readf(pdu, proto_version, "bdq",
220 &qid->type, &qid->version,
225 struct p9_wstat *stbuf =
226 va_arg(ap, struct p9_wstat *);
228 memset(stbuf, 0, sizeof(struct p9_wstat));
229 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
232 p9pdu_readf(pdu, proto_version,
234 &stbuf->size, &stbuf->type,
235 &stbuf->dev, &stbuf->qid,
236 &stbuf->mode, &stbuf->atime,
237 &stbuf->mtime, &stbuf->length,
238 &stbuf->name, &stbuf->uid,
239 &stbuf->gid, &stbuf->muid,
241 &stbuf->n_uid, &stbuf->n_gid,
248 uint32_t *count = va_arg(ap, uint32_t *);
249 void **data = va_arg(ap, void **);
252 p9pdu_readf(pdu, proto_version, "d", count);
255 min_t(uint32_t, *count,
256 pdu->size - pdu->offset);
257 *data = &pdu->sdata[pdu->offset];
262 uint16_t *nwname = va_arg(ap, uint16_t *);
263 char ***wnames = va_arg(ap, char ***);
265 errcode = p9pdu_readf(pdu, proto_version,
269 kmalloc(sizeof(char *) * *nwname,
278 for (i = 0; i < *nwname; i++) {
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, proto_version, "w", nwqid);
313 sizeof(struct p9_qid),
322 for (i = 0; i < *nwqid; i++) {
340 struct p9_stat_dotl *stbuf =
341 va_arg(ap, struct p9_stat_dotl *);
343 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
345 p9pdu_readf(pdu, proto_version,
346 "qQdddqqqqqqqqqqqqqqq",
347 &stbuf->st_result_mask,
350 &stbuf->st_uid, &stbuf->st_gid,
352 &stbuf->st_rdev, &stbuf->st_size,
353 &stbuf->st_blksize, &stbuf->st_blocks,
354 &stbuf->st_atime_sec,
355 &stbuf->st_atime_nsec,
356 &stbuf->st_mtime_sec,
357 &stbuf->st_mtime_nsec,
358 &stbuf->st_ctime_sec,
359 &stbuf->st_ctime_nsec,
360 &stbuf->st_btime_sec,
361 &stbuf->st_btime_nsec,
363 &stbuf->st_data_version);
367 if ((proto_version != p9_proto_2000u) &&
368 (proto_version != p9_proto_2000L))
384 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
390 for (ptr = fmt; *ptr; ptr++) {
393 int8_t val = va_arg(ap, int);
394 if (pdu_write(pdu, &val, sizeof(val)))
399 __le16 val = cpu_to_le16(va_arg(ap, int));
400 if (pdu_write(pdu, &val, sizeof(val)))
405 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
406 if (pdu_write(pdu, &val, sizeof(val)))
411 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
412 if (pdu_write(pdu, &val, sizeof(val)))
417 const char *sptr = va_arg(ap, const char *);
420 len = min_t(uint16_t, strlen(sptr),
423 errcode = p9pdu_writef(pdu, proto_version,
425 if (!errcode && pdu_write(pdu, sptr, len))
430 const struct p9_qid *qid =
431 va_arg(ap, const struct p9_qid *);
433 p9pdu_writef(pdu, proto_version, "bdq",
434 qid->type, qid->version,
438 const struct p9_wstat *stbuf =
439 va_arg(ap, const struct p9_wstat *);
441 p9pdu_writef(pdu, proto_version,
443 stbuf->size, stbuf->type,
444 stbuf->dev, &stbuf->qid,
445 stbuf->mode, stbuf->atime,
446 stbuf->mtime, stbuf->length,
447 stbuf->name, stbuf->uid,
448 stbuf->gid, stbuf->muid,
449 stbuf->extension, stbuf->n_uid,
450 stbuf->n_gid, stbuf->n_muid);
453 uint32_t count = va_arg(ap, uint32_t);
454 const void *data = va_arg(ap, const void *);
456 errcode = p9pdu_writef(pdu, proto_version, "d",
458 if (!errcode && pdu_write(pdu, data, count))
463 int32_t cnt = va_arg(ap, int32_t);
464 const char *k = va_arg(ap, const void *);
465 const char __user *u = va_arg(ap,
466 const void __user *);
467 errcode = p9pdu_writef(pdu, proto_version, "d",
469 if (!errcode && pdu_write_urw(pdu, k, u, cnt))
474 int32_t cnt = va_arg(ap, int32_t);
475 const char *k = va_arg(ap, const void *);
476 errcode = p9pdu_writef(pdu, proto_version, "d",
478 if (!errcode && pdu_write_readdir(pdu, k, cnt))
483 int32_t count = va_arg(ap, int32_t);
484 const char __user *udata =
485 va_arg(ap, const void __user *);
486 errcode = p9pdu_writef(pdu, proto_version, "d",
488 if (!errcode && pdu_write_u(pdu, udata, count))
493 uint16_t nwname = va_arg(ap, int);
494 const char **wnames = va_arg(ap, const char **);
496 errcode = p9pdu_writef(pdu, proto_version, "w",
501 for (i = 0; i < nwname; i++) {
514 int16_t nwqid = va_arg(ap, int);
515 struct p9_qid *wqids =
516 va_arg(ap, struct p9_qid *);
518 errcode = p9pdu_writef(pdu, proto_version, "w",
523 for (i = 0; i < nwqid; i++) {
536 struct p9_iattr_dotl *p9attr = va_arg(ap,
537 struct p9_iattr_dotl *);
539 errcode = p9pdu_writef(pdu, proto_version,
553 if ((proto_version != p9_proto_2000u) &&
554 (proto_version != p9_proto_2000L))
569 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
575 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
582 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
588 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
594 int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
596 struct p9_fcall fake_pdu;
600 fake_pdu.capacity = len;
601 fake_pdu.sdata = buf;
604 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
606 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
607 P9_DUMP_PKT(0, &fake_pdu);
612 EXPORT_SYMBOL(p9stat_read);
614 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
617 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
620 int p9pdu_finalize(struct p9_fcall *pdu)
622 int size = pdu->size;
626 err = p9pdu_writef(pdu, 0, "d", size);
630 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
636 void p9pdu_reset(struct p9_fcall *pdu)
646 int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
649 struct p9_fcall fake_pdu;
654 fake_pdu.capacity = len;
655 fake_pdu.sdata = buf;
658 ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
659 &dirent->d_off, &dirent->d_type, &nameptr);
661 P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
662 P9_DUMP_PKT(1, &fake_pdu);
666 strcpy(dirent->d_name, nameptr);
670 return fake_pdu.offset;
672 EXPORT_SYMBOL(p9dirent_read);