blob: 931ea00c4fedb9a8a2c4d2ae9a1f24d7e51d7d04 [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>
Al Viro4f3b35c2015-04-01 19:57:53 -040036#include <linux/uio.h>
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050037#include <net/9p/9p.h>
38#include <net/9p/client.h>
39#include "protocol.h"
40
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +053041#include <trace/events/9p.h>
42
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050043static int
Sripathi Kodi342fee12010-03-05 18:50:14 +000044p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050045
46void p9stat_free(struct p9_wstat *stbuf)
47{
48 kfree(stbuf->name);
49 kfree(stbuf->uid);
50 kfree(stbuf->gid);
51 kfree(stbuf->muid);
52 kfree(stbuf->extension);
53}
54EXPORT_SYMBOL(p9stat_free);
55
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +053056size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050057{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000058 size_t len = min(pdu->size - pdu->offset, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050059 memcpy(data, &pdu->sdata[pdu->offset], len);
60 pdu->offset += len;
61 return size - len;
62}
63
64static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
65{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000066 size_t len = min(pdu->capacity - pdu->size, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050067 memcpy(&pdu->sdata[pdu->size], data, len);
68 pdu->size += len;
69 return size - len;
70}
71
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050072static size_t
Al Viro4f3b35c2015-04-01 19:57:53 -040073pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050074{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000075 size_t len = min(pdu->capacity - pdu->size, size);
Al Viro4f3b35c2015-04-01 19:57:53 -040076 struct iov_iter i = *from;
Al Viro1c512a72017-02-17 23:16:09 -050077 if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, &i))
Aneesh Kumar K.V7b3bb3f2010-10-19 09:17:02 +053078 len = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050079
80 pdu->size += len;
81 return size - len;
82}
83
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050084/*
85 b - int8_t
86 w - int16_t
87 d - int32_t
88 q - int64_t
89 s - string
Eric W. Biederman97fc8b12013-01-29 17:07:42 -080090 u - numeric uid
91 g - numeric gid
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050092 S - stat
93 Q - qid
94 D - data blob (int32_t size followed by void *, results are not freed)
95 T - array of strings (int16_t count, followed by strings)
96 R - array of qids (int16_t count, followed by qids)
Sripathi Kodif0853122010-07-12 20:07:23 +053097 A - stat for 9p2000.L (p9_stat_dotl)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050098 ? - if optional = 1, continue parsing
99*/
100
101static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000102p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
103 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500104{
105 const char *ptr;
106 int errcode = 0;
107
108 for (ptr = fmt; *ptr; ptr++) {
109 switch (*ptr) {
110 case 'b':{
111 int8_t *val = va_arg(ap, int8_t *);
112 if (pdu_read(pdu, val, sizeof(*val))) {
113 errcode = -EFAULT;
114 break;
115 }
116 }
117 break;
118 case 'w':{
119 int16_t *val = va_arg(ap, int16_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800120 __le16 le_val;
121 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500122 errcode = -EFAULT;
123 break;
124 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800125 *val = le16_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500126 }
127 break;
128 case 'd':{
129 int32_t *val = va_arg(ap, int32_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800130 __le32 le_val;
131 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500132 errcode = -EFAULT;
133 break;
134 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800135 *val = le32_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500136 }
137 break;
138 case 'q':{
139 int64_t *val = va_arg(ap, int64_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800140 __le64 le_val;
141 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500142 errcode = -EFAULT;
143 break;
144 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800145 *val = le64_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500146 }
147 break;
148 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500149 char **sptr = va_arg(ap, char **);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600150 uint16_t len;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500151
Sripathi Kodi342fee12010-03-05 18:50:14 +0000152 errcode = p9pdu_readf(pdu, proto_version,
153 "w", &len);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500154 if (errcode)
155 break;
156
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530157 *sptr = kmalloc(len + 1, GFP_NOFS);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500158 if (*sptr == NULL) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500159 errcode = -EFAULT;
160 break;
161 }
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600162 if (pdu_read(pdu, *sptr, len)) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500163 errcode = -EFAULT;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500164 kfree(*sptr);
165 *sptr = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500166 } else
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600167 (*sptr)[len] = 0;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500168 }
169 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800170 case 'u': {
171 kuid_t *uid = va_arg(ap, kuid_t *);
172 __le32 le_val;
173 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
174 errcode = -EFAULT;
175 break;
176 }
177 *uid = make_kuid(&init_user_ns,
178 le32_to_cpu(le_val));
179 } break;
180 case 'g': {
181 kgid_t *gid = va_arg(ap, kgid_t *);
182 __le32 le_val;
183 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
184 errcode = -EFAULT;
185 break;
186 }
187 *gid = make_kgid(&init_user_ns,
188 le32_to_cpu(le_val));
189 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500190 case 'Q':{
191 struct p9_qid *qid =
192 va_arg(ap, struct p9_qid *);
193
Sripathi Kodi342fee12010-03-05 18:50:14 +0000194 errcode = p9pdu_readf(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500195 &qid->type, &qid->version,
196 &qid->path);
197 }
198 break;
199 case 'S':{
200 struct p9_wstat *stbuf =
201 va_arg(ap, struct p9_wstat *);
202
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500203 memset(stbuf, 0, sizeof(struct p9_wstat));
Eric W. Biederman447c5092013-01-29 16:18:50 -0800204 stbuf->n_uid = stbuf->n_muid = INVALID_UID;
205 stbuf->n_gid = INVALID_GID;
206
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500207 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000208 p9pdu_readf(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800209 "wwdQdddqssss?sugu",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500210 &stbuf->size, &stbuf->type,
211 &stbuf->dev, &stbuf->qid,
212 &stbuf->mode, &stbuf->atime,
213 &stbuf->mtime, &stbuf->length,
214 &stbuf->name, &stbuf->uid,
215 &stbuf->gid, &stbuf->muid,
216 &stbuf->extension,
217 &stbuf->n_uid, &stbuf->n_gid,
218 &stbuf->n_muid);
219 if (errcode)
220 p9stat_free(stbuf);
221 }
222 break;
223 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600224 uint32_t *count = va_arg(ap, uint32_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500225 void **data = va_arg(ap, void **);
226
227 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000228 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500229 if (!errcode) {
230 *count =
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600231 min_t(uint32_t, *count,
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000232 pdu->size - pdu->offset);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500233 *data = &pdu->sdata[pdu->offset];
234 }
235 }
236 break;
237 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530238 uint16_t *nwname = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500239 char ***wnames = va_arg(ap, char ***);
240
Sripathi Kodi342fee12010-03-05 18:50:14 +0000241 errcode = p9pdu_readf(pdu, proto_version,
242 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500243 if (!errcode) {
244 *wnames =
Kees Cook6da2ec52018-06-12 13:55:00 -0700245 kmalloc_array(*nwname,
246 sizeof(char *),
247 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500248 if (!*wnames)
249 errcode = -ENOMEM;
250 }
251
252 if (!errcode) {
253 int i;
254
255 for (i = 0; i < *nwname; i++) {
256 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000257 p9pdu_readf(pdu,
258 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500259 "s",
260 &(*wnames)[i]);
261 if (errcode)
262 break;
263 }
264 }
265
266 if (errcode) {
267 if (*wnames) {
268 int i;
269
270 for (i = 0; i < *nwname; i++)
271 kfree((*wnames)[i]);
272 }
273 kfree(*wnames);
274 *wnames = NULL;
275 }
276 }
277 break;
278 case 'R':{
Kirill A. Shutemov6250a8b2014-12-30 02:48:09 +0200279 uint16_t *nwqid = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500280 struct p9_qid **wqids =
281 va_arg(ap, struct p9_qid **);
282
283 *wqids = NULL;
284
285 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000286 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500287 if (!errcode) {
288 *wqids =
Kees Cook6da2ec52018-06-12 13:55:00 -0700289 kmalloc_array(*nwqid,
290 sizeof(struct p9_qid),
291 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500292 if (*wqids == NULL)
293 errcode = -ENOMEM;
294 }
295
296 if (!errcode) {
297 int i;
298
299 for (i = 0; i < *nwqid; i++) {
300 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000301 p9pdu_readf(pdu,
302 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500303 "Q",
304 &(*wqids)[i]);
305 if (errcode)
306 break;
307 }
308 }
309
310 if (errcode) {
311 kfree(*wqids);
312 *wqids = NULL;
313 }
314 }
315 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530316 case 'A': {
317 struct p9_stat_dotl *stbuf =
318 va_arg(ap, struct p9_stat_dotl *);
319
320 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
321 errcode =
322 p9pdu_readf(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800323 "qQdugqqqqqqqqqqqqqqq",
Sripathi Kodif0853122010-07-12 20:07:23 +0530324 &stbuf->st_result_mask,
325 &stbuf->qid,
326 &stbuf->st_mode,
327 &stbuf->st_uid, &stbuf->st_gid,
328 &stbuf->st_nlink,
329 &stbuf->st_rdev, &stbuf->st_size,
330 &stbuf->st_blksize, &stbuf->st_blocks,
331 &stbuf->st_atime_sec,
332 &stbuf->st_atime_nsec,
333 &stbuf->st_mtime_sec,
334 &stbuf->st_mtime_nsec,
335 &stbuf->st_ctime_sec,
336 &stbuf->st_ctime_nsec,
337 &stbuf->st_btime_sec,
338 &stbuf->st_btime_nsec,
339 &stbuf->st_gen,
340 &stbuf->st_data_version);
341 }
342 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500343 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000344 if ((proto_version != p9_proto_2000u) &&
345 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500346 return 0;
347 break;
348 default:
349 BUG();
350 break;
351 }
352
353 if (errcode)
354 break;
355 }
356
357 return errcode;
358}
359
360int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000361p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
362 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500363{
364 const char *ptr;
365 int errcode = 0;
366
367 for (ptr = fmt; *ptr; ptr++) {
368 switch (*ptr) {
369 case 'b':{
370 int8_t val = va_arg(ap, int);
371 if (pdu_write(pdu, &val, sizeof(val)))
372 errcode = -EFAULT;
373 }
374 break;
375 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800376 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500377 if (pdu_write(pdu, &val, sizeof(val)))
378 errcode = -EFAULT;
379 }
380 break;
381 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800382 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500383 if (pdu_write(pdu, &val, sizeof(val)))
384 errcode = -EFAULT;
385 }
386 break;
387 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800388 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500389 if (pdu_write(pdu, &val, sizeof(val)))
390 errcode = -EFAULT;
391 }
392 break;
393 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500394 const char *sptr = va_arg(ap, const char *);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600395 uint16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500396 if (sptr)
Dan Carpenterd31bb4f2012-06-26 23:01:41 +0000397 len = min_t(size_t, strlen(sptr),
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600398 USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500399
Sripathi Kodi342fee12010-03-05 18:50:14 +0000400 errcode = p9pdu_writef(pdu, proto_version,
401 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500402 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500403 errcode = -EFAULT;
404 }
405 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800406 case 'u': {
407 kuid_t uid = va_arg(ap, kuid_t);
408 __le32 val = cpu_to_le32(
409 from_kuid(&init_user_ns, uid));
410 if (pdu_write(pdu, &val, sizeof(val)))
411 errcode = -EFAULT;
412 } break;
413 case 'g': {
414 kgid_t gid = va_arg(ap, kgid_t);
415 __le32 val = cpu_to_le32(
416 from_kgid(&init_user_ns, gid));
417 if (pdu_write(pdu, &val, sizeof(val)))
418 errcode = -EFAULT;
419 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500420 case 'Q':{
421 const struct p9_qid *qid =
422 va_arg(ap, const struct p9_qid *);
423 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000424 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500425 qid->type, qid->version,
426 qid->path);
427 } break;
428 case 'S':{
429 const struct p9_wstat *stbuf =
430 va_arg(ap, const struct p9_wstat *);
431 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000432 p9pdu_writef(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800433 "wwdQdddqssss?sugu",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500434 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500435 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500436 stbuf->mode, stbuf->atime,
437 stbuf->mtime, stbuf->length,
438 stbuf->name, stbuf->uid,
439 stbuf->gid, stbuf->muid,
440 stbuf->extension, stbuf->n_uid,
441 stbuf->n_gid, stbuf->n_muid);
442 } break;
Al Viro4f3b35c2015-04-01 19:57:53 -0400443 case 'V':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600444 uint32_t count = va_arg(ap, uint32_t);
Al Viro4f3b35c2015-04-01 19:57:53 -0400445 struct iov_iter *from =
446 va_arg(ap, struct iov_iter *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000447 errcode = p9pdu_writef(pdu, proto_version, "d",
448 count);
Al Viro4f3b35c2015-04-01 19:57:53 -0400449 if (!errcode && pdu_write_u(pdu, from, count))
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500450 errcode = -EFAULT;
451 }
452 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500453 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530454 uint16_t nwname = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500455 const char **wnames = va_arg(ap, const char **);
456
Sripathi Kodi342fee12010-03-05 18:50:14 +0000457 errcode = p9pdu_writef(pdu, proto_version, "w",
458 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500459 if (!errcode) {
460 int i;
461
462 for (i = 0; i < nwname; i++) {
463 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000464 p9pdu_writef(pdu,
465 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500466 "s",
467 wnames[i]);
468 if (errcode)
469 break;
470 }
471 }
472 }
473 break;
474 case 'R':{
Kirill A. Shutemov6250a8b2014-12-30 02:48:09 +0200475 uint16_t nwqid = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500476 struct p9_qid *wqids =
477 va_arg(ap, struct p9_qid *);
478
Sripathi Kodi342fee12010-03-05 18:50:14 +0000479 errcode = p9pdu_writef(pdu, proto_version, "w",
480 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500481 if (!errcode) {
482 int i;
483
484 for (i = 0; i < nwqid; i++) {
485 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000486 p9pdu_writef(pdu,
487 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500488 "Q",
489 &wqids[i]);
490 if (errcode)
491 break;
492 }
493 }
494 }
495 break;
Sripathi Kodi87d78452010-06-18 11:50:10 +0530496 case 'I':{
497 struct p9_iattr_dotl *p9attr = va_arg(ap,
498 struct p9_iattr_dotl *);
499
500 errcode = p9pdu_writef(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800501 "ddugqqqqq",
Sripathi Kodi87d78452010-06-18 11:50:10 +0530502 p9attr->valid,
503 p9attr->mode,
504 p9attr->uid,
505 p9attr->gid,
506 p9attr->size,
507 p9attr->atime_sec,
508 p9attr->atime_nsec,
509 p9attr->mtime_sec,
510 p9attr->mtime_nsec);
511 }
512 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500513 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000514 if ((proto_version != p9_proto_2000u) &&
515 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500516 return 0;
517 break;
518 default:
519 BUG();
520 break;
521 }
522
523 if (errcode)
524 break;
525 }
526
527 return errcode;
528}
529
Sripathi Kodi342fee12010-03-05 18:50:14 +0000530int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500531{
532 va_list ap;
533 int ret;
534
535 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000536 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500537 va_end(ap);
538
539 return ret;
540}
541
542static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000543p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500544{
545 va_list ap;
546 int ret;
547
548 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000549 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500550 va_end(ap);
551
552 return ret;
553}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500554
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530555int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500556{
557 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500558 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500559
560 fake_pdu.size = len;
561 fake_pdu.capacity = len;
562 fake_pdu.sdata = buf;
563 fake_pdu.offset = 0;
564
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530565 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500566 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800567 p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530568 trace_9p_protocol_dump(clnt, &fake_pdu);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500569 }
570
571 return ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500572}
573EXPORT_SYMBOL(p9stat_read);
574
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500575int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
576{
Venkateswararao Jujjuri (JV)9bb6c102011-02-02 17:52:46 -0800577 pdu->id = type;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500578 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
579}
580
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530581int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500582{
583 int size = pdu->size;
584 int err;
585
586 pdu->size = 0;
587 err = p9pdu_writef(pdu, 0, "d", size);
588 pdu->size = size;
589
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530590 trace_9p_protocol_dump(clnt, pdu);
Joe Perches5d385152011-11-28 10:40:46 -0800591 p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
592 pdu->size, pdu->id, pdu->tag);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500593
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500594 return err;
595}
596
597void p9pdu_reset(struct p9_fcall *pdu)
598{
599 pdu->offset = 0;
600 pdu->size = 0;
601}
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000602
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530603int p9dirent_read(struct p9_client *clnt, char *buf, int len,
604 struct p9_dirent *dirent)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000605{
606 struct p9_fcall fake_pdu;
607 int ret;
608 char *nameptr;
609
610 fake_pdu.size = len;
611 fake_pdu.capacity = len;
612 fake_pdu.sdata = buf;
613 fake_pdu.offset = 0;
614
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530615 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
616 &dirent->d_off, &dirent->d_type, &nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000617 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800618 p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530619 trace_9p_protocol_dump(clnt, &fake_pdu);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000620 goto out;
621 }
622
623 strcpy(dirent->d_name, nameptr);
Pedro Scarapicchia Junior1b0bcbcf62011-05-09 14:10:49 +0000624 kfree(nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000625
626out:
627 return fake_pdu.offset;
628}
629EXPORT_SYMBOL(p9dirent_read);