blob: ab9127ec5b7a6881e7dd2116e49819186675562f [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 W. Biederman447c5092013-01-29 16:18:50 -0800202 stbuf->n_uid = stbuf->n_muid = INVALID_UID;
203 stbuf->n_gid = INVALID_GID;
204
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500205 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000206 p9pdu_readf(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800207 "wwdQdddqssss?sugu",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500208 &stbuf->size, &stbuf->type,
209 &stbuf->dev, &stbuf->qid,
210 &stbuf->mode, &stbuf->atime,
211 &stbuf->mtime, &stbuf->length,
212 &stbuf->name, &stbuf->uid,
213 &stbuf->gid, &stbuf->muid,
214 &stbuf->extension,
215 &stbuf->n_uid, &stbuf->n_gid,
216 &stbuf->n_muid);
217 if (errcode)
218 p9stat_free(stbuf);
219 }
220 break;
221 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600222 uint32_t *count = va_arg(ap, uint32_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500223 void **data = va_arg(ap, void **);
224
225 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000226 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500227 if (!errcode) {
228 *count =
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600229 min_t(uint32_t, *count,
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000230 pdu->size - pdu->offset);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500231 *data = &pdu->sdata[pdu->offset];
232 }
233 }
234 break;
235 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530236 uint16_t *nwname = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500237 char ***wnames = va_arg(ap, char ***);
238
Sripathi Kodi342fee12010-03-05 18:50:14 +0000239 errcode = p9pdu_readf(pdu, proto_version,
240 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500241 if (!errcode) {
242 *wnames =
243 kmalloc(sizeof(char *) * *nwname,
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530244 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500245 if (!*wnames)
246 errcode = -ENOMEM;
247 }
248
249 if (!errcode) {
250 int i;
251
252 for (i = 0; i < *nwname; i++) {
253 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000254 p9pdu_readf(pdu,
255 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500256 "s",
257 &(*wnames)[i]);
258 if (errcode)
259 break;
260 }
261 }
262
263 if (errcode) {
264 if (*wnames) {
265 int i;
266
267 for (i = 0; i < *nwname; i++)
268 kfree((*wnames)[i]);
269 }
270 kfree(*wnames);
271 *wnames = NULL;
272 }
273 }
274 break;
275 case 'R':{
276 int16_t *nwqid = va_arg(ap, int16_t *);
277 struct p9_qid **wqids =
278 va_arg(ap, struct p9_qid **);
279
280 *wqids = NULL;
281
282 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000283 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500284 if (!errcode) {
285 *wqids =
286 kmalloc(*nwqid *
287 sizeof(struct p9_qid),
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530288 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500289 if (*wqids == NULL)
290 errcode = -ENOMEM;
291 }
292
293 if (!errcode) {
294 int i;
295
296 for (i = 0; i < *nwqid; i++) {
297 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000298 p9pdu_readf(pdu,
299 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500300 "Q",
301 &(*wqids)[i]);
302 if (errcode)
303 break;
304 }
305 }
306
307 if (errcode) {
308 kfree(*wqids);
309 *wqids = NULL;
310 }
311 }
312 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530313 case 'A': {
314 struct p9_stat_dotl *stbuf =
315 va_arg(ap, struct p9_stat_dotl *);
316
317 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
318 errcode =
319 p9pdu_readf(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800320 "qQdugqqqqqqqqqqqqqqq",
Sripathi Kodif0853122010-07-12 20:07:23 +0530321 &stbuf->st_result_mask,
322 &stbuf->qid,
323 &stbuf->st_mode,
324 &stbuf->st_uid, &stbuf->st_gid,
325 &stbuf->st_nlink,
326 &stbuf->st_rdev, &stbuf->st_size,
327 &stbuf->st_blksize, &stbuf->st_blocks,
328 &stbuf->st_atime_sec,
329 &stbuf->st_atime_nsec,
330 &stbuf->st_mtime_sec,
331 &stbuf->st_mtime_nsec,
332 &stbuf->st_ctime_sec,
333 &stbuf->st_ctime_nsec,
334 &stbuf->st_btime_sec,
335 &stbuf->st_btime_nsec,
336 &stbuf->st_gen,
337 &stbuf->st_data_version);
338 }
339 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500340 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000341 if ((proto_version != p9_proto_2000u) &&
342 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500343 return 0;
344 break;
345 default:
346 BUG();
347 break;
348 }
349
350 if (errcode)
351 break;
352 }
353
354 return errcode;
355}
356
357int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000358p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
359 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500360{
361 const char *ptr;
362 int errcode = 0;
363
364 for (ptr = fmt; *ptr; ptr++) {
365 switch (*ptr) {
366 case 'b':{
367 int8_t val = va_arg(ap, int);
368 if (pdu_write(pdu, &val, sizeof(val)))
369 errcode = -EFAULT;
370 }
371 break;
372 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800373 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500374 if (pdu_write(pdu, &val, sizeof(val)))
375 errcode = -EFAULT;
376 }
377 break;
378 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800379 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500380 if (pdu_write(pdu, &val, sizeof(val)))
381 errcode = -EFAULT;
382 }
383 break;
384 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800385 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500386 if (pdu_write(pdu, &val, sizeof(val)))
387 errcode = -EFAULT;
388 }
389 break;
390 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500391 const char *sptr = va_arg(ap, const char *);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600392 uint16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500393 if (sptr)
Dan Carpenterd31bb4f2012-06-26 23:01:41 +0000394 len = min_t(size_t, strlen(sptr),
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600395 USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500396
Sripathi Kodi342fee12010-03-05 18:50:14 +0000397 errcode = p9pdu_writef(pdu, proto_version,
398 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500399 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500400 errcode = -EFAULT;
401 }
402 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800403 case 'u': {
404 kuid_t uid = va_arg(ap, kuid_t);
405 __le32 val = cpu_to_le32(
406 from_kuid(&init_user_ns, uid));
407 if (pdu_write(pdu, &val, sizeof(val)))
408 errcode = -EFAULT;
409 } break;
410 case 'g': {
411 kgid_t gid = va_arg(ap, kgid_t);
412 __le32 val = cpu_to_le32(
413 from_kgid(&init_user_ns, gid));
414 if (pdu_write(pdu, &val, sizeof(val)))
415 errcode = -EFAULT;
416 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500417 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 W. Biederman447c5092013-01-29 16:18:50 -0800430 "wwdQdddqssss?sugu",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500431 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':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600441 uint32_t count = va_arg(ap, uint32_t);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500442 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':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530461 uint16_t nwname = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500462 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,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800508 "ddugqqqqq",
Sripathi Kodi87d78452010-06-18 11:50:10 +0530509 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
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530562int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
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
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530572 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500573 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800574 p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530575 trace_9p_protocol_dump(clnt, &fake_pdu);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500576 }
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{
Venkateswararao Jujjuri (JV)9bb6c102011-02-02 17:52:46 -0800584 pdu->id = type;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500585 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
586}
587
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530588int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500589{
590 int size = pdu->size;
591 int err;
592
593 pdu->size = 0;
594 err = p9pdu_writef(pdu, 0, "d", size);
595 pdu->size = size;
596
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530597 trace_9p_protocol_dump(clnt, pdu);
Joe Perches5d385152011-11-28 10:40:46 -0800598 p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
599 pdu->size, pdu->id, pdu->tag);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500600
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500601 return err;
602}
603
604void p9pdu_reset(struct p9_fcall *pdu)
605{
606 pdu->offset = 0;
607 pdu->size = 0;
608}
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000609
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530610int p9dirent_read(struct p9_client *clnt, char *buf, int len,
611 struct p9_dirent *dirent)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000612{
613 struct p9_fcall fake_pdu;
614 int ret;
615 char *nameptr;
616
617 fake_pdu.size = len;
618 fake_pdu.capacity = len;
619 fake_pdu.sdata = buf;
620 fake_pdu.offset = 0;
621
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530622 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
623 &dirent->d_off, &dirent->d_type, &nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000624 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800625 p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530626 trace_9p_protocol_dump(clnt, &fake_pdu);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000627 goto out;
628 }
629
630 strcpy(dirent->d_name, nameptr);
Pedro Scarapicchia Junior1b0bcbcf62011-05-09 14:10:49 +0000631 kfree(nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000632
633out:
634 return fake_pdu.offset;
635}
636EXPORT_SYMBOL(p9dirent_read);