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>
40 #include <trace/events/9p.h>
43 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...);
45 void p9stat_free(struct p9_wstat
*stbuf
)
51 kfree(stbuf
->extension
);
53 EXPORT_SYMBOL(p9stat_free
);
55 size_t pdu_read(struct p9_fcall
*pdu
, void *data
, size_t size
)
57 size_t len
= min(pdu
->size
- pdu
->offset
, size
);
58 memcpy(data
, &pdu
->sdata
[pdu
->offset
], len
);
63 static size_t pdu_write(struct p9_fcall
*pdu
, const void *data
, size_t size
)
65 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
66 memcpy(&pdu
->sdata
[pdu
->size
], data
, len
);
72 pdu_write_u(struct p9_fcall
*pdu
, const char __user
*udata
, size_t size
)
74 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
75 if (copy_from_user(&pdu
->sdata
[pdu
->size
], udata
, len
))
90 D - data blob (int32_t size followed by void *, results are not freed)
91 T - array of strings (int16_t count, followed by strings)
92 R - array of qids (int16_t count, followed by qids)
93 A - stat for 9p2000.L (p9_stat_dotl)
94 ? - if optional = 1, continue parsing
98 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
104 for (ptr
= fmt
; *ptr
; ptr
++) {
107 int8_t *val
= va_arg(ap
, int8_t *);
108 if (pdu_read(pdu
, val
, sizeof(*val
))) {
115 int16_t *val
= va_arg(ap
, int16_t *);
117 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
121 *val
= le16_to_cpu(le_val
);
125 int32_t *val
= va_arg(ap
, int32_t *);
127 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
131 *val
= le32_to_cpu(le_val
);
135 int64_t *val
= va_arg(ap
, int64_t *);
137 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
141 *val
= le64_to_cpu(le_val
);
145 char **sptr
= va_arg(ap
, char **);
148 errcode
= p9pdu_readf(pdu
, proto_version
,
153 *sptr
= kmalloc(len
+ 1, GFP_NOFS
);
158 if (pdu_read(pdu
, *sptr
, len
)) {
168 va_arg(ap
, struct p9_qid
*);
170 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
171 &qid
->type
, &qid
->version
,
176 struct p9_wstat
*stbuf
=
177 va_arg(ap
, struct p9_wstat
*);
179 memset(stbuf
, 0, sizeof(struct p9_wstat
));
180 stbuf
->n_uid
= stbuf
->n_gid
= stbuf
->n_muid
=
183 p9pdu_readf(pdu
, proto_version
,
185 &stbuf
->size
, &stbuf
->type
,
186 &stbuf
->dev
, &stbuf
->qid
,
187 &stbuf
->mode
, &stbuf
->atime
,
188 &stbuf
->mtime
, &stbuf
->length
,
189 &stbuf
->name
, &stbuf
->uid
,
190 &stbuf
->gid
, &stbuf
->muid
,
192 &stbuf
->n_uid
, &stbuf
->n_gid
,
199 uint32_t *count
= va_arg(ap
, uint32_t *);
200 void **data
= va_arg(ap
, void **);
203 p9pdu_readf(pdu
, proto_version
, "d", count
);
206 min_t(uint32_t, *count
,
207 pdu
->size
- pdu
->offset
);
208 *data
= &pdu
->sdata
[pdu
->offset
];
213 uint16_t *nwname
= va_arg(ap
, uint16_t *);
214 char ***wnames
= va_arg(ap
, char ***);
216 errcode
= p9pdu_readf(pdu
, proto_version
,
220 kmalloc(sizeof(char *) * *nwname
,
229 for (i
= 0; i
< *nwname
; i
++) {
244 for (i
= 0; i
< *nwname
; i
++)
253 int16_t *nwqid
= va_arg(ap
, int16_t *);
254 struct p9_qid
**wqids
=
255 va_arg(ap
, struct p9_qid
**);
260 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
264 sizeof(struct p9_qid
),
273 for (i
= 0; i
< *nwqid
; i
++) {
291 struct p9_stat_dotl
*stbuf
=
292 va_arg(ap
, struct p9_stat_dotl
*);
294 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
296 p9pdu_readf(pdu
, proto_version
,
297 "qQdddqqqqqqqqqqqqqqq",
298 &stbuf
->st_result_mask
,
301 &stbuf
->st_uid
, &stbuf
->st_gid
,
303 &stbuf
->st_rdev
, &stbuf
->st_size
,
304 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
305 &stbuf
->st_atime_sec
,
306 &stbuf
->st_atime_nsec
,
307 &stbuf
->st_mtime_sec
,
308 &stbuf
->st_mtime_nsec
,
309 &stbuf
->st_ctime_sec
,
310 &stbuf
->st_ctime_nsec
,
311 &stbuf
->st_btime_sec
,
312 &stbuf
->st_btime_nsec
,
314 &stbuf
->st_data_version
);
318 if ((proto_version
!= p9_proto_2000u
) &&
319 (proto_version
!= p9_proto_2000L
))
335 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
341 for (ptr
= fmt
; *ptr
; ptr
++) {
344 int8_t val
= va_arg(ap
, int);
345 if (pdu_write(pdu
, &val
, sizeof(val
)))
350 __le16 val
= cpu_to_le16(va_arg(ap
, int));
351 if (pdu_write(pdu
, &val
, sizeof(val
)))
356 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
357 if (pdu_write(pdu
, &val
, sizeof(val
)))
362 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
363 if (pdu_write(pdu
, &val
, sizeof(val
)))
368 const char *sptr
= va_arg(ap
, const char *);
371 len
= min_t(uint16_t, strlen(sptr
),
374 errcode
= p9pdu_writef(pdu
, proto_version
,
376 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
381 const struct p9_qid
*qid
=
382 va_arg(ap
, const struct p9_qid
*);
384 p9pdu_writef(pdu
, proto_version
, "bdq",
385 qid
->type
, qid
->version
,
389 const struct p9_wstat
*stbuf
=
390 va_arg(ap
, const struct p9_wstat
*);
392 p9pdu_writef(pdu
, proto_version
,
394 stbuf
->size
, stbuf
->type
,
395 stbuf
->dev
, &stbuf
->qid
,
396 stbuf
->mode
, stbuf
->atime
,
397 stbuf
->mtime
, stbuf
->length
,
398 stbuf
->name
, stbuf
->uid
,
399 stbuf
->gid
, stbuf
->muid
,
400 stbuf
->extension
, stbuf
->n_uid
,
401 stbuf
->n_gid
, stbuf
->n_muid
);
404 uint32_t count
= va_arg(ap
, uint32_t);
405 const void *data
= va_arg(ap
, const void *);
407 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
409 if (!errcode
&& pdu_write(pdu
, data
, count
))
414 int32_t count
= va_arg(ap
, int32_t);
415 const char __user
*udata
=
416 va_arg(ap
, const void __user
*);
417 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
419 if (!errcode
&& pdu_write_u(pdu
, udata
, count
))
424 uint16_t nwname
= va_arg(ap
, int);
425 const char **wnames
= va_arg(ap
, const char **);
427 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
432 for (i
= 0; i
< nwname
; i
++) {
445 int16_t nwqid
= va_arg(ap
, int);
446 struct p9_qid
*wqids
=
447 va_arg(ap
, struct p9_qid
*);
449 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
454 for (i
= 0; i
< nwqid
; i
++) {
467 struct p9_iattr_dotl
*p9attr
= va_arg(ap
,
468 struct p9_iattr_dotl
*);
470 errcode
= p9pdu_writef(pdu
, proto_version
,
484 if ((proto_version
!= p9_proto_2000u
) &&
485 (proto_version
!= p9_proto_2000L
))
500 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
506 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
513 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
519 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
525 int p9stat_read(struct p9_client
*clnt
, char *buf
, int len
, struct p9_wstat
*st
)
527 struct p9_fcall fake_pdu
;
531 fake_pdu
.capacity
= len
;
532 fake_pdu
.sdata
= buf
;
535 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "S", st
);
537 p9_debug(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
538 trace_9p_protocol_dump(clnt
, &fake_pdu
);
543 EXPORT_SYMBOL(p9stat_read
);
545 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
548 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
551 int p9pdu_finalize(struct p9_client
*clnt
, struct p9_fcall
*pdu
)
553 int size
= pdu
->size
;
557 err
= p9pdu_writef(pdu
, 0, "d", size
);
560 trace_9p_protocol_dump(clnt
, pdu
);
561 p9_debug(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n",
562 pdu
->size
, pdu
->id
, pdu
->tag
);
567 void p9pdu_reset(struct p9_fcall
*pdu
)
573 int p9dirent_read(struct p9_client
*clnt
, char *buf
, int len
,
574 struct p9_dirent
*dirent
)
576 struct p9_fcall fake_pdu
;
581 fake_pdu
.capacity
= len
;
582 fake_pdu
.sdata
= buf
;
585 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "Qqbs", &dirent
->qid
,
586 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
588 p9_debug(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
589 trace_9p_protocol_dump(clnt
, &fake_pdu
);
593 strcpy(dirent
->d_name
, nameptr
);
597 return fake_pdu
.offset
;
599 EXPORT_SYMBOL(p9dirent_read
);