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