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