xref: /linux/net/9p/protocol.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
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  */
43 size_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 
193 static int
194 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
195 
196 void 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 }
209 EXPORT_SYMBOL(p9stat_free);
210 
211 size_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 
220 static 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 
229 static size_t
230 pdu_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 
257 static int
258 p9pdu_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 				*wnames = NULL;
398 
399 				errcode = p9pdu_readf(pdu, proto_version,
400 								"w", nwname);
401 				if (!errcode) {
402 					*wnames =
403 					    kmalloc_array(*nwname,
404 							  sizeof(char *),
405 							  GFP_NOFS);
406 					if (!*wnames)
407 						errcode = -ENOMEM;
408 					else
409 						(*wnames)[0] = NULL;
410 				}
411 
412 				if (!errcode) {
413 					int i;
414 
415 					for (i = 0; i < *nwname; i++) {
416 						errcode =
417 						    p9pdu_readf(pdu,
418 								proto_version,
419 								"s",
420 								&(*wnames)[i]);
421 						if (errcode) {
422 							(*wnames)[i] = NULL;
423 							break;
424 						}
425 					}
426 				}
427 
428 				if (errcode) {
429 					if (*wnames) {
430 						int i;
431 
432 						for (i = 0; i < *nwname; i++) {
433 							if (!(*wnames)[i])
434 								break;
435 							kfree((*wnames)[i]);
436 						}
437 						kfree(*wnames);
438 						*wnames = NULL;
439 					}
440 				}
441 			}
442 			break;
443 		case 'R':{
444 				uint16_t *nwqid = va_arg(ap, uint16_t *);
445 				struct p9_qid **wqids =
446 				    va_arg(ap, struct p9_qid **);
447 
448 				*wqids = NULL;
449 
450 				errcode =
451 				    p9pdu_readf(pdu, proto_version, "w", nwqid);
452 				if (!errcode) {
453 					*wqids =
454 					    kmalloc_objs(struct p9_qid, *nwqid,
455 							 GFP_NOFS);
456 					if (*wqids == NULL)
457 						errcode = -ENOMEM;
458 				}
459 
460 				if (!errcode) {
461 					int i;
462 
463 					for (i = 0; i < *nwqid; i++) {
464 						errcode =
465 						    p9pdu_readf(pdu,
466 								proto_version,
467 								"Q",
468 								&(*wqids)[i]);
469 						if (errcode)
470 							break;
471 					}
472 				}
473 
474 				if (errcode) {
475 					kfree(*wqids);
476 					*wqids = NULL;
477 				}
478 			}
479 			break;
480 		case 'A': {
481 				struct p9_stat_dotl *stbuf =
482 				    va_arg(ap, struct p9_stat_dotl *);
483 
484 				memset(stbuf, 0, sizeof(struct p9_stat_dotl));
485 				errcode =
486 				    p9pdu_readf(pdu, proto_version,
487 					"qQdugqqqqqqqqqqqqqqq",
488 					&stbuf->st_result_mask,
489 					&stbuf->qid,
490 					&stbuf->st_mode,
491 					&stbuf->st_uid, &stbuf->st_gid,
492 					&stbuf->st_nlink,
493 					&stbuf->st_rdev, &stbuf->st_size,
494 					&stbuf->st_blksize, &stbuf->st_blocks,
495 					&stbuf->st_atime_sec,
496 					&stbuf->st_atime_nsec,
497 					&stbuf->st_mtime_sec,
498 					&stbuf->st_mtime_nsec,
499 					&stbuf->st_ctime_sec,
500 					&stbuf->st_ctime_nsec,
501 					&stbuf->st_btime_sec,
502 					&stbuf->st_btime_nsec,
503 					&stbuf->st_gen,
504 					&stbuf->st_data_version);
505 			}
506 			break;
507 		case '?':
508 			if ((proto_version != p9_proto_2000u) &&
509 				(proto_version != p9_proto_2000L))
510 				return 0;
511 			break;
512 		default:
513 			BUG();
514 			break;
515 		}
516 
517 		if (errcode)
518 			break;
519 	}
520 
521 	return errcode;
522 }
523 
524 int
525 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
526 	va_list ap)
527 {
528 	const char *ptr;
529 	int errcode = 0;
530 
531 	for (ptr = fmt; *ptr; ptr++) {
532 		switch (*ptr) {
533 		case 'b':{
534 				int8_t val = va_arg(ap, int);
535 				if (pdu_write(pdu, &val, sizeof(val)))
536 					errcode = -EFAULT;
537 			}
538 			break;
539 		case 'w':{
540 				__le16 val = cpu_to_le16(va_arg(ap, int));
541 				if (pdu_write(pdu, &val, sizeof(val)))
542 					errcode = -EFAULT;
543 			}
544 			break;
545 		case 'd':{
546 				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
547 				if (pdu_write(pdu, &val, sizeof(val)))
548 					errcode = -EFAULT;
549 			}
550 			break;
551 		case 'q':{
552 				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
553 				if (pdu_write(pdu, &val, sizeof(val)))
554 					errcode = -EFAULT;
555 			}
556 			break;
557 		case 's':{
558 				const char *sptr = va_arg(ap, const char *);
559 				uint16_t len = 0;
560 				if (sptr)
561 					len = min_t(size_t, strlen(sptr),
562 								USHRT_MAX);
563 
564 				errcode = p9pdu_writef(pdu, proto_version,
565 								"w", len);
566 				if (!errcode && pdu_write(pdu, sptr, len))
567 					errcode = -EFAULT;
568 			}
569 			break;
570 		case 'u': {
571 				kuid_t uid = va_arg(ap, kuid_t);
572 				__le32 val = cpu_to_le32(
573 						from_kuid(&init_user_ns, uid));
574 				if (pdu_write(pdu, &val, sizeof(val)))
575 					errcode = -EFAULT;
576 			} break;
577 		case 'g': {
578 				kgid_t gid = va_arg(ap, kgid_t);
579 				__le32 val = cpu_to_le32(
580 						from_kgid(&init_user_ns, gid));
581 				if (pdu_write(pdu, &val, sizeof(val)))
582 					errcode = -EFAULT;
583 			} break;
584 		case 'Q':{
585 				const struct p9_qid *qid =
586 				    va_arg(ap, const struct p9_qid *);
587 				errcode =
588 				    p9pdu_writef(pdu, proto_version, "bdq",
589 						 qid->type, qid->version,
590 						 qid->path);
591 			} break;
592 		case 'S':{
593 				const struct p9_wstat *stbuf =
594 				    va_arg(ap, const struct p9_wstat *);
595 				errcode =
596 				    p9pdu_writef(pdu, proto_version,
597 						 "wwdQdddqssss?sugu",
598 						 stbuf->size, stbuf->type,
599 						 stbuf->dev, &stbuf->qid,
600 						 stbuf->mode, stbuf->atime,
601 						 stbuf->mtime, stbuf->length,
602 						 stbuf->name, stbuf->uid,
603 						 stbuf->gid, stbuf->muid,
604 						 stbuf->extension, stbuf->n_uid,
605 						 stbuf->n_gid, stbuf->n_muid);
606 			} break;
607 		case 'V':{
608 				uint32_t count = va_arg(ap, uint32_t);
609 				struct iov_iter *from =
610 						va_arg(ap, struct iov_iter *);
611 				errcode = p9pdu_writef(pdu, proto_version, "d",
612 									count);
613 				if (!errcode && pdu_write_u(pdu, from, count))
614 					errcode = -EFAULT;
615 			}
616 			break;
617 		case 'T':{
618 				uint16_t nwname = va_arg(ap, int);
619 				const char **wnames = va_arg(ap, const char **);
620 
621 				errcode = p9pdu_writef(pdu, proto_version, "w",
622 									nwname);
623 				if (!errcode) {
624 					int i;
625 
626 					for (i = 0; i < nwname; i++) {
627 						errcode =
628 						    p9pdu_writef(pdu,
629 								proto_version,
630 								 "s",
631 								 wnames[i]);
632 						if (errcode)
633 							break;
634 					}
635 				}
636 			}
637 			break;
638 		case 'R':{
639 				uint16_t nwqid = va_arg(ap, int);
640 				struct p9_qid *wqids =
641 				    va_arg(ap, struct p9_qid *);
642 
643 				errcode = p9pdu_writef(pdu, proto_version, "w",
644 									nwqid);
645 				if (!errcode) {
646 					int i;
647 
648 					for (i = 0; i < nwqid; i++) {
649 						errcode =
650 						    p9pdu_writef(pdu,
651 								proto_version,
652 								 "Q",
653 								 &wqids[i]);
654 						if (errcode)
655 							break;
656 					}
657 				}
658 			}
659 			break;
660 		case 'I':{
661 				struct p9_iattr_dotl *p9attr = va_arg(ap,
662 							struct p9_iattr_dotl *);
663 
664 				errcode = p9pdu_writef(pdu, proto_version,
665 							"ddugqqqqq",
666 							p9attr->valid,
667 							p9attr->mode,
668 							p9attr->uid,
669 							p9attr->gid,
670 							p9attr->size,
671 							p9attr->atime_sec,
672 							p9attr->atime_nsec,
673 							p9attr->mtime_sec,
674 							p9attr->mtime_nsec);
675 			}
676 			break;
677 		case '?':
678 			if ((proto_version != p9_proto_2000u) &&
679 				(proto_version != p9_proto_2000L))
680 				return 0;
681 			break;
682 		default:
683 			BUG();
684 			break;
685 		}
686 
687 		if (errcode)
688 			break;
689 	}
690 
691 	return errcode;
692 }
693 
694 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
695 {
696 	va_list ap;
697 	int ret;
698 
699 	va_start(ap, fmt);
700 	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
701 	va_end(ap);
702 
703 	return ret;
704 }
705 
706 static int
707 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
708 {
709 	va_list ap;
710 	int ret;
711 
712 	va_start(ap, fmt);
713 	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
714 	va_end(ap);
715 
716 	return ret;
717 }
718 
719 int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
720 {
721 	struct p9_fcall fake_pdu;
722 	int ret;
723 
724 	fake_pdu.size = len;
725 	fake_pdu.capacity = len;
726 	fake_pdu.sdata = buf;
727 	fake_pdu.offset = 0;
728 
729 	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
730 	if (ret) {
731 		p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
732 		trace_9p_protocol_dump(clnt, &fake_pdu);
733 		return ret;
734 	}
735 
736 	return fake_pdu.offset;
737 }
738 EXPORT_SYMBOL(p9stat_read);
739 
740 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
741 {
742 	pdu->id = type;
743 	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
744 }
745 
746 int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
747 {
748 	int size = pdu->size;
749 	int err;
750 
751 	pdu->size = 0;
752 	err = p9pdu_writef(pdu, 0, "d", size);
753 	pdu->size = size;
754 
755 	trace_9p_protocol_dump(clnt, pdu);
756 	p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
757 		 pdu->size, pdu->id, pdu->tag);
758 
759 	return err;
760 }
761 
762 void p9pdu_reset(struct p9_fcall *pdu)
763 {
764 	pdu->offset = 0;
765 	pdu->size = 0;
766 }
767 
768 int p9dirent_read(struct p9_client *clnt, char *buf, int len,
769 		  struct p9_dirent *dirent)
770 {
771 	struct p9_fcall fake_pdu;
772 	int ret;
773 	char *nameptr;
774 
775 	fake_pdu.size = len;
776 	fake_pdu.capacity = len;
777 	fake_pdu.sdata = buf;
778 	fake_pdu.offset = 0;
779 
780 	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
781 			  &dirent->d_off, &dirent->d_type, &nameptr);
782 	if (ret) {
783 		p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
784 		trace_9p_protocol_dump(clnt, &fake_pdu);
785 		return ret;
786 	}
787 
788 	ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
789 	if (ret < 0) {
790 		p9_debug(P9_DEBUG_ERROR,
791 			 "On the wire dirent name too long: %s\n",
792 			 nameptr);
793 		kfree(nameptr);
794 		return ret;
795 	}
796 	kfree(nameptr);
797 
798 	return fake_pdu.offset;
799 }
800 EXPORT_SYMBOL(p9dirent_read);
801