ARM: 6616/1: Fix ep93xx-fb init/exit annotations
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / 9p / protocol.c
blob798beac7f100271f0ca4607fe4b3b1fee3fc1d34
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/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"
40 static int
41 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
43 #ifdef CONFIG_NET_9P_DEBUG
44 void
45 p9pdu_dump(int way, struct p9_fcall *pdu)
47 int i, n;
48 u8 *data = pdu->sdata;
49 int datalen = pdu->size;
50 char buf[255];
51 int buflen = 255;
53 i = n = 0;
54 if (datalen > (buflen-16))
55 datalen = buflen-16;
56 while (i < datalen) {
57 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
58 if (i%4 == 3)
59 n += scnprintf(buf + n, buflen - n, " ");
60 if (i%32 == 31)
61 n += scnprintf(buf + n, buflen - n, "\n");
63 i++;
65 n += scnprintf(buf + n, buflen - n, "\n");
67 if (way)
68 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
69 else
70 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
72 #else
73 void
74 p9pdu_dump(int way, struct p9_fcall *pdu)
77 #endif
78 EXPORT_SYMBOL(p9pdu_dump);
80 void p9stat_free(struct p9_wstat *stbuf)
82 kfree(stbuf->name);
83 kfree(stbuf->uid);
84 kfree(stbuf->gid);
85 kfree(stbuf->muid);
86 kfree(stbuf->extension);
88 EXPORT_SYMBOL(p9stat_free);
90 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
92 size_t len = min(pdu->size - pdu->offset, size);
93 memcpy(data, &pdu->sdata[pdu->offset], len);
94 pdu->offset += len;
95 return size - len;
98 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
100 size_t len = min(pdu->capacity - pdu->size, size);
101 memcpy(&pdu->sdata[pdu->size], data, len);
102 pdu->size += len;
103 return size - len;
106 static size_t
107 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
109 size_t len = min(pdu->capacity - pdu->size, size);
110 if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
111 len = 0;
113 pdu->size += len;
114 return size - len;
118 b - int8_t
119 w - int16_t
120 d - int32_t
121 q - int64_t
122 s - string
123 S - stat
124 Q - qid
125 D - data blob (int32_t size followed by void *, results are not freed)
126 T - array of strings (int16_t count, followed by strings)
127 R - array of qids (int16_t count, followed by qids)
128 A - stat for 9p2000.L (p9_stat_dotl)
129 ? - if optional = 1, continue parsing
132 static int
133 p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
134 va_list ap)
136 const char *ptr;
137 int errcode = 0;
139 for (ptr = fmt; *ptr; ptr++) {
140 switch (*ptr) {
141 case 'b':{
142 int8_t *val = va_arg(ap, int8_t *);
143 if (pdu_read(pdu, val, sizeof(*val))) {
144 errcode = -EFAULT;
145 break;
148 break;
149 case 'w':{
150 int16_t *val = va_arg(ap, int16_t *);
151 __le16 le_val;
152 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
153 errcode = -EFAULT;
154 break;
156 *val = le16_to_cpu(le_val);
158 break;
159 case 'd':{
160 int32_t *val = va_arg(ap, int32_t *);
161 __le32 le_val;
162 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
163 errcode = -EFAULT;
164 break;
166 *val = le32_to_cpu(le_val);
168 break;
169 case 'q':{
170 int64_t *val = va_arg(ap, int64_t *);
171 __le64 le_val;
172 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
173 errcode = -EFAULT;
174 break;
176 *val = le64_to_cpu(le_val);
178 break;
179 case 's':{
180 char **sptr = va_arg(ap, char **);
181 int16_t len;
182 int size;
184 errcode = p9pdu_readf(pdu, proto_version,
185 "w", &len);
186 if (errcode)
187 break;
189 size = max_t(int16_t, len, 0);
191 *sptr = kmalloc(size + 1, GFP_KERNEL);
192 if (*sptr == NULL) {
193 errcode = -EFAULT;
194 break;
196 if (pdu_read(pdu, *sptr, size)) {
197 errcode = -EFAULT;
198 kfree(*sptr);
199 *sptr = NULL;
200 } else
201 (*sptr)[size] = 0;
203 break;
204 case 'Q':{
205 struct p9_qid *qid =
206 va_arg(ap, struct p9_qid *);
208 errcode = p9pdu_readf(pdu, proto_version, "bdq",
209 &qid->type, &qid->version,
210 &qid->path);
212 break;
213 case 'S':{
214 struct p9_wstat *stbuf =
215 va_arg(ap, struct p9_wstat *);
217 memset(stbuf, 0, sizeof(struct p9_wstat));
218 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
220 errcode =
221 p9pdu_readf(pdu, proto_version,
222 "wwdQdddqssss?sddd",
223 &stbuf->size, &stbuf->type,
224 &stbuf->dev, &stbuf->qid,
225 &stbuf->mode, &stbuf->atime,
226 &stbuf->mtime, &stbuf->length,
227 &stbuf->name, &stbuf->uid,
228 &stbuf->gid, &stbuf->muid,
229 &stbuf->extension,
230 &stbuf->n_uid, &stbuf->n_gid,
231 &stbuf->n_muid);
232 if (errcode)
233 p9stat_free(stbuf);
235 break;
236 case 'D':{
237 int32_t *count = va_arg(ap, int32_t *);
238 void **data = va_arg(ap, void **);
240 errcode =
241 p9pdu_readf(pdu, proto_version, "d", count);
242 if (!errcode) {
243 *count =
244 min_t(int32_t, *count,
245 pdu->size - pdu->offset);
246 *data = &pdu->sdata[pdu->offset];
249 break;
250 case 'T':{
251 int16_t *nwname = va_arg(ap, int16_t *);
252 char ***wnames = va_arg(ap, char ***);
254 errcode = p9pdu_readf(pdu, proto_version,
255 "w", nwname);
256 if (!errcode) {
257 *wnames =
258 kmalloc(sizeof(char *) * *nwname,
259 GFP_KERNEL);
260 if (!*wnames)
261 errcode = -ENOMEM;
264 if (!errcode) {
265 int i;
267 for (i = 0; i < *nwname; i++) {
268 errcode =
269 p9pdu_readf(pdu,
270 proto_version,
271 "s",
272 &(*wnames)[i]);
273 if (errcode)
274 break;
278 if (errcode) {
279 if (*wnames) {
280 int i;
282 for (i = 0; i < *nwname; i++)
283 kfree((*wnames)[i]);
285 kfree(*wnames);
286 *wnames = NULL;
289 break;
290 case 'R':{
291 int16_t *nwqid = va_arg(ap, int16_t *);
292 struct p9_qid **wqids =
293 va_arg(ap, struct p9_qid **);
295 *wqids = NULL;
297 errcode =
298 p9pdu_readf(pdu, proto_version, "w", nwqid);
299 if (!errcode) {
300 *wqids =
301 kmalloc(*nwqid *
302 sizeof(struct p9_qid),
303 GFP_KERNEL);
304 if (*wqids == NULL)
305 errcode = -ENOMEM;
308 if (!errcode) {
309 int i;
311 for (i = 0; i < *nwqid; i++) {
312 errcode =
313 p9pdu_readf(pdu,
314 proto_version,
315 "Q",
316 &(*wqids)[i]);
317 if (errcode)
318 break;
322 if (errcode) {
323 kfree(*wqids);
324 *wqids = NULL;
327 break;
328 case 'A': {
329 struct p9_stat_dotl *stbuf =
330 va_arg(ap, struct p9_stat_dotl *);
332 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
333 errcode =
334 p9pdu_readf(pdu, proto_version,
335 "qQdddqqqqqqqqqqqqqqq",
336 &stbuf->st_result_mask,
337 &stbuf->qid,
338 &stbuf->st_mode,
339 &stbuf->st_uid, &stbuf->st_gid,
340 &stbuf->st_nlink,
341 &stbuf->st_rdev, &stbuf->st_size,
342 &stbuf->st_blksize, &stbuf->st_blocks,
343 &stbuf->st_atime_sec,
344 &stbuf->st_atime_nsec,
345 &stbuf->st_mtime_sec,
346 &stbuf->st_mtime_nsec,
347 &stbuf->st_ctime_sec,
348 &stbuf->st_ctime_nsec,
349 &stbuf->st_btime_sec,
350 &stbuf->st_btime_nsec,
351 &stbuf->st_gen,
352 &stbuf->st_data_version);
354 break;
355 case '?':
356 if ((proto_version != p9_proto_2000u) &&
357 (proto_version != p9_proto_2000L))
358 return 0;
359 break;
360 default:
361 BUG();
362 break;
365 if (errcode)
366 break;
369 return errcode;
373 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
374 va_list ap)
376 const char *ptr;
377 int errcode = 0;
379 for (ptr = fmt; *ptr; ptr++) {
380 switch (*ptr) {
381 case 'b':{
382 int8_t val = va_arg(ap, int);
383 if (pdu_write(pdu, &val, sizeof(val)))
384 errcode = -EFAULT;
386 break;
387 case 'w':{
388 __le16 val = cpu_to_le16(va_arg(ap, int));
389 if (pdu_write(pdu, &val, sizeof(val)))
390 errcode = -EFAULT;
392 break;
393 case 'd':{
394 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
395 if (pdu_write(pdu, &val, sizeof(val)))
396 errcode = -EFAULT;
398 break;
399 case 'q':{
400 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
401 if (pdu_write(pdu, &val, sizeof(val)))
402 errcode = -EFAULT;
404 break;
405 case 's':{
406 const char *sptr = va_arg(ap, const char *);
407 int16_t len = 0;
408 if (sptr)
409 len = min_t(int16_t, strlen(sptr), USHRT_MAX);
411 errcode = p9pdu_writef(pdu, proto_version,
412 "w", len);
413 if (!errcode && pdu_write(pdu, sptr, len))
414 errcode = -EFAULT;
416 break;
417 case 'Q':{
418 const struct p9_qid *qid =
419 va_arg(ap, const struct p9_qid *);
420 errcode =
421 p9pdu_writef(pdu, proto_version, "bdq",
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 =
429 p9pdu_writef(pdu, proto_version,
430 "wwdQdddqssss?sddd",
431 stbuf->size, stbuf->type,
432 stbuf->dev, &stbuf->qid,
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':{
441 int32_t count = va_arg(ap, int32_t);
442 const void *data = va_arg(ap, const void *);
444 errcode = p9pdu_writef(pdu, proto_version, "d",
445 count);
446 if (!errcode && pdu_write(pdu, data, count))
447 errcode = -EFAULT;
449 break;
450 case 'U':{
451 int32_t count = va_arg(ap, int32_t);
452 const char __user *udata =
453 va_arg(ap, const void __user *);
454 errcode = p9pdu_writef(pdu, proto_version, "d",
455 count);
456 if (!errcode && pdu_write_u(pdu, udata, count))
457 errcode = -EFAULT;
459 break;
460 case 'T':{
461 int16_t nwname = va_arg(ap, int);
462 const char **wnames = va_arg(ap, const char **);
464 errcode = p9pdu_writef(pdu, proto_version, "w",
465 nwname);
466 if (!errcode) {
467 int i;
469 for (i = 0; i < nwname; i++) {
470 errcode =
471 p9pdu_writef(pdu,
472 proto_version,
473 "s",
474 wnames[i]);
475 if (errcode)
476 break;
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 *);
486 errcode = p9pdu_writef(pdu, proto_version, "w",
487 nwqid);
488 if (!errcode) {
489 int i;
491 for (i = 0; i < nwqid; i++) {
492 errcode =
493 p9pdu_writef(pdu,
494 proto_version,
495 "Q",
496 &wqids[i]);
497 if (errcode)
498 break;
502 break;
503 case 'I':{
504 struct p9_iattr_dotl *p9attr = va_arg(ap,
505 struct p9_iattr_dotl *);
507 errcode = p9pdu_writef(pdu, proto_version,
508 "ddddqqqqq",
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);
519 break;
520 case '?':
521 if ((proto_version != p9_proto_2000u) &&
522 (proto_version != p9_proto_2000L))
523 return 0;
524 break;
525 default:
526 BUG();
527 break;
530 if (errcode)
531 break;
534 return errcode;
537 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
539 va_list ap;
540 int ret;
542 va_start(ap, fmt);
543 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
544 va_end(ap);
546 return ret;
549 static int
550 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
552 va_list ap;
553 int ret;
555 va_start(ap, fmt);
556 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
557 va_end(ap);
559 return ret;
562 int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
564 struct p9_fcall fake_pdu;
565 int ret;
567 fake_pdu.size = len;
568 fake_pdu.capacity = len;
569 fake_pdu.sdata = buf;
570 fake_pdu.offset = 0;
572 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
573 if (ret) {
574 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
575 p9pdu_dump(1, &fake_pdu);
578 return ret;
580 EXPORT_SYMBOL(p9stat_read);
582 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
584 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
587 int p9pdu_finalize(struct p9_fcall *pdu)
589 int size = pdu->size;
590 int err;
592 pdu->size = 0;
593 err = p9pdu_writef(pdu, 0, "d", size);
594 pdu->size = size;
596 #ifdef CONFIG_NET_9P_DEBUG
597 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
598 p9pdu_dump(0, pdu);
599 #endif
601 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
602 pdu->id, pdu->tag);
604 return err;
607 void p9pdu_reset(struct p9_fcall *pdu)
609 pdu->offset = 0;
610 pdu->size = 0;
613 int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
614 int proto_version)
616 struct p9_fcall fake_pdu;
617 int ret;
618 char *nameptr;
620 fake_pdu.size = len;
621 fake_pdu.capacity = len;
622 fake_pdu.sdata = buf;
623 fake_pdu.offset = 0;
625 ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
626 &dirent->d_off, &dirent->d_type, &nameptr);
627 if (ret) {
628 P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
629 p9pdu_dump(1, &fake_pdu);
630 goto out;
633 strcpy(dirent->d_name, nameptr);
635 out:
636 return fake_pdu.offset;
638 EXPORT_SYMBOL(p9dirent_read);