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 <net/9p/9p.h>
33 #include <net/9p/client.h>
37 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
41 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
45 #define offset_of(type, memb) \
46 ((unsigned long)(&((type *)0)->memb))
49 #define container_of(obj, type, memb) \
50 ((type *)(((char *)obj) - offset_of(type, memb)))
54 p9pdu_writef(struct p9_fcall
*pdu
, int optional
, const char *fmt
, ...);
56 #ifdef CONFIG_NET_9P_DEBUG
58 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
61 u8
*data
= pdu
->sdata
;
62 int datalen
= pdu
->size
;
67 if (datalen
> (buflen
-16))
70 n
+= scnprintf(buf
+ n
, buflen
- n
, "%02x ", data
[i
]);
72 n
+= scnprintf(buf
+ n
, buflen
- n
, " ");
74 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
78 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
81 P9_DPRINTK(P9_DEBUG_PKT
, "[[[(%d) %s\n", datalen
, buf
);
83 P9_DPRINTK(P9_DEBUG_PKT
, "]]](%d) %s\n", datalen
, buf
);
87 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
91 EXPORT_SYMBOL(p9pdu_dump
);
93 void p9stat_free(struct p9_wstat
*stbuf
)
99 kfree(stbuf
->extension
);
101 EXPORT_SYMBOL(p9stat_free
);
103 static size_t pdu_read(struct p9_fcall
*pdu
, void *data
, size_t size
)
105 size_t len
= MIN(pdu
->size
- pdu
->offset
, size
);
106 memcpy(data
, &pdu
->sdata
[pdu
->offset
], len
);
111 static size_t pdu_write(struct p9_fcall
*pdu
, const void *data
, size_t size
)
113 size_t len
= MIN(pdu
->capacity
- pdu
->size
, size
);
114 memcpy(&pdu
->sdata
[pdu
->size
], data
, len
);
120 pdu_write_u(struct p9_fcall
*pdu
, const char __user
*udata
, size_t size
)
122 size_t len
= MIN(pdu
->capacity
- pdu
->size
, size
);
123 int err
= copy_from_user(&pdu
->sdata
[pdu
->size
], udata
, len
);
125 printk(KERN_WARNING
"pdu_write_u returning: %d\n", err
);
139 D - data blob (int32_t size followed by void *, results are not freed)
140 T - array of strings (int16_t count, followed by strings)
141 R - array of qids (int16_t count, followed by qids)
142 ? - if optional = 1, continue parsing
146 p9pdu_vreadf(struct p9_fcall
*pdu
, int optional
, const char *fmt
, va_list ap
)
151 for (ptr
= fmt
; *ptr
; ptr
++) {
154 int8_t *val
= va_arg(ap
, int8_t *);
155 if (pdu_read(pdu
, val
, sizeof(*val
))) {
162 int16_t *val
= va_arg(ap
, int16_t *);
163 if (pdu_read(pdu
, val
, sizeof(*val
))) {
167 *val
= cpu_to_le16(*val
);
171 int32_t *val
= va_arg(ap
, int32_t *);
172 if (pdu_read(pdu
, val
, sizeof(*val
))) {
176 *val
= cpu_to_le32(*val
);
180 int64_t *val
= va_arg(ap
, int64_t *);
181 if (pdu_read(pdu
, val
, sizeof(*val
))) {
185 *val
= cpu_to_le64(*val
);
189 char **sptr
= va_arg(ap
, char **);
193 errcode
= p9pdu_readf(pdu
, optional
, "w", &len
);
199 *sptr
= kmalloc(size
+ 1, GFP_KERNEL
);
204 if (pdu_read(pdu
, *sptr
, size
)) {
214 va_arg(ap
, struct p9_qid
*);
216 errcode
= p9pdu_readf(pdu
, optional
, "bdq",
217 &qid
->type
, &qid
->version
,
222 struct p9_wstat
*stbuf
=
223 va_arg(ap
, struct p9_wstat
*);
225 memset(stbuf
, 0, sizeof(struct p9_wstat
));
226 stbuf
->n_uid
= stbuf
->n_gid
= stbuf
->n_muid
=
229 p9pdu_readf(pdu
, optional
,
231 &stbuf
->size
, &stbuf
->type
,
232 &stbuf
->dev
, &stbuf
->qid
,
233 &stbuf
->mode
, &stbuf
->atime
,
234 &stbuf
->mtime
, &stbuf
->length
,
235 &stbuf
->name
, &stbuf
->uid
,
236 &stbuf
->gid
, &stbuf
->muid
,
238 &stbuf
->n_uid
, &stbuf
->n_gid
,
245 int32_t *count
= va_arg(ap
, int32_t *);
246 void **data
= va_arg(ap
, void **);
249 p9pdu_readf(pdu
, optional
, "d", count
);
253 pdu
->size
- pdu
->offset
);
254 *data
= &pdu
->sdata
[pdu
->offset
];
259 int16_t *nwname
= va_arg(ap
, int16_t *);
260 char ***wnames
= va_arg(ap
, char ***);
263 p9pdu_readf(pdu
, optional
, "w", nwname
);
266 kmalloc(sizeof(char *) * *nwname
,
275 for (i
= 0; i
< *nwname
; i
++) {
277 p9pdu_readf(pdu
, optional
,
289 for (i
= 0; i
< *nwname
; i
++)
298 int16_t *nwqid
= va_arg(ap
, int16_t *);
299 struct p9_qid
**wqids
=
300 va_arg(ap
, struct p9_qid
**);
305 p9pdu_readf(pdu
, optional
, "w", nwqid
);
309 sizeof(struct p9_qid
),
318 for (i
= 0; i
< *nwqid
; i
++) {
320 p9pdu_readf(pdu
, optional
,
351 p9pdu_vwritef(struct p9_fcall
*pdu
, int optional
, const char *fmt
, va_list ap
)
356 for (ptr
= fmt
; *ptr
; ptr
++) {
359 int8_t val
= va_arg(ap
, int);
360 if (pdu_write(pdu
, &val
, sizeof(val
)))
365 int16_t val
= va_arg(ap
, int);
366 if (pdu_write(pdu
, &val
, sizeof(val
)))
371 int32_t val
= va_arg(ap
, int32_t);
372 if (pdu_write(pdu
, &val
, sizeof(val
)))
377 int64_t val
= va_arg(ap
, int64_t);
378 if (pdu_write(pdu
, &val
, sizeof(val
)))
383 const char *sptr
= va_arg(ap
, const char *);
386 len
= MIN(strlen(sptr
), USHORT_MAX
);
388 errcode
= p9pdu_writef(pdu
, optional
, "w", len
);
389 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
394 const struct p9_qid
*qid
=
395 va_arg(ap
, const struct p9_qid
*);
397 p9pdu_writef(pdu
, optional
, "bdq",
398 qid
->type
, qid
->version
,
402 const struct p9_wstat
*stbuf
=
403 va_arg(ap
, const struct p9_wstat
*);
405 p9pdu_writef(pdu
, optional
,
407 stbuf
->size
, stbuf
->type
,
408 stbuf
->dev
, &stbuf
->qid
,
409 stbuf
->mode
, stbuf
->atime
,
410 stbuf
->mtime
, stbuf
->length
,
411 stbuf
->name
, stbuf
->uid
,
412 stbuf
->gid
, stbuf
->muid
,
413 stbuf
->extension
, stbuf
->n_uid
,
414 stbuf
->n_gid
, stbuf
->n_muid
);
417 int32_t count
= va_arg(ap
, int32_t);
418 const void *data
= va_arg(ap
, const void *);
421 p9pdu_writef(pdu
, optional
, "d", count
);
422 if (!errcode
&& pdu_write(pdu
, data
, count
))
427 int32_t count
= va_arg(ap
, int32_t);
428 const char __user
*udata
=
429 va_arg(ap
, const void __user
*);
431 p9pdu_writef(pdu
, optional
, "d", count
);
432 if (!errcode
&& pdu_write_u(pdu
, udata
, count
))
437 int16_t nwname
= va_arg(ap
, int);
438 const char **wnames
= va_arg(ap
, const char **);
441 p9pdu_writef(pdu
, optional
, "w", nwname
);
445 for (i
= 0; i
< nwname
; i
++) {
447 p9pdu_writef(pdu
, optional
,
457 int16_t nwqid
= va_arg(ap
, int);
458 struct p9_qid
*wqids
=
459 va_arg(ap
, struct p9_qid
*);
462 p9pdu_writef(pdu
, optional
, "w", nwqid
);
466 for (i
= 0; i
< nwqid
; i
++) {
468 p9pdu_writef(pdu
, optional
,
493 int p9pdu_readf(struct p9_fcall
*pdu
, int optional
, const char *fmt
, ...)
499 ret
= p9pdu_vreadf(pdu
, optional
, fmt
, ap
);
506 p9pdu_writef(struct p9_fcall
*pdu
, int optional
, const char *fmt
, ...)
512 ret
= p9pdu_vwritef(pdu
, optional
, fmt
, ap
);
518 int p9stat_read(char *buf
, int len
, struct p9_wstat
*st
, int dotu
)
520 struct p9_fcall fake_pdu
;
524 fake_pdu
.capacity
= len
;
525 fake_pdu
.sdata
= buf
;
528 ret
= p9pdu_readf(&fake_pdu
, dotu
, "S", st
);
530 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
531 p9pdu_dump(1, &fake_pdu
);
536 EXPORT_SYMBOL(p9stat_read
);
538 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
540 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
543 int p9pdu_finalize(struct p9_fcall
*pdu
)
545 int size
= pdu
->size
;
549 err
= p9pdu_writef(pdu
, 0, "d", size
);
552 #ifdef CONFIG_NET_9P_DEBUG
553 if ((p9_debug_level
& P9_DEBUG_PKT
) == P9_DEBUG_PKT
)
557 P9_DPRINTK(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n", pdu
->size
,
563 void p9pdu_reset(struct p9_fcall
*pdu
)