1 // SPDX-License-Identifier: GPL-2.0-only
5 * 9P Protocol Support Code
7 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
9 * Base on code from Anthony Liguori <aliguori@us.ibm.com>
10 * Copyright (C) 2008 by IBM, Corp.
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/uaccess.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/stddef.h>
20 #include <linux/types.h>
21 #include <linux/uio.h>
22 #include <net/9p/9p.h>
23 #include <net/9p/client.h>
26 #include <trace/events/9p.h>
29 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
31 void p9stat_free(struct p9_wstat *stbuf)
41 kfree(stbuf->extension);
42 stbuf->extension = NULL;
44 EXPORT_SYMBOL(p9stat_free);
46 size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
48 size_t len = min(pdu->size - pdu->offset, size);
50 memcpy(data, &pdu->sdata[pdu->offset], len);
55 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
57 size_t len = min(pdu->capacity - pdu->size, size);
59 memcpy(&pdu->sdata[pdu->size], data, len);
65 pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
67 size_t len = min(pdu->capacity - pdu->size, size);
68 struct iov_iter i = *from;
70 if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, &i))
86 * D - data blob (int32_t size followed by void *, results are not freed)
87 * T - array of strings (int16_t count, followed by strings)
88 * R - array of qids (int16_t count, followed by qids)
89 * A - stat for 9p2000.L (p9_stat_dotl)
90 * ? - if optional = 1, continue parsing
94 p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
100 for (ptr = fmt; *ptr; ptr++) {
103 int8_t *val = va_arg(ap, int8_t *);
104 if (pdu_read(pdu, val, sizeof(*val))) {
111 int16_t *val = va_arg(ap, int16_t *);
113 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
117 *val = le16_to_cpu(le_val);
121 int32_t *val = va_arg(ap, int32_t *);
123 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
127 *val = le32_to_cpu(le_val);
131 int64_t *val = va_arg(ap, int64_t *);
133 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
137 *val = le64_to_cpu(le_val);
141 char **sptr = va_arg(ap, char **);
144 errcode = p9pdu_readf(pdu, proto_version,
149 *sptr = kmalloc(len + 1, GFP_NOFS);
154 if (pdu_read(pdu, *sptr, len)) {
163 kuid_t *uid = va_arg(ap, kuid_t *);
165 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
169 *uid = make_kuid(&init_user_ns,
170 le32_to_cpu(le_val));
173 kgid_t *gid = va_arg(ap, kgid_t *);
175 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
179 *gid = make_kgid(&init_user_ns,
180 le32_to_cpu(le_val));
184 va_arg(ap, struct p9_qid *);
186 errcode = p9pdu_readf(pdu, proto_version, "bdq",
187 &qid->type, &qid->version,
192 struct p9_wstat *stbuf =
193 va_arg(ap, struct p9_wstat *);
195 memset(stbuf, 0, sizeof(struct p9_wstat));
196 stbuf->n_uid = stbuf->n_muid = INVALID_UID;
197 stbuf->n_gid = INVALID_GID;
200 p9pdu_readf(pdu, proto_version,
202 &stbuf->size, &stbuf->type,
203 &stbuf->dev, &stbuf->qid,
204 &stbuf->mode, &stbuf->atime,
205 &stbuf->mtime, &stbuf->length,
206 &stbuf->name, &stbuf->uid,
207 &stbuf->gid, &stbuf->muid,
209 &stbuf->n_uid, &stbuf->n_gid,
216 uint32_t *count = va_arg(ap, uint32_t *);
217 void **data = va_arg(ap, void **);
220 p9pdu_readf(pdu, proto_version, "d", count);
223 min_t(uint32_t, *count,
224 pdu->size - pdu->offset);
225 *data = &pdu->sdata[pdu->offset];
230 uint16_t *nwname = va_arg(ap, uint16_t *);
231 char ***wnames = va_arg(ap, char ***);
233 errcode = p9pdu_readf(pdu, proto_version,
237 kmalloc_array(*nwname,
247 for (i = 0; i < *nwname; i++) {
262 for (i = 0; i < *nwname; i++)
271 uint16_t *nwqid = va_arg(ap, uint16_t *);
272 struct p9_qid **wqids =
273 va_arg(ap, struct p9_qid **);
278 p9pdu_readf(pdu, proto_version, "w", nwqid);
281 kmalloc_array(*nwqid,
282 sizeof(struct p9_qid),
291 for (i = 0; i < *nwqid; i++) {
309 struct p9_stat_dotl *stbuf =
310 va_arg(ap, struct p9_stat_dotl *);
312 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
314 p9pdu_readf(pdu, proto_version,
315 "qQdugqqqqqqqqqqqqqqq",
316 &stbuf->st_result_mask,
319 &stbuf->st_uid, &stbuf->st_gid,
321 &stbuf->st_rdev, &stbuf->st_size,
322 &stbuf->st_blksize, &stbuf->st_blocks,
323 &stbuf->st_atime_sec,
324 &stbuf->st_atime_nsec,
325 &stbuf->st_mtime_sec,
326 &stbuf->st_mtime_nsec,
327 &stbuf->st_ctime_sec,
328 &stbuf->st_ctime_nsec,
329 &stbuf->st_btime_sec,
330 &stbuf->st_btime_nsec,
332 &stbuf->st_data_version);
336 if ((proto_version != p9_proto_2000u) &&
337 (proto_version != p9_proto_2000L))
353 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
359 for (ptr = fmt; *ptr; ptr++) {
362 int8_t val = va_arg(ap, int);
363 if (pdu_write(pdu, &val, sizeof(val)))
368 __le16 val = cpu_to_le16(va_arg(ap, int));
369 if (pdu_write(pdu, &val, sizeof(val)))
374 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
375 if (pdu_write(pdu, &val, sizeof(val)))
380 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
381 if (pdu_write(pdu, &val, sizeof(val)))
386 const char *sptr = va_arg(ap, const char *);
389 len = min_t(size_t, strlen(sptr),
392 errcode = p9pdu_writef(pdu, proto_version,
394 if (!errcode && pdu_write(pdu, sptr, len))
399 kuid_t uid = va_arg(ap, kuid_t);
400 __le32 val = cpu_to_le32(
401 from_kuid(&init_user_ns, uid));
402 if (pdu_write(pdu, &val, sizeof(val)))
406 kgid_t gid = va_arg(ap, kgid_t);
407 __le32 val = cpu_to_le32(
408 from_kgid(&init_user_ns, gid));
409 if (pdu_write(pdu, &val, sizeof(val)))
413 const struct p9_qid *qid =
414 va_arg(ap, const struct p9_qid *);
416 p9pdu_writef(pdu, proto_version, "bdq",
417 qid->type, qid->version,
421 const struct p9_wstat *stbuf =
422 va_arg(ap, const struct p9_wstat *);
424 p9pdu_writef(pdu, proto_version,
426 stbuf->size, stbuf->type,
427 stbuf->dev, &stbuf->qid,
428 stbuf->mode, stbuf->atime,
429 stbuf->mtime, stbuf->length,
430 stbuf->name, stbuf->uid,
431 stbuf->gid, stbuf->muid,
432 stbuf->extension, stbuf->n_uid,
433 stbuf->n_gid, stbuf->n_muid);
436 uint32_t count = va_arg(ap, uint32_t);
437 struct iov_iter *from =
438 va_arg(ap, struct iov_iter *);
439 errcode = p9pdu_writef(pdu, proto_version, "d",
441 if (!errcode && pdu_write_u(pdu, from, count))
446 uint16_t nwname = va_arg(ap, int);
447 const char **wnames = va_arg(ap, const char **);
449 errcode = p9pdu_writef(pdu, proto_version, "w",
454 for (i = 0; i < nwname; i++) {
467 uint16_t nwqid = va_arg(ap, int);
468 struct p9_qid *wqids =
469 va_arg(ap, struct p9_qid *);
471 errcode = p9pdu_writef(pdu, proto_version, "w",
476 for (i = 0; i < nwqid; i++) {
489 struct p9_iattr_dotl *p9attr = va_arg(ap,
490 struct p9_iattr_dotl *);
492 errcode = p9pdu_writef(pdu, proto_version,
506 if ((proto_version != p9_proto_2000u) &&
507 (proto_version != p9_proto_2000L))
522 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
528 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
535 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
541 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
547 int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
549 struct p9_fcall fake_pdu;
553 fake_pdu.capacity = len;
554 fake_pdu.sdata = buf;
557 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
559 p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
560 trace_9p_protocol_dump(clnt, &fake_pdu);
564 return fake_pdu.offset;
566 EXPORT_SYMBOL(p9stat_read);
568 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
571 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
574 int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
576 int size = pdu->size;
580 err = p9pdu_writef(pdu, 0, "d", size);
583 trace_9p_protocol_dump(clnt, pdu);
584 p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
585 pdu->size, pdu->id, pdu->tag);
590 void p9pdu_reset(struct p9_fcall *pdu)
596 int p9dirent_read(struct p9_client *clnt, char *buf, int len,
597 struct p9_dirent *dirent)
599 struct p9_fcall fake_pdu;
604 fake_pdu.capacity = len;
605 fake_pdu.sdata = buf;
608 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
609 &dirent->d_off, &dirent->d_type, &nameptr);
611 p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
612 trace_9p_protocol_dump(clnt, &fake_pdu);
616 ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
618 p9_debug(P9_DEBUG_ERROR,
619 "On the wire dirent name too long: %s\n",
626 return fake_pdu.offset;
628 EXPORT_SYMBOL(p9dirent_read);