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 <linux/uio.h>
37 #include <net/9p/9p.h>
38 #include <net/9p/client.h>
41 #include <trace/events/9p.h>
44 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
46 void p9stat_free(struct p9_wstat *stbuf)
56 kfree(stbuf->extension);
57 stbuf->extension = NULL;
59 EXPORT_SYMBOL(p9stat_free);
61 size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
63 size_t len = min(pdu->size - pdu->offset, size);
64 memcpy(data, &pdu->sdata[pdu->offset], len);
69 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
71 size_t len = min(pdu->capacity - pdu->size, size);
72 memcpy(&pdu->sdata[pdu->size], data, len);
78 pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
80 size_t len = min(pdu->capacity - pdu->size, size);
81 struct iov_iter i = *from;
82 if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, &i))
99 D - data blob (int32_t size followed by void *, results are not freed)
100 T - array of strings (int16_t count, followed by strings)
101 R - array of qids (int16_t count, followed by qids)
102 A - stat for 9p2000.L (p9_stat_dotl)
103 ? - if optional = 1, continue parsing
107 p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
113 for (ptr = fmt; *ptr; ptr++) {
116 int8_t *val = va_arg(ap, int8_t *);
117 if (pdu_read(pdu, val, sizeof(*val))) {
124 int16_t *val = va_arg(ap, int16_t *);
126 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
130 *val = le16_to_cpu(le_val);
134 int32_t *val = va_arg(ap, int32_t *);
136 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
140 *val = le32_to_cpu(le_val);
144 int64_t *val = va_arg(ap, int64_t *);
146 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
150 *val = le64_to_cpu(le_val);
154 char **sptr = va_arg(ap, char **);
157 errcode = p9pdu_readf(pdu, proto_version,
162 *sptr = kmalloc(len + 1, GFP_NOFS);
167 if (pdu_read(pdu, *sptr, len)) {
176 kuid_t *uid = va_arg(ap, kuid_t *);
178 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
182 *uid = make_kuid(&init_user_ns,
183 le32_to_cpu(le_val));
186 kgid_t *gid = va_arg(ap, kgid_t *);
188 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
192 *gid = make_kgid(&init_user_ns,
193 le32_to_cpu(le_val));
197 va_arg(ap, struct p9_qid *);
199 errcode = p9pdu_readf(pdu, proto_version, "bdq",
200 &qid->type, &qid->version,
205 struct p9_wstat *stbuf =
206 va_arg(ap, struct p9_wstat *);
208 memset(stbuf, 0, sizeof(struct p9_wstat));
209 stbuf->n_uid = stbuf->n_muid = INVALID_UID;
210 stbuf->n_gid = INVALID_GID;
213 p9pdu_readf(pdu, proto_version,
215 &stbuf->size, &stbuf->type,
216 &stbuf->dev, &stbuf->qid,
217 &stbuf->mode, &stbuf->atime,
218 &stbuf->mtime, &stbuf->length,
219 &stbuf->name, &stbuf->uid,
220 &stbuf->gid, &stbuf->muid,
222 &stbuf->n_uid, &stbuf->n_gid,
229 uint32_t *count = va_arg(ap, uint32_t *);
230 void **data = va_arg(ap, void **);
233 p9pdu_readf(pdu, proto_version, "d", count);
236 min_t(uint32_t, *count,
237 pdu->size - pdu->offset);
238 *data = &pdu->sdata[pdu->offset];
243 uint16_t *nwname = va_arg(ap, uint16_t *);
244 char ***wnames = va_arg(ap, char ***);
246 errcode = p9pdu_readf(pdu, proto_version,
250 kmalloc_array(*nwname,
260 for (i = 0; i < *nwname; i++) {
275 for (i = 0; i < *nwname; i++)
284 uint16_t *nwqid = va_arg(ap, uint16_t *);
285 struct p9_qid **wqids =
286 va_arg(ap, struct p9_qid **);
291 p9pdu_readf(pdu, proto_version, "w", nwqid);
294 kmalloc_array(*nwqid,
295 sizeof(struct p9_qid),
304 for (i = 0; i < *nwqid; i++) {
322 struct p9_stat_dotl *stbuf =
323 va_arg(ap, struct p9_stat_dotl *);
325 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
327 p9pdu_readf(pdu, proto_version,
328 "qQdugqqqqqqqqqqqqqqq",
329 &stbuf->st_result_mask,
332 &stbuf->st_uid, &stbuf->st_gid,
334 &stbuf->st_rdev, &stbuf->st_size,
335 &stbuf->st_blksize, &stbuf->st_blocks,
336 &stbuf->st_atime_sec,
337 &stbuf->st_atime_nsec,
338 &stbuf->st_mtime_sec,
339 &stbuf->st_mtime_nsec,
340 &stbuf->st_ctime_sec,
341 &stbuf->st_ctime_nsec,
342 &stbuf->st_btime_sec,
343 &stbuf->st_btime_nsec,
345 &stbuf->st_data_version);
349 if ((proto_version != p9_proto_2000u) &&
350 (proto_version != p9_proto_2000L))
366 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
372 for (ptr = fmt; *ptr; ptr++) {
375 int8_t val = va_arg(ap, int);
376 if (pdu_write(pdu, &val, sizeof(val)))
381 __le16 val = cpu_to_le16(va_arg(ap, int));
382 if (pdu_write(pdu, &val, sizeof(val)))
387 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
388 if (pdu_write(pdu, &val, sizeof(val)))
393 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
394 if (pdu_write(pdu, &val, sizeof(val)))
399 const char *sptr = va_arg(ap, const char *);
402 len = min_t(size_t, strlen(sptr),
405 errcode = p9pdu_writef(pdu, proto_version,
407 if (!errcode && pdu_write(pdu, sptr, len))
412 kuid_t uid = va_arg(ap, kuid_t);
413 __le32 val = cpu_to_le32(
414 from_kuid(&init_user_ns, uid));
415 if (pdu_write(pdu, &val, sizeof(val)))
419 kgid_t gid = va_arg(ap, kgid_t);
420 __le32 val = cpu_to_le32(
421 from_kgid(&init_user_ns, gid));
422 if (pdu_write(pdu, &val, sizeof(val)))
426 const struct p9_qid *qid =
427 va_arg(ap, const struct p9_qid *);
429 p9pdu_writef(pdu, proto_version, "bdq",
430 qid->type, qid->version,
434 const struct p9_wstat *stbuf =
435 va_arg(ap, const struct p9_wstat *);
437 p9pdu_writef(pdu, proto_version,
439 stbuf->size, stbuf->type,
440 stbuf->dev, &stbuf->qid,
441 stbuf->mode, stbuf->atime,
442 stbuf->mtime, stbuf->length,
443 stbuf->name, stbuf->uid,
444 stbuf->gid, stbuf->muid,
445 stbuf->extension, stbuf->n_uid,
446 stbuf->n_gid, stbuf->n_muid);
449 uint32_t count = va_arg(ap, uint32_t);
450 struct iov_iter *from =
451 va_arg(ap, struct iov_iter *);
452 errcode = p9pdu_writef(pdu, proto_version, "d",
454 if (!errcode && pdu_write_u(pdu, from, count))
459 uint16_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 uint16_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(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
562 struct p9_fcall fake_pdu;
566 fake_pdu.capacity = len;
567 fake_pdu.sdata = buf;
570 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
572 p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
573 trace_9p_protocol_dump(clnt, &fake_pdu);
577 return fake_pdu.offset;
579 EXPORT_SYMBOL(p9stat_read);
581 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
584 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
587 int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
589 int size = pdu->size;
593 err = p9pdu_writef(pdu, 0, "d", size);
596 trace_9p_protocol_dump(clnt, pdu);
597 p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
598 pdu->size, pdu->id, pdu->tag);
603 void p9pdu_reset(struct p9_fcall *pdu)
609 int p9dirent_read(struct p9_client *clnt, char *buf, int len,
610 struct p9_dirent *dirent)
612 struct p9_fcall fake_pdu;
617 fake_pdu.capacity = len;
618 fake_pdu.sdata = buf;
621 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
622 &dirent->d_off, &dirent->d_type, &nameptr);
624 p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
625 trace_9p_protocol_dump(clnt, &fake_pdu);
629 ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
631 p9_debug(P9_DEBUG_ERROR,
632 "On the wire dirent name too long: %s\n",
639 return fake_pdu.offset;
641 EXPORT_SYMBOL(p9dirent_read);