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>
41 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...);
43 #ifdef CONFIG_NET_9P_DEBUG
45 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
49 if ((p9_debug_level
& P9_DEBUG_VPKT
) != P9_DEBUG_VPKT
) {
50 if ((p9_debug_level
& P9_DEBUG_PKT
) == P9_DEBUG_PKT
) {
54 /* shouldn't happen */
60 print_hex_dump_bytes("[9P] ", DUMP_PREFIX_OFFSET
, pdu
->sdata
,
63 print_hex_dump_bytes("]9P[ ", DUMP_PREFIX_OFFSET
, pdu
->sdata
,
68 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
72 EXPORT_SYMBOL(p9pdu_dump
);
74 void p9stat_free(struct p9_wstat
*stbuf
)
80 kfree(stbuf
->extension
);
82 EXPORT_SYMBOL(p9stat_free
);
84 size_t pdu_read(struct p9_fcall
*pdu
, void *data
, size_t size
)
86 size_t len
= min(pdu
->size
- pdu
->offset
, size
);
87 memcpy(data
, &pdu
->sdata
[pdu
->offset
], len
);
92 static size_t pdu_write(struct p9_fcall
*pdu
, const void *data
, size_t size
)
94 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
95 memcpy(&pdu
->sdata
[pdu
->size
], data
, len
);
101 pdu_write_u(struct p9_fcall
*pdu
, const char __user
*udata
, size_t size
)
103 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
104 if (copy_from_user(&pdu
->sdata
[pdu
->size
], udata
, len
))
119 D - data blob (int32_t size followed by void *, results are not freed)
120 T - array of strings (int16_t count, followed by strings)
121 R - array of qids (int16_t count, followed by qids)
122 A - stat for 9p2000.L (p9_stat_dotl)
123 ? - if optional = 1, continue parsing
127 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
133 for (ptr
= fmt
; *ptr
; ptr
++) {
136 int8_t *val
= va_arg(ap
, int8_t *);
137 if (pdu_read(pdu
, val
, sizeof(*val
))) {
144 int16_t *val
= va_arg(ap
, int16_t *);
146 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
150 *val
= le16_to_cpu(le_val
);
154 int32_t *val
= va_arg(ap
, int32_t *);
156 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
160 *val
= le32_to_cpu(le_val
);
164 int64_t *val
= va_arg(ap
, int64_t *);
166 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
170 *val
= le64_to_cpu(le_val
);
174 char **sptr
= va_arg(ap
, char **);
177 errcode
= p9pdu_readf(pdu
, proto_version
,
182 *sptr
= kmalloc(len
+ 1, GFP_NOFS
);
187 if (pdu_read(pdu
, *sptr
, len
)) {
197 va_arg(ap
, struct p9_qid
*);
199 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
200 &qid
->type
, &qid
->version
,
205 struct p9_wstat
*stbuf
=
206 va_arg(ap
, struct p9_wstat
*);
208 memset(stbuf
, 0, sizeof(struct p9_wstat
));
209 stbuf
->n_uid
= stbuf
->n_gid
= stbuf
->n_muid
=
212 p9pdu_readf(pdu
, proto_version
,
214 &stbuf
->size
, &stbuf
->type
,
215 &stbuf
->dev
, &stbuf
->qid
,
216 &stbuf
->mode
, &stbuf
->atime
,
217 &stbuf
->mtime
, &stbuf
->length
,
218 &stbuf
->name
, &stbuf
->uid
,
219 &stbuf
->gid
, &stbuf
->muid
,
221 &stbuf
->n_uid
, &stbuf
->n_gid
,
228 uint32_t *count
= va_arg(ap
, uint32_t *);
229 void **data
= va_arg(ap
, void **);
232 p9pdu_readf(pdu
, proto_version
, "d", count
);
235 min_t(uint32_t, *count
,
236 pdu
->size
- pdu
->offset
);
237 *data
= &pdu
->sdata
[pdu
->offset
];
242 uint16_t *nwname
= va_arg(ap
, uint16_t *);
243 char ***wnames
= va_arg(ap
, char ***);
245 errcode
= p9pdu_readf(pdu
, proto_version
,
249 kmalloc(sizeof(char *) * *nwname
,
258 for (i
= 0; i
< *nwname
; i
++) {
273 for (i
= 0; i
< *nwname
; i
++)
282 int16_t *nwqid
= va_arg(ap
, int16_t *);
283 struct p9_qid
**wqids
=
284 va_arg(ap
, struct p9_qid
**);
289 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
293 sizeof(struct p9_qid
),
302 for (i
= 0; i
< *nwqid
; i
++) {
320 struct p9_stat_dotl
*stbuf
=
321 va_arg(ap
, struct p9_stat_dotl
*);
323 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
325 p9pdu_readf(pdu
, proto_version
,
326 "qQdddqqqqqqqqqqqqqqq",
327 &stbuf
->st_result_mask
,
330 &stbuf
->st_uid
, &stbuf
->st_gid
,
332 &stbuf
->st_rdev
, &stbuf
->st_size
,
333 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
334 &stbuf
->st_atime_sec
,
335 &stbuf
->st_atime_nsec
,
336 &stbuf
->st_mtime_sec
,
337 &stbuf
->st_mtime_nsec
,
338 &stbuf
->st_ctime_sec
,
339 &stbuf
->st_ctime_nsec
,
340 &stbuf
->st_btime_sec
,
341 &stbuf
->st_btime_nsec
,
343 &stbuf
->st_data_version
);
347 if ((proto_version
!= p9_proto_2000u
) &&
348 (proto_version
!= p9_proto_2000L
))
364 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
370 for (ptr
= fmt
; *ptr
; ptr
++) {
373 int8_t val
= va_arg(ap
, int);
374 if (pdu_write(pdu
, &val
, sizeof(val
)))
379 __le16 val
= cpu_to_le16(va_arg(ap
, int));
380 if (pdu_write(pdu
, &val
, sizeof(val
)))
385 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
386 if (pdu_write(pdu
, &val
, sizeof(val
)))
391 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
392 if (pdu_write(pdu
, &val
, sizeof(val
)))
397 const char *sptr
= va_arg(ap
, const char *);
400 len
= min_t(uint16_t, strlen(sptr
),
403 errcode
= p9pdu_writef(pdu
, proto_version
,
405 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
410 const struct p9_qid
*qid
=
411 va_arg(ap
, const struct p9_qid
*);
413 p9pdu_writef(pdu
, proto_version
, "bdq",
414 qid
->type
, qid
->version
,
418 const struct p9_wstat
*stbuf
=
419 va_arg(ap
, const struct p9_wstat
*);
421 p9pdu_writef(pdu
, proto_version
,
423 stbuf
->size
, stbuf
->type
,
424 stbuf
->dev
, &stbuf
->qid
,
425 stbuf
->mode
, stbuf
->atime
,
426 stbuf
->mtime
, stbuf
->length
,
427 stbuf
->name
, stbuf
->uid
,
428 stbuf
->gid
, stbuf
->muid
,
429 stbuf
->extension
, stbuf
->n_uid
,
430 stbuf
->n_gid
, stbuf
->n_muid
);
433 uint32_t count
= va_arg(ap
, uint32_t);
434 const void *data
= va_arg(ap
, const void *);
436 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
438 if (!errcode
&& pdu_write(pdu
, data
, count
))
443 int32_t count
= va_arg(ap
, int32_t);
444 const char __user
*udata
=
445 va_arg(ap
, const void __user
*);
446 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
448 if (!errcode
&& pdu_write_u(pdu
, udata
, count
))
453 uint16_t nwname
= va_arg(ap
, int);
454 const char **wnames
= va_arg(ap
, const char **);
456 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
461 for (i
= 0; i
< nwname
; i
++) {
474 int16_t nwqid
= va_arg(ap
, int);
475 struct p9_qid
*wqids
=
476 va_arg(ap
, struct p9_qid
*);
478 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
483 for (i
= 0; i
< nwqid
; i
++) {
496 struct p9_iattr_dotl
*p9attr
= va_arg(ap
,
497 struct p9_iattr_dotl
*);
499 errcode
= p9pdu_writef(pdu
, proto_version
,
513 if ((proto_version
!= p9_proto_2000u
) &&
514 (proto_version
!= p9_proto_2000L
))
529 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
535 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
542 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
548 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
554 int p9stat_read(char *buf
, int len
, struct p9_wstat
*st
, int proto_version
)
556 struct p9_fcall fake_pdu
;
560 fake_pdu
.capacity
= len
;
561 fake_pdu
.sdata
= buf
;
564 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "S", st
);
566 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
567 P9_DUMP_PKT(0, &fake_pdu
);
572 EXPORT_SYMBOL(p9stat_read
);
574 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
577 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
580 int p9pdu_finalize(struct p9_fcall
*pdu
)
582 int size
= pdu
->size
;
586 err
= p9pdu_writef(pdu
, 0, "d", size
);
590 P9_DPRINTK(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n", pdu
->size
,
596 void p9pdu_reset(struct p9_fcall
*pdu
)
602 int p9dirent_read(char *buf
, int len
, struct p9_dirent
*dirent
,
605 struct p9_fcall fake_pdu
;
610 fake_pdu
.capacity
= len
;
611 fake_pdu
.sdata
= buf
;
614 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "Qqbs", &dirent
->qid
,
615 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
617 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
618 P9_DUMP_PKT(1, &fake_pdu
);
622 strcpy(dirent
->d_name
, nameptr
);
626 return fake_pdu
.offset
;
628 EXPORT_SYMBOL(p9dirent_read
);