blob: 462ba144cb3934d6761a66c84f7d332f0559c804 [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 Martinet62e39412018-08-28 07:32:35 +090049 stbuf->name = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050050 kfree(stbuf->uid);
Dominique Martinet62e39412018-08-28 07:32:35 +090051 stbuf->uid = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050052 kfree(stbuf->gid);
Dominique Martinet62e39412018-08-28 07:32:35 +090053 stbuf->gid = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050054 kfree(stbuf->muid);
Dominique Martinet62e39412018-08-28 07:32:35 +090055 stbuf->muid = NULL;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -050056 kfree(stbuf->extension);
Dominique Martinet62e39412018-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;
Al Viro1c512a72017-02-17 23:16:09 -050082 if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, &i))
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) {
piaojunb87d1d22018-07-11 08:43:49 +0800164 errcode = -ENOMEM;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500165 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 =
Kees Cook6da2ec52018-06-12 13:55:00 -0700250 kmalloc_array(*nwname,
251 sizeof(char *),
252 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500253 if (!*wnames)
254 errcode = -ENOMEM;
255 }
256
257 if (!errcode) {
258 int i;
259
260 for (i = 0; i < *nwname; i++) {
261 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000262 p9pdu_readf(pdu,
263 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500264 "s",
265 &(*wnames)[i]);
266 if (errcode)
267 break;
268 }
269 }
270
271 if (errcode) {
272 if (*wnames) {
273 int i;
274
275 for (i = 0; i < *nwname; i++)
276 kfree((*wnames)[i]);
277 }
278 kfree(*wnames);
279 *wnames = NULL;
280 }
281 }
282 break;
283 case 'R':{
Kirill A. Shutemov6250a8b2014-12-30 02:48:09 +0200284 uint16_t *nwqid = va_arg(ap, uint16_t *);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500285 struct p9_qid **wqids =
286 va_arg(ap, struct p9_qid **);
287
288 *wqids = NULL;
289
290 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000291 p9pdu_readf(pdu, proto_version, "w", nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500292 if (!errcode) {
293 *wqids =
Kees Cook6da2ec52018-06-12 13:55:00 -0700294 kmalloc_array(*nwqid,
295 sizeof(struct p9_qid),
296 GFP_NOFS);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500297 if (*wqids == NULL)
298 errcode = -ENOMEM;
299 }
300
301 if (!errcode) {
302 int i;
303
304 for (i = 0; i < *nwqid; i++) {
305 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000306 p9pdu_readf(pdu,
307 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500308 "Q",
309 &(*wqids)[i]);
310 if (errcode)
311 break;
312 }
313 }
314
315 if (errcode) {
316 kfree(*wqids);
317 *wqids = NULL;
318 }
319 }
320 break;
Sripathi Kodif0853122010-07-12 20:07:23 +0530321 case 'A': {
322 struct p9_stat_dotl *stbuf =
323 va_arg(ap, struct p9_stat_dotl *);
324
325 memset(stbuf, 0, sizeof(struct p9_stat_dotl));
326 errcode =
327 p9pdu_readf(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800328 "qQdugqqqqqqqqqqqqqqq",
Sripathi Kodif0853122010-07-12 20:07:23 +0530329 &stbuf->st_result_mask,
330 &stbuf->qid,
331 &stbuf->st_mode,
332 &stbuf->st_uid, &stbuf->st_gid,
333 &stbuf->st_nlink,
334 &stbuf->st_rdev, &stbuf->st_size,
335 &stbuf->st_blksize, &stbuf->st_blocks,
336 &stbuf->st_atime_sec,
337 &stbuf->st_atime_nsec,
338 &stbuf->st_mtime_sec,
339 &stbuf->st_mtime_nsec,
340 &stbuf->st_ctime_sec,
341 &stbuf->st_ctime_nsec,
342 &stbuf->st_btime_sec,
343 &stbuf->st_btime_nsec,
344 &stbuf->st_gen,
345 &stbuf->st_data_version);
346 }
347 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500348 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000349 if ((proto_version != p9_proto_2000u) &&
350 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500351 return 0;
352 break;
353 default:
354 BUG();
355 break;
356 }
357
358 if (errcode)
359 break;
360 }
361
362 return errcode;
363}
364
365int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000366p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
367 va_list ap)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500368{
369 const char *ptr;
370 int errcode = 0;
371
372 for (ptr = fmt; *ptr; ptr++) {
373 switch (*ptr) {
374 case 'b':{
375 int8_t val = va_arg(ap, int);
376 if (pdu_write(pdu, &val, sizeof(val)))
377 errcode = -EFAULT;
378 }
379 break;
380 case 'w':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800381 __le16 val = cpu_to_le16(va_arg(ap, int));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500382 if (pdu_write(pdu, &val, sizeof(val)))
383 errcode = -EFAULT;
384 }
385 break;
386 case 'd':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800387 __le32 val = cpu_to_le32(va_arg(ap, int32_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500388 if (pdu_write(pdu, &val, sizeof(val)))
389 errcode = -EFAULT;
390 }
391 break;
392 case 'q':{
Eric Van Hensbergenbeeebc92009-02-06 22:07:41 -0800393 __le64 val = cpu_to_le64(va_arg(ap, int64_t));
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500394 if (pdu_write(pdu, &val, sizeof(val)))
395 errcode = -EFAULT;
396 }
397 break;
398 case 's':{
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500399 const char *sptr = va_arg(ap, const char *);
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600400 uint16_t len = 0;
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500401 if (sptr)
Dan Carpenterd31bb4f2012-06-26 23:01:41 +0000402 len = min_t(size_t, strlen(sptr),
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600403 USHRT_MAX);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500404
Sripathi Kodi342fee12010-03-05 18:50:14 +0000405 errcode = p9pdu_writef(pdu, proto_version,
406 "w", len);
Eric Van Hensbergene45c5402008-10-22 18:54:47 -0500407 if (!errcode && pdu_write(pdu, sptr, len))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500408 errcode = -EFAULT;
409 }
410 break;
Eric W. Biederman97fc8b12013-01-29 17:07:42 -0800411 case 'u': {
412 kuid_t uid = va_arg(ap, kuid_t);
413 __le32 val = cpu_to_le32(
414 from_kuid(&init_user_ns, uid));
415 if (pdu_write(pdu, &val, sizeof(val)))
416 errcode = -EFAULT;
417 } break;
418 case 'g': {
419 kgid_t gid = va_arg(ap, kgid_t);
420 __le32 val = cpu_to_le32(
421 from_kgid(&init_user_ns, gid));
422 if (pdu_write(pdu, &val, sizeof(val)))
423 errcode = -EFAULT;
424 } break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500425 case 'Q':{
426 const struct p9_qid *qid =
427 va_arg(ap, const struct p9_qid *);
428 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000429 p9pdu_writef(pdu, proto_version, "bdq",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500430 qid->type, qid->version,
431 qid->path);
432 } break;
433 case 'S':{
434 const struct p9_wstat *stbuf =
435 va_arg(ap, const struct p9_wstat *);
436 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000437 p9pdu_writef(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800438 "wwdQdddqssss?sugu",
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500439 stbuf->size, stbuf->type,
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500440 stbuf->dev, &stbuf->qid,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500441 stbuf->mode, stbuf->atime,
442 stbuf->mtime, stbuf->length,
443 stbuf->name, stbuf->uid,
444 stbuf->gid, stbuf->muid,
445 stbuf->extension, stbuf->n_uid,
446 stbuf->n_gid, stbuf->n_muid);
447 } break;
Al Viro4f3b35c2015-04-01 19:57:53 -0400448 case 'V':{
M. Mohan Kumar219fd582011-01-10 14:23:53 -0600449 uint32_t count = va_arg(ap, uint32_t);
Al Viro4f3b35c2015-04-01 19:57:53 -0400450 struct iov_iter *from =
451 va_arg(ap, struct iov_iter *);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000452 errcode = p9pdu_writef(pdu, proto_version, "d",
453 count);
Al Viro4f3b35c2015-04-01 19:57:53 -0400454 if (!errcode && pdu_write_u(pdu, from, count))
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500455 errcode = -EFAULT;
456 }
457 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500458 case 'T':{
Harsh Prateek Borab76225e2011-03-31 15:49:39 +0530459 uint16_t nwname = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500460 const char **wnames = va_arg(ap, const char **);
461
Sripathi Kodi342fee12010-03-05 18:50:14 +0000462 errcode = p9pdu_writef(pdu, proto_version, "w",
463 nwname);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500464 if (!errcode) {
465 int i;
466
467 for (i = 0; i < nwname; i++) {
468 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000469 p9pdu_writef(pdu,
470 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500471 "s",
472 wnames[i]);
473 if (errcode)
474 break;
475 }
476 }
477 }
478 break;
479 case 'R':{
Kirill A. Shutemov6250a8b2014-12-30 02:48:09 +0200480 uint16_t nwqid = va_arg(ap, int);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500481 struct p9_qid *wqids =
482 va_arg(ap, struct p9_qid *);
483
Sripathi Kodi342fee12010-03-05 18:50:14 +0000484 errcode = p9pdu_writef(pdu, proto_version, "w",
485 nwqid);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500486 if (!errcode) {
487 int i;
488
489 for (i = 0; i < nwqid; i++) {
490 errcode =
Sripathi Kodi342fee12010-03-05 18:50:14 +0000491 p9pdu_writef(pdu,
492 proto_version,
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500493 "Q",
494 &wqids[i]);
495 if (errcode)
496 break;
497 }
498 }
499 }
500 break;
Sripathi Kodi87d78452010-06-18 11:50:10 +0530501 case 'I':{
502 struct p9_iattr_dotl *p9attr = va_arg(ap,
503 struct p9_iattr_dotl *);
504
505 errcode = p9pdu_writef(pdu, proto_version,
Eric W. Biederman447c5092013-01-29 16:18:50 -0800506 "ddugqqqqq",
Sripathi Kodi87d78452010-06-18 11:50:10 +0530507 p9attr->valid,
508 p9attr->mode,
509 p9attr->uid,
510 p9attr->gid,
511 p9attr->size,
512 p9attr->atime_sec,
513 p9attr->atime_nsec,
514 p9attr->mtime_sec,
515 p9attr->mtime_nsec);
516 }
517 break;
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500518 case '?':
Sripathi Kodic56e4ac2010-03-25 12:40:35 +0000519 if ((proto_version != p9_proto_2000u) &&
520 (proto_version != p9_proto_2000L))
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500521 return 0;
522 break;
523 default:
524 BUG();
525 break;
526 }
527
528 if (errcode)
529 break;
530 }
531
532 return errcode;
533}
534
Sripathi Kodi342fee12010-03-05 18:50:14 +0000535int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500536{
537 va_list ap;
538 int ret;
539
540 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000541 ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500542 va_end(ap);
543
544 return ret;
545}
546
547static int
Sripathi Kodi342fee12010-03-05 18:50:14 +0000548p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500549{
550 va_list ap;
551 int ret;
552
553 va_start(ap, fmt);
Sripathi Kodi342fee12010-03-05 18:50:14 +0000554 ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
Eric Van Hensbergenace51c42008-10-13 20:40:27 -0500555 va_end(ap);
556
557 return ret;
558}
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500559
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530560int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500561{
562 struct p9_fcall fake_pdu;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500563 int ret;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500564
565 fake_pdu.size = len;
566 fake_pdu.capacity = len;
567 fake_pdu.sdata = buf;
568 fake_pdu.offset = 0;
569
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530570 ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500571 if (ret) {
Joe Perches5d385152011-11-28 10:40:46 -0800572 p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530573 trace_9p_protocol_dump(clnt, &fake_pdu);
Gertjan Halkes2803cf42018-09-05 15:41:29 +0900574 return ret;
Eric Van Hensbergene7f4b8f2008-10-17 16:20:07 -0500575 }
576
Gertjan Halkes2803cf42018-09-05 15:41:29 +0900577 return fake_pdu.offset;
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);
Dominique Martinetef5305f2018-09-08 00:36:08 +0900626 return ret;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000627 }
628
Dominique Martinetef5305f2018-09-08 00:36:08 +0900629 ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
630 if (ret < 0) {
631 p9_debug(P9_DEBUG_ERROR,
632 "On the wire dirent name too long: %s\n",
633 nameptr);
634 kfree(nameptr);
635 return ret;
636 }
Pedro Scarapicchia Junior1b0bcbcf62011-05-09 14:10:49 +0000637 kfree(nameptr);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000638
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000639 return fake_pdu.offset;
640}
641EXPORT_SYMBOL(p9dirent_read);