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)
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))
125 D - data blob (int32_t size followed by void *, results are not freed)
126 T - array of strings (int16_t count, followed by strings)
127 R - array of qids (int16_t count, followed by qids)
128 A - stat for 9p2000.L (p9_stat_dotl)
129 ? - if optional = 1, continue parsing
133 p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
139 for (ptr = fmt; *ptr; ptr++) {
142 int8_t *val = va_arg(ap, int8_t *);
143 if (pdu_read(pdu, val, sizeof(*val))) {
150 int16_t *val = va_arg(ap, int16_t *);
152 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
156 *val = le16_to_cpu(le_val);
160 int32_t *val = va_arg(ap, int32_t *);
162 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
166 *val = le32_to_cpu(le_val);
170 int64_t *val = va_arg(ap, int64_t *);
172 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
176 *val = le64_to_cpu(le_val);
180 char **sptr = va_arg(ap, char **);
183 errcode = p9pdu_readf(pdu, proto_version,
188 *sptr = kmalloc(len + 1, GFP_KERNEL);
193 if (pdu_read(pdu, *sptr, len)) {
203 va_arg(ap, struct p9_qid *);
205 errcode = p9pdu_readf(pdu, proto_version, "bdq",
206 &qid->type, &qid->version,
211 struct p9_wstat *stbuf =
212 va_arg(ap, struct p9_wstat *);
214 memset(stbuf, 0, sizeof(struct p9_wstat));
215 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
218 p9pdu_readf(pdu, proto_version,
220 &stbuf->size, &stbuf->type,
221 &stbuf->dev, &stbuf->qid,
222 &stbuf->mode, &stbuf->atime,
223 &stbuf->mtime, &stbuf->length,
224 &stbuf->name, &stbuf->uid,
225 &stbuf->gid, &stbuf->muid,
227 &stbuf->n_uid, &stbuf->n_gid,
234 uint32_t *count = va_arg(ap, uint32_t *);
235 void **data = va_arg(ap, void **);
238 p9pdu_readf(pdu, proto_version, "d", count);
241 min_t(uint32_t, *count,
242 pdu->size - pdu->offset);
243 *data = &pdu->sdata[pdu->offset];
248 int16_t *nwname = va_arg(ap, int16_t *);
249 char ***wnames = va_arg(ap, char ***);
251 errcode = p9pdu_readf(pdu, proto_version,
255 kmalloc(sizeof(char *) * *nwname,
264 for (i = 0; i < *nwname; i++) {
279 for (i = 0; i < *nwname; i++)
288 int16_t *nwqid = va_arg(ap, int16_t *);
289 struct p9_qid **wqids =
290 va_arg(ap, struct p9_qid **);
295 p9pdu_readf(pdu, proto_version, "w", nwqid);
299 sizeof(struct p9_qid),
308 for (i = 0; i < *nwqid; i++) {
326 struct p9_stat_dotl *stbuf =
327 va_arg(ap, struct p9_stat_dotl *);
329 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
331 p9pdu_readf(pdu, proto_version,
332 "qQdddqqqqqqqqqqqqqqq",
333 &stbuf->st_result_mask,
336 &stbuf->st_uid, &stbuf->st_gid,
338 &stbuf->st_rdev, &stbuf->st_size,
339 &stbuf->st_blksize, &stbuf->st_blocks,
340 &stbuf->st_atime_sec,
341 &stbuf->st_atime_nsec,
342 &stbuf->st_mtime_sec,
343 &stbuf->st_mtime_nsec,
344 &stbuf->st_ctime_sec,
345 &stbuf->st_ctime_nsec,
346 &stbuf->st_btime_sec,
347 &stbuf->st_btime_nsec,
349 &stbuf->st_data_version);
353 if ((proto_version != p9_proto_2000u) &&
354 (proto_version != p9_proto_2000L))
370 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
376 for (ptr = fmt; *ptr; ptr++) {
379 int8_t val = va_arg(ap, int);
380 if (pdu_write(pdu, &val, sizeof(val)))
385 __le16 val = cpu_to_le16(va_arg(ap, int));
386 if (pdu_write(pdu, &val, sizeof(val)))
391 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
392 if (pdu_write(pdu, &val, sizeof(val)))
397 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
398 if (pdu_write(pdu, &val, sizeof(val)))
403 const char *sptr = va_arg(ap, const char *);
406 len = min_t(uint16_t, strlen(sptr),
409 errcode = p9pdu_writef(pdu, proto_version,
411 if (!errcode && pdu_write(pdu, sptr, len))
416 const struct p9_qid *qid =
417 va_arg(ap, const struct p9_qid *);
419 p9pdu_writef(pdu, proto_version, "bdq",
420 qid->type, qid->version,
424 const struct p9_wstat *stbuf =
425 va_arg(ap, const struct p9_wstat *);
427 p9pdu_writef(pdu, proto_version,
429 stbuf->size, stbuf->type,
430 stbuf->dev, &stbuf->qid,
431 stbuf->mode, stbuf->atime,
432 stbuf->mtime, stbuf->length,
433 stbuf->name, stbuf->uid,
434 stbuf->gid, stbuf->muid,
435 stbuf->extension, stbuf->n_uid,
436 stbuf->n_gid, stbuf->n_muid);
439 uint32_t count = va_arg(ap, uint32_t);
440 const void *data = va_arg(ap, const void *);
442 errcode = p9pdu_writef(pdu, proto_version, "d",
444 if (!errcode && pdu_write(pdu, data, count))
449 int32_t count = va_arg(ap, int32_t);
450 const char __user *udata =
451 va_arg(ap, const void __user *);
452 errcode = p9pdu_writef(pdu, proto_version, "d",
454 if (!errcode && pdu_write_u(pdu, udata, count))
459 int16_t nwname = va_arg(ap, int);
460 const char **wnames = va_arg(ap, const char **);
462 errcode = p9pdu_writef(pdu, proto_version, "w",
467 for (i = 0; i < nwname; i++) {
480 int16_t nwqid = va_arg(ap, int);
481 struct p9_qid *wqids =
482 va_arg(ap, struct p9_qid *);
484 errcode = p9pdu_writef(pdu, proto_version, "w",
489 for (i = 0; i < nwqid; i++) {
502 struct p9_iattr_dotl *p9attr = va_arg(ap,
503 struct p9_iattr_dotl *);
505 errcode = p9pdu_writef(pdu, proto_version,
519 if ((proto_version != p9_proto_2000u) &&
520 (proto_version != p9_proto_2000L))
535 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
541 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
548 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
554 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
560 int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
562 struct p9_fcall fake_pdu;
566 fake_pdu.capacity = len;
567 fake_pdu.sdata = buf;
570 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
572 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
573 p9pdu_dump(1, &fake_pdu);
578 EXPORT_SYMBOL(p9stat_read);
580 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
582 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
585 int p9pdu_finalize(struct p9_fcall *pdu)
587 int size = pdu->size;
591 err = p9pdu_writef(pdu, 0, "d", size);
594 #ifdef CONFIG_NET_9P_DEBUG
595 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
599 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
605 void p9pdu_reset(struct p9_fcall *pdu)
611 int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
614 struct p9_fcall fake_pdu;
619 fake_pdu.capacity = len;
620 fake_pdu.sdata = buf;
623 ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
624 &dirent->d_off, &dirent->d_type, &nameptr);
626 P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
627 p9pdu_dump(1, &fake_pdu);
631 strcpy(dirent->d_name, nameptr);
634 return fake_pdu.offset;
636 EXPORT_SYMBOL(p9dirent_read);