1 // SPDX-License-Identifier: GPL-2.0-only
5 * 9P Protocol Support Code
7 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
9 * Base on code from Anthony Liguori <aliguori@us.ibm.com>
10 * Copyright (C) 2008 by IBM, Corp.
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/uaccess.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/stddef.h>
20 #include <linux/types.h>
21 #include <linux/uio.h>
22 #include <net/9p/9p.h>
23 #include <net/9p/client.h>
26 #include <trace/events/9p.h>
29 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...);
31 void p9stat_free(struct p9_wstat
*stbuf
)
41 kfree(stbuf
->extension
);
42 stbuf
->extension
= NULL
;
44 EXPORT_SYMBOL(p9stat_free
);
46 size_t pdu_read(struct p9_fcall
*pdu
, void *data
, size_t size
)
48 size_t len
= min(pdu
->size
- pdu
->offset
, size
);
49 memcpy(data
, &pdu
->sdata
[pdu
->offset
], len
);
54 static size_t pdu_write(struct p9_fcall
*pdu
, const void *data
, size_t size
)
56 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
57 memcpy(&pdu
->sdata
[pdu
->size
], data
, len
);
63 pdu_write_u(struct p9_fcall
*pdu
, struct iov_iter
*from
, size_t size
)
65 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
66 struct iov_iter i
= *from
;
67 if (!copy_from_iter_full(&pdu
->sdata
[pdu
->size
], len
, &i
))
84 D - data blob (int32_t size followed by void *, results are not freed)
85 T - array of strings (int16_t count, followed by strings)
86 R - array of qids (int16_t count, followed by qids)
87 A - stat for 9p2000.L (p9_stat_dotl)
88 ? - if optional = 1, continue parsing
92 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
98 for (ptr
= fmt
; *ptr
; ptr
++) {
101 int8_t *val
= va_arg(ap
, int8_t *);
102 if (pdu_read(pdu
, val
, sizeof(*val
))) {
109 int16_t *val
= va_arg(ap
, int16_t *);
111 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
115 *val
= le16_to_cpu(le_val
);
119 int32_t *val
= va_arg(ap
, int32_t *);
121 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
125 *val
= le32_to_cpu(le_val
);
129 int64_t *val
= va_arg(ap
, int64_t *);
131 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
135 *val
= le64_to_cpu(le_val
);
139 char **sptr
= va_arg(ap
, char **);
142 errcode
= p9pdu_readf(pdu
, proto_version
,
147 *sptr
= kmalloc(len
+ 1, GFP_NOFS
);
152 if (pdu_read(pdu
, *sptr
, len
)) {
161 kuid_t
*uid
= va_arg(ap
, kuid_t
*);
163 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
167 *uid
= make_kuid(&init_user_ns
,
168 le32_to_cpu(le_val
));
171 kgid_t
*gid
= va_arg(ap
, kgid_t
*);
173 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
177 *gid
= make_kgid(&init_user_ns
,
178 le32_to_cpu(le_val
));
182 va_arg(ap
, struct p9_qid
*);
184 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
185 &qid
->type
, &qid
->version
,
190 struct p9_wstat
*stbuf
=
191 va_arg(ap
, struct p9_wstat
*);
193 memset(stbuf
, 0, sizeof(struct p9_wstat
));
194 stbuf
->n_uid
= stbuf
->n_muid
= INVALID_UID
;
195 stbuf
->n_gid
= INVALID_GID
;
198 p9pdu_readf(pdu
, proto_version
,
200 &stbuf
->size
, &stbuf
->type
,
201 &stbuf
->dev
, &stbuf
->qid
,
202 &stbuf
->mode
, &stbuf
->atime
,
203 &stbuf
->mtime
, &stbuf
->length
,
204 &stbuf
->name
, &stbuf
->uid
,
205 &stbuf
->gid
, &stbuf
->muid
,
207 &stbuf
->n_uid
, &stbuf
->n_gid
,
214 uint32_t *count
= va_arg(ap
, uint32_t *);
215 void **data
= va_arg(ap
, void **);
218 p9pdu_readf(pdu
, proto_version
, "d", count
);
221 min_t(uint32_t, *count
,
222 pdu
->size
- pdu
->offset
);
223 *data
= &pdu
->sdata
[pdu
->offset
];
228 uint16_t *nwname
= va_arg(ap
, uint16_t *);
229 char ***wnames
= va_arg(ap
, char ***);
231 errcode
= p9pdu_readf(pdu
, proto_version
,
235 kmalloc_array(*nwname
,
245 for (i
= 0; i
< *nwname
; i
++) {
260 for (i
= 0; i
< *nwname
; i
++)
269 uint16_t *nwqid
= va_arg(ap
, uint16_t *);
270 struct p9_qid
**wqids
=
271 va_arg(ap
, struct p9_qid
**);
276 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
279 kmalloc_array(*nwqid
,
280 sizeof(struct p9_qid
),
289 for (i
= 0; i
< *nwqid
; i
++) {
307 struct p9_stat_dotl
*stbuf
=
308 va_arg(ap
, struct p9_stat_dotl
*);
310 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
312 p9pdu_readf(pdu
, proto_version
,
313 "qQdugqqqqqqqqqqqqqqq",
314 &stbuf
->st_result_mask
,
317 &stbuf
->st_uid
, &stbuf
->st_gid
,
319 &stbuf
->st_rdev
, &stbuf
->st_size
,
320 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
321 &stbuf
->st_atime_sec
,
322 &stbuf
->st_atime_nsec
,
323 &stbuf
->st_mtime_sec
,
324 &stbuf
->st_mtime_nsec
,
325 &stbuf
->st_ctime_sec
,
326 &stbuf
->st_ctime_nsec
,
327 &stbuf
->st_btime_sec
,
328 &stbuf
->st_btime_nsec
,
330 &stbuf
->st_data_version
);
334 if ((proto_version
!= p9_proto_2000u
) &&
335 (proto_version
!= p9_proto_2000L
))
351 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
357 for (ptr
= fmt
; *ptr
; ptr
++) {
360 int8_t val
= va_arg(ap
, int);
361 if (pdu_write(pdu
, &val
, sizeof(val
)))
366 __le16 val
= cpu_to_le16(va_arg(ap
, int));
367 if (pdu_write(pdu
, &val
, sizeof(val
)))
372 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
373 if (pdu_write(pdu
, &val
, sizeof(val
)))
378 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
379 if (pdu_write(pdu
, &val
, sizeof(val
)))
384 const char *sptr
= va_arg(ap
, const char *);
387 len
= min_t(size_t, strlen(sptr
),
390 errcode
= p9pdu_writef(pdu
, proto_version
,
392 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
397 kuid_t uid
= va_arg(ap
, kuid_t
);
398 __le32 val
= cpu_to_le32(
399 from_kuid(&init_user_ns
, uid
));
400 if (pdu_write(pdu
, &val
, sizeof(val
)))
404 kgid_t gid
= va_arg(ap
, kgid_t
);
405 __le32 val
= cpu_to_le32(
406 from_kgid(&init_user_ns
, gid
));
407 if (pdu_write(pdu
, &val
, sizeof(val
)))
411 const struct p9_qid
*qid
=
412 va_arg(ap
, const struct p9_qid
*);
414 p9pdu_writef(pdu
, proto_version
, "bdq",
415 qid
->type
, qid
->version
,
419 const struct p9_wstat
*stbuf
=
420 va_arg(ap
, const struct p9_wstat
*);
422 p9pdu_writef(pdu
, proto_version
,
424 stbuf
->size
, stbuf
->type
,
425 stbuf
->dev
, &stbuf
->qid
,
426 stbuf
->mode
, stbuf
->atime
,
427 stbuf
->mtime
, stbuf
->length
,
428 stbuf
->name
, stbuf
->uid
,
429 stbuf
->gid
, stbuf
->muid
,
430 stbuf
->extension
, stbuf
->n_uid
,
431 stbuf
->n_gid
, stbuf
->n_muid
);
434 uint32_t count
= va_arg(ap
, uint32_t);
435 struct iov_iter
*from
=
436 va_arg(ap
, struct iov_iter
*);
437 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
439 if (!errcode
&& pdu_write_u(pdu
, from
, count
))
444 uint16_t nwname
= va_arg(ap
, int);
445 const char **wnames
= va_arg(ap
, const char **);
447 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
452 for (i
= 0; i
< nwname
; i
++) {
465 uint16_t nwqid
= va_arg(ap
, int);
466 struct p9_qid
*wqids
=
467 va_arg(ap
, struct p9_qid
*);
469 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
474 for (i
= 0; i
< nwqid
; i
++) {
487 struct p9_iattr_dotl
*p9attr
= va_arg(ap
,
488 struct p9_iattr_dotl
*);
490 errcode
= p9pdu_writef(pdu
, proto_version
,
504 if ((proto_version
!= p9_proto_2000u
) &&
505 (proto_version
!= p9_proto_2000L
))
520 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
526 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
533 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
539 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
545 int p9stat_read(struct p9_client
*clnt
, char *buf
, int len
, struct p9_wstat
*st
)
547 struct p9_fcall fake_pdu
;
551 fake_pdu
.capacity
= len
;
552 fake_pdu
.sdata
= buf
;
555 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "S", st
);
557 p9_debug(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
558 trace_9p_protocol_dump(clnt
, &fake_pdu
);
562 return fake_pdu
.offset
;
564 EXPORT_SYMBOL(p9stat_read
);
566 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
569 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
572 int p9pdu_finalize(struct p9_client
*clnt
, struct p9_fcall
*pdu
)
574 int size
= pdu
->size
;
578 err
= p9pdu_writef(pdu
, 0, "d", size
);
581 trace_9p_protocol_dump(clnt
, pdu
);
582 p9_debug(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n",
583 pdu
->size
, pdu
->id
, pdu
->tag
);
588 void p9pdu_reset(struct p9_fcall
*pdu
)
594 int p9dirent_read(struct p9_client
*clnt
, char *buf
, int len
,
595 struct p9_dirent
*dirent
)
597 struct p9_fcall fake_pdu
;
602 fake_pdu
.capacity
= len
;
603 fake_pdu
.sdata
= buf
;
606 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "Qqbs", &dirent
->qid
,
607 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
609 p9_debug(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
610 trace_9p_protocol_dump(clnt
, &fake_pdu
);
614 ret
= strscpy(dirent
->d_name
, nameptr
, sizeof(dirent
->d_name
));
616 p9_debug(P9_DEBUG_ERROR
,
617 "On the wire dirent name too long: %s\n",
624 return fake_pdu
.offset
;
626 EXPORT_SYMBOL(p9dirent_read
);