blob: 798beac7f100271f0ca4607fe4b3b1fee3fc1d34 [file] [log] [blame]
Eric Van Hensbergenace51c42008-10-13 20:40:27 -05001/*
2 * net/9p/protocol.c
3 *
4 * 9P Protocol Support Code
5 *
6 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
7 *
8 * Base on code from Anthony Liguori <aliguori@us.ibm.com>
9 * Copyright (C) 2008 by IBM, Corp.
10 *
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.
14 *
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.
19 *
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
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/errno.h>
Thiago Farina01b0c5c2010-12-04 15:22:46 +000030#include <linux/kernel.h>
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050031#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -050033#include <linux/sched.h>
Thiago Farina01b0c5c2010-12-04 15:22:46 +000034#include <linux/stddef.h>
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -080035#include <linux/types.h>
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050036#include <net/9p/9p.h>
37#include <net/9p/client.h>
38#include "protocol.h"
39
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050040static int
Sripathi Kodi342fee12010-03-05 18:50:14 +000041p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050042
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -050043#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050044void
45p9pdu_dump(int way, struct p9_fcall *pdu)
46{
47 int i, n;
48 u8 *data = pdu->sdata;
49 int datalen = pdu->size;
50 char buf[255];
51 int buflen = 255;
52
53 i = n = 0;
54 if (datalen > (buflen-16))
55 datalen = buflen-16;
56 while (i < datalen) {
57 n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
58 if (i%4 == 3)
59 n += scnprintf(buf + n, buflen - n, " ");
60 if (i%32 == 31)
61 n += scnprintf(buf + n, buflen - n, "\n");
62
63 i++;
64 }
65 n += scnprintf(buf + n, buflen - n, "\n");
66
67 if (way)
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -050068 P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050069 else
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -050070 P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050071}
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -050072#else
73void
74p9pdu_dump(int way, struct p9_fcall *pdu)
75{
76}
77#endif
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050078EXPORT_SYMBOL(p9pdu_dump);
79
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050080void p9stat_free(struct p9_wstat *stbuf)
81{
82 kfree(stbuf->name);
83 kfree(stbuf->uid);
84 kfree(stbuf->gid);
85 kfree(stbuf->muid);
86 kfree(stbuf->extension);
87}
88EXPORT_SYMBOL(p9stat_free);
89
90static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
91{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000092 size_t len = min(pdu->size - pdu->offset, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050093 memcpy(data, &pdu->sdata[pdu->offset], len);
94 pdu->offset += len;
95 return size - len;
96}
97
98static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
99{
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000100 size_t len = min(pdu->capacity - pdu->size, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500101 memcpy(&pdu->sdata[pdu->size], data, len);
102 pdu->size += len;
103 return size - len;
104}
105
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500106static size_t
107pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
108{
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000109 size_t len = min(pdu->capacity - pdu->size, size);
Aneesh Kumar K.V7b3bb3f2010-10-19 09:17:02 +0530110 if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
111 len = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500112
113 pdu->size += len;
114 return size - len;
115}
116
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500117/*
118 b - int8_t
119 w - int16_t
120 d - int32_t
121 q - int64_t
122 s - string
123 S - stat
124 Q - qid
125 D - data blob (int32_t size followed by void *, results are not freed)
126 T - array of strings (int16_t count, followed by strings)
127 R - array of qids (int16_t count, followed by qids)
Sripathi Kodif0853122010-07-12 20:07:23 +0530128 A - stat for 9p2000.L (p9_stat_dotl)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500129 ? - if optional = 1, continue parsing
130*/
131
132static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000133p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
134 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500135{
136 const char *ptr;
137 int errcode = 0;
138
139 for (ptr = fmt; *ptr; ptr++) {
140 switch (*ptr) {
141 case 'b':{
142 int8_t *val = va_arg(ap, int8_t *);
143 if (pdu_read(pdu, val, sizeof(*val))) {
144 errcode = -EFAULT;
145 break;
146 }
147 }
148 break;
149 case 'w':{
150 int16_t *val = va_arg(ap, int16_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800151 __le16 le_val;
152 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500153 errcode = -EFAULT;
154 break;
155 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800156 *val = le16_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500157 }
158 break;
159 case 'd':{
160 int32_t *val = va_arg(ap, int32_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800161 __le32 le_val;
162 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500163 errcode = -EFAULT;
164 break;
165 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800166 *val = le32_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500167 }
168 break;
169 case 'q':{
170 int64_t *val = va_arg(ap, int64_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800171 __le64 le_val;
172 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500173 errcode = -EFAULT;
174 break;
175 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800176 *val = le64_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500177 }
178 break;
179 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500180 char **sptr = va_arg(ap, char **);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500181 int16_t len;
182 int size;
183
Sripathi Kodi342fee12010-03-05 18:50:14 +0000184 errcode = p9pdu_readf(pdu, proto_version,
185 "w", &len);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500186 if (errcode)
187 break;
188
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000189 size = max_t(int16_t, len, 0);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500190
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500191 *sptr = kmalloc(size + 1, GFP_KERNEL);
192 if (*sptr == NULL) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500193 errcode = -EFAULT;
194 break;
195 }
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500196 if (pdu_read(pdu, *sptr, size)) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500197 errcode = -EFAULT;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500198 kfree(*sptr);
199 *sptr = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500200 } else
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500201 (*sptr)[size] = 0;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500202 }
203 break;
204 case 'Q':{
205 struct p9_qid *qid =
206 va_arg(ap, struct p9_qid *);
207
Sripathi Kodi342fee12010-03-05 18:50:14 +0000208 errcode = p9pdu_readf(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500209 &qid->type, &qid->version,
210 &qid->path);
211 }
212 break;
213 case 'S':{
214 struct p9_wstat *stbuf =
215 va_arg(ap, struct p9_wstat *);
216
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500217 memset(stbuf, 0, sizeof(struct p9_wstat));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500218 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500219 -1;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500220 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000221 p9pdu_readf(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500222 "wwdQdddqssss?sddd",
223 &stbuf->size, &stbuf->type,
224 &stbuf->dev, &stbuf->qid,
225 &stbuf->mode, &stbuf->atime,
226 &stbuf->mtime, &stbuf->length,
227 &stbuf->name, &stbuf->uid,
228 &stbuf->gid, &stbuf->muid,
229 &stbuf->extension,
230 &stbuf->n_uid, &stbuf->n_gid,
231 &stbuf->n_muid);
232 if (errcode)
233 p9stat_free(stbuf);
234 }
235 break;
236 case 'D':{
237 int32_t *count = va_arg(ap, int32_t *);
238 void **data = va_arg(ap, void **);
239
240 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000241 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500242 if (!errcode) {
243 *count =
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000244 min_t(int32_t, *count,
245 pdu->size - pdu->offset);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500246 *data = &pdu->sdata[pdu->offset];
247 }
248 }
249 break;
250 case 'T':{
251 int16_t *nwname = va_arg(ap, int16_t *);
252 char ***wnames = va_arg(ap, char ***);
253
Sripathi Kodi342fee12010-03-05 18:50:14 +0000254 errcode = p9pdu_readf(pdu, proto_version,
255 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500256 if (!errcode) {
257 *wnames =
258 kmalloc(sizeof(char *) * *nwname,
259 GFP_KERNEL);
260 if (!*wnames)
261 errcode = -ENOMEM;
262 }
263
264 if (!errcode) {
265 int i;
266
267 for (i = 0; i < *nwname; i++) {
268 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000269 p9pdu_readf(pdu,
270 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500271 "s",
272 &(*wnames)[i]);
273 if (errcode)
274 break;
275 }
276 }
277
278 if (errcode) {
279 if (*wnames) {
280 int i;
281
282 for (i = 0; i < *nwname; i++)
283 kfree((*wnames)[i]);
284 }
285 kfree(*wnames);
286 *wnames = NULL;
287 }
288 }
289 break;
290 case 'R':{
291 int16_t *nwqid = va_arg(ap, int16_t *);
292 struct p9_qid **wqids =
293 va_arg(ap, struct p9_qid **);
294
295 *wqids = NULL;
296
297 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000298 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500299 if (!errcode) {
300 *wqids =
301 kmalloc(*nwqid *
302 sizeof(struct p9_qid),
303 GFP_KERNEL);
304 if (*wqids == NULL)
305 errcode = -ENOMEM;
306 }
307
308 if (!errcode) {
309 int i;
310
311 for (i = 0; i < *nwqid; i++) {
312 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000313 p9pdu_readf(pdu,
314 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500315 "Q",
316 &(*wqids)[i]);
317 if (errcode)
318 break;
319 }
320 }
321
322 if (errcode) {
323 kfree(*wqids);
324 *wqids = NULL;
325 }
326 }
327 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530328 case 'A': {
329 struct p9_stat_dotl *stbuf =
330 va_arg(ap, struct p9_stat_dotl *);
331
332 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
333 errcode =
334 p9pdu_readf(pdu, proto_version,
335 "qQdddqqqqqqqqqqqqqqq",
336 &stbuf->st_result_mask,
337 &stbuf->qid,
338 &stbuf->st_mode,
339 &stbuf->st_uid, &stbuf->st_gid,
340 &stbuf->st_nlink,
341 &stbuf->st_rdev, &stbuf->st_size,
342 &stbuf->st_blksize, &stbuf->st_blocks,
343 &stbuf->st_atime_sec,
344 &stbuf->st_atime_nsec,
345 &stbuf->st_mtime_sec,
346 &stbuf->st_mtime_nsec,
347 &stbuf->st_ctime_sec,
348 &stbuf->st_ctime_nsec,
349 &stbuf->st_btime_sec,
350 &stbuf->st_btime_nsec,
351 &stbuf->st_gen,
352 &stbuf->st_data_version);
353 }
354 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500355 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000356 if ((proto_version != p9_proto_2000u) &&
357 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500358 return 0;
359 break;
360 default:
361 BUG();
362 break;
363 }
364
365 if (errcode)
366 break;
367 }
368
369 return errcode;
370}
371
372int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000373p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
374 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500375{
376 const char *ptr;
377 int errcode = 0;
378
379 for (ptr = fmt; *ptr; ptr++) {
380 switch (*ptr) {
381 case 'b':{
382 int8_t val = va_arg(ap, int);
383 if (pdu_write(pdu, &val, sizeof(val)))
384 errcode = -EFAULT;
385 }
386 break;
387 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800388 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500389 if (pdu_write(pdu, &val, sizeof(val)))
390 errcode = -EFAULT;
391 }
392 break;
393 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800394 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500395 if (pdu_write(pdu, &val, sizeof(val)))
396 errcode = -EFAULT;
397 }
398 break;
399 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800400 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500401 if (pdu_write(pdu, &val, sizeof(val)))
402 errcode = -EFAULT;
403 }
404 break;
405 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500406 const char *sptr = va_arg(ap, const char *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500407 int16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500408 if (sptr)
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000409 len = min_t(int16_t, strlen(sptr), USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500410
Sripathi Kodi342fee12010-03-05 18:50:14 +0000411 errcode = p9pdu_writef(pdu, proto_version,
412 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500413 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500414 errcode = -EFAULT;
415 }
416 break;
417 case 'Q':{
418 const struct p9_qid *qid =
419 va_arg(ap, const struct p9_qid *);
420 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000421 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500422 qid->type, qid->version,
423 qid->path);
424 } break;
425 case 'S':{
426 const struct p9_wstat *stbuf =
427 va_arg(ap, const struct p9_wstat *);
428 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000429 p9pdu_writef(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500430 "wwdQdddqssss?sddd",
431 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500432 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500433 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);
439 } break;
440 case 'D':{
441 int32_t count = va_arg(ap, int32_t);
442 const void *data = va_arg(ap, const void *);
443
Sripathi Kodi342fee12010-03-05 18:50:14 +0000444 errcode = p9pdu_writef(pdu, proto_version, "d",
445 count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500446 if (!errcode && pdu_write(pdu, data, count))
447 errcode = -EFAULT;
448 }
449 break;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500450 case 'U':{
451 int32_t count = va_arg(ap, int32_t);
452 const char __user *udata =
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500453 va_arg(ap, const void __user *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000454 errcode = p9pdu_writef(pdu, proto_version, "d",
455 count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500456 if (!errcode && pdu_write_u(pdu, udata, count))
457 errcode = -EFAULT;
458 }
459 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500460 case 'T':{
461 int16_t nwname = va_arg(ap, int);
462 const char **wnames = va_arg(ap, const char **);
463
Sripathi Kodi342fee12010-03-05 18:50:14 +0000464 errcode = p9pdu_writef(pdu, proto_version, "w",
465 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500466 if (!errcode) {
467 int i;
468
469 for (i = 0; i < nwname; i++) {
470 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000471 p9pdu_writef(pdu,
472 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500473 "s",
474 wnames[i]);
475 if (errcode)
476 break;
477 }
478 }
479 }
480 break;
481 case 'R':{
482 int16_t nwqid = va_arg(ap, int);
483 struct p9_qid *wqids =
484 va_arg(ap, struct p9_qid *);
485
Sripathi Kodi342fee12010-03-05 18:50:14 +0000486 errcode = p9pdu_writef(pdu, proto_version, "w",
487 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500488 if (!errcode) {
489 int i;
490
491 for (i = 0; i < nwqid; i++) {
492 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000493 p9pdu_writef(pdu,
494 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500495 "Q",
496 &wqids[i]);
497 if (errcode)
498 break;
499 }
500 }
501 }
502 break;
Sripathi Kodi87d78452010-06-18 11:50:10 +0530503 case 'I':{
504 struct p9_iattr_dotl *p9attr = va_arg(ap,
505 struct p9_iattr_dotl *);
506
507 errcode = p9pdu_writef(pdu, proto_version,
508 "ddddqqqqq",
509 p9attr->valid,
510 p9attr->mode,
511 p9attr->uid,
512 p9attr->gid,
513 p9attr->size,
514 p9attr->atime_sec,
515 p9attr->atime_nsec,
516 p9attr->mtime_sec,
517 p9attr->mtime_nsec);
518 }
519 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500520 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000521 if ((proto_version != p9_proto_2000u) &&
522 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500523 return 0;
524 break;
525 default:
526 BUG();
527 break;
528 }
529
530 if (errcode)
531 break;
532 }
533
534 return errcode;
535}
536
Sripathi Kodi342fee12010-03-05 18:50:14 +0000537int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500538{
539 va_list ap;
540 int ret;
541
542 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000543 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500544 va_end(ap);
545
546 return ret;
547}
548
549static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000550p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500551{
552 va_list ap;
553 int ret;
554
555 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000556 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500557 va_end(ap);
558
559 return ret;
560}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500561
Sripathi Kodi342fee12010-03-05 18:50:14 +0000562int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500563{
564 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500565 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500566
567 fake_pdu.size = len;
568 fake_pdu.capacity = len;
569 fake_pdu.sdata = buf;
570 fake_pdu.offset = 0;
571
Sripathi Kodi342fee12010-03-05 18:50:14 +0000572 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500573 if (ret) {
574 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
575 p9pdu_dump(1, &fake_pdu);
576 }
577
578 return ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500579}
580EXPORT_SYMBOL(p9stat_read);
581
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500582int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
583{
584 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
585}
586
587int p9pdu_finalize(struct p9_fcall *pdu)
588{
589 int size = pdu->size;
590 int err;
591
592 pdu->size = 0;
593 err = p9pdu_writef(pdu, 0, "d", size);
594 pdu->size = size;
595
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500596#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500597 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500598 p9pdu_dump(0, pdu);
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500599#endif
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500600
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500601 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
602 pdu->id, pdu->tag);
603
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500604 return err;
605}
606
607void p9pdu_reset(struct p9_fcall *pdu)
608{
609 pdu->offset = 0;
610 pdu->size = 0;
611}
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000612
613int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
614 int proto_version)
615{
616 struct p9_fcall fake_pdu;
617 int ret;
618 char *nameptr;
619
620 fake_pdu.size = len;
621 fake_pdu.capacity = len;
622 fake_pdu.sdata = buf;
623 fake_pdu.offset = 0;
624
625 ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
626 &dirent->d_off, &dirent->d_type, &nameptr);
627 if (ret) {
628 P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
629 p9pdu_dump(1, &fake_pdu);
630 goto out;
631 }
632
633 strcpy(dirent->d_name, nameptr);
634
635out:
636 return fake_pdu.offset;
637}
638EXPORT_SYMBOL(p9dirent_read);