leds-gpio: fix possible crash on OF device unbinding
[linux-2.6/mini2440.git] / net / 9p / protocol.c
blobfc70147c771e15c2542a92e7286560f7faa1b98a
1 /*
2 * net/9p/protocol.c
4 * 9P Protocol Support Code
6 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Base on code from Anthony Liguori <aliguori@us.ibm.com>
9 * Copyright (C) 2008 by IBM, Corp.
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.
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.
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
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/uaccess.h>
31 #include <linux/sched.h>
32 #include <linux/types.h>
33 #include <net/9p/9p.h>
34 #include <net/9p/client.h>
35 #include "protocol.h"
37 #ifndef MIN
38 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
39 #endif
41 #ifndef MAX
42 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
43 #endif
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
54 static int
55 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...);
57 #ifdef CONFIG_NET_9P_DEBUG
58 void
59 p9pdu_dump(int way, struct p9_fcall *pdu)
61 int i, n;
62 u8 *data = pdu->sdata;
63 int datalen = pdu->size;
64 char buf[255];
65 int buflen = 255;
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");
77 i++;
79 n += scnprintf(buf + n, buflen - n, "\n");
81 if (way)
82 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
83 else
84 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
86 #else
87 void
88 p9pdu_dump(int way, struct p9_fcall *pdu)
91 #endif
92 EXPORT_SYMBOL(p9pdu_dump);
94 void p9stat_free(struct p9_wstat *stbuf)
96 kfree(stbuf->name);
97 kfree(stbuf->uid);
98 kfree(stbuf->gid);
99 kfree(stbuf->muid);
100 kfree(stbuf->extension);
102 EXPORT_SYMBOL(p9stat_free);
104 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
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;
112 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
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;
120 static size_t
121 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
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);
128 pdu->size += len;
129 return size - len;
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
146 static int
147 p9pdu_vreadf(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
149 const char *ptr;
150 int errcode = 0;
152 for (ptr = fmt; *ptr; ptr++) {
153 switch (*ptr) {
154 case 'b':{
155 int8_t *val = va_arg(ap, int8_t *);
156 if (pdu_read(pdu, val, sizeof(*val))) {
157 errcode = -EFAULT;
158 break;
161 break;
162 case 'w':{
163 int16_t *val = va_arg(ap, int16_t *);
164 __le16 le_val;
165 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
166 errcode = -EFAULT;
167 break;
169 *val = le16_to_cpu(le_val);
171 break;
172 case 'd':{
173 int32_t *val = va_arg(ap, int32_t *);
174 __le32 le_val;
175 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
176 errcode = -EFAULT;
177 break;
179 *val = le32_to_cpu(le_val);
181 break;
182 case 'q':{
183 int64_t *val = va_arg(ap, int64_t *);
184 __le64 le_val;
185 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
186 errcode = -EFAULT;
187 break;
189 *val = le64_to_cpu(le_val);
191 break;
192 case 's':{
193 char **sptr = va_arg(ap, char **);
194 int16_t len;
195 int size;
197 errcode = p9pdu_readf(pdu, optional, "w", &len);
198 if (errcode)
199 break;
201 size = MAX(len, 0);
203 *sptr = kmalloc(size + 1, GFP_KERNEL);
204 if (*sptr == NULL) {
205 errcode = -EFAULT;
206 break;
208 if (pdu_read(pdu, *sptr, size)) {
209 errcode = -EFAULT;
210 kfree(*sptr);
211 *sptr = NULL;
212 } else
213 (*sptr)[size] = 0;
215 break;
216 case 'Q':{
217 struct p9_qid *qid =
218 va_arg(ap, struct p9_qid *);
220 errcode = p9pdu_readf(pdu, optional, "bdq",
221 &qid->type, &qid->version,
222 &qid->path);
224 break;
225 case 'S':{
226 struct p9_wstat *stbuf =
227 va_arg(ap, struct p9_wstat *);
229 memset(stbuf, 0, sizeof(struct p9_wstat));
230 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
232 errcode =
233 p9pdu_readf(pdu, optional,
234 "wwdQdddqssss?sddd",
235 &stbuf->size, &stbuf->type,
236 &stbuf->dev, &stbuf->qid,
237 &stbuf->mode, &stbuf->atime,
238 &stbuf->mtime, &stbuf->length,
239 &stbuf->name, &stbuf->uid,
240 &stbuf->gid, &stbuf->muid,
241 &stbuf->extension,
242 &stbuf->n_uid, &stbuf->n_gid,
243 &stbuf->n_muid);
244 if (errcode)
245 p9stat_free(stbuf);
247 break;
248 case 'D':{
249 int32_t *count = va_arg(ap, int32_t *);
250 void **data = va_arg(ap, void **);
252 errcode =
253 p9pdu_readf(pdu, optional, "d", count);
254 if (!errcode) {
255 *count =
256 MIN(*count,
257 pdu->size - pdu->offset);
258 *data = &pdu->sdata[pdu->offset];
261 break;
262 case 'T':{
263 int16_t *nwname = va_arg(ap, int16_t *);
264 char ***wnames = va_arg(ap, char ***);
266 errcode =
267 p9pdu_readf(pdu, optional, "w", nwname);
268 if (!errcode) {
269 *wnames =
270 kmalloc(sizeof(char *) * *nwname,
271 GFP_KERNEL);
272 if (!*wnames)
273 errcode = -ENOMEM;
276 if (!errcode) {
277 int i;
279 for (i = 0; i < *nwname; i++) {
280 errcode =
281 p9pdu_readf(pdu, optional,
282 "s",
283 &(*wnames)[i]);
284 if (errcode)
285 break;
289 if (errcode) {
290 if (*wnames) {
291 int i;
293 for (i = 0; i < *nwname; i++)
294 kfree((*wnames)[i]);
296 kfree(*wnames);
297 *wnames = NULL;
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 **);
306 *wqids = NULL;
308 errcode =
309 p9pdu_readf(pdu, optional, "w", nwqid);
310 if (!errcode) {
311 *wqids =
312 kmalloc(*nwqid *
313 sizeof(struct p9_qid),
314 GFP_KERNEL);
315 if (*wqids == NULL)
316 errcode = -ENOMEM;
319 if (!errcode) {
320 int i;
322 for (i = 0; i < *nwqid; i++) {
323 errcode =
324 p9pdu_readf(pdu, optional,
325 "Q",
326 &(*wqids)[i]);
327 if (errcode)
328 break;
332 if (errcode) {
333 kfree(*wqids);
334 *wqids = NULL;
337 break;
338 case '?':
339 if (!optional)
340 return 0;
341 break;
342 default:
343 BUG();
344 break;
347 if (errcode)
348 break;
351 return errcode;
355 p9pdu_vwritef(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
357 const char *ptr;
358 int errcode = 0;
360 for (ptr = fmt; *ptr; ptr++) {
361 switch (*ptr) {
362 case 'b':{
363 int8_t val = va_arg(ap, int);
364 if (pdu_write(pdu, &val, sizeof(val)))
365 errcode = -EFAULT;
367 break;
368 case 'w':{
369 __le16 val = cpu_to_le16(va_arg(ap, int));
370 if (pdu_write(pdu, &val, sizeof(val)))
371 errcode = -EFAULT;
373 break;
374 case 'd':{
375 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
376 if (pdu_write(pdu, &val, sizeof(val)))
377 errcode = -EFAULT;
379 break;
380 case 'q':{
381 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
382 if (pdu_write(pdu, &val, sizeof(val)))
383 errcode = -EFAULT;
385 break;
386 case 's':{
387 const char *sptr = va_arg(ap, const char *);
388 int16_t len = 0;
389 if (sptr)
390 len = MIN(strlen(sptr), USHORT_MAX);
392 errcode = p9pdu_writef(pdu, optional, "w", len);
393 if (!errcode && pdu_write(pdu, sptr, len))
394 errcode = -EFAULT;
396 break;
397 case 'Q':{
398 const struct p9_qid *qid =
399 va_arg(ap, const struct p9_qid *);
400 errcode =
401 p9pdu_writef(pdu, optional, "bdq",
402 qid->type, qid->version,
403 qid->path);
404 } break;
405 case 'S':{
406 const struct p9_wstat *stbuf =
407 va_arg(ap, const struct p9_wstat *);
408 errcode =
409 p9pdu_writef(pdu, optional,
410 "wwdQdddqssss?sddd",
411 stbuf->size, stbuf->type,
412 stbuf->dev, &stbuf->qid,
413 stbuf->mode, stbuf->atime,
414 stbuf->mtime, stbuf->length,
415 stbuf->name, stbuf->uid,
416 stbuf->gid, stbuf->muid,
417 stbuf->extension, stbuf->n_uid,
418 stbuf->n_gid, stbuf->n_muid);
419 } break;
420 case 'D':{
421 int32_t count = va_arg(ap, int32_t);
422 const void *data = va_arg(ap, const void *);
424 errcode =
425 p9pdu_writef(pdu, optional, "d", count);
426 if (!errcode && pdu_write(pdu, data, count))
427 errcode = -EFAULT;
429 break;
430 case 'U':{
431 int32_t count = va_arg(ap, int32_t);
432 const char __user *udata =
433 va_arg(ap, const void __user *);
434 errcode =
435 p9pdu_writef(pdu, optional, "d", count);
436 if (!errcode && pdu_write_u(pdu, udata, count))
437 errcode = -EFAULT;
439 break;
440 case 'T':{
441 int16_t nwname = va_arg(ap, int);
442 const char **wnames = va_arg(ap, const char **);
444 errcode =
445 p9pdu_writef(pdu, optional, "w", nwname);
446 if (!errcode) {
447 int i;
449 for (i = 0; i < nwname; i++) {
450 errcode =
451 p9pdu_writef(pdu, optional,
452 "s",
453 wnames[i]);
454 if (errcode)
455 break;
459 break;
460 case 'R':{
461 int16_t nwqid = va_arg(ap, int);
462 struct p9_qid *wqids =
463 va_arg(ap, struct p9_qid *);
465 errcode =
466 p9pdu_writef(pdu, optional, "w", nwqid);
467 if (!errcode) {
468 int i;
470 for (i = 0; i < nwqid; i++) {
471 errcode =
472 p9pdu_writef(pdu, optional,
473 "Q",
474 &wqids[i]);
475 if (errcode)
476 break;
480 break;
481 case '?':
482 if (!optional)
483 return 0;
484 break;
485 default:
486 BUG();
487 break;
490 if (errcode)
491 break;
494 return errcode;
497 int p9pdu_readf(struct p9_fcall *pdu, int optional, const char *fmt, ...)
499 va_list ap;
500 int ret;
502 va_start(ap, fmt);
503 ret = p9pdu_vreadf(pdu, optional, fmt, ap);
504 va_end(ap);
506 return ret;
509 static int
510 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...)
512 va_list ap;
513 int ret;
515 va_start(ap, fmt);
516 ret = p9pdu_vwritef(pdu, optional, fmt, ap);
517 va_end(ap);
519 return ret;
522 int p9stat_read(char *buf, int len, struct p9_wstat *st, int dotu)
524 struct p9_fcall fake_pdu;
525 int ret;
527 fake_pdu.size = len;
528 fake_pdu.capacity = len;
529 fake_pdu.sdata = buf;
530 fake_pdu.offset = 0;
532 ret = p9pdu_readf(&fake_pdu, dotu, "S", st);
533 if (ret) {
534 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
535 p9pdu_dump(1, &fake_pdu);
538 return ret;
540 EXPORT_SYMBOL(p9stat_read);
542 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
544 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
547 int p9pdu_finalize(struct p9_fcall *pdu)
549 int size = pdu->size;
550 int err;
552 pdu->size = 0;
553 err = p9pdu_writef(pdu, 0, "d", size);
554 pdu->size = size;
556 #ifdef CONFIG_NET_9P_DEBUG
557 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
558 p9pdu_dump(0, pdu);
559 #endif
561 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
562 pdu->id, pdu->tag);
564 return err;
567 void p9pdu_reset(struct p9_fcall *pdu)
569 pdu->offset = 0;
570 pdu->size = 0;