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 if (copy_from_user(&pdu
->sdata
[pdu
->size
], udata
, len
))
140 D - data blob (int32_t size followed by void *, results are not freed)
141 T - array of strings (int16_t count, followed by strings)
142 R - array of qids (int16_t count, followed by qids)
143 A - stat for 9p2000.L (p9_stat_dotl)
144 ? - if optional = 1, continue parsing
148 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
154 for (ptr
= fmt
; *ptr
; ptr
++) {
157 int8_t *val
= va_arg(ap
, int8_t *);
158 if (pdu_read(pdu
, val
, sizeof(*val
))) {
165 int16_t *val
= va_arg(ap
, int16_t *);
167 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
171 *val
= le16_to_cpu(le_val
);
175 int32_t *val
= va_arg(ap
, int32_t *);
177 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
181 *val
= le32_to_cpu(le_val
);
185 int64_t *val
= va_arg(ap
, int64_t *);
187 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
191 *val
= le64_to_cpu(le_val
);
195 char **sptr
= va_arg(ap
, char **);
199 errcode
= p9pdu_readf(pdu
, proto_version
,
206 *sptr
= kmalloc(size
+ 1, GFP_KERNEL
);
211 if (pdu_read(pdu
, *sptr
, size
)) {
221 va_arg(ap
, struct p9_qid
*);
223 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
224 &qid
->type
, &qid
->version
,
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
=
236 p9pdu_readf(pdu
, proto_version
,
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
,
245 &stbuf
->n_uid
, &stbuf
->n_gid
,
252 int32_t *count
= va_arg(ap
, int32_t *);
253 void **data
= va_arg(ap
, void **);
256 p9pdu_readf(pdu
, proto_version
, "d", count
);
260 pdu
->size
- pdu
->offset
);
261 *data
= &pdu
->sdata
[pdu
->offset
];
266 int16_t *nwname
= va_arg(ap
, int16_t *);
267 char ***wnames
= va_arg(ap
, char ***);
269 errcode
= p9pdu_readf(pdu
, proto_version
,
273 kmalloc(sizeof(char *) * *nwname
,
282 for (i
= 0; i
< *nwname
; i
++) {
297 for (i
= 0; i
< *nwname
; i
++)
306 int16_t *nwqid
= va_arg(ap
, int16_t *);
307 struct p9_qid
**wqids
=
308 va_arg(ap
, struct p9_qid
**);
313 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
317 sizeof(struct p9_qid
),
326 for (i
= 0; i
< *nwqid
; i
++) {
344 struct p9_stat_dotl
*stbuf
=
345 va_arg(ap
, struct p9_stat_dotl
*);
347 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
349 p9pdu_readf(pdu
, proto_version
,
350 "qQdddqqqqqqqqqqqqqqq",
351 &stbuf
->st_result_mask
,
354 &stbuf
->st_uid
, &stbuf
->st_gid
,
356 &stbuf
->st_rdev
, &stbuf
->st_size
,
357 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
358 &stbuf
->st_atime_sec
,
359 &stbuf
->st_atime_nsec
,
360 &stbuf
->st_mtime_sec
,
361 &stbuf
->st_mtime_nsec
,
362 &stbuf
->st_ctime_sec
,
363 &stbuf
->st_ctime_nsec
,
364 &stbuf
->st_btime_sec
,
365 &stbuf
->st_btime_nsec
,
367 &stbuf
->st_data_version
);
371 if ((proto_version
!= p9_proto_2000u
) &&
372 (proto_version
!= p9_proto_2000L
))
388 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
394 for (ptr
= fmt
; *ptr
; ptr
++) {
397 int8_t val
= va_arg(ap
, int);
398 if (pdu_write(pdu
, &val
, sizeof(val
)))
403 __le16 val
= cpu_to_le16(va_arg(ap
, int));
404 if (pdu_write(pdu
, &val
, sizeof(val
)))
409 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
410 if (pdu_write(pdu
, &val
, sizeof(val
)))
415 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
416 if (pdu_write(pdu
, &val
, sizeof(val
)))
421 const char *sptr
= va_arg(ap
, const char *);
424 len
= MIN(strlen(sptr
), USHRT_MAX
);
426 errcode
= p9pdu_writef(pdu
, proto_version
,
428 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
433 const struct p9_qid
*qid
=
434 va_arg(ap
, const struct p9_qid
*);
436 p9pdu_writef(pdu
, proto_version
, "bdq",
437 qid
->type
, qid
->version
,
441 const struct p9_wstat
*stbuf
=
442 va_arg(ap
, const struct p9_wstat
*);
444 p9pdu_writef(pdu
, proto_version
,
446 stbuf
->size
, stbuf
->type
,
447 stbuf
->dev
, &stbuf
->qid
,
448 stbuf
->mode
, stbuf
->atime
,
449 stbuf
->mtime
, stbuf
->length
,
450 stbuf
->name
, stbuf
->uid
,
451 stbuf
->gid
, stbuf
->muid
,
452 stbuf
->extension
, stbuf
->n_uid
,
453 stbuf
->n_gid
, stbuf
->n_muid
);
456 int32_t count
= va_arg(ap
, int32_t);
457 const void *data
= va_arg(ap
, const void *);
459 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
461 if (!errcode
&& pdu_write(pdu
, data
, count
))
466 int32_t count
= va_arg(ap
, int32_t);
467 const char __user
*udata
=
468 va_arg(ap
, const void __user
*);
469 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
471 if (!errcode
&& pdu_write_u(pdu
, udata
, count
))
476 int16_t nwname
= va_arg(ap
, int);
477 const char **wnames
= va_arg(ap
, const char **);
479 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
484 for (i
= 0; i
< nwname
; i
++) {
497 int16_t nwqid
= va_arg(ap
, int);
498 struct p9_qid
*wqids
=
499 va_arg(ap
, struct p9_qid
*);
501 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
506 for (i
= 0; i
< nwqid
; i
++) {
519 struct p9_iattr_dotl
*p9attr
= va_arg(ap
,
520 struct p9_iattr_dotl
*);
522 errcode
= p9pdu_writef(pdu
, proto_version
,
536 if ((proto_version
!= p9_proto_2000u
) &&
537 (proto_version
!= p9_proto_2000L
))
552 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
558 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
565 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
571 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
577 int p9stat_read(char *buf
, int len
, struct p9_wstat
*st
, int proto_version
)
579 struct p9_fcall fake_pdu
;
583 fake_pdu
.capacity
= len
;
584 fake_pdu
.sdata
= buf
;
587 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "S", st
);
589 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
590 p9pdu_dump(1, &fake_pdu
);
595 EXPORT_SYMBOL(p9stat_read
);
597 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
599 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
602 int p9pdu_finalize(struct p9_fcall
*pdu
)
604 int size
= pdu
->size
;
608 err
= p9pdu_writef(pdu
, 0, "d", size
);
611 #ifdef CONFIG_NET_9P_DEBUG
612 if ((p9_debug_level
& P9_DEBUG_PKT
) == P9_DEBUG_PKT
)
616 P9_DPRINTK(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n", pdu
->size
,
622 void p9pdu_reset(struct p9_fcall
*pdu
)
628 int p9dirent_read(char *buf
, int len
, struct p9_dirent
*dirent
,
631 struct p9_fcall fake_pdu
;
636 fake_pdu
.capacity
= len
;
637 fake_pdu
.sdata
= buf
;
640 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "Qqbs", &dirent
->qid
,
641 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
643 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
644 p9pdu_dump(1, &fake_pdu
);
648 strcpy(dirent
->d_name
, nameptr
);
651 return fake_pdu
.offset
;
653 EXPORT_SYMBOL(p9dirent_read
);