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