of: check for IS_ERR()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / 9p / protocol.c
blobe7541d5b011831e6c945d9cfb9163c90f9b1d1fd
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/slab.h>
32 #include <linux/sched.h>
33 #include <linux/types.h>
34 #include <net/9p/9p.h>
35 #include <net/9p/client.h>
36 #include "protocol.h"
38 #ifndef MIN
39 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
40 #endif
42 #ifndef MAX
43 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
44 #endif
46 #ifndef offset_of
47 #define offset_of(type, memb) \
48 ((unsigned long)(&((type *)0)->memb))
49 #endif
50 #ifndef container_of
51 #define container_of(obj, type, memb) \
52 ((type *)(((char *)obj) - offset_of(type, memb)))
53 #endif
55 static int
56 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
58 #ifdef CONFIG_NET_9P_DEBUG
59 void
60 p9pdu_dump(int way, struct p9_fcall *pdu)
62 int i, n;
63 u8 *data = pdu->sdata;
64 int datalen = pdu->size;
65 char buf[255];
66 int buflen = 255;
68 i = n = 0;
69 if (datalen > (buflen-16))
70 datalen = buflen-16;
71 while (i < datalen) {
72 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
73 if (i%4 == 3)
74 n += scnprintf(buf + n, buflen - n, " ");
75 if (i%32 == 31)
76 n += scnprintf(buf + n, buflen - n, "\n");
78 i++;
80 n += scnprintf(buf + n, buflen - n, "\n");
82 if (way)
83 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
84 else
85 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
87 #else
88 void
89 p9pdu_dump(int way, struct p9_fcall *pdu)
92 #endif
93 EXPORT_SYMBOL(p9pdu_dump);
95 void p9stat_free(struct p9_wstat *stbuf)
97 kfree(stbuf->name);
98 kfree(stbuf->uid);
99 kfree(stbuf->gid);
100 kfree(stbuf->muid);
101 kfree(stbuf->extension);
103 EXPORT_SYMBOL(p9stat_free);
105 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
107 size_t len = MIN(pdu->size - pdu->offset, size);
108 memcpy(data, &pdu->sdata[pdu->offset], len);
109 pdu->offset += len;
110 return size - len;
113 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
115 size_t len = MIN(pdu->capacity - pdu->size, size);
116 memcpy(&pdu->sdata[pdu->size], data, len);
117 pdu->size += len;
118 return size - len;
121 static size_t
122 pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
124 size_t len = MIN(pdu->capacity - pdu->size, size);
125 int err = copy_from_user(&pdu->sdata[pdu->size], udata, len);
126 if (err)
127 printk(KERN_WARNING "pdu_write_u returning: %d\n", err);
129 pdu->size += len;
130 return size - len;
134 b - int8_t
135 w - int16_t
136 d - int32_t
137 q - int64_t
138 s - string
139 S - stat
140 Q - qid
141 D - data blob (int32_t size followed by void *, results are not freed)
142 T - array of strings (int16_t count, followed by strings)
143 R - array of qids (int16_t count, followed by qids)
144 ? - if optional = 1, continue parsing
147 static int
148 p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
149 va_list ap)
151 const char *ptr;
152 int errcode = 0;
154 for (ptr = fmt; *ptr; ptr++) {
155 switch (*ptr) {
156 case 'b':{
157 int8_t *val = va_arg(ap, int8_t *);
158 if (pdu_read(pdu, val, sizeof(*val))) {
159 errcode = -EFAULT;
160 break;
163 break;
164 case 'w':{
165 int16_t *val = va_arg(ap, int16_t *);
166 __le16 le_val;
167 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
168 errcode = -EFAULT;
169 break;
171 *val = le16_to_cpu(le_val);
173 break;
174 case 'd':{
175 int32_t *val = va_arg(ap, int32_t *);
176 __le32 le_val;
177 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
178 errcode = -EFAULT;
179 break;
181 *val = le32_to_cpu(le_val);
183 break;
184 case 'q':{
185 int64_t *val = va_arg(ap, int64_t *);
186 __le64 le_val;
187 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
188 errcode = -EFAULT;
189 break;
191 *val = le64_to_cpu(le_val);
193 break;
194 case 's':{
195 char **sptr = va_arg(ap, char **);
196 int16_t len;
197 int size;
199 errcode = p9pdu_readf(pdu, proto_version,
200 "w", &len);
201 if (errcode)
202 break;
204 size = MAX(len, 0);
206 *sptr = kmalloc(size + 1, GFP_KERNEL);
207 if (*sptr == NULL) {
208 errcode = -EFAULT;
209 break;
211 if (pdu_read(pdu, *sptr, size)) {
212 errcode = -EFAULT;
213 kfree(*sptr);
214 *sptr = NULL;
215 } else
216 (*sptr)[size] = 0;
218 break;
219 case 'Q':{
220 struct p9_qid *qid =
221 va_arg(ap, struct p9_qid *);
223 errcode = p9pdu_readf(pdu, proto_version, "bdq",
224 &qid->type, &qid->version,
225 &qid->path);
227 break;
228 case 'S':{
229 struct p9_wstat *stbuf =
230 va_arg(ap, struct p9_wstat *);
232 memset(stbuf, 0, sizeof(struct p9_wstat));
233 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
235 errcode =
236 p9pdu_readf(pdu, proto_version,
237 "wwdQdddqssss?sddd",
238 &stbuf->size, &stbuf->type,
239 &stbuf->dev, &stbuf->qid,
240 &stbuf->mode, &stbuf->atime,
241 &stbuf->mtime, &stbuf->length,
242 &stbuf->name, &stbuf->uid,
243 &stbuf->gid, &stbuf->muid,
244 &stbuf->extension,
245 &stbuf->n_uid, &stbuf->n_gid,
246 &stbuf->n_muid);
247 if (errcode)
248 p9stat_free(stbuf);
250 break;
251 case 'D':{
252 int32_t *count = va_arg(ap, int32_t *);
253 void **data = va_arg(ap, void **);
255 errcode =
256 p9pdu_readf(pdu, proto_version, "d", count);
257 if (!errcode) {
258 *count =
259 MIN(*count,
260 pdu->size - pdu->offset);
261 *data = &pdu->sdata[pdu->offset];
264 break;
265 case 'T':{
266 int16_t *nwname = va_arg(ap, int16_t *);
267 char ***wnames = va_arg(ap, char ***);
269 errcode = p9pdu_readf(pdu, proto_version,
270 "w", nwname);
271 if (!errcode) {
272 *wnames =
273 kmalloc(sizeof(char *) * *nwname,
274 GFP_KERNEL);
275 if (!*wnames)
276 errcode = -ENOMEM;
279 if (!errcode) {
280 int i;
282 for (i = 0; i < *nwname; i++) {
283 errcode =
284 p9pdu_readf(pdu,
285 proto_version,
286 "s",
287 &(*wnames)[i]);
288 if (errcode)
289 break;
293 if (errcode) {
294 if (*wnames) {
295 int i;
297 for (i = 0; i < *nwname; i++)
298 kfree((*wnames)[i]);
300 kfree(*wnames);
301 *wnames = NULL;
304 break;
305 case 'R':{
306 int16_t *nwqid = va_arg(ap, int16_t *);
307 struct p9_qid **wqids =
308 va_arg(ap, struct p9_qid **);
310 *wqids = NULL;
312 errcode =
313 p9pdu_readf(pdu, proto_version, "w", nwqid);
314 if (!errcode) {
315 *wqids =
316 kmalloc(*nwqid *
317 sizeof(struct p9_qid),
318 GFP_KERNEL);
319 if (*wqids == NULL)
320 errcode = -ENOMEM;
323 if (!errcode) {
324 int i;
326 for (i = 0; i < *nwqid; i++) {
327 errcode =
328 p9pdu_readf(pdu,
329 proto_version,
330 "Q",
331 &(*wqids)[i]);
332 if (errcode)
333 break;
337 if (errcode) {
338 kfree(*wqids);
339 *wqids = NULL;
342 break;
343 case '?':
344 if (proto_version != p9_proto_2000u)
345 return 0;
346 break;
347 default:
348 BUG();
349 break;
352 if (errcode)
353 break;
356 return errcode;
360 p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
361 va_list ap)
363 const char *ptr;
364 int errcode = 0;
366 for (ptr = fmt; *ptr; ptr++) {
367 switch (*ptr) {
368 case 'b':{
369 int8_t val = va_arg(ap, int);
370 if (pdu_write(pdu, &val, sizeof(val)))
371 errcode = -EFAULT;
373 break;
374 case 'w':{
375 __le16 val = cpu_to_le16(va_arg(ap, int));
376 if (pdu_write(pdu, &val, sizeof(val)))
377 errcode = -EFAULT;
379 break;
380 case 'd':{
381 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
382 if (pdu_write(pdu, &val, sizeof(val)))
383 errcode = -EFAULT;
385 break;
386 case 'q':{
387 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
388 if (pdu_write(pdu, &val, sizeof(val)))
389 errcode = -EFAULT;
391 break;
392 case 's':{
393 const char *sptr = va_arg(ap, const char *);
394 int16_t len = 0;
395 if (sptr)
396 len = MIN(strlen(sptr), USHORT_MAX);
398 errcode = p9pdu_writef(pdu, proto_version,
399 "w", len);
400 if (!errcode && pdu_write(pdu, sptr, len))
401 errcode = -EFAULT;
403 break;
404 case 'Q':{
405 const struct p9_qid *qid =
406 va_arg(ap, const struct p9_qid *);
407 errcode =
408 p9pdu_writef(pdu, proto_version, "bdq",
409 qid->type, qid->version,
410 qid->path);
411 } break;
412 case 'S':{
413 const struct p9_wstat *stbuf =
414 va_arg(ap, const struct p9_wstat *);
415 errcode =
416 p9pdu_writef(pdu, proto_version,
417 "wwdQdddqssss?sddd",
418 stbuf->size, stbuf->type,
419 stbuf->dev, &stbuf->qid,
420 stbuf->mode, stbuf->atime,
421 stbuf->mtime, stbuf->length,
422 stbuf->name, stbuf->uid,
423 stbuf->gid, stbuf->muid,
424 stbuf->extension, stbuf->n_uid,
425 stbuf->n_gid, stbuf->n_muid);
426 } break;
427 case 'D':{
428 int32_t count = va_arg(ap, int32_t);
429 const void *data = va_arg(ap, const void *);
431 errcode = p9pdu_writef(pdu, proto_version, "d",
432 count);
433 if (!errcode && pdu_write(pdu, data, count))
434 errcode = -EFAULT;
436 break;
437 case 'U':{
438 int32_t count = va_arg(ap, int32_t);
439 const char __user *udata =
440 va_arg(ap, const void __user *);
441 errcode = p9pdu_writef(pdu, proto_version, "d",
442 count);
443 if (!errcode && pdu_write_u(pdu, udata, count))
444 errcode = -EFAULT;
446 break;
447 case 'T':{
448 int16_t nwname = va_arg(ap, int);
449 const char **wnames = va_arg(ap, const char **);
451 errcode = p9pdu_writef(pdu, proto_version, "w",
452 nwname);
453 if (!errcode) {
454 int i;
456 for (i = 0; i < nwname; i++) {
457 errcode =
458 p9pdu_writef(pdu,
459 proto_version,
460 "s",
461 wnames[i]);
462 if (errcode)
463 break;
467 break;
468 case 'R':{
469 int16_t nwqid = va_arg(ap, int);
470 struct p9_qid *wqids =
471 va_arg(ap, struct p9_qid *);
473 errcode = p9pdu_writef(pdu, proto_version, "w",
474 nwqid);
475 if (!errcode) {
476 int i;
478 for (i = 0; i < nwqid; i++) {
479 errcode =
480 p9pdu_writef(pdu,
481 proto_version,
482 "Q",
483 &wqids[i]);
484 if (errcode)
485 break;
489 break;
490 case '?':
491 if (proto_version != p9_proto_2000u)
492 return 0;
493 break;
494 default:
495 BUG();
496 break;
499 if (errcode)
500 break;
503 return errcode;
506 int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
508 va_list ap;
509 int ret;
511 va_start(ap, fmt);
512 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
513 va_end(ap);
515 return ret;
518 static int
519 p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
521 va_list ap;
522 int ret;
524 va_start(ap, fmt);
525 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
526 va_end(ap);
528 return ret;
531 int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
533 struct p9_fcall fake_pdu;
534 int ret;
536 fake_pdu.size = len;
537 fake_pdu.capacity = len;
538 fake_pdu.sdata = buf;
539 fake_pdu.offset = 0;
541 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
542 if (ret) {
543 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
544 p9pdu_dump(1, &fake_pdu);
547 return ret;
549 EXPORT_SYMBOL(p9stat_read);
551 int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
553 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
556 int p9pdu_finalize(struct p9_fcall *pdu)
558 int size = pdu->size;
559 int err;
561 pdu->size = 0;
562 err = p9pdu_writef(pdu, 0, "d", size);
563 pdu->size = size;
565 #ifdef CONFIG_NET_9P_DEBUG
566 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
567 p9pdu_dump(0, pdu);
568 #endif
570 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
571 pdu->id, pdu->tag);
573 return err;
576 void p9pdu_reset(struct p9_fcall *pdu)
578 pdu->offset = 0;
579 pdu->size = 0;