blob: c289c6cee98e1d315eb25d8a35776ad59b93e5dd [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
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +053040#include <trace/events/9p.h>
41
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050042static int
Sripathi Kodi342fee12010-03-05 18:50:14 +000043p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050044
45void p9stat_free(struct p9_wstat *stbuf)
46{
47 kfree(stbuf->name);
48 kfree(stbuf->uid);
49 kfree(stbuf->gid);
50 kfree(stbuf->muid);
51 kfree(stbuf->extension);
52}
53EXPORT_SYMBOL(p9stat_free);
54
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +053055size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050056{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000057 size_t len = min(pdu->size - pdu->offset, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050058 memcpy(data, &pdu->sdata[pdu->offset], len);
59 pdu->offset += len;
60 return size - len;
61}
62
63static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
64{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000065 size_t len = min(pdu->capacity - pdu->size, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050066 memcpy(&pdu->sdata[pdu->size], data, len);
67 pdu->size += len;
68 return size - len;
69}
70
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050071static size_t
72pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
73{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000074 size_t len = min(pdu->capacity - pdu->size, size);
Aneesh Kumar K.V7b3bb3f2010-10-19 09:17:02 +053075 if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
76 len = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050077
78 pdu->size += len;
79 return size - len;
80}
81
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050082/*
83 b - int8_t
84 w - int16_t
85 d - int32_t
86 q - int64_t
87 s - string
Eric W. Biederman97fc8b12013-01-29 17:07:42 -080088 u - numeric uid
89 g - numeric gid
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050090 S - stat
91 Q - qid
92 D - data blob (int32_t size followed by void *, results are not freed)
93 T - array of strings (int16_t count, followed by strings)
94 R - array of qids (int16_t count, followed by qids)
Sripathi Kodif0853122010-07-12 20:07:23 +053095 A - stat for 9p2000.L (p9_stat_dotl)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050096 ? - if optional = 1, continue parsing
97*/
98
99static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000100p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
101 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500102{
103 const char *ptr;
104 int errcode = 0;
105
106 for (ptr = fmt; *ptr; ptr++) {
107 switch (*ptr) {
108 case 'b':{
109 int8_t *val = va_arg(ap, int8_t *);
110 if (pdu_read(pdu, val, sizeof(*val))) {
111 errcode = -EFAULT;
112 break;
113 }
114 }
115 break;
116 case 'w':{
117 int16_t *val = va_arg(ap, int16_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800118 __le16 le_val;
119 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500120 errcode = -EFAULT;
121 break;
122 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800123 *val = le16_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500124 }
125 break;
126 case 'd':{
127 int32_t *val = va_arg(ap, int32_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800128 __le32 le_val;
129 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500130 errcode = -EFAULT;
131 break;
132 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800133 *val = le32_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500134 }
135 break;
136 case 'q':{
137 int64_t *val = va_arg(ap, int64_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800138 __le64 le_val;
139 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500140 errcode = -EFAULT;
141 break;
142 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800143 *val = le64_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500144 }
145 break;
146 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500147 char **sptr = va_arg(ap, char **);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600148 uint16_t len;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500149
Sripathi Kodi342fee12010-03-05 18:50:14 +0000150 errcode = p9pdu_readf(pdu, proto_version,
151 "w", &len);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500152 if (errcode)
153 break;
154
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530155 *sptr = kmalloc(len + 1, GFP_NOFS);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500156 if (*sptr == NULL) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500157 errcode = -EFAULT;
158 break;
159 }
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600160 if (pdu_read(pdu, *sptr, len)) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500161 errcode = -EFAULT;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500162 kfree(*sptr);
163 *sptr = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500164 } else
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600165 (*sptr)[len] = 0;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500166 }
167 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800168 case 'u': {
169 kuid_t *uid = va_arg(ap, kuid_t *);
170 __le32 le_val;
171 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
172 errcode = -EFAULT;
173 break;
174 }
175 *uid = make_kuid(&init_user_ns,
176 le32_to_cpu(le_val));
177 } break;
178 case 'g': {
179 kgid_t *gid = va_arg(ap, kgid_t *);
180 __le32 le_val;
181 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
182 errcode = -EFAULT;
183 break;
184 }
185 *gid = make_kgid(&init_user_ns,
186 le32_to_cpu(le_val));
187 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500188 case 'Q':{
189 struct p9_qid *qid =
190 va_arg(ap, struct p9_qid *);
191
Sripathi Kodi342fee12010-03-05 18:50:14 +0000192 errcode = p9pdu_readf(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500193 &qid->type, &qid->version,
194 &qid->path);
195 }
196 break;
197 case 'S':{
198 struct p9_wstat *stbuf =
199 va_arg(ap, struct p9_wstat *);
200
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500201 memset(stbuf, 0, sizeof(struct p9_wstat));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500202 stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500203 -1;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500204 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000205 p9pdu_readf(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500206 "wwdQdddqssss?sddd",
207 &stbuf->size, &stbuf->type,
208 &stbuf->dev, &stbuf->qid,
209 &stbuf->mode, &stbuf->atime,
210 &stbuf->mtime, &stbuf->length,
211 &stbuf->name, &stbuf->uid,
212 &stbuf->gid, &stbuf->muid,
213 &stbuf->extension,
214 &stbuf->n_uid, &stbuf->n_gid,
215 &stbuf->n_muid);
216 if (errcode)
217 p9stat_free(stbuf);
218 }
219 break;
220 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600221 uint32_t *count = va_arg(ap, uint32_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500222 void **data = va_arg(ap, void **);
223
224 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000225 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500226 if (!errcode) {
227 *count =
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600228 min_t(uint32_t, *count,
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000229 pdu->size - pdu->offset);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500230 *data = &pdu->sdata[pdu->offset];
231 }
232 }
233 break;
234 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530235 uint16_t *nwname = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500236 char ***wnames = va_arg(ap, char ***);
237
Sripathi Kodi342fee12010-03-05 18:50:14 +0000238 errcode = p9pdu_readf(pdu, proto_version,
239 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500240 if (!errcode) {
241 *wnames =
242 kmalloc(sizeof(char *) * *nwname,
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530243 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500244 if (!*wnames)
245 errcode = -ENOMEM;
246 }
247
248 if (!errcode) {
249 int i;
250
251 for (i = 0; i < *nwname; i++) {
252 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000253 p9pdu_readf(pdu,
254 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500255 "s",
256 &(*wnames)[i]);
257 if (errcode)
258 break;
259 }
260 }
261
262 if (errcode) {
263 if (*wnames) {
264 int i;
265
266 for (i = 0; i < *nwname; i++)
267 kfree((*wnames)[i]);
268 }
269 kfree(*wnames);
270 *wnames = NULL;
271 }
272 }
273 break;
274 case 'R':{
275 int16_t *nwqid = va_arg(ap, int16_t *);
276 struct p9_qid **wqids =
277 va_arg(ap, struct p9_qid **);
278
279 *wqids = NULL;
280
281 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000282 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500283 if (!errcode) {
284 *wqids =
285 kmalloc(*nwqid *
286 sizeof(struct p9_qid),
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530287 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500288 if (*wqids == NULL)
289 errcode = -ENOMEM;
290 }
291
292 if (!errcode) {
293 int i;
294
295 for (i = 0; i < *nwqid; i++) {
296 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000297 p9pdu_readf(pdu,
298 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500299 "Q",
300 &(*wqids)[i]);
301 if (errcode)
302 break;
303 }
304 }
305
306 if (errcode) {
307 kfree(*wqids);
308 *wqids = NULL;
309 }
310 }
311 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530312 case 'A': {
313 struct p9_stat_dotl *stbuf =
314 va_arg(ap, struct p9_stat_dotl *);
315
316 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
317 errcode =
318 p9pdu_readf(pdu, proto_version,
319 "qQdddqqqqqqqqqqqqqqq",
320 &stbuf->st_result_mask,
321 &stbuf->qid,
322 &stbuf->st_mode,
323 &stbuf->st_uid, &stbuf->st_gid,
324 &stbuf->st_nlink,
325 &stbuf->st_rdev, &stbuf->st_size,
326 &stbuf->st_blksize, &stbuf->st_blocks,
327 &stbuf->st_atime_sec,
328 &stbuf->st_atime_nsec,
329 &stbuf->st_mtime_sec,
330 &stbuf->st_mtime_nsec,
331 &stbuf->st_ctime_sec,
332 &stbuf->st_ctime_nsec,
333 &stbuf->st_btime_sec,
334 &stbuf->st_btime_nsec,
335 &stbuf->st_gen,
336 &stbuf->st_data_version);
337 }
338 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500339 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000340 if ((proto_version != p9_proto_2000u) &&
341 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500342 return 0;
343 break;
344 default:
345 BUG();
346 break;
347 }
348
349 if (errcode)
350 break;
351 }
352
353 return errcode;
354}
355
356int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000357p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
358 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500359{
360 const char *ptr;
361 int errcode = 0;
362
363 for (ptr = fmt; *ptr; ptr++) {
364 switch (*ptr) {
365 case 'b':{
366 int8_t val = va_arg(ap, int);
367 if (pdu_write(pdu, &val, sizeof(val)))
368 errcode = -EFAULT;
369 }
370 break;
371 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800372 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500373 if (pdu_write(pdu, &val, sizeof(val)))
374 errcode = -EFAULT;
375 }
376 break;
377 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800378 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500379 if (pdu_write(pdu, &val, sizeof(val)))
380 errcode = -EFAULT;
381 }
382 break;
383 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800384 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500385 if (pdu_write(pdu, &val, sizeof(val)))
386 errcode = -EFAULT;
387 }
388 break;
389 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500390 const char *sptr = va_arg(ap, const char *);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600391 uint16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500392 if (sptr)
Dan Carpenterd31bb4f2012-06-26 23:01:41 +0000393 len = min_t(size_t, strlen(sptr),
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600394 USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500395
Sripathi Kodi342fee12010-03-05 18:50:14 +0000396 errcode = p9pdu_writef(pdu, proto_version,
397 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500398 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500399 errcode = -EFAULT;
400 }
401 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800402 case 'u': {
403 kuid_t uid = va_arg(ap, kuid_t);
404 __le32 val = cpu_to_le32(
405 from_kuid(&init_user_ns, uid));
406 if (pdu_write(pdu, &val, sizeof(val)))
407 errcode = -EFAULT;
408 } break;
409 case 'g': {
410 kgid_t gid = va_arg(ap, kgid_t);
411 __le32 val = cpu_to_le32(
412 from_kgid(&init_user_ns, gid));
413 if (pdu_write(pdu, &val, sizeof(val)))
414 errcode = -EFAULT;
415 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500416 case 'Q':{
417 const struct p9_qid *qid =
418 va_arg(ap, const struct p9_qid *);
419 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000420 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500421 qid->type, qid->version,
422 qid->path);
423 } break;
424 case 'S':{
425 const struct p9_wstat *stbuf =
426 va_arg(ap, const struct p9_wstat *);
427 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000428 p9pdu_writef(pdu, proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500429 "wwdQdddqssss?sddd",
430 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500431 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500432 stbuf->mode, stbuf->atime,
433 stbuf->mtime, stbuf->length,
434 stbuf->name, stbuf->uid,
435 stbuf->gid, stbuf->muid,
436 stbuf->extension, stbuf->n_uid,
437 stbuf->n_gid, stbuf->n_muid);
438 } break;
439 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600440 uint32_t count = va_arg(ap, uint32_t);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500441 const void *data = va_arg(ap, const void *);
442
Sripathi Kodi342fee12010-03-05 18:50:14 +0000443 errcode = p9pdu_writef(pdu, proto_version, "d",
444 count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500445 if (!errcode && pdu_write(pdu, data, count))
446 errcode = -EFAULT;
447 }
448 break;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500449 case 'U':{
450 int32_t count = va_arg(ap, int32_t);
451 const char __user *udata =
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500452 va_arg(ap, const void __user *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000453 errcode = p9pdu_writef(pdu, proto_version, "d",
454 count);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500455 if (!errcode && pdu_write_u(pdu, udata, count))
456 errcode = -EFAULT;
457 }
458 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500459 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530460 uint16_t nwname = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500461 const char **wnames = va_arg(ap, const char **);
462
Sripathi Kodi342fee12010-03-05 18:50:14 +0000463 errcode = p9pdu_writef(pdu, proto_version, "w",
464 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500465 if (!errcode) {
466 int i;
467
468 for (i = 0; i < nwname; i++) {
469 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000470 p9pdu_writef(pdu,
471 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500472 "s",
473 wnames[i]);
474 if (errcode)
475 break;
476 }
477 }
478 }
479 break;
480 case 'R':{
481 int16_t nwqid = va_arg(ap, int);
482 struct p9_qid *wqids =
483 va_arg(ap, struct p9_qid *);
484
Sripathi Kodi342fee12010-03-05 18:50:14 +0000485 errcode = p9pdu_writef(pdu, proto_version, "w",
486 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500487 if (!errcode) {
488 int i;
489
490 for (i = 0; i < nwqid; i++) {
491 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000492 p9pdu_writef(pdu,
493 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500494 "Q",
495 &wqids[i]);
496 if (errcode)
497 break;
498 }
499 }
500 }
501 break;
Sripathi Kodi87d78452010-06-18 11:50:10 +0530502 case 'I':{
503 struct p9_iattr_dotl *p9attr = va_arg(ap,
504 struct p9_iattr_dotl *);
505
506 errcode = p9pdu_writef(pdu, proto_version,
507 "ddddqqqqq",
508 p9attr->valid,
509 p9attr->mode,
510 p9attr->uid,
511 p9attr->gid,
512 p9attr->size,
513 p9attr->atime_sec,
514 p9attr->atime_nsec,
515 p9attr->mtime_sec,
516 p9attr->mtime_nsec);
517 }
518 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500519 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000520 if ((proto_version != p9_proto_2000u) &&
521 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500522 return 0;
523 break;
524 default:
525 BUG();
526 break;
527 }
528
529 if (errcode)
530 break;
531 }
532
533 return errcode;
534}
535
Sripathi Kodi342fee12010-03-05 18:50:14 +0000536int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500537{
538 va_list ap;
539 int ret;
540
541 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000542 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500543 va_end(ap);
544
545 return ret;
546}
547
548static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000549p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500550{
551 va_list ap;
552 int ret;
553
554 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000555 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500556 va_end(ap);
557
558 return ret;
559}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500560
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530561int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500562{
563 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500564 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500565
566 fake_pdu.size = len;
567 fake_pdu.capacity = len;
568 fake_pdu.sdata = buf;
569 fake_pdu.offset = 0;
570
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530571 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500572 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800573 p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530574 trace_9p_protocol_dump(clnt, &fake_pdu);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500575 }
576
577 return ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500578}
579EXPORT_SYMBOL(p9stat_read);
580
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500581int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
582{
Venkateswararao Jujjuri (JV)9bb6c102011-02-02 17:52:46 -0800583 pdu->id = type;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500584 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
585}
586
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530587int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500588{
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
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530596 trace_9p_protocol_dump(clnt, pdu);
Joe Perches5d385152011-11-28 10:40:46 -0800597 p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
598 pdu->size, pdu->id, pdu->tag);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500599
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500600 return err;
601}
602
603void p9pdu_reset(struct p9_fcall *pdu)
604{
605 pdu->offset = 0;
606 pdu->size = 0;
607}
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000608
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530609int p9dirent_read(struct p9_client *clnt, char *buf, int len,
610 struct p9_dirent *dirent)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000611{
612 struct p9_fcall fake_pdu;
613 int ret;
614 char *nameptr;
615
616 fake_pdu.size = len;
617 fake_pdu.capacity = len;
618 fake_pdu.sdata = buf;
619 fake_pdu.offset = 0;
620
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530621 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
622 &dirent->d_off, &dirent->d_type, &nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000623 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800624 p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530625 trace_9p_protocol_dump(clnt, &fake_pdu);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000626 goto out;
627 }
628
629 strcpy(dirent->d_name, nameptr);
Pedro Scarapicchia Junior1b0bcbc2011-05-09 14:10:49 +0000630 kfree(nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000631
632out:
633 return fake_pdu.offset;
634}
635EXPORT_SYMBOL(p9dirent_read);