iwlwifi: don't include iwl-dev.h from iwl-devtrace.h
[deliverable/linux.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>
51a87c55 30#include <linux/uaccess.h>
e7f4b8f1 31#include <linux/sched.h>
beeebc92 32#include <linux/types.h>
ace51c4d
EVH
33#include <net/9p/9p.h>
34#include <net/9p/client.h>
35#include "protocol.h"
36
37#ifndef MIN
38#define MIN(a, b) (((a) < (b)) ? (a) : (b))
39#endif
40
41#ifndef MAX
42#define MAX(a, b) (((a) > (b)) ? (a) : (b))
43#endif
44
45#ifndef offset_of
46#define offset_of(type, memb) \
47 ((unsigned long)(&((type *)0)->memb))
48#endif
49#ifndef container_of
50#define container_of(obj, type, memb) \
51 ((type *)(((char *)obj) - offset_of(type, memb)))
52#endif
53
54static int
342fee1d 55p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
ace51c4d 56
0b15a3a5 57#ifdef CONFIG_NET_9P_DEBUG
51a87c55
EVH
58void
59p9pdu_dump(int way, struct p9_fcall *pdu)
60{
61 int i, n;
62 u8 *data = pdu->sdata;
63 int datalen = pdu->size;
64 char buf[255];
65 int buflen = 255;
66
67 i = n = 0;
68 if (datalen > (buflen-16))
69 datalen = buflen-16;
70 while (i < datalen) {
71 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
72 if (i%4 == 3)
73 n += scnprintf(buf + n, buflen - n, " ");
74 if (i%32 == 31)
75 n += scnprintf(buf + n, buflen - n, "\n");
76
77 i++;
78 }
79 n += scnprintf(buf + n, buflen - n, "\n");
80
81 if (way)
e7f4b8f1 82 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
51a87c55 83 else
e7f4b8f1 84 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
51a87c55 85}
0b15a3a5
EVH
86#else
87void
88p9pdu_dump(int way, struct p9_fcall *pdu)
89{
90}
91#endif
51a87c55
EVH
92EXPORT_SYMBOL(p9pdu_dump);
93
ace51c4d
EVH
94void p9stat_free(struct p9_wstat *stbuf)
95{
96 kfree(stbuf->name);
97 kfree(stbuf->uid);
98 kfree(stbuf->gid);
99 kfree(stbuf->muid);
100 kfree(stbuf->extension);
101}
102EXPORT_SYMBOL(p9stat_free);
103
104static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
105{
106 size_t len = MIN(pdu->size - pdu->offset, size);
107 memcpy(data, &pdu->sdata[pdu->offset], len);
108 pdu->offset += len;
109 return size - len;
110}
111
112static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
113{
114 size_t len = MIN(pdu->capacity - pdu->size, size);
115 memcpy(&pdu->sdata[pdu->size], data, len);
116 pdu->size += len;
117 return size - len;
118}
119
51a87c55
EVH
120static size_t
121pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
122{
123 size_t len = MIN(pdu->capacity - pdu->size, size);
124 int err = copy_from_user(&pdu->sdata[pdu->size], udata, len);
125 if (err)
126 printk(KERN_WARNING "pdu_write_u returning: %d\n", err);
127
128 pdu->size += len;
129 return size - len;
130}
131
ace51c4d
EVH
132/*
133 b - int8_t
134 w - int16_t
135 d - int32_t
136 q - int64_t
137 s - string
138 S - stat
139 Q - qid
140 D - data blob (int32_t size followed by void *, results are not freed)
141 T - array of strings (int16_t count, followed by strings)
142 R - array of qids (int16_t count, followed by qids)
143 ? - if optional = 1, continue parsing
144*/
145
146static int
342fee1d
SK
147p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
148 va_list ap)
ace51c4d
EVH
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 *);
beeebc92
EVH
165 __le16 le_val;
166 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
ace51c4d
EVH
167 errcode = -EFAULT;
168 break;
169 }
beeebc92 170 *val = le16_to_cpu(le_val);
ace51c4d
EVH
171 }
172 break;
173 case 'd':{
174 int32_t *val = va_arg(ap, int32_t *);
beeebc92
EVH
175 __le32 le_val;
176 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
ace51c4d
EVH
177 errcode = -EFAULT;
178 break;
179 }
beeebc92 180 *val = le32_to_cpu(le_val);
ace51c4d
EVH
181 }
182 break;
183 case 'q':{
184 int64_t *val = va_arg(ap, int64_t *);
beeebc92
EVH
185 __le64 le_val;
186 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
ace51c4d
EVH
187 errcode = -EFAULT;
188 break;
189 }
beeebc92 190 *val = le64_to_cpu(le_val);
ace51c4d
EVH
191 }
192 break;
193 case 's':{
e45c5405 194 char **sptr = va_arg(ap, char **);
ace51c4d
EVH
195 int16_t len;
196 int size;
197
342fee1d
SK
198 errcode = p9pdu_readf(pdu, proto_version,
199 "w", &len);
ace51c4d
EVH
200 if (errcode)
201 break;
202
203 size = MAX(len, 0);
204
e45c5405
EVH
205 *sptr = kmalloc(size + 1, GFP_KERNEL);
206 if (*sptr == NULL) {
ace51c4d
EVH
207 errcode = -EFAULT;
208 break;
209 }
e45c5405 210 if (pdu_read(pdu, *sptr, size)) {
ace51c4d 211 errcode = -EFAULT;
e45c5405
EVH
212 kfree(*sptr);
213 *sptr = NULL;
ace51c4d 214 } else
e45c5405 215 (*sptr)[size] = 0;
ace51c4d
EVH
216 }
217 break;
218 case 'Q':{
219 struct p9_qid *qid =
220 va_arg(ap, struct p9_qid *);
221
342fee1d 222 errcode = p9pdu_readf(pdu, proto_version, "bdq",
ace51c4d
EVH
223 &qid->type, &qid->version,
224 &qid->path);
225 }
226 break;
227 case 'S':{
228 struct p9_wstat *stbuf =
229 va_arg(ap, struct p9_wstat *);
230
f0a0ac2e 231 memset(stbuf, 0, sizeof(struct p9_wstat));
ace51c4d 232 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
f0a0ac2e 233 -1;
ace51c4d 234 errcode =
342fee1d 235 p9pdu_readf(pdu, proto_version,
ace51c4d
EVH
236 "wwdQdddqssss?sddd",
237 &stbuf->size, &stbuf->type,
238 &stbuf->dev, &stbuf->qid,
239 &stbuf->mode, &stbuf->atime,
240 &stbuf->mtime, &stbuf->length,
241 &stbuf->name, &stbuf->uid,
242 &stbuf->gid, &stbuf->muid,
243 &stbuf->extension,
244 &stbuf->n_uid, &stbuf->n_gid,
245 &stbuf->n_muid);
246 if (errcode)
247 p9stat_free(stbuf);
248 }
249 break;
250 case 'D':{
251 int32_t *count = va_arg(ap, int32_t *);
252 void **data = va_arg(ap, void **);
253
254 errcode =
342fee1d 255 p9pdu_readf(pdu, proto_version, "d", count);
ace51c4d
EVH
256 if (!errcode) {
257 *count =
258 MIN(*count,
259 pdu->size - pdu->offset);
260 *data = &pdu->sdata[pdu->offset];
261 }
262 }
263 break;
264 case 'T':{
265 int16_t *nwname = va_arg(ap, int16_t *);
266 char ***wnames = va_arg(ap, char ***);
267
342fee1d
SK
268 errcode = p9pdu_readf(pdu, proto_version,
269 "w", nwname);
ace51c4d
EVH
270 if (!errcode) {
271 *wnames =
272 kmalloc(sizeof(char *) * *nwname,
273 GFP_KERNEL);
274 if (!*wnames)
275 errcode = -ENOMEM;
276 }
277
278 if (!errcode) {
279 int i;
280
281 for (i = 0; i < *nwname; i++) {
282 errcode =
342fee1d
SK
283 p9pdu_readf(pdu,
284 proto_version,
ace51c4d
EVH
285 "s",
286 &(*wnames)[i]);
287 if (errcode)
288 break;
289 }
290 }
291
292 if (errcode) {
293 if (*wnames) {
294 int i;
295
296 for (i = 0; i < *nwname; i++)
297 kfree((*wnames)[i]);
298 }
299 kfree(*wnames);
300 *wnames = NULL;
301 }
302 }
303 break;
304 case 'R':{
305 int16_t *nwqid = va_arg(ap, int16_t *);
306 struct p9_qid **wqids =
307 va_arg(ap, struct p9_qid **);
308
309 *wqids = NULL;
310
311 errcode =
342fee1d 312 p9pdu_readf(pdu, proto_version, "w", nwqid);
ace51c4d
EVH
313 if (!errcode) {
314 *wqids =
315 kmalloc(*nwqid *
316 sizeof(struct p9_qid),
317 GFP_KERNEL);
318 if (*wqids == NULL)
319 errcode = -ENOMEM;
320 }
321
322 if (!errcode) {
323 int i;
324
325 for (i = 0; i < *nwqid; i++) {
326 errcode =
342fee1d
SK
327 p9pdu_readf(pdu,
328 proto_version,
ace51c4d
EVH
329 "Q",
330 &(*wqids)[i]);
331 if (errcode)
332 break;
333 }
334 }
335
336 if (errcode) {
337 kfree(*wqids);
338 *wqids = NULL;
339 }
340 }
341 break;
342 case '?':
342fee1d 343 if (proto_version != p9_proto_2000u)
ace51c4d
EVH
344 return 0;
345 break;
346 default:
347 BUG();
348 break;
349 }
350
351 if (errcode)
352 break;
353 }
354
355 return errcode;
356}
357
358int
342fee1d
SK
359p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
360 va_list ap)
ace51c4d
EVH
361{
362 const char *ptr;
363 int errcode = 0;
364
365 for (ptr = fmt; *ptr; ptr++) {
366 switch (*ptr) {
367 case 'b':{
368 int8_t val = va_arg(ap, int);
369 if (pdu_write(pdu, &val, sizeof(val)))
370 errcode = -EFAULT;
371 }
372 break;
373 case 'w':{
beeebc92 374 __le16 val = cpu_to_le16(va_arg(ap, int));
ace51c4d
EVH
375 if (pdu_write(pdu, &val, sizeof(val)))
376 errcode = -EFAULT;
377 }
378 break;
379 case 'd':{
beeebc92 380 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
ace51c4d
EVH
381 if (pdu_write(pdu, &val, sizeof(val)))
382 errcode = -EFAULT;
383 }
384 break;
385 case 'q':{
beeebc92 386 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
ace51c4d
EVH
387 if (pdu_write(pdu, &val, sizeof(val)))
388 errcode = -EFAULT;
389 }
390 break;
391 case 's':{
e45c5405 392 const char *sptr = va_arg(ap, const char *);
ace51c4d 393 int16_t len = 0;
e45c5405
EVH
394 if (sptr)
395 len = MIN(strlen(sptr), USHORT_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;
403 case 'Q':{
404 const struct p9_qid *qid =
405 va_arg(ap, const struct p9_qid *);
406 errcode =
342fee1d 407 p9pdu_writef(pdu, proto_version, "bdq",
ace51c4d
EVH
408 qid->type, qid->version,
409 qid->path);
410 } break;
411 case 'S':{
412 const struct p9_wstat *stbuf =
413 va_arg(ap, const struct p9_wstat *);
414 errcode =
342fee1d 415 p9pdu_writef(pdu, proto_version,
ace51c4d
EVH
416 "wwdQdddqssss?sddd",
417 stbuf->size, stbuf->type,
51a87c55 418 stbuf->dev, &stbuf->qid,
ace51c4d
EVH
419 stbuf->mode, stbuf->atime,
420 stbuf->mtime, stbuf->length,
421 stbuf->name, stbuf->uid,
422 stbuf->gid, stbuf->muid,
423 stbuf->extension, stbuf->n_uid,
424 stbuf->n_gid, stbuf->n_muid);
425 } break;
426 case 'D':{
427 int32_t count = va_arg(ap, int32_t);
428 const void *data = va_arg(ap, const void *);
429
342fee1d
SK
430 errcode = p9pdu_writef(pdu, proto_version, "d",
431 count);
ace51c4d
EVH
432 if (!errcode && pdu_write(pdu, data, count))
433 errcode = -EFAULT;
434 }
435 break;
51a87c55
EVH
436 case 'U':{
437 int32_t count = va_arg(ap, int32_t);
438 const char __user *udata =
e45c5405 439 va_arg(ap, const void __user *);
342fee1d
SK
440 errcode = p9pdu_writef(pdu, proto_version, "d",
441 count);
51a87c55
EVH
442 if (!errcode && pdu_write_u(pdu, udata, count))
443 errcode = -EFAULT;
444 }
445 break;
ace51c4d
EVH
446 case 'T':{
447 int16_t nwname = va_arg(ap, int);
448 const char **wnames = va_arg(ap, const char **);
449
342fee1d
SK
450 errcode = p9pdu_writef(pdu, proto_version, "w",
451 nwname);
ace51c4d
EVH
452 if (!errcode) {
453 int i;
454
455 for (i = 0; i < nwname; i++) {
456 errcode =
342fee1d
SK
457 p9pdu_writef(pdu,
458 proto_version,
ace51c4d
EVH
459 "s",
460 wnames[i]);
461 if (errcode)
462 break;
463 }
464 }
465 }
466 break;
467 case 'R':{
468 int16_t nwqid = va_arg(ap, int);
469 struct p9_qid *wqids =
470 va_arg(ap, struct p9_qid *);
471
342fee1d
SK
472 errcode = p9pdu_writef(pdu, proto_version, "w",
473 nwqid);
ace51c4d
EVH
474 if (!errcode) {
475 int i;
476
477 for (i = 0; i < nwqid; i++) {
478 errcode =
342fee1d
SK
479 p9pdu_writef(pdu,
480 proto_version,
ace51c4d
EVH
481 "Q",
482 &wqids[i]);
483 if (errcode)
484 break;
485 }
486 }
487 }
488 break;
489 case '?':
342fee1d 490 if (proto_version != p9_proto_2000u)
ace51c4d
EVH
491 return 0;
492 break;
493 default:
494 BUG();
495 break;
496 }
497
498 if (errcode)
499 break;
500 }
501
502 return errcode;
503}
504
342fee1d 505int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
ace51c4d
EVH
506{
507 va_list ap;
508 int ret;
509
510 va_start(ap, fmt);
342fee1d 511 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
ace51c4d
EVH
512 va_end(ap);
513
514 return ret;
515}
516
517static int
342fee1d 518p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
ace51c4d
EVH
519{
520 va_list ap;
521 int ret;
522
523 va_start(ap, fmt);
342fee1d 524 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
ace51c4d
EVH
525 va_end(ap);
526
527 return ret;
528}
51a87c55 529
342fee1d 530int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
02da398b
EVH
531{
532 struct p9_fcall fake_pdu;
e7f4b8f1 533 int ret;
02da398b
EVH
534
535 fake_pdu.size = len;
536 fake_pdu.capacity = len;
537 fake_pdu.sdata = buf;
538 fake_pdu.offset = 0;
539
342fee1d 540 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
e7f4b8f1
EVH
541 if (ret) {
542 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
543 p9pdu_dump(1, &fake_pdu);
544 }
545
546 return ret;
02da398b
EVH
547}
548EXPORT_SYMBOL(p9stat_read);
549
51a87c55
EVH
550int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
551{
552 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
553}
554
555int p9pdu_finalize(struct p9_fcall *pdu)
556{
557 int size = pdu->size;
558 int err;
559
560 pdu->size = 0;
561 err = p9pdu_writef(pdu, 0, "d", size);
562 pdu->size = size;
563
0b15a3a5 564#ifdef CONFIG_NET_9P_DEBUG
e7f4b8f1 565 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
51a87c55 566 p9pdu_dump(0, pdu);
0b15a3a5 567#endif
51a87c55 568
e7f4b8f1
EVH
569 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
570 pdu->id, pdu->tag);
571
51a87c55
EVH
572 return err;
573}
574
575void p9pdu_reset(struct p9_fcall *pdu)
576{
577 pdu->offset = 0;
578 pdu->size = 0;
579}
This page took 0.134354 seconds and 5 git commands to generate.