blob: ed1e39ccaebf20b4eae13f11da95d026b7be6984 [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);
Dominique Martinet0cf4fa72018-08-28 07:32:35 +090049 stbuf->name = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050050 kfree(stbuf->uid);
Dominique Martinet0cf4fa72018-08-28 07:32:35 +090051 stbuf->uid = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050052 kfree(stbuf->gid);
Dominique Martinet0cf4fa72018-08-28 07:32:35 +090053 stbuf->gid = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050054 kfree(stbuf->muid);
Dominique Martinet0cf4fa72018-08-28 07:32:35 +090055 stbuf->muid = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050056 kfree(stbuf->extension);
Dominique Martinet0cf4fa72018-08-28 07:32:35 +090057 stbuf->extension = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050058}
59EXPORT_SYMBOL(p9stat_free);
60
Aneesh Kumar K.Vabfa0342011-08-16 10:50:10 +053061size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050062{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000063 size_t len = min(pdu->size - pdu->offset, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050064 memcpy(data, &pdu->sdata[pdu->offset], len);
65 pdu->offset += len;
66 return size - len;
67}
68
69static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
70{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000071 size_t len = min(pdu->capacity - pdu->size, size);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050072 memcpy(&pdu->sdata[pdu->size], data, len);
73 pdu->size += len;
74 return size - len;
75}
76
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050077static size_t
Al Viro4f3b35c2015-04-01 19:57:53 -040078pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050079{
Thiago Farina01b0c5c2010-12-04 15:22:46 +000080 size_t len = min(pdu->capacity - pdu->size, size);
Al Viro4f3b35c2015-04-01 19:57:53 -040081 struct iov_iter i = *from;
82 if (copy_from_iter(&pdu->sdata[pdu->size], len, &i) != len)
Aneesh Kumar K.V7b3bb3f2010-10-19 09:17:02 +053083 len = 0;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -050084
85 pdu->size += len;
86 return size - len;
87}
88
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050089/*
90 b - int8_t
91 w - int16_t
92 d - int32_t
93 q - int64_t
94 s - string
Eric W. Biederman97fc8b12013-01-29 17:07:42 -080095 u - numeric uid
96 g - numeric gid
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050097 S - stat
98 Q - qid
99 D - data blob (int32_t size followed by void *, results are not freed)
100 T - array of strings (int16_t count, followed by strings)
101 R - array of qids (int16_t count, followed by qids)
Sripathi Kodif0853122010-07-12 20:07:23 +0530102 A - stat for 9p2000.L (p9_stat_dotl)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500103 ? - if optional = 1, continue parsing
104*/
105
106static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000107p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
108 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500109{
110 const char *ptr;
111 int errcode = 0;
112
113 for (ptr = fmt; *ptr; ptr++) {
114 switch (*ptr) {
115 case 'b':{
116 int8_t *val = va_arg(ap, int8_t *);
117 if (pdu_read(pdu, val, sizeof(*val))) {
118 errcode = -EFAULT;
119 break;
120 }
121 }
122 break;
123 case 'w':{
124 int16_t *val = va_arg(ap, int16_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800125 __le16 le_val;
126 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500127 errcode = -EFAULT;
128 break;
129 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800130 *val = le16_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500131 }
132 break;
133 case 'd':{
134 int32_t *val = va_arg(ap, int32_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800135 __le32 le_val;
136 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500137 errcode = -EFAULT;
138 break;
139 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800140 *val = le32_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500141 }
142 break;
143 case 'q':{
144 int64_t *val = va_arg(ap, int64_t *);
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800145 __le64 le_val;
146 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500147 errcode = -EFAULT;
148 break;
149 }
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800150 *val = le64_to_cpu(le_val);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500151 }
152 break;
153 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500154 char **sptr = va_arg(ap, char **);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600155 uint16_t len;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500156
Sripathi Kodi342fee12010-03-05 18:50:14 +0000157 errcode = p9pdu_readf(pdu, proto_version,
158 "w", &len);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500159 if (errcode)
160 break;
161
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530162 *sptr = kmalloc(len + 1, GFP_NOFS);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500163 if (*sptr == NULL) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500164 errcode = -EFAULT;
165 break;
166 }
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600167 if (pdu_read(pdu, *sptr, len)) {
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500168 errcode = -EFAULT;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500169 kfree(*sptr);
170 *sptr = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500171 } else
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600172 (*sptr)[len] = 0;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500173 }
174 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800175 case 'u': {
176 kuid_t *uid = va_arg(ap, kuid_t *);
177 __le32 le_val;
178 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
179 errcode = -EFAULT;
180 break;
181 }
182 *uid = make_kuid(&init_user_ns,
183 le32_to_cpu(le_val));
184 } break;
185 case 'g': {
186 kgid_t *gid = va_arg(ap, kgid_t *);
187 __le32 le_val;
188 if (pdu_read(pdu, &le_val, sizeof(le_val))) {
189 errcode = -EFAULT;
190 break;
191 }
192 *gid = make_kgid(&init_user_ns,
193 le32_to_cpu(le_val));
194 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500195 case 'Q':{
196 struct p9_qid *qid =
197 va_arg(ap, struct p9_qid *);
198
Sripathi Kodi342fee12010-03-05 18:50:14 +0000199 errcode = p9pdu_readf(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500200 &qid->type, &qid->version,
201 &qid->path);
202 }
203 break;
204 case 'S':{
205 struct p9_wstat *stbuf =
206 va_arg(ap, struct p9_wstat *);
207
Eric Van Hensbergenf0a0ac22008-10-17 12:45:23 -0500208 memset(stbuf, 0, sizeof(struct p9_wstat));
Eric W. Biederman447c5092013-01-29 16:18:50 -0800209 stbuf->n_uid = stbuf->n_muid = INVALID_UID;
210 stbuf->n_gid = INVALID_GID;
211
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500212 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000213 p9pdu_readf(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800214 "wwdQdddqssss?sugu",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500215 &stbuf->size, &stbuf->type,
216 &stbuf->dev, &stbuf->qid,
217 &stbuf->mode, &stbuf->atime,
218 &stbuf->mtime, &stbuf->length,
219 &stbuf->name, &stbuf->uid,
220 &stbuf->gid, &stbuf->muid,
221 &stbuf->extension,
222 &stbuf->n_uid, &stbuf->n_gid,
223 &stbuf->n_muid);
224 if (errcode)
225 p9stat_free(stbuf);
226 }
227 break;
228 case 'D':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600229 uint32_t *count = va_arg(ap, uint32_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500230 void **data = va_arg(ap, void **);
231
232 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000233 p9pdu_readf(pdu, proto_version, "d", count);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500234 if (!errcode) {
235 *count =
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600236 min_t(uint32_t, *count,
Thiago Farina01b0c5c2010-12-04 15:22:46 +0000237 pdu->size - pdu->offset);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500238 *data = &pdu->sdata[pdu->offset];
239 }
240 }
241 break;
242 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530243 uint16_t *nwname = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500244 char ***wnames = va_arg(ap, char ***);
245
Sripathi Kodi342fee12010-03-05 18:50:14 +0000246 errcode = p9pdu_readf(pdu, proto_version,
247 "w", nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500248 if (!errcode) {
249 *wnames =
250 kmalloc(sizeof(char *) * *nwname,
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530251 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500252 if (!*wnames)
253 errcode = -ENOMEM;
254 }
255
256 if (!errcode) {
257 int i;
258
259 for (i = 0; i < *nwname; i++) {
260 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000261 p9pdu_readf(pdu,
262 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500263 "s",
264 &(*wnames)[i]);
265 if (errcode)
266 break;
267 }
268 }
269
270 if (errcode) {
271 if (*wnames) {
272 int i;
273
274 for (i = 0; i < *nwname; i++)
275 kfree((*wnames)[i]);
276 }
277 kfree(*wnames);
278 *wnames = NULL;
279 }
280 }
281 break;
282 case 'R':{
Kirill A. Shutemov6250a8b2014-12-30 02:48:09 +0200283 uint16_t *nwqid = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500284 struct p9_qid **wqids =
285 va_arg(ap, struct p9_qid **);
286
287 *wqids = NULL;
288
289 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000290 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500291 if (!errcode) {
292 *wqids =
293 kmalloc(*nwqid *
294 sizeof(struct p9_qid),
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530295 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500296 if (*wqids == NULL)
297 errcode = -ENOMEM;
298 }
299
300 if (!errcode) {
301 int i;
302
303 for (i = 0; i < *nwqid; i++) {
304 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000305 p9pdu_readf(pdu,
306 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500307 "Q",
308 &(*wqids)[i]);
309 if (errcode)
310 break;
311 }
312 }
313
314 if (errcode) {
315 kfree(*wqids);
316 *wqids = NULL;
317 }
318 }
319 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530320 case 'A': {
321 struct p9_stat_dotl *stbuf =
322 va_arg(ap, struct p9_stat_dotl *);
323
324 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
325 errcode =
326 p9pdu_readf(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800327 "qQdugqqqqqqqqqqqqqqq",
Sripathi Kodif0853122010-07-12 20:07:23 +0530328 &stbuf->st_result_mask,
329 &stbuf->qid,
330 &stbuf->st_mode,
331 &stbuf->st_uid, &stbuf->st_gid,
332 &stbuf->st_nlink,
333 &stbuf->st_rdev, &stbuf->st_size,
334 &stbuf->st_blksize, &stbuf->st_blocks,
335 &stbuf->st_atime_sec,
336 &stbuf->st_atime_nsec,
337 &stbuf->st_mtime_sec,
338 &stbuf->st_mtime_nsec,
339 &stbuf->st_ctime_sec,
340 &stbuf->st_ctime_nsec,
341 &stbuf->st_btime_sec,
342 &stbuf->st_btime_nsec,
343 &stbuf->st_gen,
344 &stbuf->st_data_version);
345 }
346 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500347 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000348 if ((proto_version != p9_proto_2000u) &&
349 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500350 return 0;
351 break;
352 default:
353 BUG();
354 break;
355 }
356
357 if (errcode)
358 break;
359 }
360
361 return errcode;
362}
363
364int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000365p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
366 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500367{
368 const char *ptr;
369 int errcode = 0;
370
371 for (ptr = fmt; *ptr; ptr++) {
372 switch (*ptr) {
373 case 'b':{
374 int8_t val = va_arg(ap, int);
375 if (pdu_write(pdu, &val, sizeof(val)))
376 errcode = -EFAULT;
377 }
378 break;
379 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800380 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500381 if (pdu_write(pdu, &val, sizeof(val)))
382 errcode = -EFAULT;
383 }
384 break;
385 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800386 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500387 if (pdu_write(pdu, &val, sizeof(val)))
388 errcode = -EFAULT;
389 }
390 break;
391 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800392 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500393 if (pdu_write(pdu, &val, sizeof(val)))
394 errcode = -EFAULT;
395 }
396 break;
397 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500398 const char *sptr = va_arg(ap, const char *);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600399 uint16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500400 if (sptr)
Dan Carpenterd31bb4f2012-06-26 23:01:41 +0000401 len = min_t(size_t, strlen(sptr),
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600402 USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500403
Sripathi Kodi342fee12010-03-05 18:50:14 +0000404 errcode = p9pdu_writef(pdu, proto_version,
405 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500406 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500407 errcode = -EFAULT;
408 }
409 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800410 case 'u': {
411 kuid_t uid = va_arg(ap, kuid_t);
412 __le32 val = cpu_to_le32(
413 from_kuid(&init_user_ns, uid));
414 if (pdu_write(pdu, &val, sizeof(val)))
415 errcode = -EFAULT;
416 } break;
417 case 'g': {
418 kgid_t gid = va_arg(ap, kgid_t);
419 __le32 val = cpu_to_le32(
420 from_kgid(&init_user_ns, gid));
421 if (pdu_write(pdu, &val, sizeof(val)))
422 errcode = -EFAULT;
423 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500424 case 'Q':{
425 const struct p9_qid *qid =
426 va_arg(ap, const struct p9_qid *);
427 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000428 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500429 qid->type, qid->version,
430 qid->path);
431 } break;
432 case 'S':{
433 const struct p9_wstat *stbuf =
434 va_arg(ap, const struct p9_wstat *);
435 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000436 p9pdu_writef(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800437 "wwdQdddqssss?sugu",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500438 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500439 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500440 stbuf->mode, stbuf->atime,
441 stbuf->mtime, stbuf->length,
442 stbuf->name, stbuf->uid,
443 stbuf->gid, stbuf->muid,
444 stbuf->extension, stbuf->n_uid,
445 stbuf->n_gid, stbuf->n_muid);
446 } break;
Al Viro4f3b35c2015-04-01 19:57:53 -0400447 case 'V':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600448 uint32_t count = va_arg(ap, uint32_t);
Al Viro4f3b35c2015-04-01 19:57:53 -0400449 struct iov_iter *from =
450 va_arg(ap, struct iov_iter *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000451 errcode = p9pdu_writef(pdu, proto_version, "d",
452 count);
Al Viro4f3b35c2015-04-01 19:57:53 -0400453 if (!errcode && pdu_write_u(pdu, from, count))
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500454 errcode = -EFAULT;
455 }
456 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500457 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530458 uint16_t nwname = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500459 const char **wnames = va_arg(ap, const char **);
460
Sripathi Kodi342fee12010-03-05 18:50:14 +0000461 errcode = p9pdu_writef(pdu, proto_version, "w",
462 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500463 if (!errcode) {
464 int i;
465
466 for (i = 0; i < nwname; i++) {
467 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000468 p9pdu_writef(pdu,
469 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500470 "s",
471 wnames[i]);
472 if (errcode)
473 break;
474 }
475 }
476 }
477 break;
478 case 'R':{
Kirill A. Shutemov6250a8b2014-12-30 02:48:09 +0200479 uint16_t nwqid = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500480 struct p9_qid *wqids =
481 va_arg(ap, struct p9_qid *);
482
Sripathi Kodi342fee12010-03-05 18:50:14 +0000483 errcode = p9pdu_writef(pdu, proto_version, "w",
484 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500485 if (!errcode) {
486 int i;
487
488 for (i = 0; i < nwqid; i++) {
489 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000490 p9pdu_writef(pdu,
491 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500492 "Q",
493 &wqids[i]);
494 if (errcode)
495 break;
496 }
497 }
498 }
499 break;
Sripathi Kodi87d78452010-06-18 11:50:10 +0530500 case 'I':{
501 struct p9_iattr_dotl *p9attr = va_arg(ap,
502 struct p9_iattr_dotl *);
503
504 errcode = p9pdu_writef(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800505 "ddugqqqqq",
Sripathi Kodi87d78452010-06-18 11:50:10 +0530506 p9attr->valid,
507 p9attr->mode,
508 p9attr->uid,
509 p9attr->gid,
510 p9attr->size,
511 p9attr->atime_sec,
512 p9attr->atime_nsec,
513 p9attr->mtime_sec,
514 p9attr->mtime_nsec);
515 }
516 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500517 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000518 if ((proto_version != p9_proto_2000u) &&
519 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500520 return 0;
521 break;
522 default:
523 BUG();
524 break;
525 }
526
527 if (errcode)
528 break;
529 }
530
531 return errcode;
532}
533
Sripathi Kodi342fee12010-03-05 18:50:14 +0000534int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500535{
536 va_list ap;
537 int ret;
538
539 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000540 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500541 va_end(ap);
542
543 return ret;
544}
545
546static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000547p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500548{
549 va_list ap;
550 int ret;
551
552 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000553 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500554 va_end(ap);
555
556 return ret;
557}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500558
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530559int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500560{
561 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500562 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500563
564 fake_pdu.size = len;
565 fake_pdu.capacity = len;
566 fake_pdu.sdata = buf;
567 fake_pdu.offset = 0;
568
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530569 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500570 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800571 p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530572 trace_9p_protocol_dump(clnt, &fake_pdu);
Gertjan Halkesfea7d0d2018-09-05 15:41:29 +0900573 return ret;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500574 }
575
Gertjan Halkesfea7d0d2018-09-05 15:41:29 +0900576 return fake_pdu.offset;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500577}
578EXPORT_SYMBOL(p9stat_read);
579
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500580int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
581{
Venkateswararao Jujjuri (JV)9bb6c102011-02-02 17:52:46 -0800582 pdu->id = type;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500583 return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
584}
585
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530586int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500587{
588 int size = pdu->size;
589 int err;
590
591 pdu->size = 0;
592 err = p9pdu_writef(pdu, 0, "d", size);
593 pdu->size = size;
594
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530595 trace_9p_protocol_dump(clnt, pdu);
Joe Perches5d385152011-11-28 10:40:46 -0800596 p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
597 pdu->size, pdu->id, pdu->tag);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500598
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500599 return err;
600}
601
602void p9pdu_reset(struct p9_fcall *pdu)
603{
604 pdu->offset = 0;
605 pdu->size = 0;
606}
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000607
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530608int p9dirent_read(struct p9_client *clnt, char *buf, int len,
609 struct p9_dirent *dirent)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000610{
611 struct p9_fcall fake_pdu;
612 int ret;
613 char *nameptr;
614
615 fake_pdu.size = len;
616 fake_pdu.capacity = len;
617 fake_pdu.sdata = buf;
618 fake_pdu.offset = 0;
619
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530620 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
621 &dirent->d_off, &dirent->d_type, &nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000622 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800623 p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530624 trace_9p_protocol_dump(clnt, &fake_pdu);
Dominique Martinet99af1792018-09-08 00:36:08 +0900625 return ret;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000626 }
627
Dominique Martinet99af1792018-09-08 00:36:08 +0900628 ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
629 if (ret < 0) {
630 p9_debug(P9_DEBUG_ERROR,
631 "On the wire dirent name too long: %s\n",
632 nameptr);
633 kfree(nameptr);
634 return ret;
635 }
Pedro Scarapicchia Junior1b0bcbcf62011-05-09 14:10:49 +0000636 kfree(nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000637
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000638 return fake_pdu.offset;
639}
640EXPORT_SYMBOL(p9dirent_read);