]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - 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/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
)
48 u8
*data
= pdu
->sdata
;
49 int datalen
= pdu
->size
;
54 if (datalen
> (buflen
-16))
57 n
+= scnprintf(buf
+ n
, buflen
- n
, "%02x ", data
[i
]);
59 n
+= scnprintf(buf
+ n
, buflen
- n
, " ");
61 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
65 n
+= scnprintf(buf
+ n
, buflen
- n
, "\n");
68 P9_DPRINTK(P9_DEBUG_PKT
, "[[[(%d) %s\n", datalen
, buf
);
70 P9_DPRINTK(P9_DEBUG_PKT
, "]]](%d) %s\n", datalen
, buf
);
74 p9pdu_dump(int way
, struct p9_fcall
*pdu
)
78 EXPORT_SYMBOL(p9pdu_dump
);
80 void p9stat_free(struct p9_wstat
*stbuf
)
86 kfree(stbuf
->extension
);
88 EXPORT_SYMBOL(p9stat_free
);
90 static size_t pdu_read(struct p9_fcall
*pdu
, void *data
, size_t size
)
92 size_t len
= min(pdu
->size
- pdu
->offset
, size
);
93 memcpy(data
, &pdu
->sdata
[pdu
->offset
], len
);
98 static size_t pdu_write(struct p9_fcall
*pdu
, const void *data
, size_t size
)
100 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
101 memcpy(&pdu
->sdata
[pdu
->size
], data
, len
);
107 pdu_write_u(struct p9_fcall
*pdu
, const char __user
*udata
, size_t size
)
109 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
110 if (copy_from_user(&pdu
->sdata
[pdu
->size
], udata
, len
))
125 D - data blob (int32_t size followed by void *, results are not freed)
126 T - array of strings (int16_t count, followed by strings)
127 R - array of qids (int16_t count, followed by qids)
128 A - stat for 9p2000.L (p9_stat_dotl)
129 ? - if optional = 1, continue parsing
133 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
139 for (ptr
= fmt
; *ptr
; ptr
++) {
142 int8_t *val
= va_arg(ap
, int8_t *);
143 if (pdu_read(pdu
, val
, sizeof(*val
))) {
150 int16_t *val
= va_arg(ap
, int16_t *);
152 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
156 *val
= le16_to_cpu(le_val
);
160 int32_t *val
= va_arg(ap
, int32_t *);
162 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
166 *val
= le32_to_cpu(le_val
);
170 int64_t *val
= va_arg(ap
, int64_t *);
172 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
176 *val
= le64_to_cpu(le_val
);
180 char **sptr
= va_arg(ap
, char **);
184 errcode
= p9pdu_readf(pdu
, proto_version
,
189 size
= max_t(int16_t, len
, 0);
191 *sptr
= kmalloc(size
+ 1, GFP_KERNEL
);
196 if (pdu_read(pdu
, *sptr
, size
)) {
206 va_arg(ap
, struct p9_qid
*);
208 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
209 &qid
->type
, &qid
->version
,
214 struct p9_wstat
*stbuf
=
215 va_arg(ap
, struct p9_wstat
*);
217 memset(stbuf
, 0, sizeof(struct p9_wstat
));
218 stbuf
->n_uid
= stbuf
->n_gid
= stbuf
->n_muid
=
221 p9pdu_readf(pdu
, proto_version
,
223 &stbuf
->size
, &stbuf
->type
,
224 &stbuf
->dev
, &stbuf
->qid
,
225 &stbuf
->mode
, &stbuf
->atime
,
226 &stbuf
->mtime
, &stbuf
->length
,
227 &stbuf
->name
, &stbuf
->uid
,
228 &stbuf
->gid
, &stbuf
->muid
,
230 &stbuf
->n_uid
, &stbuf
->n_gid
,
237 int32_t *count
= va_arg(ap
, int32_t *);
238 void **data
= va_arg(ap
, void **);
241 p9pdu_readf(pdu
, proto_version
, "d", count
);
244 min_t(int32_t, *count
,
245 pdu
->size
- pdu
->offset
);
246 *data
= &pdu
->sdata
[pdu
->offset
];
251 int16_t *nwname
= va_arg(ap
, int16_t *);
252 char ***wnames
= va_arg(ap
, char ***);
254 errcode
= p9pdu_readf(pdu
, proto_version
,
258 kmalloc(sizeof(char *) * *nwname
,
267 for (i
= 0; i
< *nwname
; i
++) {
282 for (i
= 0; i
< *nwname
; i
++)
291 int16_t *nwqid
= va_arg(ap
, int16_t *);
292 struct p9_qid
**wqids
=
293 va_arg(ap
, struct p9_qid
**);
298 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
302 sizeof(struct p9_qid
),
311 for (i
= 0; i
< *nwqid
; i
++) {
329 struct p9_stat_dotl
*stbuf
=
330 va_arg(ap
, struct p9_stat_dotl
*);
332 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
334 p9pdu_readf(pdu
, proto_version
,
335 "qQdddqqqqqqqqqqqqqqq",
336 &stbuf
->st_result_mask
,
339 &stbuf
->st_uid
, &stbuf
->st_gid
,
341 &stbuf
->st_rdev
, &stbuf
->st_size
,
342 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
343 &stbuf
->st_atime_sec
,
344 &stbuf
->st_atime_nsec
,
345 &stbuf
->st_mtime_sec
,
346 &stbuf
->st_mtime_nsec
,
347 &stbuf
->st_ctime_sec
,
348 &stbuf
->st_ctime_nsec
,
349 &stbuf
->st_btime_sec
,
350 &stbuf
->st_btime_nsec
,
352 &stbuf
->st_data_version
);
356 if ((proto_version
!= p9_proto_2000u
) &&
357 (proto_version
!= p9_proto_2000L
))
373 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
379 for (ptr
= fmt
; *ptr
; ptr
++) {
382 int8_t val
= va_arg(ap
, int);
383 if (pdu_write(pdu
, &val
, sizeof(val
)))
388 __le16 val
= cpu_to_le16(va_arg(ap
, int));
389 if (pdu_write(pdu
, &val
, sizeof(val
)))
394 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
395 if (pdu_write(pdu
, &val
, sizeof(val
)))
400 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
401 if (pdu_write(pdu
, &val
, sizeof(val
)))
406 const char *sptr
= va_arg(ap
, const char *);
409 len
= min_t(int16_t, strlen(sptr
), USHRT_MAX
);
411 errcode
= p9pdu_writef(pdu
, proto_version
,
413 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
418 const struct p9_qid
*qid
=
419 va_arg(ap
, const struct p9_qid
*);
421 p9pdu_writef(pdu
, proto_version
, "bdq",
422 qid
->type
, qid
->version
,
426 const struct p9_wstat
*stbuf
=
427 va_arg(ap
, const struct p9_wstat
*);
429 p9pdu_writef(pdu
, proto_version
,
431 stbuf
->size
, stbuf
->type
,
432 stbuf
->dev
, &stbuf
->qid
,
433 stbuf
->mode
, stbuf
->atime
,
434 stbuf
->mtime
, stbuf
->length
,
435 stbuf
->name
, stbuf
->uid
,
436 stbuf
->gid
, stbuf
->muid
,
437 stbuf
->extension
, stbuf
->n_uid
,
438 stbuf
->n_gid
, stbuf
->n_muid
);
441 int32_t count
= va_arg(ap
, int32_t);
442 const void *data
= va_arg(ap
, const void *);
444 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
446 if (!errcode
&& pdu_write(pdu
, data
, count
))
451 int32_t count
= va_arg(ap
, int32_t);
452 const char __user
*udata
=
453 va_arg(ap
, const void __user
*);
454 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
456 if (!errcode
&& pdu_write_u(pdu
, udata
, count
))
461 int16_t nwname
= va_arg(ap
, int);
462 const char **wnames
= va_arg(ap
, const char **);
464 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
469 for (i
= 0; i
< nwname
; i
++) {
482 int16_t nwqid
= va_arg(ap
, int);
483 struct p9_qid
*wqids
=
484 va_arg(ap
, struct p9_qid
*);
486 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
491 for (i
= 0; i
< nwqid
; i
++) {
504 struct p9_iattr_dotl
*p9attr
= va_arg(ap
,
505 struct p9_iattr_dotl
*);
507 errcode
= p9pdu_writef(pdu
, proto_version
,
521 if ((proto_version
!= p9_proto_2000u
) &&
522 (proto_version
!= p9_proto_2000L
))
537 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
543 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
550 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
556 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
562 int p9stat_read(char *buf
, int len
, struct p9_wstat
*st
, int proto_version
)
564 struct p9_fcall fake_pdu
;
568 fake_pdu
.capacity
= len
;
569 fake_pdu
.sdata
= buf
;
572 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "S", st
);
574 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
575 p9pdu_dump(1, &fake_pdu
);
580 EXPORT_SYMBOL(p9stat_read
);
582 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
584 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
587 int p9pdu_finalize(struct p9_fcall
*pdu
)
589 int size
= pdu
->size
;
593 err
= p9pdu_writef(pdu
, 0, "d", size
);
596 #ifdef CONFIG_NET_9P_DEBUG
597 if ((p9_debug_level
& P9_DEBUG_PKT
) == P9_DEBUG_PKT
)
601 P9_DPRINTK(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n", pdu
->size
,
607 void p9pdu_reset(struct p9_fcall
*pdu
)
613 int p9dirent_read(char *buf
, int len
, struct p9_dirent
*dirent
,
616 struct p9_fcall fake_pdu
;
621 fake_pdu
.capacity
= len
;
622 fake_pdu
.sdata
= buf
;
625 ret
= p9pdu_readf(&fake_pdu
, proto_version
, "Qqbs", &dirent
->qid
,
626 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
628 P9_DPRINTK(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
629 p9pdu_dump(1, &fake_pdu
);
633 strcpy(dirent
->d_name
, nameptr
);
636 return fake_pdu
.offset
;
638 EXPORT_SYMBOL(p9dirent_read
);