Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
 
 
  3 * 9P Protocol Support Code
  4 *
  5 *  Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
  6 *
  7 *  Base on code from Anthony Liguori <aliguori@us.ibm.com>
  8 *  Copyright (C) 2008 by IBM, Corp.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  9 */
 10
 11#include <linux/module.h>
 12#include <linux/errno.h>
 13#include <linux/kernel.h>
 14#include <linux/uaccess.h>
 15#include <linux/slab.h>
 16#include <linux/sched.h>
 17#include <linux/stddef.h>
 18#include <linux/types.h>
 19#include <linux/uio.h>
 20#include <net/9p/9p.h>
 21#include <net/9p/client.h>
 22#include "protocol.h"
 23
 24#include <trace/events/9p.h>
 25
 26/* len[2] text[len] */
 27#define P9_STRLEN(s) \
 28	(2 + min_t(size_t, s ? strlen(s) : 0, USHRT_MAX))
 29
 30/**
 31 * p9_msg_buf_size - Returns a buffer size sufficiently large to hold the
 32 * intended 9p message.
 33 * @c: client
 34 * @type: message type
 35 * @fmt: format template for assembling request message
 36 * (see p9pdu_vwritef)
 37 * @ap: variable arguments to be fed to passed format template
 38 * (see p9pdu_vwritef)
 39 *
 40 * Note: Even for response types (P9_R*) the format template and variable
 41 * arguments must always be for the originating request type (P9_T*).
 42 */
 43size_t p9_msg_buf_size(struct p9_client *c, enum p9_msg_t type,
 44			const char *fmt, va_list ap)
 45{
 46	/* size[4] type[1] tag[2] */
 47	const int hdr = 4 + 1 + 2;
 48	/* ename[s] errno[4] */
 49	const int rerror_size = hdr + P9_ERRMAX + 4;
 50	/* ecode[4] */
 51	const int rlerror_size = hdr + 4;
 52	const int err_size =
 53		c->proto_version == p9_proto_2000L ? rlerror_size : rerror_size;
 54
 55	static_assert(NAME_MAX <= 4*1024, "p9_msg_buf_size() currently assumes "
 56				  "a max. allowed directory entry name length of 4k");
 57
 58	switch (type) {
 59
 60	/* message types not used at all */
 61	case P9_TERROR:
 62	case P9_TLERROR:
 63	case P9_TAUTH:
 64	case P9_RAUTH:
 65		BUG();
 66
 67	/* variable length & potentially large message types */
 68	case P9_TATTACH:
 69		BUG_ON(strcmp("ddss?u", fmt));
 70		va_arg(ap, int32_t);
 71		va_arg(ap, int32_t);
 72		{
 73			const char *uname = va_arg(ap, const char *);
 74			const char *aname = va_arg(ap, const char *);
 75			/* fid[4] afid[4] uname[s] aname[s] n_uname[4] */
 76			return hdr + 4 + 4 + P9_STRLEN(uname) + P9_STRLEN(aname) + 4;
 77		}
 78	case P9_TWALK:
 79		BUG_ON(strcmp("ddT", fmt));
 80		va_arg(ap, int32_t);
 81		va_arg(ap, int32_t);
 82		{
 83			uint i, nwname = va_arg(ap, int);
 84			size_t wname_all;
 85			const char **wnames = va_arg(ap, const char **);
 86			for (i = 0, wname_all = 0; i < nwname; ++i) {
 87				wname_all += P9_STRLEN(wnames[i]);
 88			}
 89			/* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
 90			return hdr + 4 + 4 + 2 + wname_all;
 91		}
 92	case P9_RWALK:
 93		BUG_ON(strcmp("ddT", fmt));
 94		va_arg(ap, int32_t);
 95		va_arg(ap, int32_t);
 96		{
 97			uint nwname = va_arg(ap, int);
 98			/* nwqid[2] nwqid*(wqid[13]) */
 99			return max_t(size_t, hdr + 2 + nwname * 13, err_size);
100		}
101	case P9_TCREATE:
102		BUG_ON(strcmp("dsdb?s", fmt));
103		va_arg(ap, int32_t);
104		{
105			const char *name = va_arg(ap, const char *);
106			if (c->proto_version == p9_proto_legacy) {
107				/* fid[4] name[s] perm[4] mode[1] */
108				return hdr + 4 + P9_STRLEN(name) + 4 + 1;
109			} else {
110				va_arg(ap, int32_t);
111				va_arg(ap, int);
112				{
113					const char *ext = va_arg(ap, const char *);
114					/* fid[4] name[s] perm[4] mode[1] extension[s] */
115					return hdr + 4 + P9_STRLEN(name) + 4 + 1 + P9_STRLEN(ext);
116				}
117			}
118		}
119	case P9_TLCREATE:
120		BUG_ON(strcmp("dsddg", fmt));
121		va_arg(ap, int32_t);
122		{
123			const char *name = va_arg(ap, const char *);
124			/* fid[4] name[s] flags[4] mode[4] gid[4] */
125			return hdr + 4 + P9_STRLEN(name) + 4 + 4 + 4;
126		}
127	case P9_RREAD:
128	case P9_RREADDIR:
129		BUG_ON(strcmp("dqd", fmt));
130		va_arg(ap, int32_t);
131		va_arg(ap, int64_t);
132		{
133			const int32_t count = va_arg(ap, int32_t);
134			/* count[4] data[count] */
135			return max_t(size_t, hdr + 4 + count, err_size);
136		}
137	case P9_TWRITE:
138		BUG_ON(strcmp("dqV", fmt));
139		va_arg(ap, int32_t);
140		va_arg(ap, int64_t);
141		{
142			const int32_t count = va_arg(ap, int32_t);
143			/* fid[4] offset[8] count[4] data[count] */
144			return hdr + 4 + 8 + 4 + count;
145		}
146	case P9_TRENAMEAT:
147		BUG_ON(strcmp("dsds", fmt));
148		va_arg(ap, int32_t);
149		{
150			const char *oldname, *newname;
151			oldname = va_arg(ap, const char *);
152			va_arg(ap, int32_t);
153			newname = va_arg(ap, const char *);
154			/* olddirfid[4] oldname[s] newdirfid[4] newname[s] */
155			return hdr + 4 + P9_STRLEN(oldname) + 4 + P9_STRLEN(newname);
156		}
157	case P9_TSYMLINK:
158		BUG_ON(strcmp("dssg", fmt));
159		va_arg(ap, int32_t);
160		{
161			const char *name = va_arg(ap, const char *);
162			const char *symtgt = va_arg(ap, const char *);
163			/* fid[4] name[s] symtgt[s] gid[4] */
164			return hdr + 4 + P9_STRLEN(name) + P9_STRLEN(symtgt) + 4;
165		}
166
167	case P9_RERROR:
168		return rerror_size;
169	case P9_RLERROR:
170		return rlerror_size;
171
172	/* small message types */
173	case P9_TWSTAT:
174	case P9_RSTAT:
175	case P9_RREADLINK:
176	case P9_TXATTRWALK:
177	case P9_TXATTRCREATE:
178	case P9_TLINK:
179	case P9_TMKDIR:
180	case P9_TMKNOD:
181	case P9_TRENAME:
182	case P9_TUNLINKAT:
183	case P9_TLOCK:
184		return 8 * 1024;
185
186	/* tiny message types */
187	default:
188		return 4 * 1024;
189
190	}
191}
192
193static int
194p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
195
196void p9stat_free(struct p9_wstat *stbuf)
197{
198	kfree(stbuf->name);
199	stbuf->name = NULL;
200	kfree(stbuf->uid);
201	stbuf->uid = NULL;
202	kfree(stbuf->gid);
203	stbuf->gid = NULL;
204	kfree(stbuf->muid);
205	stbuf->muid = NULL;
206	kfree(stbuf->extension);
207	stbuf->extension = NULL;
208}
209EXPORT_SYMBOL(p9stat_free);
210
211size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
212{
213	size_t len = min(pdu->size - pdu->offset, size);
214
215	memcpy(data, &pdu->sdata[pdu->offset], len);
216	pdu->offset += len;
217	return size - len;
218}
219
220static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
221{
222	size_t len = min(pdu->capacity - pdu->size, size);
223
224	memcpy(&pdu->sdata[pdu->size], data, len);
225	pdu->size += len;
226	return size - len;
227}
228
229static size_t
230pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
231{
232	size_t len = min(pdu->capacity - pdu->size, size);
233
234	if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, from))
235		len = 0;
236
237	pdu->size += len;
238	return size - len;
239}
240
241/*	b - int8_t
242 *	w - int16_t
243 *	d - int32_t
244 *	q - int64_t
245 *	s - string
246 *	u - numeric uid
247 *	g - numeric gid
248 *	S - stat
249 *	Q - qid
250 *	D - data blob (int32_t size followed by void *, results are not freed)
251 *	T - array of strings (int16_t count, followed by strings)
252 *	R - array of qids (int16_t count, followed by qids)
253 *	A - stat for 9p2000.L (p9_stat_dotl)
254 *	? - if optional = 1, continue parsing
255 */
 
256
257static int
258p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
259	     va_list ap)
260{
261	const char *ptr;
262	int errcode = 0;
263
264	for (ptr = fmt; *ptr; ptr++) {
265		switch (*ptr) {
266		case 'b':{
267				int8_t *val = va_arg(ap, int8_t *);
268				if (pdu_read(pdu, val, sizeof(*val))) {
269					errcode = -EFAULT;
270					break;
271				}
272			}
273			break;
274		case 'w':{
275				int16_t *val = va_arg(ap, int16_t *);
276				__le16 le_val;
277				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
278					errcode = -EFAULT;
279					break;
280				}
281				*val = le16_to_cpu(le_val);
282			}
283			break;
284		case 'd':{
285				int32_t *val = va_arg(ap, int32_t *);
286				__le32 le_val;
287				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
288					errcode = -EFAULT;
289					break;
290				}
291				*val = le32_to_cpu(le_val);
292			}
293			break;
294		case 'q':{
295				int64_t *val = va_arg(ap, int64_t *);
296				__le64 le_val;
297				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
298					errcode = -EFAULT;
299					break;
300				}
301				*val = le64_to_cpu(le_val);
302			}
303			break;
304		case 's':{
305				char **sptr = va_arg(ap, char **);
306				uint16_t len;
307
308				errcode = p9pdu_readf(pdu, proto_version,
309								"w", &len);
310				if (errcode)
311					break;
312
313				*sptr = kmalloc(len + 1, GFP_NOFS);
314				if (*sptr == NULL) {
315					errcode = -ENOMEM;
316					break;
317				}
318				if (pdu_read(pdu, *sptr, len)) {
319					errcode = -EFAULT;
320					kfree(*sptr);
321					*sptr = NULL;
322				} else
323					(*sptr)[len] = 0;
324			}
325			break;
326		case 'u': {
327				kuid_t *uid = va_arg(ap, kuid_t *);
328				__le32 le_val;
329				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
330					errcode = -EFAULT;
331					break;
332				}
333				*uid = make_kuid(&init_user_ns,
334						 le32_to_cpu(le_val));
335			} break;
336		case 'g': {
337				kgid_t *gid = va_arg(ap, kgid_t *);
338				__le32 le_val;
339				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
340					errcode = -EFAULT;
341					break;
342				}
343				*gid = make_kgid(&init_user_ns,
344						 le32_to_cpu(le_val));
345			} break;
346		case 'Q':{
347				struct p9_qid *qid =
348				    va_arg(ap, struct p9_qid *);
349
350				errcode = p9pdu_readf(pdu, proto_version, "bdq",
351						      &qid->type, &qid->version,
352						      &qid->path);
353			}
354			break;
355		case 'S':{
356				struct p9_wstat *stbuf =
357				    va_arg(ap, struct p9_wstat *);
358
359				memset(stbuf, 0, sizeof(struct p9_wstat));
360				stbuf->n_uid = stbuf->n_muid = INVALID_UID;
361				stbuf->n_gid = INVALID_GID;
362
363				errcode =
364				    p9pdu_readf(pdu, proto_version,
365						"wwdQdddqssss?sugu",
366						&stbuf->size, &stbuf->type,
367						&stbuf->dev, &stbuf->qid,
368						&stbuf->mode, &stbuf->atime,
369						&stbuf->mtime, &stbuf->length,
370						&stbuf->name, &stbuf->uid,
371						&stbuf->gid, &stbuf->muid,
372						&stbuf->extension,
373						&stbuf->n_uid, &stbuf->n_gid,
374						&stbuf->n_muid);
375				if (errcode)
376					p9stat_free(stbuf);
377			}
378			break;
379		case 'D':{
380				uint32_t *count = va_arg(ap, uint32_t *);
381				void **data = va_arg(ap, void **);
382
383				errcode =
384				    p9pdu_readf(pdu, proto_version, "d", count);
385				if (!errcode) {
386					*count =
387					    min_t(uint32_t, *count,
388						  pdu->size - pdu->offset);
389					*data = &pdu->sdata[pdu->offset];
390				}
391			}
392			break;
393		case 'T':{
394				uint16_t *nwname = va_arg(ap, uint16_t *);
395				char ***wnames = va_arg(ap, char ***);
396
397				errcode = p9pdu_readf(pdu, proto_version,
398								"w", nwname);
399				if (!errcode) {
400					*wnames =
401					    kmalloc_array(*nwname,
402							  sizeof(char *),
403							  GFP_NOFS);
404					if (!*wnames)
405						errcode = -ENOMEM;
406				}
407
408				if (!errcode) {
409					int i;
410
411					for (i = 0; i < *nwname; i++) {
412						errcode =
413						    p9pdu_readf(pdu,
414								proto_version,
415								"s",
416								&(*wnames)[i]);
417						if (errcode)
418							break;
419					}
420				}
421
422				if (errcode) {
423					if (*wnames) {
424						int i;
425
426						for (i = 0; i < *nwname; i++)
427							kfree((*wnames)[i]);
428					}
429					kfree(*wnames);
430					*wnames = NULL;
431				}
432			}
433			break;
434		case 'R':{
435				uint16_t *nwqid = va_arg(ap, uint16_t *);
436				struct p9_qid **wqids =
437				    va_arg(ap, struct p9_qid **);
438
439				*wqids = NULL;
440
441				errcode =
442				    p9pdu_readf(pdu, proto_version, "w", nwqid);
443				if (!errcode) {
444					*wqids =
445					    kmalloc_array(*nwqid,
446							  sizeof(struct p9_qid),
447							  GFP_NOFS);
448					if (*wqids == NULL)
449						errcode = -ENOMEM;
450				}
451
452				if (!errcode) {
453					int i;
454
455					for (i = 0; i < *nwqid; i++) {
456						errcode =
457						    p9pdu_readf(pdu,
458								proto_version,
459								"Q",
460								&(*wqids)[i]);
461						if (errcode)
462							break;
463					}
464				}
465
466				if (errcode) {
467					kfree(*wqids);
468					*wqids = NULL;
469				}
470			}
471			break;
472		case 'A': {
473				struct p9_stat_dotl *stbuf =
474				    va_arg(ap, struct p9_stat_dotl *);
475
476				memset(stbuf, 0, sizeof(struct p9_stat_dotl));
477				errcode =
478				    p9pdu_readf(pdu, proto_version,
479					"qQdugqqqqqqqqqqqqqqq",
480					&stbuf->st_result_mask,
481					&stbuf->qid,
482					&stbuf->st_mode,
483					&stbuf->st_uid, &stbuf->st_gid,
484					&stbuf->st_nlink,
485					&stbuf->st_rdev, &stbuf->st_size,
486					&stbuf->st_blksize, &stbuf->st_blocks,
487					&stbuf->st_atime_sec,
488					&stbuf->st_atime_nsec,
489					&stbuf->st_mtime_sec,
490					&stbuf->st_mtime_nsec,
491					&stbuf->st_ctime_sec,
492					&stbuf->st_ctime_nsec,
493					&stbuf->st_btime_sec,
494					&stbuf->st_btime_nsec,
495					&stbuf->st_gen,
496					&stbuf->st_data_version);
497			}
498			break;
499		case '?':
500			if ((proto_version != p9_proto_2000u) &&
501				(proto_version != p9_proto_2000L))
502				return 0;
503			break;
504		default:
505			BUG();
506			break;
507		}
508
509		if (errcode)
510			break;
511	}
512
513	return errcode;
514}
515
516int
517p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
518	va_list ap)
519{
520	const char *ptr;
521	int errcode = 0;
522
523	for (ptr = fmt; *ptr; ptr++) {
524		switch (*ptr) {
525		case 'b':{
526				int8_t val = va_arg(ap, int);
527				if (pdu_write(pdu, &val, sizeof(val)))
528					errcode = -EFAULT;
529			}
530			break;
531		case 'w':{
532				__le16 val = cpu_to_le16(va_arg(ap, int));
533				if (pdu_write(pdu, &val, sizeof(val)))
534					errcode = -EFAULT;
535			}
536			break;
537		case 'd':{
538				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
539				if (pdu_write(pdu, &val, sizeof(val)))
540					errcode = -EFAULT;
541			}
542			break;
543		case 'q':{
544				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
545				if (pdu_write(pdu, &val, sizeof(val)))
546					errcode = -EFAULT;
547			}
548			break;
549		case 's':{
550				const char *sptr = va_arg(ap, const char *);
551				uint16_t len = 0;
552				if (sptr)
553					len = min_t(size_t, strlen(sptr),
554								USHRT_MAX);
555
556				errcode = p9pdu_writef(pdu, proto_version,
557								"w", len);
558				if (!errcode && pdu_write(pdu, sptr, len))
559					errcode = -EFAULT;
560			}
561			break;
562		case 'u': {
563				kuid_t uid = va_arg(ap, kuid_t);
564				__le32 val = cpu_to_le32(
565						from_kuid(&init_user_ns, uid));
566				if (pdu_write(pdu, &val, sizeof(val)))
567					errcode = -EFAULT;
568			} break;
569		case 'g': {
570				kgid_t gid = va_arg(ap, kgid_t);
571				__le32 val = cpu_to_le32(
572						from_kgid(&init_user_ns, gid));
573				if (pdu_write(pdu, &val, sizeof(val)))
574					errcode = -EFAULT;
575			} break;
576		case 'Q':{
577				const struct p9_qid *qid =
578				    va_arg(ap, const struct p9_qid *);
579				errcode =
580				    p9pdu_writef(pdu, proto_version, "bdq",
581						 qid->type, qid->version,
582						 qid->path);
583			} break;
584		case 'S':{
585				const struct p9_wstat *stbuf =
586				    va_arg(ap, const struct p9_wstat *);
587				errcode =
588				    p9pdu_writef(pdu, proto_version,
589						 "wwdQdddqssss?sugu",
590						 stbuf->size, stbuf->type,
591						 stbuf->dev, &stbuf->qid,
592						 stbuf->mode, stbuf->atime,
593						 stbuf->mtime, stbuf->length,
594						 stbuf->name, stbuf->uid,
595						 stbuf->gid, stbuf->muid,
596						 stbuf->extension, stbuf->n_uid,
597						 stbuf->n_gid, stbuf->n_muid);
598			} break;
599		case 'V':{
600				uint32_t count = va_arg(ap, uint32_t);
601				struct iov_iter *from =
602						va_arg(ap, struct iov_iter *);
 
 
 
 
 
 
 
 
 
 
603				errcode = p9pdu_writef(pdu, proto_version, "d",
604									count);
605				if (!errcode && pdu_write_u(pdu, from, count))
606					errcode = -EFAULT;
607			}
608			break;
609		case 'T':{
610				uint16_t nwname = va_arg(ap, int);
611				const char **wnames = va_arg(ap, const char **);
612
613				errcode = p9pdu_writef(pdu, proto_version, "w",
614									nwname);
615				if (!errcode) {
616					int i;
617
618					for (i = 0; i < nwname; i++) {
619						errcode =
620						    p9pdu_writef(pdu,
621								proto_version,
622								 "s",
623								 wnames[i]);
624						if (errcode)
625							break;
626					}
627				}
628			}
629			break;
630		case 'R':{
631				uint16_t nwqid = va_arg(ap, int);
632				struct p9_qid *wqids =
633				    va_arg(ap, struct p9_qid *);
634
635				errcode = p9pdu_writef(pdu, proto_version, "w",
636									nwqid);
637				if (!errcode) {
638					int i;
639
640					for (i = 0; i < nwqid; i++) {
641						errcode =
642						    p9pdu_writef(pdu,
643								proto_version,
644								 "Q",
645								 &wqids[i]);
646						if (errcode)
647							break;
648					}
649				}
650			}
651			break;
652		case 'I':{
653				struct p9_iattr_dotl *p9attr = va_arg(ap,
654							struct p9_iattr_dotl *);
655
656				errcode = p9pdu_writef(pdu, proto_version,
657							"ddugqqqqq",
658							p9attr->valid,
659							p9attr->mode,
660							p9attr->uid,
661							p9attr->gid,
662							p9attr->size,
663							p9attr->atime_sec,
664							p9attr->atime_nsec,
665							p9attr->mtime_sec,
666							p9attr->mtime_nsec);
667			}
668			break;
669		case '?':
670			if ((proto_version != p9_proto_2000u) &&
671				(proto_version != p9_proto_2000L))
672				return 0;
673			break;
674		default:
675			BUG();
676			break;
677		}
678
679		if (errcode)
680			break;
681	}
682
683	return errcode;
684}
685
686int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
687{
688	va_list ap;
689	int ret;
690
691	va_start(ap, fmt);
692	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
693	va_end(ap);
694
695	return ret;
696}
697
698static int
699p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
700{
701	va_list ap;
702	int ret;
703
704	va_start(ap, fmt);
705	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
706	va_end(ap);
707
708	return ret;
709}
710
711int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
712{
713	struct p9_fcall fake_pdu;
714	int ret;
715
716	fake_pdu.size = len;
717	fake_pdu.capacity = len;
718	fake_pdu.sdata = buf;
719	fake_pdu.offset = 0;
720
721	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
722	if (ret) {
723		p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
724		trace_9p_protocol_dump(clnt, &fake_pdu);
725		return ret;
726	}
727
728	return fake_pdu.offset;
729}
730EXPORT_SYMBOL(p9stat_read);
731
732int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
733{
734	pdu->id = type;
735	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
736}
737
738int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
739{
740	int size = pdu->size;
741	int err;
742
743	pdu->size = 0;
744	err = p9pdu_writef(pdu, 0, "d", size);
745	pdu->size = size;
746
747	trace_9p_protocol_dump(clnt, pdu);
748	p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
749		 pdu->size, pdu->id, pdu->tag);
750
751	return err;
752}
753
754void p9pdu_reset(struct p9_fcall *pdu)
755{
756	pdu->offset = 0;
757	pdu->size = 0;
758}
759
760int p9dirent_read(struct p9_client *clnt, char *buf, int len,
761		  struct p9_dirent *dirent)
762{
763	struct p9_fcall fake_pdu;
764	int ret;
765	char *nameptr;
766
767	fake_pdu.size = len;
768	fake_pdu.capacity = len;
769	fake_pdu.sdata = buf;
770	fake_pdu.offset = 0;
771
772	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
773			  &dirent->d_off, &dirent->d_type, &nameptr);
774	if (ret) {
775		p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
776		trace_9p_protocol_dump(clnt, &fake_pdu);
777		return ret;
778	}
779
780	ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
781	if (ret < 0) {
782		p9_debug(P9_DEBUG_ERROR,
783			 "On the wire dirent name too long: %s\n",
784			 nameptr);
785		kfree(nameptr);
786		return ret;
787	}
788	kfree(nameptr);
789
 
790	return fake_pdu.offset;
791}
792EXPORT_SYMBOL(p9dirent_read);
v3.15
 
  1/*
  2 * net/9p/protocol.c
  3 *
  4 * 9P Protocol Support Code
  5 *
  6 *  Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
  7 *
  8 *  Base on code from Anthony Liguori <aliguori@us.ibm.com>
  9 *  Copyright (C) 2008 by IBM, Corp.
 10 *
 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.
 14 *
 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.
 19 *
 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
 25 *
 26 */
 27
 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>
 38#include "protocol.h"
 39
 40#include <trace/events/9p.h>
 41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 42static int
 43p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
 44
 45void p9stat_free(struct p9_wstat *stbuf)
 46{
 47	kfree(stbuf->name);
 
 48	kfree(stbuf->uid);
 
 49	kfree(stbuf->gid);
 
 50	kfree(stbuf->muid);
 
 51	kfree(stbuf->extension);
 
 52}
 53EXPORT_SYMBOL(p9stat_free);
 54
 55size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
 56{
 57	size_t len = min(pdu->size - pdu->offset, size);
 
 58	memcpy(data, &pdu->sdata[pdu->offset], len);
 59	pdu->offset += len;
 60	return size - len;
 61}
 62
 63static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
 64{
 65	size_t len = min(pdu->capacity - pdu->size, size);
 
 66	memcpy(&pdu->sdata[pdu->size], data, len);
 67	pdu->size += len;
 68	return size - len;
 69}
 70
 71static size_t
 72pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
 73{
 74	size_t len = min(pdu->capacity - pdu->size, size);
 75	if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
 
 76		len = 0;
 77
 78	pdu->size += len;
 79	return size - len;
 80}
 81
 82/*
 83	b - int8_t
 84	w - int16_t
 85	d - int32_t
 86	q - int64_t
 87	s - string
 88	u - numeric uid
 89	g - numeric gid
 90	S - stat
 91	Q - qid
 92	D - data blob (int32_t size followed by void *, results are not freed)
 93	T - array of strings (int16_t count, followed by strings)
 94	R - array of qids (int16_t count, followed by qids)
 95	A - stat for 9p2000.L (p9_stat_dotl)
 96	? - if optional = 1, continue parsing
 97*/
 98
 99static int
100p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
101	va_list ap)
102{
103	const char *ptr;
104	int errcode = 0;
105
106	for (ptr = fmt; *ptr; ptr++) {
107		switch (*ptr) {
108		case 'b':{
109				int8_t *val = va_arg(ap, int8_t *);
110				if (pdu_read(pdu, val, sizeof(*val))) {
111					errcode = -EFAULT;
112					break;
113				}
114			}
115			break;
116		case 'w':{
117				int16_t *val = va_arg(ap, int16_t *);
118				__le16 le_val;
119				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
120					errcode = -EFAULT;
121					break;
122				}
123				*val = le16_to_cpu(le_val);
124			}
125			break;
126		case 'd':{
127				int32_t *val = va_arg(ap, int32_t *);
128				__le32 le_val;
129				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
130					errcode = -EFAULT;
131					break;
132				}
133				*val = le32_to_cpu(le_val);
134			}
135			break;
136		case 'q':{
137				int64_t *val = va_arg(ap, int64_t *);
138				__le64 le_val;
139				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
140					errcode = -EFAULT;
141					break;
142				}
143				*val = le64_to_cpu(le_val);
144			}
145			break;
146		case 's':{
147				char **sptr = va_arg(ap, char **);
148				uint16_t len;
149
150				errcode = p9pdu_readf(pdu, proto_version,
151								"w", &len);
152				if (errcode)
153					break;
154
155				*sptr = kmalloc(len + 1, GFP_NOFS);
156				if (*sptr == NULL) {
157					errcode = -EFAULT;
158					break;
159				}
160				if (pdu_read(pdu, *sptr, len)) {
161					errcode = -EFAULT;
162					kfree(*sptr);
163					*sptr = NULL;
164				} else
165					(*sptr)[len] = 0;
166			}
167			break;
168		case 'u': {
169				kuid_t *uid = va_arg(ap, kuid_t *);
170				__le32 le_val;
171				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
172					errcode = -EFAULT;
173					break;
174				}
175				*uid = make_kuid(&init_user_ns,
176						 le32_to_cpu(le_val));
177			} break;
178		case 'g': {
179				kgid_t *gid = va_arg(ap, kgid_t *);
180				__le32 le_val;
181				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
182					errcode = -EFAULT;
183					break;
184				}
185				*gid = make_kgid(&init_user_ns,
186						 le32_to_cpu(le_val));
187			} break;
188		case 'Q':{
189				struct p9_qid *qid =
190				    va_arg(ap, struct p9_qid *);
191
192				errcode = p9pdu_readf(pdu, proto_version, "bdq",
193						      &qid->type, &qid->version,
194						      &qid->path);
195			}
196			break;
197		case 'S':{
198				struct p9_wstat *stbuf =
199				    va_arg(ap, struct p9_wstat *);
200
201				memset(stbuf, 0, sizeof(struct p9_wstat));
202				stbuf->n_uid = stbuf->n_muid = INVALID_UID;
203				stbuf->n_gid = INVALID_GID;
204
205				errcode =
206				    p9pdu_readf(pdu, proto_version,
207						"wwdQdddqssss?sugu",
208						&stbuf->size, &stbuf->type,
209						&stbuf->dev, &stbuf->qid,
210						&stbuf->mode, &stbuf->atime,
211						&stbuf->mtime, &stbuf->length,
212						&stbuf->name, &stbuf->uid,
213						&stbuf->gid, &stbuf->muid,
214						&stbuf->extension,
215						&stbuf->n_uid, &stbuf->n_gid,
216						&stbuf->n_muid);
217				if (errcode)
218					p9stat_free(stbuf);
219			}
220			break;
221		case 'D':{
222				uint32_t *count = va_arg(ap, uint32_t *);
223				void **data = va_arg(ap, void **);
224
225				errcode =
226				    p9pdu_readf(pdu, proto_version, "d", count);
227				if (!errcode) {
228					*count =
229					    min_t(uint32_t, *count,
230						  pdu->size - pdu->offset);
231					*data = &pdu->sdata[pdu->offset];
232				}
233			}
234			break;
235		case 'T':{
236				uint16_t *nwname = va_arg(ap, uint16_t *);
237				char ***wnames = va_arg(ap, char ***);
238
239				errcode = p9pdu_readf(pdu, proto_version,
240								"w", nwname);
241				if (!errcode) {
242					*wnames =
243					    kmalloc(sizeof(char *) * *nwname,
244						    GFP_NOFS);
 
245					if (!*wnames)
246						errcode = -ENOMEM;
247				}
248
249				if (!errcode) {
250					int i;
251
252					for (i = 0; i < *nwname; i++) {
253						errcode =
254						    p9pdu_readf(pdu,
255								proto_version,
256								"s",
257								&(*wnames)[i]);
258						if (errcode)
259							break;
260					}
261				}
262
263				if (errcode) {
264					if (*wnames) {
265						int i;
266
267						for (i = 0; i < *nwname; i++)
268							kfree((*wnames)[i]);
269					}
270					kfree(*wnames);
271					*wnames = NULL;
272				}
273			}
274			break;
275		case 'R':{
276				int16_t *nwqid = va_arg(ap, int16_t *);
277				struct p9_qid **wqids =
278				    va_arg(ap, struct p9_qid **);
279
280				*wqids = NULL;
281
282				errcode =
283				    p9pdu_readf(pdu, proto_version, "w", nwqid);
284				if (!errcode) {
285					*wqids =
286					    kmalloc(*nwqid *
287						    sizeof(struct p9_qid),
288						    GFP_NOFS);
289					if (*wqids == NULL)
290						errcode = -ENOMEM;
291				}
292
293				if (!errcode) {
294					int i;
295
296					for (i = 0; i < *nwqid; i++) {
297						errcode =
298						    p9pdu_readf(pdu,
299								proto_version,
300								"Q",
301								&(*wqids)[i]);
302						if (errcode)
303							break;
304					}
305				}
306
307				if (errcode) {
308					kfree(*wqids);
309					*wqids = NULL;
310				}
311			}
312			break;
313		case 'A': {
314				struct p9_stat_dotl *stbuf =
315				    va_arg(ap, struct p9_stat_dotl *);
316
317				memset(stbuf, 0, sizeof(struct p9_stat_dotl));
318				errcode =
319				    p9pdu_readf(pdu, proto_version,
320					"qQdugqqqqqqqqqqqqqqq",
321					&stbuf->st_result_mask,
322					&stbuf->qid,
323					&stbuf->st_mode,
324					&stbuf->st_uid, &stbuf->st_gid,
325					&stbuf->st_nlink,
326					&stbuf->st_rdev, &stbuf->st_size,
327					&stbuf->st_blksize, &stbuf->st_blocks,
328					&stbuf->st_atime_sec,
329					&stbuf->st_atime_nsec,
330					&stbuf->st_mtime_sec,
331					&stbuf->st_mtime_nsec,
332					&stbuf->st_ctime_sec,
333					&stbuf->st_ctime_nsec,
334					&stbuf->st_btime_sec,
335					&stbuf->st_btime_nsec,
336					&stbuf->st_gen,
337					&stbuf->st_data_version);
338			}
339			break;
340		case '?':
341			if ((proto_version != p9_proto_2000u) &&
342				(proto_version != p9_proto_2000L))
343				return 0;
344			break;
345		default:
346			BUG();
347			break;
348		}
349
350		if (errcode)
351			break;
352	}
353
354	return errcode;
355}
356
357int
358p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
359	va_list ap)
360{
361	const char *ptr;
362	int errcode = 0;
363
364	for (ptr = fmt; *ptr; ptr++) {
365		switch (*ptr) {
366		case 'b':{
367				int8_t val = va_arg(ap, int);
368				if (pdu_write(pdu, &val, sizeof(val)))
369					errcode = -EFAULT;
370			}
371			break;
372		case 'w':{
373				__le16 val = cpu_to_le16(va_arg(ap, int));
374				if (pdu_write(pdu, &val, sizeof(val)))
375					errcode = -EFAULT;
376			}
377			break;
378		case 'd':{
379				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
380				if (pdu_write(pdu, &val, sizeof(val)))
381					errcode = -EFAULT;
382			}
383			break;
384		case 'q':{
385				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
386				if (pdu_write(pdu, &val, sizeof(val)))
387					errcode = -EFAULT;
388			}
389			break;
390		case 's':{
391				const char *sptr = va_arg(ap, const char *);
392				uint16_t len = 0;
393				if (sptr)
394					len = min_t(size_t, strlen(sptr),
395								USHRT_MAX);
396
397				errcode = p9pdu_writef(pdu, proto_version,
398								"w", len);
399				if (!errcode && pdu_write(pdu, sptr, len))
400					errcode = -EFAULT;
401			}
402			break;
403		case 'u': {
404				kuid_t uid = va_arg(ap, kuid_t);
405				__le32 val = cpu_to_le32(
406						from_kuid(&init_user_ns, uid));
407				if (pdu_write(pdu, &val, sizeof(val)))
408					errcode = -EFAULT;
409			} break;
410		case 'g': {
411				kgid_t gid = va_arg(ap, kgid_t);
412				__le32 val = cpu_to_le32(
413						from_kgid(&init_user_ns, gid));
414				if (pdu_write(pdu, &val, sizeof(val)))
415					errcode = -EFAULT;
416			} break;
417		case 'Q':{
418				const struct p9_qid *qid =
419				    va_arg(ap, const struct p9_qid *);
420				errcode =
421				    p9pdu_writef(pdu, proto_version, "bdq",
422						 qid->type, qid->version,
423						 qid->path);
424			} break;
425		case 'S':{
426				const struct p9_wstat *stbuf =
427				    va_arg(ap, const struct p9_wstat *);
428				errcode =
429				    p9pdu_writef(pdu, proto_version,
430						 "wwdQdddqssss?sugu",
431						 stbuf->size, stbuf->type,
432						 stbuf->dev, &stbuf->qid,
433						 stbuf->mode, stbuf->atime,
434						 stbuf->mtime, stbuf->length,
435						 stbuf->name, stbuf->uid,
436						 stbuf->gid, stbuf->muid,
437						 stbuf->extension, stbuf->n_uid,
438						 stbuf->n_gid, stbuf->n_muid);
439			} break;
440		case 'D':{
441				uint32_t count = va_arg(ap, uint32_t);
442				const void *data = va_arg(ap, const void *);
443
444				errcode = p9pdu_writef(pdu, proto_version, "d",
445									count);
446				if (!errcode && pdu_write(pdu, data, count))
447					errcode = -EFAULT;
448			}
449			break;
450		case 'U':{
451				int32_t count = va_arg(ap, int32_t);
452				const char __user *udata =
453						va_arg(ap, const void __user *);
454				errcode = p9pdu_writef(pdu, proto_version, "d",
455									count);
456				if (!errcode && pdu_write_u(pdu, udata, count))
457					errcode = -EFAULT;
458			}
459			break;
460		case 'T':{
461				uint16_t nwname = va_arg(ap, int);
462				const char **wnames = va_arg(ap, const char **);
463
464				errcode = p9pdu_writef(pdu, proto_version, "w",
465									nwname);
466				if (!errcode) {
467					int i;
468
469					for (i = 0; i < nwname; i++) {
470						errcode =
471						    p9pdu_writef(pdu,
472								proto_version,
473								 "s",
474								 wnames[i]);
475						if (errcode)
476							break;
477					}
478				}
479			}
480			break;
481		case 'R':{
482				int16_t nwqid = va_arg(ap, int);
483				struct p9_qid *wqids =
484				    va_arg(ap, struct p9_qid *);
485
486				errcode = p9pdu_writef(pdu, proto_version, "w",
487									nwqid);
488				if (!errcode) {
489					int i;
490
491					for (i = 0; i < nwqid; i++) {
492						errcode =
493						    p9pdu_writef(pdu,
494								proto_version,
495								 "Q",
496								 &wqids[i]);
497						if (errcode)
498							break;
499					}
500				}
501			}
502			break;
503		case 'I':{
504				struct p9_iattr_dotl *p9attr = va_arg(ap,
505							struct p9_iattr_dotl *);
506
507				errcode = p9pdu_writef(pdu, proto_version,
508							"ddugqqqqq",
509							p9attr->valid,
510							p9attr->mode,
511							p9attr->uid,
512							p9attr->gid,
513							p9attr->size,
514							p9attr->atime_sec,
515							p9attr->atime_nsec,
516							p9attr->mtime_sec,
517							p9attr->mtime_nsec);
518			}
519			break;
520		case '?':
521			if ((proto_version != p9_proto_2000u) &&
522				(proto_version != p9_proto_2000L))
523				return 0;
524			break;
525		default:
526			BUG();
527			break;
528		}
529
530		if (errcode)
531			break;
532	}
533
534	return errcode;
535}
536
537int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
538{
539	va_list ap;
540	int ret;
541
542	va_start(ap, fmt);
543	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
544	va_end(ap);
545
546	return ret;
547}
548
549static int
550p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
551{
552	va_list ap;
553	int ret;
554
555	va_start(ap, fmt);
556	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
557	va_end(ap);
558
559	return ret;
560}
561
562int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
563{
564	struct p9_fcall fake_pdu;
565	int ret;
566
567	fake_pdu.size = len;
568	fake_pdu.capacity = len;
569	fake_pdu.sdata = buf;
570	fake_pdu.offset = 0;
571
572	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
573	if (ret) {
574		p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
575		trace_9p_protocol_dump(clnt, &fake_pdu);
 
576	}
577
578	return ret;
579}
580EXPORT_SYMBOL(p9stat_read);
581
582int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
583{
584	pdu->id = type;
585	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
586}
587
588int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
589{
590	int size = pdu->size;
591	int err;
592
593	pdu->size = 0;
594	err = p9pdu_writef(pdu, 0, "d", size);
595	pdu->size = size;
596
597	trace_9p_protocol_dump(clnt, pdu);
598	p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
599		 pdu->size, pdu->id, pdu->tag);
600
601	return err;
602}
603
604void p9pdu_reset(struct p9_fcall *pdu)
605{
606	pdu->offset = 0;
607	pdu->size = 0;
608}
609
610int p9dirent_read(struct p9_client *clnt, char *buf, int len,
611		  struct p9_dirent *dirent)
612{
613	struct p9_fcall fake_pdu;
614	int ret;
615	char *nameptr;
616
617	fake_pdu.size = len;
618	fake_pdu.capacity = len;
619	fake_pdu.sdata = buf;
620	fake_pdu.offset = 0;
621
622	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
623			  &dirent->d_off, &dirent->d_type, &nameptr);
624	if (ret) {
625		p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
626		trace_9p_protocol_dump(clnt, &fake_pdu);
627		goto out;
628	}
629
630	strcpy(dirent->d_name, nameptr);
 
 
 
 
 
 
 
631	kfree(nameptr);
632
633out:
634	return fake_pdu.offset;
635}
636EXPORT_SYMBOL(p9dirent_read);