blob: a873277cb996810162ee875db9485952144541bb [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
Venkateswararao Jujjuri (JV)2c665232011-02-16 18:43:20 -0800128static size_t
129pdu_write_readdir(struct p9_fcall *pdu, const char *kdata, size_t size)
130{
131 BUG_ON(pdu->size > P9_READDIRHDRSZ);
132 pdu->pkbuf = (char *)kdata;
133 pdu->pbuf_size = size;
134 return 0;
135}
136
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500137/*
138 b - int8_t
139 w - int16_t
140 d - int32_t
141 q - int64_t
142 s - string
143 S - stat
144 Q - qid
145 D - data blob (int32_t size followed by void *, results are not freed)
146 T - array of strings (int16_t count, followed by strings)
147 R - array of qids (int16_t count, followed by qids)
Sripathi Kodif0853122010-07-12 20:07:23 +0530148 A - stat for 9p2000.L (p9_stat_dotl)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500149 ? - if optional = 1, continue parsing
150*/
151
152static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000153p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
154 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500155{
156 const char *ptr;
157 int errcode = 0;
158
159 for (ptr = fmt; *ptr; ptr++) {
160 switch (*ptr) {
161 case 'b':{
162 int8_t *val = va_arg(ap, int8_t *);
163 if (pdu_read(pdu, val, sizeof(*val))) {
164 errcode = -EFAULT;
165 break;
166 }
167 }
168 break;
169 case 'w':{
170 int16_t *val = va_arg(ap, int16_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800171 __le16 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 = le16_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500177 }
178 break;
179 case 'd':{
180 int32_t *val = va_arg(ap, int32_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800181 __le32 le_val;
182 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500183 errcode = -EFAULT;
184 break;
185 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800186 *val = le32_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500187 }
188 break;
189 case 'q':{
190 int64_t *val = va_arg(ap, int64_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800191 __le64 le_val;
192 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500193 errcode = -EFAULT;
194 break;
195 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800196 *val = le64_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500197 }
198 break;
199 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500200 char **sptr = va_arg(ap, char **);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600201 uint16_t len;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500202
Sripathi Kodi342fee12010-03-05 18:50:14 +0000203 errcode = p9pdu_readf(pdu, proto_version,
204 "w", &len);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500205 if (errcode)
206 break;
207
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530208 *sptr = kmalloc(len + 1, GFP_NOFS);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500209 if (*sptr == NULL) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500210 errcode = -EFAULT;
211 break;
212 }
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600213 if (pdu_read(pdu, *sptr, len)) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500214 errcode = -EFAULT;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500215 kfree(*sptr);
216 *sptr = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500217 } else
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600218 (*sptr)[len] = 0;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500219 }
220 break;
221 case 'Q':{
222 struct p9_qid *qid =
223 va_arg(ap, struct p9_qid *);
224
Sripathi Kodi342fee12010-03-05 18:50:14 +0000225 errcode = p9pdu_readf(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500226 &qid->type, &qid->version,
227 &qid->path);
228 }
229 break;
230 case 'S':{
231 struct p9_wstat *stbuf =
232 va_arg(ap, struct p9_wstat *);
233
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500234 memset(stbuf, 0, sizeof(struct p9_wstat));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500235 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500236 -1;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500237 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000238 p9pdu_readf(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500239 "wwdQdddqssss?sddd",
240 &stbuf->size, &stbuf->type,
241 &stbuf->dev, &stbuf->qid,
242 &stbuf->mode, &stbuf->atime,
243 &stbuf->mtime, &stbuf->length,
244 &stbuf->name, &stbuf->uid,
245 &stbuf->gid, &stbuf->muid,
246 &stbuf->extension,
247 &stbuf->n_uid, &stbuf->n_gid,
248 &stbuf->n_muid);
249 if (errcode)
250 p9stat_free(stbuf);
251 }
252 break;
253 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600254 uint32_t *count = va_arg(ap, uint32_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500255 void **data = va_arg(ap, void **);
256
257 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000258 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500259 if (!errcode) {
260 *count =
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600261 min_t(uint32_t, *count,
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000262 pdu->size - pdu->offset);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500263 *data = &pdu->sdata[pdu->offset];
264 }
265 }
266 break;
267 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530268 uint16_t *nwname = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500269 char ***wnames = va_arg(ap, char ***);
270
Sripathi Kodi342fee12010-03-05 18:50:14 +0000271 errcode = p9pdu_readf(pdu, proto_version,
272 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500273 if (!errcode) {
274 *wnames =
275 kmalloc(sizeof(char *) * *nwname,
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530276 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500277 if (!*wnames)
278 errcode = -ENOMEM;
279 }
280
281 if (!errcode) {
282 int i;
283
284 for (i = 0; i < *nwname; i++) {
285 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000286 p9pdu_readf(pdu,
287 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500288 "s",
289 &(*wnames)[i]);
290 if (errcode)
291 break;
292 }
293 }
294
295 if (errcode) {
296 if (*wnames) {
297 int i;
298
299 for (i = 0; i < *nwname; i++)
300 kfree((*wnames)[i]);
301 }
302 kfree(*wnames);
303 *wnames = NULL;
304 }
305 }
306 break;
307 case 'R':{
308 int16_t *nwqid = va_arg(ap, int16_t *);
309 struct p9_qid **wqids =
310 va_arg(ap, struct p9_qid **);
311
312 *wqids = NULL;
313
314 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000315 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500316 if (!errcode) {
317 *wqids =
318 kmalloc(*nwqid *
319 sizeof(struct p9_qid),
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530320 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500321 if (*wqids == NULL)
322 errcode = -ENOMEM;
323 }
324
325 if (!errcode) {
326 int i;
327
328 for (i = 0; i < *nwqid; i++) {
329 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000330 p9pdu_readf(pdu,
331 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500332 "Q",
333 &(*wqids)[i]);
334 if (errcode)
335 break;
336 }
337 }
338
339 if (errcode) {
340 kfree(*wqids);
341 *wqids = NULL;
342 }
343 }
344 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530345 case 'A': {
346 struct p9_stat_dotl *stbuf =
347 va_arg(ap, struct p9_stat_dotl *);
348
349 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
350 errcode =
351 p9pdu_readf(pdu, proto_version,
352 "qQdddqqqqqqqqqqqqqqq",
353 &stbuf->st_result_mask,
354 &stbuf->qid,
355 &stbuf->st_mode,
356 &stbuf->st_uid, &stbuf->st_gid,
357 &stbuf->st_nlink,
358 &stbuf->st_rdev, &stbuf->st_size,
359 &stbuf->st_blksize, &stbuf->st_blocks,
360 &stbuf->st_atime_sec,
361 &stbuf->st_atime_nsec,
362 &stbuf->st_mtime_sec,
363 &stbuf->st_mtime_nsec,
364 &stbuf->st_ctime_sec,
365 &stbuf->st_ctime_nsec,
366 &stbuf->st_btime_sec,
367 &stbuf->st_btime_nsec,
368 &stbuf->st_gen,
369 &stbuf->st_data_version);
370 }
371 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500372 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000373 if ((proto_version != p9_proto_2000u) &&
374 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500375 return 0;
376 break;
377 default:
378 BUG();
379 break;
380 }
381
382 if (errcode)
383 break;
384 }
385
386 return errcode;
387}
388
389int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000390p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
391 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500392{
393 const char *ptr;
394 int errcode = 0;
395
396 for (ptr = fmt; *ptr; ptr++) {
397 switch (*ptr) {
398 case 'b':{
399 int8_t val = va_arg(ap, int);
400 if (pdu_write(pdu, &val, sizeof(val)))
401 errcode = -EFAULT;
402 }
403 break;
404 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800405 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500406 if (pdu_write(pdu, &val, sizeof(val)))
407 errcode = -EFAULT;
408 }
409 break;
410 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800411 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500412 if (pdu_write(pdu, &val, sizeof(val)))
413 errcode = -EFAULT;
414 }
415 break;
416 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800417 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500418 if (pdu_write(pdu, &val, sizeof(val)))
419 errcode = -EFAULT;
420 }
421 break;
422 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500423 const char *sptr = va_arg(ap, const char *);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600424 uint16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500425 if (sptr)
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600426 len = min_t(uint16_t, strlen(sptr),
427 USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500428
Sripathi Kodi342fee12010-03-05 18:50:14 +0000429 errcode = p9pdu_writef(pdu, proto_version,
430 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500431 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500432 errcode = -EFAULT;
433 }
434 break;
435 case 'Q':{
436 const struct p9_qid *qid =
437 va_arg(ap, const struct p9_qid *);
438 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000439 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500440 qid->type, qid->version,
441 qid->path);
442 } break;
443 case 'S':{
444 const struct p9_wstat *stbuf =
445 va_arg(ap, const struct p9_wstat *);
446 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000447 p9pdu_writef(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500448 "wwdQdddqssss?sddd",
449 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500450 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500451 stbuf->mode, stbuf->atime,
452 stbuf->mtime, stbuf->length,
453 stbuf->name, stbuf->uid,
454 stbuf->gid, stbuf->muid,
455 stbuf->extension, stbuf->n_uid,
456 stbuf->n_gid, stbuf->n_muid);
457 } break;
458 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600459 uint32_t count = va_arg(ap, uint32_t);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500460 const void *data = va_arg(ap, const void *);
461
Sripathi Kodi342fee12010-03-05 18:50:14 +0000462 errcode = p9pdu_writef(pdu, proto_version, "d",
463 count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500464 if (!errcode && pdu_write(pdu, data, count))
465 errcode = -EFAULT;
466 }
467 break;
Venkateswararao Jujjuri (JV)bb2f8a52011-01-28 17:05:59 -0800468 case 'E':{
469 int32_t cnt = va_arg(ap, int32_t);
470 const char *k = va_arg(ap, const void *);
Aneesh Kumar K.Vbd8c8ad2011-03-24 23:14:46 +0530471 const char __user *u = va_arg(ap,
472 const void __user *);
Venkateswararao Jujjuri (JV)bb2f8a52011-01-28 17:05:59 -0800473 errcode = p9pdu_writef(pdu, proto_version, "d",
474 cnt);
475 if (!errcode && pdu_write_urw(pdu, k, u, cnt))
476 errcode = -EFAULT;
477 }
478 break;
Venkateswararao Jujjuri (JV)2c665232011-02-16 18:43:20 -0800479 case 'F':{
480 int32_t cnt = va_arg(ap, int32_t);
481 const char *k = va_arg(ap, const void *);
482 errcode = p9pdu_writef(pdu, proto_version, "d",
483 cnt);
484 if (!errcode && pdu_write_readdir(pdu, k, cnt))
485 errcode = -EFAULT;
486 }
487 break;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500488 case 'U':{
489 int32_t count = va_arg(ap, int32_t);
490 const char __user *udata =
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500491 va_arg(ap, const void __user *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000492 errcode = p9pdu_writef(pdu, proto_version, "d",
493 count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500494 if (!errcode && pdu_write_u(pdu, udata, count))
495 errcode = -EFAULT;
496 }
497 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500498 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530499 uint16_t nwname = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500500 const char **wnames = va_arg(ap, const char **);
501
Sripathi Kodi342fee12010-03-05 18:50:14 +0000502 errcode = p9pdu_writef(pdu, proto_version, "w",
503 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500504 if (!errcode) {
505 int i;
506
507 for (i = 0; i < nwname; i++) {
508 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000509 p9pdu_writef(pdu,
510 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500511 "s",
512 wnames[i]);
513 if (errcode)
514 break;
515 }
516 }
517 }
518 break;
519 case 'R':{
520 int16_t nwqid = va_arg(ap, int);
521 struct p9_qid *wqids =
522 va_arg(ap, struct p9_qid *);
523
Sripathi Kodi342fee12010-03-05 18:50:14 +0000524 errcode = p9pdu_writef(pdu, proto_version, "w",
525 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500526 if (!errcode) {
527 int i;
528
529 for (i = 0; i < nwqid; i++) {
530 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000531 p9pdu_writef(pdu,
532 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500533 "Q",
534 &wqids[i]);
535 if (errcode)
536 break;
537 }
538 }
539 }
540 break;
Sripathi Kodi87d78452010-06-18 11:50:10 +0530541 case 'I':{
542 struct p9_iattr_dotl *p9attr = va_arg(ap,
543 struct p9_iattr_dotl *);
544
545 errcode = p9pdu_writef(pdu, proto_version,
546 "ddddqqqqq",
547 p9attr->valid,
548 p9attr->mode,
549 p9attr->uid,
550 p9attr->gid,
551 p9attr->size,
552 p9attr->atime_sec,
553 p9attr->atime_nsec,
554 p9attr->mtime_sec,
555 p9attr->mtime_nsec);
556 }
557 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500558 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000559 if ((proto_version != p9_proto_2000u) &&
560 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500561 return 0;
562 break;
563 default:
564 BUG();
565 break;
566 }
567
568 if (errcode)
569 break;
570 }
571
572 return errcode;
573}
574
Sripathi Kodi342fee12010-03-05 18:50:14 +0000575int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500576{
577 va_list ap;
578 int ret;
579
580 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000581 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500582 va_end(ap);
583
584 return ret;
585}
586
587static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000588p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500589{
590 va_list ap;
591 int ret;
592
593 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000594 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500595 va_end(ap);
596
597 return ret;
598}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500599
Sripathi Kodi342fee12010-03-05 18:50:14 +0000600int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500601{
602 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500603 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500604
605 fake_pdu.size = len;
606 fake_pdu.capacity = len;
607 fake_pdu.sdata = buf;
608 fake_pdu.offset = 0;
609
Sripathi Kodi342fee12010-03-05 18:50:14 +0000610 ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500611 if (ret) {
612 P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
613 p9pdu_dump(1, &fake_pdu);
614 }
615
616 return ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500617}
618EXPORT_SYMBOL(p9stat_read);
619
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500620int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
621{
Venkateswararao Jujjuri (JV)9bb6c102011-02-02 17:52:46 -0800622 pdu->id = type;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500623 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
624}
625
626int p9pdu_finalize(struct p9_fcall *pdu)
627{
628 int size = pdu->size;
629 int err;
630
631 pdu->size = 0;
632 err = p9pdu_writef(pdu, 0, "d", size);
633 pdu->size = size;
634
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500635#ifdef CONFIG_NET_9P_DEBUG
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500636 if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500637 p9pdu_dump(0, pdu);
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500638#endif
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500639
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500640 P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
641 pdu->id, pdu->tag);
642
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500643 return err;
644}
645
646void p9pdu_reset(struct p9_fcall *pdu)
647{
648 pdu->offset = 0;
649 pdu->size = 0;
Venkateswararao Jujjuri (JV)022cae362011-01-28 14:11:13 -0800650 pdu->private = NULL;
651 pdu->pubuf = NULL;
652 pdu->pkbuf = NULL;
653 pdu->pbuf_size = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500654}
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000655
656int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
657 int proto_version)
658{
659 struct p9_fcall fake_pdu;
660 int ret;
661 char *nameptr;
662
663 fake_pdu.size = len;
664 fake_pdu.capacity = len;
665 fake_pdu.sdata = buf;
666 fake_pdu.offset = 0;
667
668 ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
669 &dirent->d_off, &dirent->d_type, &nameptr);
670 if (ret) {
671 P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
672 p9pdu_dump(1, &fake_pdu);
673 goto out;
674 }
675
676 strcpy(dirent->d_name, nameptr);
Pedro Scarapicchia Junior1b0bcbcf62011-05-09 14:10:49 +0000677 kfree(nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000678
679out:
680 return fake_pdu.offset;
681}
682EXPORT_SYMBOL(p9dirent_read);