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 <linux/uio.h>
37 #include <net/9p/9p.h>
38 #include <net/9p/client.h>
41 #include <trace/events/9p.h>
44 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...);
46 void p9stat_free(struct p9_wstat
*stbuf
)
52 kfree(stbuf
->extension
);
54 EXPORT_SYMBOL(p9stat_free
);
56 size_t pdu_read(struct p9_fcall
*pdu
, void *data
, size_t size
)
58 size_t len
= min(pdu
->size
- pdu
->offset
, size
);
59 memcpy(data
, &pdu
->sdata
[pdu
->offset
], len
);
64 static size_t pdu_write(struct p9_fcall
*pdu
, const void *data
, size_t size
)
66 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
67 memcpy(&pdu
->sdata
[pdu
->size
], data
, len
);
73 pdu_write_u(struct p9_fcall
*pdu
, struct iov_iter
*from
, size_t size
)
75 size_t len
= min(pdu
->capacity
- pdu
->size
, size
);
76 struct iov_iter i
= *from
;
77 if (!copy_from_iter_full(&pdu
->sdata
[pdu
->size
], len
, &i
))
94 D - data blob (int32_t size followed by void *, results are not freed)
95 T - array of strings (int16_t count, followed by strings)
96 R - array of qids (int16_t count, followed by qids)
97 A - stat for 9p2000.L (p9_stat_dotl)
98 ? - if optional = 1, continue parsing
102 p9pdu_vreadf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
108 for (ptr
= fmt
; *ptr
; ptr
++) {
111 int8_t *val
= va_arg(ap
, int8_t *);
112 if (pdu_read(pdu
, val
, sizeof(*val
))) {
119 int16_t *val
= va_arg(ap
, int16_t *);
121 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
125 *val
= le16_to_cpu(le_val
);
129 int32_t *val
= va_arg(ap
, int32_t *);
131 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
135 *val
= le32_to_cpu(le_val
);
139 int64_t *val
= va_arg(ap
, int64_t *);
141 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
145 *val
= le64_to_cpu(le_val
);
149 char **sptr
= va_arg(ap
, char **);
152 errcode
= p9pdu_readf(pdu
, proto_version
,
157 *sptr
= kmalloc(len
+ 1, GFP_NOFS
);
162 if (pdu_read(pdu
, *sptr
, len
)) {
171 kuid_t
*uid
= va_arg(ap
, kuid_t
*);
173 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
177 *uid
= make_kuid(&init_user_ns
,
178 le32_to_cpu(le_val
));
181 kgid_t
*gid
= va_arg(ap
, kgid_t
*);
183 if (pdu_read(pdu
, &le_val
, sizeof(le_val
))) {
187 *gid
= make_kgid(&init_user_ns
,
188 le32_to_cpu(le_val
));
192 va_arg(ap
, struct p9_qid
*);
194 errcode
= p9pdu_readf(pdu
, proto_version
, "bdq",
195 &qid
->type
, &qid
->version
,
200 struct p9_wstat
*stbuf
=
201 va_arg(ap
, struct p9_wstat
*);
203 memset(stbuf
, 0, sizeof(struct p9_wstat
));
204 stbuf
->n_uid
= stbuf
->n_muid
= INVALID_UID
;
205 stbuf
->n_gid
= INVALID_GID
;
208 p9pdu_readf(pdu
, proto_version
,
210 &stbuf
->size
, &stbuf
->type
,
211 &stbuf
->dev
, &stbuf
->qid
,
212 &stbuf
->mode
, &stbuf
->atime
,
213 &stbuf
->mtime
, &stbuf
->length
,
214 &stbuf
->name
, &stbuf
->uid
,
215 &stbuf
->gid
, &stbuf
->muid
,
217 &stbuf
->n_uid
, &stbuf
->n_gid
,
224 uint32_t *count
= va_arg(ap
, uint32_t *);
225 void **data
= va_arg(ap
, void **);
228 p9pdu_readf(pdu
, proto_version
, "d", count
);
231 min_t(uint32_t, *count
,
232 pdu
->size
- pdu
->offset
);
233 *data
= &pdu
->sdata
[pdu
->offset
];
238 uint16_t *nwname
= va_arg(ap
, uint16_t *);
239 char ***wnames
= va_arg(ap
, char ***);
241 errcode
= p9pdu_readf(pdu
, proto_version
,
245 kmalloc(sizeof(char *) * *nwname
,
254 for (i
= 0; i
< *nwname
; i
++) {
269 for (i
= 0; i
< *nwname
; i
++)
278 uint16_t *nwqid
= va_arg(ap
, uint16_t *);
279 struct p9_qid
**wqids
=
280 va_arg(ap
, struct p9_qid
**);
285 p9pdu_readf(pdu
, proto_version
, "w", nwqid
);
289 sizeof(struct p9_qid
),
298 for (i
= 0; i
< *nwqid
; i
++) {
316 struct p9_stat_dotl
*stbuf
=
317 va_arg(ap
, struct p9_stat_dotl
*);
319 memset(stbuf
, 0, sizeof(struct p9_stat_dotl
));
321 p9pdu_readf(pdu
, proto_version
,
322 "qQdugqqqqqqqqqqqqqqq",
323 &stbuf
->st_result_mask
,
326 &stbuf
->st_uid
, &stbuf
->st_gid
,
328 &stbuf
->st_rdev
, &stbuf
->st_size
,
329 &stbuf
->st_blksize
, &stbuf
->st_blocks
,
330 &stbuf
->st_atime_sec
,
331 &stbuf
->st_atime_nsec
,
332 &stbuf
->st_mtime_sec
,
333 &stbuf
->st_mtime_nsec
,
334 &stbuf
->st_ctime_sec
,
335 &stbuf
->st_ctime_nsec
,
336 &stbuf
->st_btime_sec
,
337 &stbuf
->st_btime_nsec
,
339 &stbuf
->st_data_version
);
343 if ((proto_version
!= p9_proto_2000u
) &&
344 (proto_version
!= p9_proto_2000L
))
360 p9pdu_vwritef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
,
366 for (ptr
= fmt
; *ptr
; ptr
++) {
369 int8_t val
= va_arg(ap
, int);
370 if (pdu_write(pdu
, &val
, sizeof(val
)))
375 __le16 val
= cpu_to_le16(va_arg(ap
, int));
376 if (pdu_write(pdu
, &val
, sizeof(val
)))
381 __le32 val
= cpu_to_le32(va_arg(ap
, int32_t));
382 if (pdu_write(pdu
, &val
, sizeof(val
)))
387 __le64 val
= cpu_to_le64(va_arg(ap
, int64_t));
388 if (pdu_write(pdu
, &val
, sizeof(val
)))
393 const char *sptr
= va_arg(ap
, const char *);
396 len
= min_t(size_t, strlen(sptr
),
399 errcode
= p9pdu_writef(pdu
, proto_version
,
401 if (!errcode
&& pdu_write(pdu
, sptr
, len
))
406 kuid_t uid
= va_arg(ap
, kuid_t
);
407 __le32 val
= cpu_to_le32(
408 from_kuid(&init_user_ns
, uid
));
409 if (pdu_write(pdu
, &val
, sizeof(val
)))
413 kgid_t gid
= va_arg(ap
, kgid_t
);
414 __le32 val
= cpu_to_le32(
415 from_kgid(&init_user_ns
, gid
));
416 if (pdu_write(pdu
, &val
, sizeof(val
)))
420 const struct p9_qid
*qid
=
421 va_arg(ap
, const struct p9_qid
*);
423 p9pdu_writef(pdu
, proto_version
, "bdq",
424 qid
->type
, qid
->version
,
428 const struct p9_wstat
*stbuf
=
429 va_arg(ap
, const struct p9_wstat
*);
431 p9pdu_writef(pdu
, proto_version
,
433 stbuf
->size
, stbuf
->type
,
434 stbuf
->dev
, &stbuf
->qid
,
435 stbuf
->mode
, stbuf
->atime
,
436 stbuf
->mtime
, stbuf
->length
,
437 stbuf
->name
, stbuf
->uid
,
438 stbuf
->gid
, stbuf
->muid
,
439 stbuf
->extension
, stbuf
->n_uid
,
440 stbuf
->n_gid
, stbuf
->n_muid
);
443 uint32_t count
= va_arg(ap
, uint32_t);
444 struct iov_iter
*from
=
445 va_arg(ap
, struct iov_iter
*);
446 errcode
= p9pdu_writef(pdu
, proto_version
, "d",
448 if (!errcode
&& pdu_write_u(pdu
, from
, count
))
453 uint16_t nwname
= va_arg(ap
, int);
454 const char **wnames
= va_arg(ap
, const char **);
456 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
461 for (i
= 0; i
< nwname
; i
++) {
474 uint16_t nwqid
= va_arg(ap
, int);
475 struct p9_qid
*wqids
=
476 va_arg(ap
, struct p9_qid
*);
478 errcode
= p9pdu_writef(pdu
, proto_version
, "w",
483 for (i
= 0; i
< nwqid
; i
++) {
496 struct p9_iattr_dotl
*p9attr
= va_arg(ap
,
497 struct p9_iattr_dotl
*);
499 errcode
= p9pdu_writef(pdu
, proto_version
,
513 if ((proto_version
!= p9_proto_2000u
) &&
514 (proto_version
!= p9_proto_2000L
))
529 int p9pdu_readf(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
535 ret
= p9pdu_vreadf(pdu
, proto_version
, fmt
, ap
);
542 p9pdu_writef(struct p9_fcall
*pdu
, int proto_version
, const char *fmt
, ...)
548 ret
= p9pdu_vwritef(pdu
, proto_version
, fmt
, ap
);
554 int p9stat_read(struct p9_client
*clnt
, char *buf
, int len
, struct p9_wstat
*st
)
556 struct p9_fcall fake_pdu
;
560 fake_pdu
.capacity
= len
;
561 fake_pdu
.sdata
= buf
;
564 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "S", st
);
566 p9_debug(P9_DEBUG_9P
, "<<< p9stat_read failed: %d\n", ret
);
567 trace_9p_protocol_dump(clnt
, &fake_pdu
);
572 EXPORT_SYMBOL(p9stat_read
);
574 int p9pdu_prepare(struct p9_fcall
*pdu
, int16_t tag
, int8_t type
)
577 return p9pdu_writef(pdu
, 0, "dbw", 0, type
, tag
);
580 int p9pdu_finalize(struct p9_client
*clnt
, struct p9_fcall
*pdu
)
582 int size
= pdu
->size
;
586 err
= p9pdu_writef(pdu
, 0, "d", size
);
589 trace_9p_protocol_dump(clnt
, pdu
);
590 p9_debug(P9_DEBUG_9P
, ">>> size=%d type: %d tag: %d\n",
591 pdu
->size
, pdu
->id
, pdu
->tag
);
596 void p9pdu_reset(struct p9_fcall
*pdu
)
602 int p9dirent_read(struct p9_client
*clnt
, char *buf
, int len
,
603 struct p9_dirent
*dirent
)
605 struct p9_fcall fake_pdu
;
610 fake_pdu
.capacity
= len
;
611 fake_pdu
.sdata
= buf
;
614 ret
= p9pdu_readf(&fake_pdu
, clnt
->proto_version
, "Qqbs", &dirent
->qid
,
615 &dirent
->d_off
, &dirent
->d_type
, &nameptr
);
617 p9_debug(P9_DEBUG_9P
, "<<< p9dirent_read failed: %d\n", ret
);
618 trace_9p_protocol_dump(clnt
, &fake_pdu
);
622 strcpy(dirent
->d_name
, nameptr
);
626 return fake_pdu
.offset
;
628 EXPORT_SYMBOL(p9dirent_read
);