Blame view

net/9p/protocol.c 13.2 KB
1f3276132   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
2
3
4
5
6
7
8
9
10
  /*
   * net/9p/protocol.c
   *
   * 9P Protocol Support Code
   *
   *  Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
   *
   *  Base on code from Anthony Liguori <aliguori@us.ibm.com>
   *  Copyright (C) 2008 by IBM, Corp.
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
11
12
13
14
   */
  
  #include <linux/module.h>
  #include <linux/errno.h>
01b0c5cfb   Thiago Farina   net/9p/protocol.c...
15
  #include <linux/kernel.h>
51a87c552   Eric Van Hensbergen   9p: rework client...
16
  #include <linux/uaccess.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
17
  #include <linux/slab.h>
e7f4b8f1a   Eric Van Hensbergen   9p: Improve debug...
18
  #include <linux/sched.h>
01b0c5cfb   Thiago Farina   net/9p/protocol.c...
19
  #include <linux/stddef.h>
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
20
  #include <linux/types.h>
4f3b35c15   Al Viro   net/9p: switch th...
21
  #include <linux/uio.h>
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
22
23
24
  #include <net/9p/9p.h>
  #include <net/9p/client.h>
  #include "protocol.h"
348b59012   Aneesh Kumar K.V   net/9p: Convert n...
25
  #include <trace/events/9p.h>
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
26
  static int
342fee1d5   Sripathi Kodi   9P2010.L handshak...
27
  p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
28
29
30
31
  
  void p9stat_free(struct p9_wstat *stbuf)
  {
  	kfree(stbuf->name);
62e394177   Dominique Martinet   9p: clear danglin...
32
  	stbuf->name = NULL;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
33
  	kfree(stbuf->uid);
62e394177   Dominique Martinet   9p: clear danglin...
34
  	stbuf->uid = NULL;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
35
  	kfree(stbuf->gid);
62e394177   Dominique Martinet   9p: clear danglin...
36
  	stbuf->gid = NULL;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
37
  	kfree(stbuf->muid);
62e394177   Dominique Martinet   9p: clear danglin...
38
  	stbuf->muid = NULL;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
39
  	kfree(stbuf->extension);
62e394177   Dominique Martinet   9p: clear danglin...
40
  	stbuf->extension = NULL;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
41
42
  }
  EXPORT_SYMBOL(p9stat_free);
abfa034e4   Aneesh Kumar K.V   fs/9p: Update zer...
43
  size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
44
  {
01b0c5cfb   Thiago Farina   net/9p/protocol.c...
45
  	size_t len = min(pdu->size - pdu->offset, size);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
46
47
48
49
50
51
52
  	memcpy(data, &pdu->sdata[pdu->offset], len);
  	pdu->offset += len;
  	return size - len;
  }
  
  static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
  {
01b0c5cfb   Thiago Farina   net/9p/protocol.c...
53
  	size_t len = min(pdu->capacity - pdu->size, size);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
54
55
56
57
  	memcpy(&pdu->sdata[pdu->size], data, len);
  	pdu->size += len;
  	return size - len;
  }
51a87c552   Eric Van Hensbergen   9p: rework client...
58
  static size_t
4f3b35c15   Al Viro   net/9p: switch th...
59
  pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
51a87c552   Eric Van Hensbergen   9p: rework client...
60
  {
01b0c5cfb   Thiago Farina   net/9p/protocol.c...
61
  	size_t len = min(pdu->capacity - pdu->size, size);
4f3b35c15   Al Viro   net/9p: switch th...
62
  	struct iov_iter i = *from;
1c512a7ca   Al Viro   net/9p: switch to...
63
  	if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, &i))
7b3bb3fe1   Aneesh Kumar K.V   net/9p: Return er...
64
  		len = 0;
51a87c552   Eric Van Hensbergen   9p: rework client...
65
66
67
68
  
  	pdu->size += len;
  	return size - len;
  }
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
69
70
71
72
73
74
  /*
  	b - int8_t
  	w - int16_t
  	d - int32_t
  	q - int64_t
  	s - string
97fc8b1eb   Eric W. Biederman   9p: Add 'u' and '...
75
76
  	u - numeric uid
  	g - numeric gid
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
77
78
79
80
81
  	S - stat
  	Q - qid
  	D - data blob (int32_t size followed by void *, results are not freed)
  	T - array of strings (int16_t count, followed by strings)
  	R - array of qids (int16_t count, followed by qids)
f08531220   Sripathi Kodi   9p: getattr clien...
82
  	A - stat for 9p2000.L (p9_stat_dotl)
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
83
84
85
86
  	? - if optional = 1, continue parsing
  */
  
  static int
342fee1d5   Sripathi Kodi   9P2010.L handshak...
87
88
  p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
  	va_list ap)
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  {
  	const char *ptr;
  	int errcode = 0;
  
  	for (ptr = fmt; *ptr; ptr++) {
  		switch (*ptr) {
  		case 'b':{
  				int8_t *val = va_arg(ap, int8_t *);
  				if (pdu_read(pdu, val, sizeof(*val))) {
  					errcode = -EFAULT;
  					break;
  				}
  			}
  			break;
  		case 'w':{
  				int16_t *val = va_arg(ap, int16_t *);
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
105
106
  				__le16 le_val;
  				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
107
108
109
  					errcode = -EFAULT;
  					break;
  				}
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
110
  				*val = le16_to_cpu(le_val);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
111
112
113
114
  			}
  			break;
  		case 'd':{
  				int32_t *val = va_arg(ap, int32_t *);
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
115
116
  				__le32 le_val;
  				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
117
118
119
  					errcode = -EFAULT;
  					break;
  				}
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
120
  				*val = le32_to_cpu(le_val);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
121
122
123
124
  			}
  			break;
  		case 'q':{
  				int64_t *val = va_arg(ap, int64_t *);
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
125
126
  				__le64 le_val;
  				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
127
128
129
  					errcode = -EFAULT;
  					break;
  				}
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
130
  				*val = le64_to_cpu(le_val);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
131
132
133
  			}
  			break;
  		case 's':{
e45c5405e   Eric Van Hensbergen   9p: fix sparse wa...
134
  				char **sptr = va_arg(ap, char **);
219fd58be   M. Mohan Kumar   net/9p: Use prope...
135
  				uint16_t len;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
136

342fee1d5   Sripathi Kodi   9P2010.L handshak...
137
138
  				errcode = p9pdu_readf(pdu, proto_version,
  								"w", &len);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
139
140
  				if (errcode)
  					break;
eeff66ef6   Aneesh Kumar K.V   net/9p: Convert t...
141
  				*sptr = kmalloc(len + 1, GFP_NOFS);
e45c5405e   Eric Van Hensbergen   9p: fix sparse wa...
142
  				if (*sptr == NULL) {
b87d1d265   piaojun   9p/net/protocol.c...
143
  					errcode = -ENOMEM;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
144
145
  					break;
  				}
219fd58be   M. Mohan Kumar   net/9p: Use prope...
146
  				if (pdu_read(pdu, *sptr, len)) {
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
147
  					errcode = -EFAULT;
e45c5405e   Eric Van Hensbergen   9p: fix sparse wa...
148
149
  					kfree(*sptr);
  					*sptr = NULL;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
150
  				} else
219fd58be   M. Mohan Kumar   net/9p: Use prope...
151
  					(*sptr)[len] = 0;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
152
153
  			}
  			break;
97fc8b1eb   Eric W. Biederman   9p: Add 'u' and '...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  		case 'u': {
  				kuid_t *uid = va_arg(ap, kuid_t *);
  				__le32 le_val;
  				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  					errcode = -EFAULT;
  					break;
  				}
  				*uid = make_kuid(&init_user_ns,
  						 le32_to_cpu(le_val));
  			} break;
  		case 'g': {
  				kgid_t *gid = va_arg(ap, kgid_t *);
  				__le32 le_val;
  				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
  					errcode = -EFAULT;
  					break;
  				}
  				*gid = make_kgid(&init_user_ns,
  						 le32_to_cpu(le_val));
  			} break;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
174
175
176
  		case 'Q':{
  				struct p9_qid *qid =
  				    va_arg(ap, struct p9_qid *);
342fee1d5   Sripathi Kodi   9P2010.L handshak...
177
  				errcode = p9pdu_readf(pdu, proto_version, "bdq",
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
178
179
180
181
182
183
184
  						      &qid->type, &qid->version,
  						      &qid->path);
  			}
  			break;
  		case 'S':{
  				struct p9_wstat *stbuf =
  				    va_arg(ap, struct p9_wstat *);
f0a0ac2ee   Eric Van Hensbergen   9p: fix oops in p...
185
  				memset(stbuf, 0, sizeof(struct p9_wstat));
447c50943   Eric W. Biederman   9p: Modify the st...
186
187
  				stbuf->n_uid = stbuf->n_muid = INVALID_UID;
  				stbuf->n_gid = INVALID_GID;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
188
  				errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
189
  				    p9pdu_readf(pdu, proto_version,
447c50943   Eric W. Biederman   9p: Modify the st...
190
  						"wwdQdddqssss?sugu",
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
191
192
193
194
195
196
197
198
199
200
201
202
203
204
  						&stbuf->size, &stbuf->type,
  						&stbuf->dev, &stbuf->qid,
  						&stbuf->mode, &stbuf->atime,
  						&stbuf->mtime, &stbuf->length,
  						&stbuf->name, &stbuf->uid,
  						&stbuf->gid, &stbuf->muid,
  						&stbuf->extension,
  						&stbuf->n_uid, &stbuf->n_gid,
  						&stbuf->n_muid);
  				if (errcode)
  					p9stat_free(stbuf);
  			}
  			break;
  		case 'D':{
219fd58be   M. Mohan Kumar   net/9p: Use prope...
205
  				uint32_t *count = va_arg(ap, uint32_t *);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
206
207
208
  				void **data = va_arg(ap, void **);
  
  				errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
209
  				    p9pdu_readf(pdu, proto_version, "d", count);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
210
211
  				if (!errcode) {
  					*count =
219fd58be   M. Mohan Kumar   net/9p: Use prope...
212
  					    min_t(uint32_t, *count,
01b0c5cfb   Thiago Farina   net/9p/protocol.c...
213
  						  pdu->size - pdu->offset);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
214
215
216
217
218
  					*data = &pdu->sdata[pdu->offset];
  				}
  			}
  			break;
  		case 'T':{
b76225e22   Harsh Prateek Bora   net/9p: nwname sh...
219
  				uint16_t *nwname = va_arg(ap, uint16_t *);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
220
  				char ***wnames = va_arg(ap, char ***);
342fee1d5   Sripathi Kodi   9P2010.L handshak...
221
222
  				errcode = p9pdu_readf(pdu, proto_version,
  								"w", nwname);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
223
224
  				if (!errcode) {
  					*wnames =
6da2ec560   Kees Cook   treewide: kmalloc...
225
226
227
  					    kmalloc_array(*nwname,
  							  sizeof(char *),
  							  GFP_NOFS);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
228
229
230
231
232
233
234
235
236
  					if (!*wnames)
  						errcode = -ENOMEM;
  				}
  
  				if (!errcode) {
  					int i;
  
  					for (i = 0; i < *nwname; i++) {
  						errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
237
238
  						    p9pdu_readf(pdu,
  								proto_version,
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  								"s",
  								&(*wnames)[i]);
  						if (errcode)
  							break;
  					}
  				}
  
  				if (errcode) {
  					if (*wnames) {
  						int i;
  
  						for (i = 0; i < *nwname; i++)
  							kfree((*wnames)[i]);
  					}
  					kfree(*wnames);
  					*wnames = NULL;
  				}
  			}
  			break;
  		case 'R':{
6250a8bad   Kirill A. Shutemov   9p: use unsigned ...
259
  				uint16_t *nwqid = va_arg(ap, uint16_t *);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
260
261
262
263
264
265
  				struct p9_qid **wqids =
  				    va_arg(ap, struct p9_qid **);
  
  				*wqids = NULL;
  
  				errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
266
  				    p9pdu_readf(pdu, proto_version, "w", nwqid);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
267
268
  				if (!errcode) {
  					*wqids =
6da2ec560   Kees Cook   treewide: kmalloc...
269
270
271
  					    kmalloc_array(*nwqid,
  							  sizeof(struct p9_qid),
  							  GFP_NOFS);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
272
273
274
275
276
277
278
279
280
  					if (*wqids == NULL)
  						errcode = -ENOMEM;
  				}
  
  				if (!errcode) {
  					int i;
  
  					for (i = 0; i < *nwqid; i++) {
  						errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
281
282
  						    p9pdu_readf(pdu,
  								proto_version,
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
283
284
285
286
287
288
289
290
291
292
293
294
295
  								"Q",
  								&(*wqids)[i]);
  						if (errcode)
  							break;
  					}
  				}
  
  				if (errcode) {
  					kfree(*wqids);
  					*wqids = NULL;
  				}
  			}
  			break;
f08531220   Sripathi Kodi   9p: getattr clien...
296
297
298
299
300
301
302
  		case 'A': {
  				struct p9_stat_dotl *stbuf =
  				    va_arg(ap, struct p9_stat_dotl *);
  
  				memset(stbuf, 0, sizeof(struct p9_stat_dotl));
  				errcode =
  				    p9pdu_readf(pdu, proto_version,
447c50943   Eric W. Biederman   9p: Modify the st...
303
  					"qQdugqqqqqqqqqqqqqqq",
f08531220   Sripathi Kodi   9p: getattr clien...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  					&stbuf->st_result_mask,
  					&stbuf->qid,
  					&stbuf->st_mode,
  					&stbuf->st_uid, &stbuf->st_gid,
  					&stbuf->st_nlink,
  					&stbuf->st_rdev, &stbuf->st_size,
  					&stbuf->st_blksize, &stbuf->st_blocks,
  					&stbuf->st_atime_sec,
  					&stbuf->st_atime_nsec,
  					&stbuf->st_mtime_sec,
  					&stbuf->st_mtime_nsec,
  					&stbuf->st_ctime_sec,
  					&stbuf->st_ctime_nsec,
  					&stbuf->st_btime_sec,
  					&stbuf->st_btime_nsec,
  					&stbuf->st_gen,
  					&stbuf->st_data_version);
  			}
  			break;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
323
  		case '?':
c56e4acf5   Sripathi Kodi   9p: VFS switches ...
324
325
  			if ((proto_version != p9_proto_2000u) &&
  				(proto_version != p9_proto_2000L))
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  				return 0;
  			break;
  		default:
  			BUG();
  			break;
  		}
  
  		if (errcode)
  			break;
  	}
  
  	return errcode;
  }
  
  int
342fee1d5   Sripathi Kodi   9P2010.L handshak...
341
342
  p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
  	va_list ap)
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
343
344
345
346
347
348
349
350
351
352
353
354
355
  {
  	const char *ptr;
  	int errcode = 0;
  
  	for (ptr = fmt; *ptr; ptr++) {
  		switch (*ptr) {
  		case 'b':{
  				int8_t val = va_arg(ap, int);
  				if (pdu_write(pdu, &val, sizeof(val)))
  					errcode = -EFAULT;
  			}
  			break;
  		case 'w':{
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
356
  				__le16 val = cpu_to_le16(va_arg(ap, int));
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
357
358
359
360
361
  				if (pdu_write(pdu, &val, sizeof(val)))
  					errcode = -EFAULT;
  			}
  			break;
  		case 'd':{
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
362
  				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
363
364
365
366
367
  				if (pdu_write(pdu, &val, sizeof(val)))
  					errcode = -EFAULT;
  			}
  			break;
  		case 'q':{
beeebc92e   Eric Van Hensbergen   9p: fix endian is...
368
  				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
369
370
371
372
373
  				if (pdu_write(pdu, &val, sizeof(val)))
  					errcode = -EFAULT;
  			}
  			break;
  		case 's':{
e45c5405e   Eric Van Hensbergen   9p: fix sparse wa...
374
  				const char *sptr = va_arg(ap, const char *);
219fd58be   M. Mohan Kumar   net/9p: Use prope...
375
  				uint16_t len = 0;
e45c5405e   Eric Van Hensbergen   9p: fix sparse wa...
376
  				if (sptr)
d31bb4f06   Dan Carpenter   9p: fix min_t() c...
377
  					len = min_t(size_t, strlen(sptr),
219fd58be   M. Mohan Kumar   net/9p: Use prope...
378
  								USHRT_MAX);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
379

342fee1d5   Sripathi Kodi   9P2010.L handshak...
380
381
  				errcode = p9pdu_writef(pdu, proto_version,
  								"w", len);
e45c5405e   Eric Van Hensbergen   9p: fix sparse wa...
382
  				if (!errcode && pdu_write(pdu, sptr, len))
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
383
384
385
  					errcode = -EFAULT;
  			}
  			break;
97fc8b1eb   Eric W. Biederman   9p: Add 'u' and '...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  		case 'u': {
  				kuid_t uid = va_arg(ap, kuid_t);
  				__le32 val = cpu_to_le32(
  						from_kuid(&init_user_ns, uid));
  				if (pdu_write(pdu, &val, sizeof(val)))
  					errcode = -EFAULT;
  			} break;
  		case 'g': {
  				kgid_t gid = va_arg(ap, kgid_t);
  				__le32 val = cpu_to_le32(
  						from_kgid(&init_user_ns, gid));
  				if (pdu_write(pdu, &val, sizeof(val)))
  					errcode = -EFAULT;
  			} break;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
400
401
402
403
  		case 'Q':{
  				const struct p9_qid *qid =
  				    va_arg(ap, const struct p9_qid *);
  				errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
404
  				    p9pdu_writef(pdu, proto_version, "bdq",
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
405
406
407
408
409
410
411
  						 qid->type, qid->version,
  						 qid->path);
  			} break;
  		case 'S':{
  				const struct p9_wstat *stbuf =
  				    va_arg(ap, const struct p9_wstat *);
  				errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
412
  				    p9pdu_writef(pdu, proto_version,
447c50943   Eric W. Biederman   9p: Modify the st...
413
  						 "wwdQdddqssss?sugu",
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
414
  						 stbuf->size, stbuf->type,
51a87c552   Eric Van Hensbergen   9p: rework client...
415
  						 stbuf->dev, &stbuf->qid,
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
416
417
418
419
420
421
422
  						 stbuf->mode, stbuf->atime,
  						 stbuf->mtime, stbuf->length,
  						 stbuf->name, stbuf->uid,
  						 stbuf->gid, stbuf->muid,
  						 stbuf->extension, stbuf->n_uid,
  						 stbuf->n_gid, stbuf->n_muid);
  			} break;
4f3b35c15   Al Viro   net/9p: switch th...
423
  		case 'V':{
6250a8bad   Kirill A. Shutemov   9p: use unsigned ...
424
  				uint32_t count = va_arg(ap, uint32_t);
4f3b35c15   Al Viro   net/9p: switch th...
425
426
  				struct iov_iter *from =
  						va_arg(ap, struct iov_iter *);
342fee1d5   Sripathi Kodi   9P2010.L handshak...
427
428
  				errcode = p9pdu_writef(pdu, proto_version, "d",
  									count);
4f3b35c15   Al Viro   net/9p: switch th...
429
  				if (!errcode && pdu_write_u(pdu, from, count))
51a87c552   Eric Van Hensbergen   9p: rework client...
430
431
432
  					errcode = -EFAULT;
  			}
  			break;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
433
  		case 'T':{
b76225e22   Harsh Prateek Bora   net/9p: nwname sh...
434
  				uint16_t nwname = va_arg(ap, int);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
435
  				const char **wnames = va_arg(ap, const char **);
342fee1d5   Sripathi Kodi   9P2010.L handshak...
436
437
  				errcode = p9pdu_writef(pdu, proto_version, "w",
  									nwname);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
438
439
440
441
442
  				if (!errcode) {
  					int i;
  
  					for (i = 0; i < nwname; i++) {
  						errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
443
444
  						    p9pdu_writef(pdu,
  								proto_version,
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
445
446
447
448
449
450
451
452
453
  								 "s",
  								 wnames[i]);
  						if (errcode)
  							break;
  					}
  				}
  			}
  			break;
  		case 'R':{
6250a8bad   Kirill A. Shutemov   9p: use unsigned ...
454
  				uint16_t nwqid = va_arg(ap, int);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
455
456
  				struct p9_qid *wqids =
  				    va_arg(ap, struct p9_qid *);
342fee1d5   Sripathi Kodi   9P2010.L handshak...
457
458
  				errcode = p9pdu_writef(pdu, proto_version, "w",
  									nwqid);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
459
460
461
462
463
  				if (!errcode) {
  					int i;
  
  					for (i = 0; i < nwqid; i++) {
  						errcode =
342fee1d5   Sripathi Kodi   9P2010.L handshak...
464
465
  						    p9pdu_writef(pdu,
  								proto_version,
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
466
467
468
469
470
471
472
473
  								 "Q",
  								 &wqids[i]);
  						if (errcode)
  							break;
  					}
  				}
  			}
  			break;
87d7845aa   Sripathi Kodi   9p: Implement cli...
474
475
476
477
478
  		case 'I':{
  				struct p9_iattr_dotl *p9attr = va_arg(ap,
  							struct p9_iattr_dotl *);
  
  				errcode = p9pdu_writef(pdu, proto_version,
447c50943   Eric W. Biederman   9p: Modify the st...
479
  							"ddugqqqqq",
87d7845aa   Sripathi Kodi   9p: Implement cli...
480
481
482
483
484
485
486
487
488
489
490
  							p9attr->valid,
  							p9attr->mode,
  							p9attr->uid,
  							p9attr->gid,
  							p9attr->size,
  							p9attr->atime_sec,
  							p9attr->atime_nsec,
  							p9attr->mtime_sec,
  							p9attr->mtime_nsec);
  			}
  			break;
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
491
  		case '?':
c56e4acf5   Sripathi Kodi   9p: VFS switches ...
492
493
  			if ((proto_version != p9_proto_2000u) &&
  				(proto_version != p9_proto_2000L))
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
494
495
496
497
498
499
500
501
502
503
504
505
506
  				return 0;
  			break;
  		default:
  			BUG();
  			break;
  		}
  
  		if (errcode)
  			break;
  	}
  
  	return errcode;
  }
342fee1d5   Sripathi Kodi   9P2010.L handshak...
507
  int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
508
509
510
511
512
  {
  	va_list ap;
  	int ret;
  
  	va_start(ap, fmt);
342fee1d5   Sripathi Kodi   9P2010.L handshak...
513
  	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
514
515
516
517
518
519
  	va_end(ap);
  
  	return ret;
  }
  
  static int
342fee1d5   Sripathi Kodi   9P2010.L handshak...
520
  p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
521
522
523
524
525
  {
  	va_list ap;
  	int ret;
  
  	va_start(ap, fmt);
342fee1d5   Sripathi Kodi   9P2010.L handshak...
526
  	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
ace51c4dd   Eric Van Hensbergen   9p: add new proto...
527
528
529
530
  	va_end(ap);
  
  	return ret;
  }
51a87c552   Eric Van Hensbergen   9p: rework client...
531

348b59012   Aneesh Kumar K.V   net/9p: Convert n...
532
  int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
02da398b9   Eric Van Hensbergen   9p: eliminate dep...
533
534
  {
  	struct p9_fcall fake_pdu;
e7f4b8f1a   Eric Van Hensbergen   9p: Improve debug...
535
  	int ret;
02da398b9   Eric Van Hensbergen   9p: eliminate dep...
536
537
538
539
540
  
  	fake_pdu.size = len;
  	fake_pdu.capacity = len;
  	fake_pdu.sdata = buf;
  	fake_pdu.offset = 0;
348b59012   Aneesh Kumar K.V   net/9p: Convert n...
541
  	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
e7f4b8f1a   Eric Van Hensbergen   9p: Improve debug...
542
  	if (ret) {
5d3851530   Joe Perches   9p: Reduce object...
543
544
  		p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d
  ", ret);
348b59012   Aneesh Kumar K.V   net/9p: Convert n...
545
  		trace_9p_protocol_dump(clnt, &fake_pdu);
2803cf437   Gertjan Halkes   9p: do not trust ...
546
  		return ret;
e7f4b8f1a   Eric Van Hensbergen   9p: Improve debug...
547
  	}
2803cf437   Gertjan Halkes   9p: do not trust ...
548
  	return fake_pdu.offset;
02da398b9   Eric Van Hensbergen   9p: eliminate dep...
549
550
  }
  EXPORT_SYMBOL(p9stat_read);
51a87c552   Eric Van Hensbergen   9p: rework client...
551
552
  int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
  {
9bb6c10a4   Venkateswararao Jujjuri (JV)   [net/9p] Assign t...
553
  	pdu->id = type;
51a87c552   Eric Van Hensbergen   9p: rework client...
554
555
  	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
  }
348b59012   Aneesh Kumar K.V   net/9p: Convert n...
556
  int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
51a87c552   Eric Van Hensbergen   9p: rework client...
557
558
559
560
561
562
563
  {
  	int size = pdu->size;
  	int err;
  
  	pdu->size = 0;
  	err = p9pdu_writef(pdu, 0, "d", size);
  	pdu->size = size;
348b59012   Aneesh Kumar K.V   net/9p: Convert n...
564
  	trace_9p_protocol_dump(clnt, pdu);
5d3851530   Joe Perches   9p: Reduce object...
565
566
567
  	p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d
  ",
  		 pdu->size, pdu->id, pdu->tag);
e7f4b8f1a   Eric Van Hensbergen   9p: Improve debug...
568

51a87c552   Eric Van Hensbergen   9p: rework client...
569
570
571
572
573
574
575
576
  	return err;
  }
  
  void p9pdu_reset(struct p9_fcall *pdu)
  {
  	pdu->offset = 0;
  	pdu->size = 0;
  }
7751bdb3a   Sripathi Kodi   9p: readdir imple...
577

348b59012   Aneesh Kumar K.V   net/9p: Convert n...
578
579
  int p9dirent_read(struct p9_client *clnt, char *buf, int len,
  		  struct p9_dirent *dirent)
7751bdb3a   Sripathi Kodi   9p: readdir imple...
580
581
582
583
584
585
586
587
588
  {
  	struct p9_fcall fake_pdu;
  	int ret;
  	char *nameptr;
  
  	fake_pdu.size = len;
  	fake_pdu.capacity = len;
  	fake_pdu.sdata = buf;
  	fake_pdu.offset = 0;
348b59012   Aneesh Kumar K.V   net/9p: Convert n...
589
590
  	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
  			  &dirent->d_off, &dirent->d_type, &nameptr);
7751bdb3a   Sripathi Kodi   9p: readdir imple...
591
  	if (ret) {
5d3851530   Joe Perches   9p: Reduce object...
592
593
  		p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d
  ", ret);
348b59012   Aneesh Kumar K.V   net/9p: Convert n...
594
  		trace_9p_protocol_dump(clnt, &fake_pdu);
ef5305f1f   Dominique Martinet   9p: p9dirent_read...
595
  		return ret;
7751bdb3a   Sripathi Kodi   9p: readdir imple...
596
  	}
ef5305f1f   Dominique Martinet   9p: p9dirent_read...
597
598
599
600
601
602
603
604
605
  	ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
  	if (ret < 0) {
  		p9_debug(P9_DEBUG_ERROR,
  			 "On the wire dirent name too long: %s
  ",
  			 nameptr);
  		kfree(nameptr);
  		return ret;
  	}
1b0bcbcf6   Pedro Scarapicchia Junior   net/9p/protocol.c...
606
  	kfree(nameptr);
7751bdb3a   Sripathi Kodi   9p: readdir imple...
607

7751bdb3a   Sripathi Kodi   9p: readdir imple...
608
609
610
  	return fake_pdu.offset;
  }
  EXPORT_SYMBOL(p9dirent_read);