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 <net/9p/9p.h>
31 #include <net/9p/client.h>
35 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
39 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
43 #define offset_of(type, memb) \
44 ((unsigned long)(&((type *)0)->memb))
47 #define container_of(obj, type, memb) \
48 ((type *)(((char *)obj) - offset_of(type, memb)))
52 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...);
54 void p9stat_free(struct p9_wstat *stbuf)
60 kfree(stbuf->extension);
62 EXPORT_SYMBOL(p9stat_free);
64 static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
66 size_t len = MIN(pdu->size - pdu->offset, size);
67 memcpy(data, &pdu->sdata[pdu->offset], len);
72 static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
74 size_t len = MIN(pdu->capacity - pdu->size, size);
75 memcpy(&pdu->sdata[pdu->size], data, len);
88 D - data blob (int32_t size followed by void *, results are not freed)
89 T - array of strings (int16_t count, followed by strings)
90 R - array of qids (int16_t count, followed by qids)
91 ? - if optional = 1, continue parsing
95 p9pdu_vreadf(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
100 for (ptr = fmt; *ptr; ptr++) {
103 int8_t *val = va_arg(ap, int8_t *);
104 if (pdu_read(pdu, val, sizeof(*val))) {
111 int16_t *val = va_arg(ap, int16_t *);
112 if (pdu_read(pdu, val, sizeof(*val))) {
116 *val = cpu_to_le16(*val);
120 int32_t *val = va_arg(ap, int32_t *);
121 if (pdu_read(pdu, val, sizeof(*val))) {
125 *val = cpu_to_le32(*val);
129 int64_t *val = va_arg(ap, int64_t *);
130 if (pdu_read(pdu, val, sizeof(*val))) {
134 *val = cpu_to_le64(*val);
138 char **ptr = va_arg(ap, char **);
142 errcode = p9pdu_readf(pdu, optional, "w", &len);
148 *ptr = kmalloc(size + 1, GFP_KERNEL);
153 if (pdu_read(pdu, *ptr, size)) {
163 va_arg(ap, struct p9_qid *);
165 errcode = p9pdu_readf(pdu, optional, "bdq",
166 &qid->type, &qid->version,
171 struct p9_wstat *stbuf =
172 va_arg(ap, struct p9_wstat *);
174 stbuf->extension = NULL;
175 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
179 p9pdu_readf(pdu, optional,
181 &stbuf->size, &stbuf->type,
182 &stbuf->dev, &stbuf->qid,
183 &stbuf->mode, &stbuf->atime,
184 &stbuf->mtime, &stbuf->length,
185 &stbuf->name, &stbuf->uid,
186 &stbuf->gid, &stbuf->muid,
188 &stbuf->n_uid, &stbuf->n_gid,
195 int32_t *count = va_arg(ap, int32_t *);
196 void **data = va_arg(ap, void **);
199 p9pdu_readf(pdu, optional, "d", count);
203 pdu->size - pdu->offset);
204 *data = &pdu->sdata[pdu->offset];
209 int16_t *nwname = va_arg(ap, int16_t *);
210 char ***wnames = va_arg(ap, char ***);
213 p9pdu_readf(pdu, optional, "w", nwname);
216 kmalloc(sizeof(char *) * *nwname,
225 for (i = 0; i < *nwname; i++) {
227 p9pdu_readf(pdu, optional,
239 for (i = 0; i < *nwname; i++)
248 int16_t *nwqid = va_arg(ap, int16_t *);
249 struct p9_qid **wqids =
250 va_arg(ap, struct p9_qid **);
255 p9pdu_readf(pdu, optional, "w", nwqid);
259 sizeof(struct p9_qid),
268 for (i = 0; i < *nwqid; i++) {
270 p9pdu_readf(pdu, optional,
301 p9pdu_vwritef(struct p9_fcall *pdu, int optional, const char *fmt, va_list ap)
306 for (ptr = fmt; *ptr; ptr++) {
309 int8_t val = va_arg(ap, int);
310 if (pdu_write(pdu, &val, sizeof(val)))
315 int16_t val = va_arg(ap, int);
316 if (pdu_write(pdu, &val, sizeof(val)))
321 int32_t val = va_arg(ap, int32_t);
322 if (pdu_write(pdu, &val, sizeof(val)))
327 int64_t val = va_arg(ap, int64_t);
328 if (pdu_write(pdu, &val, sizeof(val)))
333 const char *ptr = va_arg(ap, const char *);
337 len = MIN(strlen(ptr), USHORT_MAX);
339 errcode = p9pdu_writef(pdu, optional, "w", len);
340 if (!errcode && pdu_write(pdu, ptr, len))
345 const struct p9_qid *qid =
346 va_arg(ap, const struct p9_qid *);
348 p9pdu_writef(pdu, optional, "bdq",
349 qid->type, qid->version,
353 const struct p9_wstat *stbuf =
354 va_arg(ap, const struct p9_wstat *);
356 p9pdu_writef(pdu, optional,
358 stbuf->size, stbuf->type,
359 stbuf->dev, stbuf->qid,
360 stbuf->mode, stbuf->atime,
361 stbuf->mtime, stbuf->length,
362 stbuf->name, stbuf->uid,
363 stbuf->gid, stbuf->muid,
364 stbuf->extension, stbuf->n_uid,
365 stbuf->n_gid, stbuf->n_muid);
368 int32_t count = va_arg(ap, int32_t);
369 const void *data = va_arg(ap, const void *);
372 p9pdu_writef(pdu, optional, "d", count);
373 if (!errcode && pdu_write(pdu, data, count))
378 int16_t nwname = va_arg(ap, int);
379 const char **wnames = va_arg(ap, const char **);
382 p9pdu_writef(pdu, optional, "w", nwname);
386 for (i = 0; i < nwname; i++) {
388 p9pdu_writef(pdu, optional,
398 int16_t nwqid = va_arg(ap, int);
399 struct p9_qid *wqids =
400 va_arg(ap, struct p9_qid *);
403 p9pdu_writef(pdu, optional, "w", nwqid);
407 for (i = 0; i < nwqid; i++) {
409 p9pdu_writef(pdu, optional,
434 int p9pdu_readf(struct p9_fcall *pdu, int optional, const char *fmt, ...)
440 ret = p9pdu_vreadf(pdu, optional, fmt, ap);
447 p9pdu_writef(struct p9_fcall *pdu, int optional, const char *fmt, ...)
453 ret = p9pdu_vwritef(pdu, optional, fmt, ap);