Linux Audio

Check our new training course

Loading...
v3.1
  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
 
 
 40static int
 41p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
 42
 43#ifdef CONFIG_NET_9P_DEBUG
 44void
 45p9pdu_dump(int way, struct p9_fcall *pdu)
 46{
 47	int len = pdu->size;
 48
 49	if ((p9_debug_level & P9_DEBUG_VPKT) != P9_DEBUG_VPKT) {
 50		if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT) {
 51			if (len > 32)
 52				len = 32;
 53		} else {
 54			/* shouldn't happen */
 55			return;
 56		}
 57	}
 58
 59	if (way)
 60		print_hex_dump_bytes("[9P] ", DUMP_PREFIX_OFFSET, pdu->sdata,
 61									len);
 62	else
 63		print_hex_dump_bytes("]9P[ ", DUMP_PREFIX_OFFSET, pdu->sdata,
 64									len);
 65}
 66#else
 67void
 68p9pdu_dump(int way, struct p9_fcall *pdu)
 69{
 70}
 71#endif
 72EXPORT_SYMBOL(p9pdu_dump);
 73
 74void p9stat_free(struct p9_wstat *stbuf)
 75{
 76	kfree(stbuf->name);
 77	kfree(stbuf->uid);
 78	kfree(stbuf->gid);
 79	kfree(stbuf->muid);
 80	kfree(stbuf->extension);
 81}
 82EXPORT_SYMBOL(p9stat_free);
 83
 84static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
 85{
 86	size_t len = min(pdu->size - pdu->offset, size);
 87	memcpy(data, &pdu->sdata[pdu->offset], len);
 88	pdu->offset += len;
 89	return size - len;
 90}
 91
 92static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
 93{
 94	size_t len = min(pdu->capacity - pdu->size, size);
 95	memcpy(&pdu->sdata[pdu->size], data, len);
 96	pdu->size += len;
 97	return size - len;
 98}
 99
100static size_t
101pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
102{
103	size_t len = min(pdu->capacity - pdu->size, size);
104	if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
 
105		len = 0;
106
107	pdu->size += len;
108	return size - len;
109}
110
111static size_t
112pdu_write_urw(struct p9_fcall *pdu, const char *kdata, const char __user *udata,
113		size_t size)
114{
115	BUG_ON(pdu->size > P9_IOHDRSZ);
116	pdu->pubuf = (char __user *)udata;
117	pdu->pkbuf = (char *)kdata;
118	pdu->pbuf_size = size;
119	return 0;
120}
121
122static size_t
123pdu_write_readdir(struct p9_fcall *pdu, const char *kdata, size_t size)
124{
125	BUG_ON(pdu->size > P9_READDIRHDRSZ);
126	pdu->pkbuf = (char *)kdata;
127	pdu->pbuf_size = size;
128	return 0;
129}
130
131/*
132	b - int8_t
133	w - int16_t
134	d - int32_t
135	q - int64_t
136	s - string
 
 
137	S - stat
138	Q - qid
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
144*/
145
146static int
147p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
148	va_list ap)
149{
150	const char *ptr;
151	int errcode = 0;
152
153	for (ptr = fmt; *ptr; ptr++) {
154		switch (*ptr) {
155		case 'b':{
156				int8_t *val = va_arg(ap, int8_t *);
157				if (pdu_read(pdu, val, sizeof(*val))) {
158					errcode = -EFAULT;
159					break;
160				}
161			}
162			break;
163		case 'w':{
164				int16_t *val = va_arg(ap, int16_t *);
165				__le16 le_val;
166				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
167					errcode = -EFAULT;
168					break;
169				}
170				*val = le16_to_cpu(le_val);
171			}
172			break;
173		case 'd':{
174				int32_t *val = va_arg(ap, int32_t *);
175				__le32 le_val;
176				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
177					errcode = -EFAULT;
178					break;
179				}
180				*val = le32_to_cpu(le_val);
181			}
182			break;
183		case 'q':{
184				int64_t *val = va_arg(ap, int64_t *);
185				__le64 le_val;
186				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
187					errcode = -EFAULT;
188					break;
189				}
190				*val = le64_to_cpu(le_val);
191			}
192			break;
193		case 's':{
194				char **sptr = va_arg(ap, char **);
195				uint16_t len;
196
197				errcode = p9pdu_readf(pdu, proto_version,
198								"w", &len);
199				if (errcode)
200					break;
201
202				*sptr = kmalloc(len + 1, GFP_NOFS);
203				if (*sptr == NULL) {
204					errcode = -EFAULT;
205					break;
206				}
207				if (pdu_read(pdu, *sptr, len)) {
208					errcode = -EFAULT;
209					kfree(*sptr);
210					*sptr = NULL;
211				} else
212					(*sptr)[len] = 0;
213			}
214			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215		case 'Q':{
216				struct p9_qid *qid =
217				    va_arg(ap, struct p9_qid *);
218
219				errcode = p9pdu_readf(pdu, proto_version, "bdq",
220						      &qid->type, &qid->version,
221						      &qid->path);
222			}
223			break;
224		case 'S':{
225				struct p9_wstat *stbuf =
226				    va_arg(ap, struct p9_wstat *);
227
228				memset(stbuf, 0, sizeof(struct p9_wstat));
229				stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
230									-1;
 
231				errcode =
232				    p9pdu_readf(pdu, proto_version,
233						"wwdQdddqssss?sddd",
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,
240						&stbuf->extension,
241						&stbuf->n_uid, &stbuf->n_gid,
242						&stbuf->n_muid);
243				if (errcode)
244					p9stat_free(stbuf);
245			}
246			break;
247		case 'D':{
248				uint32_t *count = va_arg(ap, uint32_t *);
249				void **data = va_arg(ap, void **);
250
251				errcode =
252				    p9pdu_readf(pdu, proto_version, "d", count);
253				if (!errcode) {
254					*count =
255					    min_t(uint32_t, *count,
256						  pdu->size - pdu->offset);
257					*data = &pdu->sdata[pdu->offset];
258				}
259			}
260			break;
261		case 'T':{
262				uint16_t *nwname = va_arg(ap, uint16_t *);
263				char ***wnames = va_arg(ap, char ***);
264
265				errcode = p9pdu_readf(pdu, proto_version,
266								"w", nwname);
267				if (!errcode) {
268					*wnames =
269					    kmalloc(sizeof(char *) * *nwname,
270						    GFP_NOFS);
271					if (!*wnames)
272						errcode = -ENOMEM;
273				}
274
275				if (!errcode) {
276					int i;
277
278					for (i = 0; i < *nwname; i++) {
279						errcode =
280						    p9pdu_readf(pdu,
281								proto_version,
282								"s",
283								&(*wnames)[i]);
284						if (errcode)
285							break;
286					}
287				}
288
289				if (errcode) {
290					if (*wnames) {
291						int i;
292
293						for (i = 0; i < *nwname; i++)
294							kfree((*wnames)[i]);
295					}
296					kfree(*wnames);
297					*wnames = NULL;
298				}
299			}
300			break;
301		case 'R':{
302				int16_t *nwqid = va_arg(ap, int16_t *);
303				struct p9_qid **wqids =
304				    va_arg(ap, struct p9_qid **);
305
306				*wqids = NULL;
307
308				errcode =
309				    p9pdu_readf(pdu, proto_version, "w", nwqid);
310				if (!errcode) {
311					*wqids =
312					    kmalloc(*nwqid *
313						    sizeof(struct p9_qid),
314						    GFP_NOFS);
315					if (*wqids == NULL)
316						errcode = -ENOMEM;
317				}
318
319				if (!errcode) {
320					int i;
321
322					for (i = 0; i < *nwqid; i++) {
323						errcode =
324						    p9pdu_readf(pdu,
325								proto_version,
326								"Q",
327								&(*wqids)[i]);
328						if (errcode)
329							break;
330					}
331				}
332
333				if (errcode) {
334					kfree(*wqids);
335					*wqids = NULL;
336				}
337			}
338			break;
339		case 'A': {
340				struct p9_stat_dotl *stbuf =
341				    va_arg(ap, struct p9_stat_dotl *);
342
343				memset(stbuf, 0, sizeof(struct p9_stat_dotl));
344				errcode =
345				    p9pdu_readf(pdu, proto_version,
346					"qQdddqqqqqqqqqqqqqqq",
347					&stbuf->st_result_mask,
348					&stbuf->qid,
349					&stbuf->st_mode,
350					&stbuf->st_uid, &stbuf->st_gid,
351					&stbuf->st_nlink,
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,
362					&stbuf->st_gen,
363					&stbuf->st_data_version);
364			}
365			break;
366		case '?':
367			if ((proto_version != p9_proto_2000u) &&
368				(proto_version != p9_proto_2000L))
369				return 0;
370			break;
371		default:
372			BUG();
373			break;
374		}
375
376		if (errcode)
377			break;
378	}
379
380	return errcode;
381}
382
383int
384p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
385	va_list ap)
386{
387	const char *ptr;
388	int errcode = 0;
389
390	for (ptr = fmt; *ptr; ptr++) {
391		switch (*ptr) {
392		case 'b':{
393				int8_t val = va_arg(ap, int);
394				if (pdu_write(pdu, &val, sizeof(val)))
395					errcode = -EFAULT;
396			}
397			break;
398		case 'w':{
399				__le16 val = cpu_to_le16(va_arg(ap, int));
400				if (pdu_write(pdu, &val, sizeof(val)))
401					errcode = -EFAULT;
402			}
403			break;
404		case 'd':{
405				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
406				if (pdu_write(pdu, &val, sizeof(val)))
407					errcode = -EFAULT;
408			}
409			break;
410		case 'q':{
411				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
412				if (pdu_write(pdu, &val, sizeof(val)))
413					errcode = -EFAULT;
414			}
415			break;
416		case 's':{
417				const char *sptr = va_arg(ap, const char *);
418				uint16_t len = 0;
419				if (sptr)
420					len = min_t(uint16_t, strlen(sptr),
421								USHRT_MAX);
422
423				errcode = p9pdu_writef(pdu, proto_version,
424								"w", len);
425				if (!errcode && pdu_write(pdu, sptr, len))
426					errcode = -EFAULT;
427			}
428			break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
429		case 'Q':{
430				const struct p9_qid *qid =
431				    va_arg(ap, const struct p9_qid *);
432				errcode =
433				    p9pdu_writef(pdu, proto_version, "bdq",
434						 qid->type, qid->version,
435						 qid->path);
436			} break;
437		case 'S':{
438				const struct p9_wstat *stbuf =
439				    va_arg(ap, const struct p9_wstat *);
440				errcode =
441				    p9pdu_writef(pdu, proto_version,
442						 "wwdQdddqssss?sddd",
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);
451			} break;
452		case 'D':{
453				uint32_t count = va_arg(ap, uint32_t);
454				const void *data = va_arg(ap, const void *);
455
456				errcode = p9pdu_writef(pdu, proto_version, "d",
457									count);
458				if (!errcode && pdu_write(pdu, data, count))
459					errcode = -EFAULT;
460			}
461			break;
462		case 'E':{
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",
468						 cnt);
469				 if (!errcode && pdu_write_urw(pdu, k, u, cnt))
470					errcode = -EFAULT;
471			 }
472			 break;
473		case 'F':{
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",
477						 cnt);
478				 if (!errcode && pdu_write_readdir(pdu, k, cnt))
479					errcode = -EFAULT;
480			 }
481			 break;
482		case 'U':{
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",
487									count);
488				if (!errcode && pdu_write_u(pdu, udata, count))
489					errcode = -EFAULT;
490			}
491			break;
492		case 'T':{
493				uint16_t nwname = va_arg(ap, int);
494				const char **wnames = va_arg(ap, const char **);
495
496				errcode = p9pdu_writef(pdu, proto_version, "w",
497									nwname);
498				if (!errcode) {
499					int i;
500
501					for (i = 0; i < nwname; i++) {
502						errcode =
503						    p9pdu_writef(pdu,
504								proto_version,
505								 "s",
506								 wnames[i]);
507						if (errcode)
508							break;
509					}
510				}
511			}
512			break;
513		case 'R':{
514				int16_t nwqid = va_arg(ap, int);
515				struct p9_qid *wqids =
516				    va_arg(ap, struct p9_qid *);
517
518				errcode = p9pdu_writef(pdu, proto_version, "w",
519									nwqid);
520				if (!errcode) {
521					int i;
522
523					for (i = 0; i < nwqid; i++) {
524						errcode =
525						    p9pdu_writef(pdu,
526								proto_version,
527								 "Q",
528								 &wqids[i]);
529						if (errcode)
530							break;
531					}
532				}
533			}
534			break;
535		case 'I':{
536				struct p9_iattr_dotl *p9attr = va_arg(ap,
537							struct p9_iattr_dotl *);
538
539				errcode = p9pdu_writef(pdu, proto_version,
540							"ddddqqqqq",
541							p9attr->valid,
542							p9attr->mode,
543							p9attr->uid,
544							p9attr->gid,
545							p9attr->size,
546							p9attr->atime_sec,
547							p9attr->atime_nsec,
548							p9attr->mtime_sec,
549							p9attr->mtime_nsec);
550			}
551			break;
552		case '?':
553			if ((proto_version != p9_proto_2000u) &&
554				(proto_version != p9_proto_2000L))
555				return 0;
556			break;
557		default:
558			BUG();
559			break;
560		}
561
562		if (errcode)
563			break;
564	}
565
566	return errcode;
567}
568
569int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
570{
571	va_list ap;
572	int ret;
573
574	va_start(ap, fmt);
575	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
576	va_end(ap);
577
578	return ret;
579}
580
581static int
582p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
583{
584	va_list ap;
585	int ret;
586
587	va_start(ap, fmt);
588	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
589	va_end(ap);
590
591	return ret;
592}
593
594int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
595{
596	struct p9_fcall fake_pdu;
597	int ret;
598
599	fake_pdu.size = len;
600	fake_pdu.capacity = len;
601	fake_pdu.sdata = buf;
602	fake_pdu.offset = 0;
603
604	ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
605	if (ret) {
606		P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
607		P9_DUMP_PKT(0, &fake_pdu);
608	}
609
610	return ret;
611}
612EXPORT_SYMBOL(p9stat_read);
613
614int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
615{
616	pdu->id = type;
617	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
618}
619
620int p9pdu_finalize(struct p9_fcall *pdu)
621{
622	int size = pdu->size;
623	int err;
624
625	pdu->size = 0;
626	err = p9pdu_writef(pdu, 0, "d", size);
627	pdu->size = size;
628
629	P9_DUMP_PKT(0, pdu);
630	P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
631							pdu->id, pdu->tag);
632
633	return err;
634}
635
636void p9pdu_reset(struct p9_fcall *pdu)
637{
638	pdu->offset = 0;
639	pdu->size = 0;
640	pdu->private = NULL;
641	pdu->pubuf = NULL;
642	pdu->pkbuf = NULL;
643	pdu->pbuf_size = 0;
644}
645
646int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
647						int proto_version)
648{
649	struct p9_fcall fake_pdu;
650	int ret;
651	char *nameptr;
652
653	fake_pdu.size = len;
654	fake_pdu.capacity = len;
655	fake_pdu.sdata = buf;
656	fake_pdu.offset = 0;
657
658	ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
659			&dirent->d_off, &dirent->d_type, &nameptr);
660	if (ret) {
661		P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
662		P9_DUMP_PKT(1, &fake_pdu);
663		goto out;
664	}
665
666	strcpy(dirent->d_name, nameptr);
667	kfree(nameptr);
668
669out:
670	return fake_pdu.offset;
671}
672EXPORT_SYMBOL(p9dirent_read);
v4.6
  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 <linux/uio.h>
 37#include <net/9p/9p.h>
 38#include <net/9p/client.h>
 39#include "protocol.h"
 40
 41#include <trace/events/9p.h>
 42
 43static int
 44p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
 45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 46void p9stat_free(struct p9_wstat *stbuf)
 47{
 48	kfree(stbuf->name);
 49	kfree(stbuf->uid);
 50	kfree(stbuf->gid);
 51	kfree(stbuf->muid);
 52	kfree(stbuf->extension);
 53}
 54EXPORT_SYMBOL(p9stat_free);
 55
 56size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
 57{
 58	size_t len = min(pdu->size - pdu->offset, size);
 59	memcpy(data, &pdu->sdata[pdu->offset], len);
 60	pdu->offset += len;
 61	return size - len;
 62}
 63
 64static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
 65{
 66	size_t len = min(pdu->capacity - pdu->size, size);
 67	memcpy(&pdu->sdata[pdu->size], data, len);
 68	pdu->size += len;
 69	return size - len;
 70}
 71
 72static size_t
 73pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
 74{
 75	size_t len = min(pdu->capacity - pdu->size, size);
 76	struct iov_iter i = *from;
 77	if (copy_from_iter(&pdu->sdata[pdu->size], len, &i) != len)
 78		len = 0;
 79
 80	pdu->size += len;
 81	return size - len;
 82}
 83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 84/*
 85	b - int8_t
 86	w - int16_t
 87	d - int32_t
 88	q - int64_t
 89	s - string
 90	u - numeric uid
 91	g - numeric gid
 92	S - stat
 93	Q - qid
 94	D - data blob (int32_t size followed by void *, results are not freed)
 95	T - array of strings (int16_t count, followed by strings)
 96	R - array of qids (int16_t count, followed by qids)
 97	A - stat for 9p2000.L (p9_stat_dotl)
 98	? - if optional = 1, continue parsing
 99*/
100
101static int
102p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
103	va_list ap)
104{
105	const char *ptr;
106	int errcode = 0;
107
108	for (ptr = fmt; *ptr; ptr++) {
109		switch (*ptr) {
110		case 'b':{
111				int8_t *val = va_arg(ap, int8_t *);
112				if (pdu_read(pdu, val, sizeof(*val))) {
113					errcode = -EFAULT;
114					break;
115				}
116			}
117			break;
118		case 'w':{
119				int16_t *val = va_arg(ap, int16_t *);
120				__le16 le_val;
121				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
122					errcode = -EFAULT;
123					break;
124				}
125				*val = le16_to_cpu(le_val);
126			}
127			break;
128		case 'd':{
129				int32_t *val = va_arg(ap, int32_t *);
130				__le32 le_val;
131				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
132					errcode = -EFAULT;
133					break;
134				}
135				*val = le32_to_cpu(le_val);
136			}
137			break;
138		case 'q':{
139				int64_t *val = va_arg(ap, int64_t *);
140				__le64 le_val;
141				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
142					errcode = -EFAULT;
143					break;
144				}
145				*val = le64_to_cpu(le_val);
146			}
147			break;
148		case 's':{
149				char **sptr = va_arg(ap, char **);
150				uint16_t len;
151
152				errcode = p9pdu_readf(pdu, proto_version,
153								"w", &len);
154				if (errcode)
155					break;
156
157				*sptr = kmalloc(len + 1, GFP_NOFS);
158				if (*sptr == NULL) {
159					errcode = -EFAULT;
160					break;
161				}
162				if (pdu_read(pdu, *sptr, len)) {
163					errcode = -EFAULT;
164					kfree(*sptr);
165					*sptr = NULL;
166				} else
167					(*sptr)[len] = 0;
168			}
169			break;
170		case 'u': {
171				kuid_t *uid = va_arg(ap, kuid_t *);
172				__le32 le_val;
173				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
174					errcode = -EFAULT;
175					break;
176				}
177				*uid = make_kuid(&init_user_ns,
178						 le32_to_cpu(le_val));
179			} break;
180		case 'g': {
181				kgid_t *gid = va_arg(ap, kgid_t *);
182				__le32 le_val;
183				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
184					errcode = -EFAULT;
185					break;
186				}
187				*gid = make_kgid(&init_user_ns,
188						 le32_to_cpu(le_val));
189			} break;
190		case 'Q':{
191				struct p9_qid *qid =
192				    va_arg(ap, struct p9_qid *);
193
194				errcode = p9pdu_readf(pdu, proto_version, "bdq",
195						      &qid->type, &qid->version,
196						      &qid->path);
197			}
198			break;
199		case 'S':{
200				struct p9_wstat *stbuf =
201				    va_arg(ap, struct p9_wstat *);
202
203				memset(stbuf, 0, sizeof(struct p9_wstat));
204				stbuf->n_uid = stbuf->n_muid = INVALID_UID;
205				stbuf->n_gid = INVALID_GID;
206
207				errcode =
208				    p9pdu_readf(pdu, proto_version,
209						"wwdQdddqssss?sugu",
210						&stbuf->size, &stbuf->type,
211						&stbuf->dev, &stbuf->qid,
212						&stbuf->mode, &stbuf->atime,
213						&stbuf->mtime, &stbuf->length,
214						&stbuf->name, &stbuf->uid,
215						&stbuf->gid, &stbuf->muid,
216						&stbuf->extension,
217						&stbuf->n_uid, &stbuf->n_gid,
218						&stbuf->n_muid);
219				if (errcode)
220					p9stat_free(stbuf);
221			}
222			break;
223		case 'D':{
224				uint32_t *count = va_arg(ap, uint32_t *);
225				void **data = va_arg(ap, void **);
226
227				errcode =
228				    p9pdu_readf(pdu, proto_version, "d", count);
229				if (!errcode) {
230					*count =
231					    min_t(uint32_t, *count,
232						  pdu->size - pdu->offset);
233					*data = &pdu->sdata[pdu->offset];
234				}
235			}
236			break;
237		case 'T':{
238				uint16_t *nwname = va_arg(ap, uint16_t *);
239				char ***wnames = va_arg(ap, char ***);
240
241				errcode = p9pdu_readf(pdu, proto_version,
242								"w", nwname);
243				if (!errcode) {
244					*wnames =
245					    kmalloc(sizeof(char *) * *nwname,
246						    GFP_NOFS);
247					if (!*wnames)
248						errcode = -ENOMEM;
249				}
250
251				if (!errcode) {
252					int i;
253
254					for (i = 0; i < *nwname; i++) {
255						errcode =
256						    p9pdu_readf(pdu,
257								proto_version,
258								"s",
259								&(*wnames)[i]);
260						if (errcode)
261							break;
262					}
263				}
264
265				if (errcode) {
266					if (*wnames) {
267						int i;
268
269						for (i = 0; i < *nwname; i++)
270							kfree((*wnames)[i]);
271					}
272					kfree(*wnames);
273					*wnames = NULL;
274				}
275			}
276			break;
277		case 'R':{
278				uint16_t *nwqid = va_arg(ap, uint16_t *);
279				struct p9_qid **wqids =
280				    va_arg(ap, struct p9_qid **);
281
282				*wqids = NULL;
283
284				errcode =
285				    p9pdu_readf(pdu, proto_version, "w", nwqid);
286				if (!errcode) {
287					*wqids =
288					    kmalloc(*nwqid *
289						    sizeof(struct p9_qid),
290						    GFP_NOFS);
291					if (*wqids == NULL)
292						errcode = -ENOMEM;
293				}
294
295				if (!errcode) {
296					int i;
297
298					for (i = 0; i < *nwqid; i++) {
299						errcode =
300						    p9pdu_readf(pdu,
301								proto_version,
302								"Q",
303								&(*wqids)[i]);
304						if (errcode)
305							break;
306					}
307				}
308
309				if (errcode) {
310					kfree(*wqids);
311					*wqids = NULL;
312				}
313			}
314			break;
315		case 'A': {
316				struct p9_stat_dotl *stbuf =
317				    va_arg(ap, struct p9_stat_dotl *);
318
319				memset(stbuf, 0, sizeof(struct p9_stat_dotl));
320				errcode =
321				    p9pdu_readf(pdu, proto_version,
322					"qQdugqqqqqqqqqqqqqqq",
323					&stbuf->st_result_mask,
324					&stbuf->qid,
325					&stbuf->st_mode,
326					&stbuf->st_uid, &stbuf->st_gid,
327					&stbuf->st_nlink,
328					&stbuf->st_rdev, &stbuf->st_size,
329					&stbuf->st_blksize, &stbuf->st_blocks,
330					&stbuf->st_atime_sec,
331					&stbuf->st_atime_nsec,
332					&stbuf->st_mtime_sec,
333					&stbuf->st_mtime_nsec,
334					&stbuf->st_ctime_sec,
335					&stbuf->st_ctime_nsec,
336					&stbuf->st_btime_sec,
337					&stbuf->st_btime_nsec,
338					&stbuf->st_gen,
339					&stbuf->st_data_version);
340			}
341			break;
342		case '?':
343			if ((proto_version != p9_proto_2000u) &&
344				(proto_version != p9_proto_2000L))
345				return 0;
346			break;
347		default:
348			BUG();
349			break;
350		}
351
352		if (errcode)
353			break;
354	}
355
356	return errcode;
357}
358
359int
360p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
361	va_list ap)
362{
363	const char *ptr;
364	int errcode = 0;
365
366	for (ptr = fmt; *ptr; ptr++) {
367		switch (*ptr) {
368		case 'b':{
369				int8_t val = va_arg(ap, int);
370				if (pdu_write(pdu, &val, sizeof(val)))
371					errcode = -EFAULT;
372			}
373			break;
374		case 'w':{
375				__le16 val = cpu_to_le16(va_arg(ap, int));
376				if (pdu_write(pdu, &val, sizeof(val)))
377					errcode = -EFAULT;
378			}
379			break;
380		case 'd':{
381				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
382				if (pdu_write(pdu, &val, sizeof(val)))
383					errcode = -EFAULT;
384			}
385			break;
386		case 'q':{
387				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
388				if (pdu_write(pdu, &val, sizeof(val)))
389					errcode = -EFAULT;
390			}
391			break;
392		case 's':{
393				const char *sptr = va_arg(ap, const char *);
394				uint16_t len = 0;
395				if (sptr)
396					len = min_t(size_t, strlen(sptr),
397								USHRT_MAX);
398
399				errcode = p9pdu_writef(pdu, proto_version,
400								"w", len);
401				if (!errcode && pdu_write(pdu, sptr, len))
402					errcode = -EFAULT;
403			}
404			break;
405		case 'u': {
406				kuid_t uid = va_arg(ap, kuid_t);
407				__le32 val = cpu_to_le32(
408						from_kuid(&init_user_ns, uid));
409				if (pdu_write(pdu, &val, sizeof(val)))
410					errcode = -EFAULT;
411			} break;
412		case 'g': {
413				kgid_t gid = va_arg(ap, kgid_t);
414				__le32 val = cpu_to_le32(
415						from_kgid(&init_user_ns, gid));
416				if (pdu_write(pdu, &val, sizeof(val)))
417					errcode = -EFAULT;
418			} break;
419		case 'Q':{
420				const struct p9_qid *qid =
421				    va_arg(ap, const struct p9_qid *);
422				errcode =
423				    p9pdu_writef(pdu, proto_version, "bdq",
424						 qid->type, qid->version,
425						 qid->path);
426			} break;
427		case 'S':{
428				const struct p9_wstat *stbuf =
429				    va_arg(ap, const struct p9_wstat *);
430				errcode =
431				    p9pdu_writef(pdu, proto_version,
432						 "wwdQdddqssss?sugu",
433						 stbuf->size, stbuf->type,
434						 stbuf->dev, &stbuf->qid,
435						 stbuf->mode, stbuf->atime,
436						 stbuf->mtime, stbuf->length,
437						 stbuf->name, stbuf->uid,
438						 stbuf->gid, stbuf->muid,
439						 stbuf->extension, stbuf->n_uid,
440						 stbuf->n_gid, stbuf->n_muid);
441			} break;
442		case 'V':{
443				uint32_t count = va_arg(ap, uint32_t);
444				struct iov_iter *from =
445						va_arg(ap, struct iov_iter *);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446				errcode = p9pdu_writef(pdu, proto_version, "d",
447									count);
448				if (!errcode && pdu_write_u(pdu, from, count))
449					errcode = -EFAULT;
450			}
451			break;
452		case 'T':{
453				uint16_t nwname = va_arg(ap, int);
454				const char **wnames = va_arg(ap, const char **);
455
456				errcode = p9pdu_writef(pdu, proto_version, "w",
457									nwname);
458				if (!errcode) {
459					int i;
460
461					for (i = 0; i < nwname; i++) {
462						errcode =
463						    p9pdu_writef(pdu,
464								proto_version,
465								 "s",
466								 wnames[i]);
467						if (errcode)
468							break;
469					}
470				}
471			}
472			break;
473		case 'R':{
474				uint16_t nwqid = va_arg(ap, int);
475				struct p9_qid *wqids =
476				    va_arg(ap, struct p9_qid *);
477
478				errcode = p9pdu_writef(pdu, proto_version, "w",
479									nwqid);
480				if (!errcode) {
481					int i;
482
483					for (i = 0; i < nwqid; i++) {
484						errcode =
485						    p9pdu_writef(pdu,
486								proto_version,
487								 "Q",
488								 &wqids[i]);
489						if (errcode)
490							break;
491					}
492				}
493			}
494			break;
495		case 'I':{
496				struct p9_iattr_dotl *p9attr = va_arg(ap,
497							struct p9_iattr_dotl *);
498
499				errcode = p9pdu_writef(pdu, proto_version,
500							"ddugqqqqq",
501							p9attr->valid,
502							p9attr->mode,
503							p9attr->uid,
504							p9attr->gid,
505							p9attr->size,
506							p9attr->atime_sec,
507							p9attr->atime_nsec,
508							p9attr->mtime_sec,
509							p9attr->mtime_nsec);
510			}
511			break;
512		case '?':
513			if ((proto_version != p9_proto_2000u) &&
514				(proto_version != p9_proto_2000L))
515				return 0;
516			break;
517		default:
518			BUG();
519			break;
520		}
521
522		if (errcode)
523			break;
524	}
525
526	return errcode;
527}
528
529int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
530{
531	va_list ap;
532	int ret;
533
534	va_start(ap, fmt);
535	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
536	va_end(ap);
537
538	return ret;
539}
540
541static int
542p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
543{
544	va_list ap;
545	int ret;
546
547	va_start(ap, fmt);
548	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
549	va_end(ap);
550
551	return ret;
552}
553
554int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
555{
556	struct p9_fcall fake_pdu;
557	int ret;
558
559	fake_pdu.size = len;
560	fake_pdu.capacity = len;
561	fake_pdu.sdata = buf;
562	fake_pdu.offset = 0;
563
564	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
565	if (ret) {
566		p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
567		trace_9p_protocol_dump(clnt, &fake_pdu);
568	}
569
570	return ret;
571}
572EXPORT_SYMBOL(p9stat_read);
573
574int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
575{
576	pdu->id = type;
577	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
578}
579
580int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
581{
582	int size = pdu->size;
583	int err;
584
585	pdu->size = 0;
586	err = p9pdu_writef(pdu, 0, "d", size);
587	pdu->size = size;
588
589	trace_9p_protocol_dump(clnt, pdu);
590	p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
591		 pdu->size, pdu->id, pdu->tag);
592
593	return err;
594}
595
596void p9pdu_reset(struct p9_fcall *pdu)
597{
598	pdu->offset = 0;
599	pdu->size = 0;
 
 
 
 
600}
601
602int p9dirent_read(struct p9_client *clnt, char *buf, int len,
603		  struct p9_dirent *dirent)
604{
605	struct p9_fcall fake_pdu;
606	int ret;
607	char *nameptr;
608
609	fake_pdu.size = len;
610	fake_pdu.capacity = len;
611	fake_pdu.sdata = buf;
612	fake_pdu.offset = 0;
613
614	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
615			  &dirent->d_off, &dirent->d_type, &nameptr);
616	if (ret) {
617		p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
618		trace_9p_protocol_dump(clnt, &fake_pdu);
619		goto out;
620	}
621
622	strcpy(dirent->d_name, nameptr);
623	kfree(nameptr);
624
625out:
626	return fake_pdu.offset;
627}
628EXPORT_SYMBOL(p9dirent_read);