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>
39 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
43 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
47 #define offset_of(type, memb) \
48 ((unsigned long)(&((type *)0)->memb))
51 #define container_of(obj, type, memb) \
52 ((type *)(((char *)obj) - offset_of(type, memb)))
56 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...);
58 #ifdef CONFIG_NET_9P_DEBUG
60 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
63 u8
*data
= pdu
->sdata
;
64 int datalen
= pdu
->size
;
69 if (datalen
> (buflen
-16))
72 n
+= scnprintf(buf
+ n
, buflen
- n
, "%02x ", data
[i
]);
74 n
+= scnprintf(buf
+ n
, buflen
- n
, " ");
76 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
80 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
83 P9_DPRINTK(P9_DEBUG_PKT
, "[[[(%d) %s\n", datalen
, buf
);
85 P9_DPRINTK(P9_DEBUG_PKT
, "]]](%d) %s\n", datalen
, buf
);
89 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
93 EXPORT_SYMBOL(p9pdu_dump
);
95 void p9stat_free(struct p9_wstat
*stbuf
)
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
);
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
);
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
);
127 printk(KERN_WARNING
"pdu_write_u returning: %d\n", err
);
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 A - stat for 9p2000.L (p9_stat_dotl)
145 ? - if optional = 1, continue parsing
149 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
155 for (ptr
= fmt
; *ptr
; ptr
++) {
158 int8_t *val
= va_arg(ap
, int8_t *);
159 if (pdu_read(pdu
, val
, sizeof(*val
))) {
166 int16_t *val
= va_arg(ap
, int16_t *);
168 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
172 *val
= le16_to_cpu(le_val
);
176 int32_t *val
= va_arg(ap
, int32_t *);
178 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
182 *val
= le32_to_cpu(le_val
);
186 int64_t *val
= va_arg(ap
, int64_t *);
188 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
192 *val
= le64_to_cpu(le_val
);
196 char **sptr
= va_arg(ap
, char **);
200 errcode
= p9pdu_readf(pdu
, proto_version
,
207 *sptr
= kmalloc(size
+ 1, GFP_KERNEL
);
212 if (pdu_read(pdu
, *sptr
, size
)) {
222 va_arg(ap
, struct p9_qid
*);
224 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
225 &qid
->type
, &qid
->version
,
230 struct p9_wstat
*stbuf
=
231 va_arg(ap
, struct p9_wstat
*);
233 memset(stbuf
, 0, sizeof(struct p9_wstat
));
234 stbuf
->n_uid
= stbuf
->n_gid
= stbuf
->n_muid
=
237 p9pdu_readf(pdu
, proto_version
,
239 &stbuf
->size
, &stbuf
->type
,
240 &stbuf
->dev
, &stbuf
->qid
,
241 &stbuf
->mode
, &stbuf
->atime
,
242 &stbuf
->mtime
, &stbuf
->length
,
243 &stbuf
->name
, &stbuf
->uid
,
244 &stbuf
->gid
, &stbuf
->muid
,
246 &stbuf
->n_uid
, &stbuf
->n_gid
,
253 int32_t *count
= va_arg(ap
, int32_t *);
254 void **data
= va_arg(ap
, void **);
257 p9pdu_readf(pdu
, proto_version
, "d", count
);
261 pdu
->size
- pdu
->offset
);
262 *data
= &pdu
->sdata
[pdu
->offset
];
267 int16_t *nwname
= va_arg(ap
, int16_t *);
268 char ***wnames
= va_arg(ap
, char ***);
270 errcode
= p9pdu_readf(pdu
, proto_version
,
274 kmalloc(sizeof(char *) * *nwname
,
283 for (i
= 0; i
< *nwname
; i
++) {
298 for (i
= 0; i
< *nwname
; i
++)
307 int16_t *nwqid
= va_arg(ap
, int16_t *);
308 struct p9_qid
**wqids
=
309 va_arg(ap
, struct p9_qid
**);
314 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
318 sizeof(struct p9_qid
),
327 for (i
= 0; i
< *nwqid
; i
++) {
345 struct p9_stat_dotl
*stbuf
=
346 va_arg(ap
, struct p9_stat_dotl
*);
348 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
350 p9pdu_readf(pdu
, proto_version
,
351 "qQdddqqqqqqqqqqqqqqq",
352 &stbuf
->st_result_mask
,
355 &stbuf
->st_uid
, &stbuf
->st_gid
,
357 &stbuf
->st_rdev
, &stbuf
->st_size
,
358 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
359 &stbuf
->st_atime_sec
,
360 &stbuf
->st_atime_nsec
,
361 &stbuf
->st_mtime_sec
,
362 &stbuf
->st_mtime_nsec
,
363 &stbuf
->st_ctime_sec
,
364 &stbuf
->st_ctime_nsec
,
365 &stbuf
->st_btime_sec
,
366 &stbuf
->st_btime_nsec
,
368 &stbuf
->st_data_version
);
372 if ((proto_version
!= p9_proto_2000u
) &&
373 (proto_version
!= p9_proto_2000L
))
389 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
395 for (ptr
= fmt
; *ptr
; ptr
++) {
398 int8_t val
= va_arg(ap
, int);
399 if (pdu_write(pdu
, &val
, sizeof(val
)))
404 __le16 val
= cpu_to_le16(va_arg(ap
, int));
405 if (pdu_write(pdu
, &val
, sizeof(val
)))
410 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
411 if (pdu_write(pdu
, &val
, sizeof(val
)))
416 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
417 if (pdu_write(pdu
, &val
, sizeof(val
)))
422 const char *sptr
= va_arg(ap
, const char *);
425 len
= MIN(strlen(sptr
), USHRT_MAX
);
427 errcode
= p9pdu_writef(pdu
, proto_version
,
429 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
434 const struct p9_qid
*qid
=
435 va_arg(ap
, const struct p9_qid
*);
437 p9pdu_writef(pdu
, proto_version
, "bdq",
438 qid
->type
, qid
->version
,
442 const struct p9_wstat
*stbuf
=
443 va_arg(ap
, const struct p9_wstat
*);
445 p9pdu_writef(pdu
, proto_version
,
447 stbuf
->size
, stbuf
->type
,
448 stbuf
->dev
, &stbuf
->qid
,
449 stbuf
->mode
, stbuf
->atime
,
450 stbuf
->mtime
, stbuf
->length
,
451 stbuf
->name
, stbuf
->uid
,
452 stbuf
->gid
, stbuf
->muid
,
453 stbuf
->extension
, stbuf
->n_uid
,
454 stbuf
->n_gid
, stbuf
->n_muid
);
457 int32_t count
= va_arg(ap
, int32_t);
458 const void *data
= va_arg(ap
, const void *);
460 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
462 if (!errcode
&& pdu_write(pdu
, data
, count
))
467 int32_t count
= va_arg(ap
, int32_t);
468 const char __user
*udata
=
469 va_arg(ap
, const void __user
*);
470 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
472 if (!errcode
&& pdu_write_u(pdu
, udata
, count
))
477 int16_t nwname
= va_arg(ap
, int);
478 const char **wnames
= va_arg(ap
, const char **);
480 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
485 for (i
= 0; i
< nwname
; i
++) {
498 int16_t nwqid
= va_arg(ap
, int);
499 struct p9_qid
*wqids
=
500 va_arg(ap
, struct p9_qid
*);
502 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
507 for (i
= 0; i
< nwqid
; i
++) {
520 struct p9_iattr_dotl
*p9attr
= va_arg(ap
,
521 struct p9_iattr_dotl
*);
523 errcode
= p9pdu_writef(pdu
, proto_version
,
537 if ((proto_version
!= p9_proto_2000u
) &&
538 (proto_version
!= p9_proto_2000L
))
553 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
559 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
566 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
572 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
578 int p9stat_read(char *buf
, int len
, struct p9_wstat
*st
, int proto_version
)
580 struct p9_fcall fake_pdu
;
584 fake_pdu
.capacity
= len
;
585 fake_pdu
.sdata
= buf
;
588 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "S", st
);
590 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
591 p9pdu_dump(1, &fake_pdu
);
596 EXPORT_SYMBOL(p9stat_read
);
598 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
600 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
603 int p9pdu_finalize(struct p9_fcall
*pdu
)
605 int size
= pdu
->size
;
609 err
= p9pdu_writef(pdu
, 0, "d", size
);
612 #ifdef CONFIG_NET_9P_DEBUG
613 if ((p9_debug_level
& P9_DEBUG_PKT
) == P9_DEBUG_PKT
)
617 P9_DPRINTK(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n", pdu
->size
,
623 void p9pdu_reset(struct p9_fcall
*pdu
)
629 int p9dirent_read(char *buf
, int len
, struct p9_dirent
*dirent
,
632 struct p9_fcall fake_pdu
;
637 fake_pdu
.capacity
= len
;
638 fake_pdu
.sdata
= buf
;
641 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "Qqbs", &dirent
->qid
,
642 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
644 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
645 p9pdu_dump(1, &fake_pdu
);
649 strcpy(dirent
->d_name
, nameptr
);
652 return fake_pdu
.offset
;
654 EXPORT_SYMBOL(p9dirent_read
);