blob: 7bca2421bfc8afe262fb17979454fea6a1d4b2bb [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
Venkateswararao Jujjuri (JV)bb2f8a52011-01-28 17:05:59 -0800117static size_t
118pdu_write_urw(struct p9_fcall *pdu, const char *kdata, const char __user *udata,
119 size_t size)
120{
121 BUG_ON(pdu->size > P9_IOHDRSZ);
122 pdu->pubuf = (char __user *)udata;
123 pdu->pkbuf = (char *)kdata;
124 pdu->pbuf_size = size;
125 return 0;
126}
127
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500128/*
129 b - int8_t
130 w - int16_t
131 d - int32_t
132 q - int64_t
133 s - string
134 S - stat
135 Q - qid
136 D - data blob (int32_t size followed by void *, results are not freed)
137 T - array of strings (int16_t count, followed by strings)
138 R - array of qids (int16_t count, followed by qids)
Sripathi Kodif0853122010-07-12 20:07:23 +0530139 A - stat for 9p2000.L (p9_stat_dotl)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500140 ? - if optional = 1, continue parsing
141*/
142
143static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000144p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
145 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500146{
147 const char *ptr;
148 int errcode = 0;
149
150 for (ptr = fmt; *ptr; ptr++) {
151 switch (*ptr) {
152 case 'b':{
153 int8_t *val = va_arg(ap, int8_t *);
154 if (pdu_read(pdu, val, sizeof(*val))) {
155 errcode = -EFAULT;
156 break;
157 }
158 }
159 break;
160 case 'w':{
161 int16_t *val = va_arg(ap, int16_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800162 __le16 le_val;
163 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500164 errcode = -EFAULT;
165 break;
166 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800167 *val = le16_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500168 }
169 break;
170 case 'd':{
171 int32_t *val = va_arg(ap, int32_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800172 __le32 le_val;
173 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500174 errcode = -EFAULT;
175 break;
176 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800177 *val = le32_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500178 }
179 break;
180 case 'q':{
181 int64_t *val = va_arg(ap, int64_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800182 __le64 le_val;
183 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500184 errcode = -EFAULT;
185 break;
186 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800187 *val = le64_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500188 }
189 break;
190 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500191 char **sptr = va_arg(ap, char **);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600192 uint16_t len;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500193
Sripathi Kodi342fee12010-03-05 18:50:14 +0000194 errcode = p9pdu_readf(pdu, proto_version,
195 "w", &len);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500196 if (errcode)
197 break;
198
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600199 *sptr = kmalloc(len + 1, GFP_KERNEL);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500200 if (*sptr == NULL) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500201 errcode = -EFAULT;
202 break;
203 }
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600204 if (pdu_read(pdu, *sptr, len)) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500205 errcode = -EFAULT;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500206 kfree(*sptr);
207 *sptr = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500208 } else
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600209 (*sptr)[len] = 0;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500210 }
211 break;
212 case 'Q':{
213 struct p9_qid *qid =
214 va_arg(ap, struct p9_qid *);
215
Sripathi Kodi342fee12010-03-05 18:50:14 +0000216 errcode = p9pdu_readf(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500217 &qid->type, &qid->version,
218 &qid->path);
219 }
220 break;
221 case 'S':{
222 struct p9_wstat *stbuf =
223 va_arg(ap, struct p9_wstat *);
224
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500225 memset(stbuf, 0, sizeof(struct p9_wstat));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500226 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500227 -1;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500228 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000229 p9pdu_readf(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500230 "wwdQdddqssss?sddd",
231 &stbuf->size, &stbuf->type,
232 &stbuf->dev, &stbuf->qid,
233 &stbuf->mode, &stbuf->atime,
234 &stbuf->mtime, &stbuf->length,
235 &stbuf->name, &stbuf->uid,
236 &stbuf->gid, &stbuf->muid,
237 &stbuf->extension,
238 &stbuf->n_uid, &stbuf->n_gid,
239 &stbuf->n_muid);
240 if (errcode)
241 p9stat_free(stbuf);
242 }
243 break;
244 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600245 uint32_t *count = va_arg(ap, uint32_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500246 void **data = va_arg(ap, void **);
247
248 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000249 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500250 if (!errcode) {
251 *count =
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600252 min_t(uint32_t, *count,
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000253 pdu->size - pdu->offset);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500254 *data = &pdu->sdata[pdu->offset];
255 }
256 }
257 break;
258 case 'T':{
259 int16_t *nwname = va_arg(ap, int16_t *);
260 char ***wnames = va_arg(ap, char ***);
261
Sripathi Kodi342fee12010-03-05 18:50:14 +0000262 errcode = p9pdu_readf(pdu, proto_version,
263 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500264 if (!errcode) {
265 *wnames =
266 kmalloc(sizeof(char *) * *nwname,
267 GFP_KERNEL);
268 if (!*wnames)
269 errcode = -ENOMEM;
270 }
271
272 if (!errcode) {
273 int i;
274
275 for (i = 0; i < *nwname; i++) {
276 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000277 p9pdu_readf(pdu,
278 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500279 "s",
280 &(*wnames)[i]);
281 if (errcode)
282 break;
283 }
284 }
285
286 if (errcode) {
287 if (*wnames) {
288 int i;
289
290 for (i = 0; i < *nwname; i++)
291 kfree((*wnames)[i]);
292 }
293 kfree(*wnames);
294 *wnames = NULL;
295 }
296 }
297 break;
298 case 'R':{
299 int16_t *nwqid = va_arg(ap, int16_t *);
300 struct p9_qid **wqids =
301 va_arg(ap, struct p9_qid **);
302
303 *wqids = NULL;
304
305 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000306 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500307 if (!errcode) {
308 *wqids =
309 kmalloc(*nwqid *
310 sizeof(struct p9_qid),
311 GFP_KERNEL);
312 if (*wqids == NULL)
313 errcode = -ENOMEM;
314 }
315
316 if (!errcode) {
317 int i;
318
319 for (i = 0; i < *nwqid; i++) {
320 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000321 p9pdu_readf(pdu,
322 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500323 "Q",
324 &(*wqids)[i]);
325 if (errcode)
326 break;
327 }
328 }
329
330 if (errcode) {
331 kfree(*wqids);
332 *wqids = NULL;
333 }
334 }
335 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530336 case 'A': {
337 struct p9_stat_dotl *stbuf =
338 va_arg(ap, struct p9_stat_dotl *);
339
340 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
341 errcode =
342 p9pdu_readf(pdu, proto_version,
343 "qQdddqqqqqqqqqqqqqqq",
344 &stbuf->st_result_mask,
345 &stbuf->qid,
346 &stbuf->st_mode,
347 &stbuf->st_uid, &stbuf->st_gid,
348 &stbuf->st_nlink,
349 &stbuf->st_rdev, &stbuf->st_size,
350 &stbuf->st_blksize, &stbuf->st_blocks,
351 &stbuf->st_atime_sec,
352 &stbuf->st_atime_nsec,
353 &stbuf->st_mtime_sec,
354 &stbuf->st_mtime_nsec,
355 &stbuf->st_ctime_sec,
356 &stbuf->st_ctime_nsec,
357 &stbuf->st_btime_sec,
358 &stbuf->st_btime_nsec,
359 &stbuf->st_gen,
360 &stbuf->st_data_version);
361 }
362 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500363 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000364 if ((proto_version != p9_proto_2000u) &&
365 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500366 return 0;
367 break;
368 default:
369 BUG();
370 break;
371 }
372
373 if (errcode)
374 break;
375 }
376
377 return errcode;
378}
379
380int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000381p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
382 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500383{
384 const char *ptr;
385 int errcode = 0;
386
387 for (ptr = fmt; *ptr; ptr++) {
388 switch (*ptr) {
389 case 'b':{
390 int8_t val = va_arg(ap, int);
391 if (pdu_write(pdu, &val, sizeof(val)))
392 errcode = -EFAULT;
393 }
394 break;
395 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800396 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500397 if (pdu_write(pdu, &val, sizeof(val)))
398 errcode = -EFAULT;
399 }
400 break;
401 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800402 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500403 if (pdu_write(pdu, &val, sizeof(val)))
404 errcode = -EFAULT;
405 }
406 break;
407 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800408 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500409 if (pdu_write(pdu, &val, sizeof(val)))
410 errcode = -EFAULT;
411 }
412 break;
413 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500414 const char *sptr = va_arg(ap, const char *);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600415 uint16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500416 if (sptr)
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600417 len = min_t(uint16_t, strlen(sptr),
418 USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500419
Sripathi Kodi342fee12010-03-05 18:50:14 +0000420 errcode = p9pdu_writef(pdu, proto_version,
421 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500422 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500423 errcode = -EFAULT;
424 }
425 break;
426 case 'Q':{
427 const struct p9_qid *qid =
428 va_arg(ap, const struct p9_qid *);
429 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000430 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500431 qid->type, qid->version,
432 qid->path);
433 } break;
434 case 'S':{
435 const struct p9_wstat *stbuf =
436 va_arg(ap, const struct p9_wstat *);
437 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000438 p9pdu_writef(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500439 "wwdQdddqssss?sddd",
440 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500441 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500442 stbuf->mode, stbuf->atime,
443 stbuf->mtime, stbuf->length,
444 stbuf->name, stbuf->uid,
445 stbuf->gid, stbuf->muid,
446 stbuf->extension, stbuf->n_uid,
447 stbuf->n_gid, stbuf->n_muid);
448 } break;
449 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600450 uint32_t count = va_arg(ap, uint32_t);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500451 const void *data = va_arg(ap, const void *);
452
Sripathi Kodi342fee12010-03-05 18:50:14 +0000453 errcode = p9pdu_writef(pdu, proto_version, "d",
454 count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500455 if (!errcode && pdu_write(pdu, data, count))
456 errcode = -EFAULT;
457 }
458 break;
Venkateswararao Jujjuri (JV)bb2f8a52011-01-28 17:05:59 -0800459 case 'E':{
460 int32_t cnt = va_arg(ap, int32_t);
461 const char *k = va_arg(ap, const void *);
462 const char *u = va_arg(ap, const void *);
463 errcode = p9pdu_writef(pdu, proto_version, "d",
464 cnt);
465 if (!errcode && pdu_write_urw(pdu, k, u, cnt))
466 errcode = -EFAULT;
467 }
468 break;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500469 case 'U':{
470 int32_t count = va_arg(ap, int32_t);
471 const char __user *udata =
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500472 va_arg(ap, const void __user *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000473 errcode = p9pdu_writef(pdu, proto_version, "d",
474 count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500475 if (!errcode && pdu_write_u(pdu, udata, count))
476 errcode = -EFAULT;
477 }
478 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500479 case 'T':{
480 int16_t nwname = va_arg(ap, int);
481 const char **wnames = va_arg(ap, const char **);
482
Sripathi Kodi342fee12010-03-05 18:50:14 +0000483 errcode = p9pdu_writef(pdu, proto_version, "w",
484 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500485 if (!errcode) {
486 int i;
487
488 for (i = 0; i < nwname; i++) {
489 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000490 p9pdu_writef(pdu,
491 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500492 "s",
493 wnames[i]);
494 if (errcode)
495 break;
496 }
497 }
498 }
499 break;
500 case 'R':{
501 int16_t nwqid = va_arg(ap, int);
502 struct p9_qid *wqids =
503 va_arg(ap, struct p9_qid *);
504
Sripathi Kodi342fee12010-03-05 18:50:14 +0000505 errcode = p9pdu_writef(pdu, proto_version, "w",
506 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500507 if (!errcode) {
508 int i;
509
510 for (i = 0; i < nwqid; i++) {
511 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000512 p9pdu_writef(pdu,
513 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500514 "Q",
515 &wqids[i]);
516 if (errcode)
517 break;
518 }
519 }
520 }
521 break;
Sripathi Kodi87d78452010-06-18 11:50:10 +0530522 case 'I':{
523 struct p9_iattr_dotl *p9attr = va_arg(ap,
524 struct p9_iattr_dotl *);
525
526 errcode = p9pdu_writef(pdu, proto_version,
527 "ddddqqqqq",
528 p9attr->valid,
529 p9attr->mode,
530 p9attr->uid,
531 p9attr->gid,
532 p9attr->size,
533 p9attr->atime_sec,
534 p9attr->atime_nsec,
535 p9attr->mtime_sec,
536 p9attr->mtime_nsec);
537 }
538 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500539 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000540 if ((proto_version != p9_proto_2000u) &&
541 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500542 return 0;
543 break;
544 default:
545 BUG();
546 break;
547 }
548
549 if (errcode)
550 break;
551 }
552
553 return errcode;
554}
555
Sripathi Kodi342fee12010-03-05 18:50:14 +0000556int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500557{
558 va_list ap;
559 int ret;
560
561 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000562 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500563 va_end(ap);
564
565 return ret;
566}
567
568static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000569p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500570{
571 va_list ap;
572 int ret;
573
574 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000575 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500576 va_end(ap);
577
578 return ret;
579}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500580
Sripathi Kodi342fee12010-03-05 18:50:14 +0000581int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500582{
583 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500584 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500585
586 fake_pdu.size = len;
587 fake_pdu.capacity = len;
588 fake_pdu.sdata = buf;
589 fake_pdu.offset = 0;
590
Sripathi Kodi342fee12010-03-05 18:50:14 +0000591 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500592 if (ret) {
593 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
594 p9pdu_dump(1, &fake_pdu);
595 }
596
597 return ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500598}
599EXPORT_SYMBOL(p9stat_read);
600
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500601int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
602{
Venkateswararao Jujjuri (JV)9bb6c102011-02-02 17:52:46 -0800603 pdu->id = type;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500604 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
605}
606
607int p9pdu_finalize(struct p9_fcall *pdu)
608{
609 int size = pdu->size;
610 int err;
611
612 pdu->size = 0;
613 err = p9pdu_writef(pdu, 0, "d", size);
614 pdu->size = size;
615
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500616#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500617 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500618 p9pdu_dump(0, pdu);
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500619#endif
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500620
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500621 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
622 pdu->id, pdu->tag);
623
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500624 return err;
625}
626
627void p9pdu_reset(struct p9_fcall *pdu)
628{
629 pdu->offset = 0;
630 pdu->size = 0;
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -0800631 pdu->private = NULL;
632 pdu->pubuf = NULL;
633 pdu->pkbuf = NULL;
634 pdu->pbuf_size = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500635}
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000636
637int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
638 int proto_version)
639{
640 struct p9_fcall fake_pdu;
641 int ret;
642 char *nameptr;
643
644 fake_pdu.size = len;
645 fake_pdu.capacity = len;
646 fake_pdu.sdata = buf;
647 fake_pdu.offset = 0;
648
649 ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
650 &dirent->d_off, &dirent->d_type, &nameptr);
651 if (ret) {
652 P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
653 p9pdu_dump(1, &fake_pdu);
654 goto out;
655 }
656
657 strcpy(dirent->d_name, nameptr);
658
659out:
660 return fake_pdu.offset;
661}
662EXPORT_SYMBOL(p9dirent_read);