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
))
92 D - data blob (int32_t size followed by void *, results are not freed)
93 T - array of strings (int16_t count, followed by strings)
94 R - array of qids (int16_t count, followed by qids)
95 A - stat for 9p2000.L (p9_stat_dotl)
96 ? - if optional = 1, continue parsing
100 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
106 for (ptr
= fmt
; *ptr
; ptr
++) {
109 int8_t *val
= va_arg(ap
, int8_t *);
110 if (pdu_read(pdu
, val
, sizeof(*val
))) {
117 int16_t *val
= va_arg(ap
, int16_t *);
119 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
123 *val
= le16_to_cpu(le_val
);
127 int32_t *val
= va_arg(ap
, int32_t *);
129 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
133 *val
= le32_to_cpu(le_val
);
137 int64_t *val
= va_arg(ap
, int64_t *);
139 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
143 *val
= le64_to_cpu(le_val
);
147 char **sptr
= va_arg(ap
, char **);
150 errcode
= p9pdu_readf(pdu
, proto_version
,
155 *sptr
= kmalloc(len
+ 1, GFP_NOFS
);
160 if (pdu_read(pdu
, *sptr
, len
)) {
169 kuid_t
*uid
= va_arg(ap
, kuid_t
*);
171 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
175 *uid
= make_kuid(&init_user_ns
,
176 le32_to_cpu(le_val
));
179 kgid_t
*gid
= va_arg(ap
, kgid_t
*);
181 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
185 *gid
= make_kgid(&init_user_ns
,
186 le32_to_cpu(le_val
));
190 va_arg(ap
, struct p9_qid
*);
192 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
193 &qid
->type
, &qid
->version
,
198 struct p9_wstat
*stbuf
=
199 va_arg(ap
, struct p9_wstat
*);
201 memset(stbuf
, 0, sizeof(struct p9_wstat
));
202 stbuf
->n_uid
= stbuf
->n_muid
= INVALID_UID
;
203 stbuf
->n_gid
= INVALID_GID
;
206 p9pdu_readf(pdu
, proto_version
,
208 &stbuf
->size
, &stbuf
->type
,
209 &stbuf
->dev
, &stbuf
->qid
,
210 &stbuf
->mode
, &stbuf
->atime
,
211 &stbuf
->mtime
, &stbuf
->length
,
212 &stbuf
->name
, &stbuf
->uid
,
213 &stbuf
->gid
, &stbuf
->muid
,
215 &stbuf
->n_uid
, &stbuf
->n_gid
,
222 uint32_t *count
= va_arg(ap
, uint32_t *);
223 void **data
= va_arg(ap
, void **);
226 p9pdu_readf(pdu
, proto_version
, "d", count
);
229 min_t(uint32_t, *count
,
230 pdu
->size
- pdu
->offset
);
231 *data
= &pdu
->sdata
[pdu
->offset
];
236 uint16_t *nwname
= va_arg(ap
, uint16_t *);
237 char ***wnames
= va_arg(ap
, char ***);
239 errcode
= p9pdu_readf(pdu
, proto_version
,
243 kmalloc(sizeof(char *) * *nwname
,
252 for (i
= 0; i
< *nwname
; i
++) {
267 for (i
= 0; i
< *nwname
; i
++)
276 int16_t *nwqid
= va_arg(ap
, int16_t *);
277 struct p9_qid
**wqids
=
278 va_arg(ap
, struct p9_qid
**);
283 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
287 sizeof(struct p9_qid
),
296 for (i
= 0; i
< *nwqid
; i
++) {
314 struct p9_stat_dotl
*stbuf
=
315 va_arg(ap
, struct p9_stat_dotl
*);
317 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
319 p9pdu_readf(pdu
, proto_version
,
320 "qQdugqqqqqqqqqqqqqqq",
321 &stbuf
->st_result_mask
,
324 &stbuf
->st_uid
, &stbuf
->st_gid
,
326 &stbuf
->st_rdev
, &stbuf
->st_size
,
327 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
328 &stbuf
->st_atime_sec
,
329 &stbuf
->st_atime_nsec
,
330 &stbuf
->st_mtime_sec
,
331 &stbuf
->st_mtime_nsec
,
332 &stbuf
->st_ctime_sec
,
333 &stbuf
->st_ctime_nsec
,
334 &stbuf
->st_btime_sec
,
335 &stbuf
->st_btime_nsec
,
337 &stbuf
->st_data_version
);
341 if ((proto_version
!= p9_proto_2000u
) &&
342 (proto_version
!= p9_proto_2000L
))
358 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
364 for (ptr
= fmt
; *ptr
; ptr
++) {
367 int8_t val
= va_arg(ap
, int);
368 if (pdu_write(pdu
, &val
, sizeof(val
)))
373 __le16 val
= cpu_to_le16(va_arg(ap
, int));
374 if (pdu_write(pdu
, &val
, sizeof(val
)))
379 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
380 if (pdu_write(pdu
, &val
, sizeof(val
)))
385 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
386 if (pdu_write(pdu
, &val
, sizeof(val
)))
391 const char *sptr
= va_arg(ap
, const char *);
394 len
= min_t(size_t, strlen(sptr
),
397 errcode
= p9pdu_writef(pdu
, proto_version
,
399 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
404 kuid_t uid
= va_arg(ap
, kuid_t
);
405 __le32 val
= cpu_to_le32(
406 from_kuid(&init_user_ns
, uid
));
407 if (pdu_write(pdu
, &val
, sizeof(val
)))
411 kgid_t gid
= va_arg(ap
, kgid_t
);
412 __le32 val
= cpu_to_le32(
413 from_kgid(&init_user_ns
, gid
));
414 if (pdu_write(pdu
, &val
, sizeof(val
)))
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 uint32_t count
= va_arg(ap
, uint32_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 uint16_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(struct p9_client
*clnt
, char *buf
, int len
, struct p9_wstat
*st
)
564 struct p9_fcall fake_pdu
;
568 fake_pdu
.capacity
= len
;
569 fake_pdu
.sdata
= buf
;
572 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "S", st
);
574 p9_debug(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
575 trace_9p_protocol_dump(clnt
, &fake_pdu
);
580 EXPORT_SYMBOL(p9stat_read
);
582 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
585 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
588 int p9pdu_finalize(struct p9_client
*clnt
, struct p9_fcall
*pdu
)
590 int size
= pdu
->size
;
594 err
= p9pdu_writef(pdu
, 0, "d", size
);
597 trace_9p_protocol_dump(clnt
, pdu
);
598 p9_debug(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n",
599 pdu
->size
, pdu
->id
, pdu
->tag
);
604 void p9pdu_reset(struct p9_fcall
*pdu
)
610 int p9dirent_read(struct p9_client
*clnt
, char *buf
, int len
,
611 struct p9_dirent
*dirent
)
613 struct p9_fcall fake_pdu
;
618 fake_pdu
.capacity
= len
;
619 fake_pdu
.sdata
= buf
;
622 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "Qqbs", &dirent
->qid
,
623 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
625 p9_debug(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
626 trace_9p_protocol_dump(clnt
, &fake_pdu
);
630 strcpy(dirent
->d_name
, nameptr
);
634 return fake_pdu
.offset
;
636 EXPORT_SYMBOL(p9dirent_read
);