protocol.c 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * 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.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to:
 *  Free Software Foundation
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02111-1301  USA
 *
 */

#include <linux/module.h>
#include <linux/errno.h>
30
#include <linux/kernel.h>
31
#include <linux/uaccess.h>
32
#include <linux/slab.h>
E
Eric Van Hensbergen 已提交
33
#include <linux/sched.h>
34
#include <linux/stddef.h>
35
#include <linux/types.h>
36
#include <linux/uio.h>
37 38 39 40
#include <net/9p/9p.h>
#include <net/9p/client.h>
#include "protocol.h"

41 42
#include <trace/events/9p.h>

43
static int
44
p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
45 46 47 48 49 50 51 52 53 54 55

void p9stat_free(struct p9_wstat *stbuf)
{
	kfree(stbuf->name);
	kfree(stbuf->uid);
	kfree(stbuf->gid);
	kfree(stbuf->muid);
	kfree(stbuf->extension);
}
EXPORT_SYMBOL(p9stat_free);

56
size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
57
{
58
	size_t len = min(pdu->size - pdu->offset, size);
59 60 61 62 63 64 65
	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)
{
66
	size_t len = min(pdu->capacity - pdu->size, size);
67 68 69 70 71
	memcpy(&pdu->sdata[pdu->size], data, len);
	pdu->size += len;
	return size - len;
}

72
static size_t
73
pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
74
{
75
	size_t len = min(pdu->capacity - pdu->size, size);
76
	struct iov_iter i = *from;
77
	if (!copy_from_iter_full(&pdu->sdata[pdu->size], len, &i))
78
		len = 0;
79 80 81 82 83

	pdu->size += len;
	return size - len;
}

84 85 86 87 88 89
/*
	b - int8_t
	w - int16_t
	d - int32_t
	q - int64_t
	s - string
90 91
	u - numeric uid
	g - numeric gid
92 93 94 95 96
	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)
97
	A - stat for 9p2000.L (p9_stat_dotl)
98 99 100 101
	? - if optional = 1, continue parsing
*/

static int
102 103
p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
	va_list ap)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
{
	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 *);
120 121
				__le16 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
122 123 124
					errcode = -EFAULT;
					break;
				}
125
				*val = le16_to_cpu(le_val);
126 127 128 129
			}
			break;
		case 'd':{
				int32_t *val = va_arg(ap, int32_t *);
130 131
				__le32 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
132 133 134
					errcode = -EFAULT;
					break;
				}
135
				*val = le32_to_cpu(le_val);
136 137 138 139
			}
			break;
		case 'q':{
				int64_t *val = va_arg(ap, int64_t *);
140 141
				__le64 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
142 143 144
					errcode = -EFAULT;
					break;
				}
145
				*val = le64_to_cpu(le_val);
146 147 148
			}
			break;
		case 's':{
E
Eric Van Hensbergen 已提交
149
				char **sptr = va_arg(ap, char **);
M
M. Mohan Kumar 已提交
150
				uint16_t len;
151

152 153
				errcode = p9pdu_readf(pdu, proto_version,
								"w", &len);
154 155 156
				if (errcode)
					break;

157
				*sptr = kmalloc(len + 1, GFP_NOFS);
E
Eric Van Hensbergen 已提交
158
				if (*sptr == NULL) {
159 160 161
					errcode = -EFAULT;
					break;
				}
M
M. Mohan Kumar 已提交
162
				if (pdu_read(pdu, *sptr, len)) {
163
					errcode = -EFAULT;
E
Eric Van Hensbergen 已提交
164 165
					kfree(*sptr);
					*sptr = NULL;
166
				} else
M
M. Mohan Kumar 已提交
167
					(*sptr)[len] = 0;
168 169
			}
			break;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
		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;
190 191 192 193
		case 'Q':{
				struct p9_qid *qid =
				    va_arg(ap, struct p9_qid *);

194
				errcode = p9pdu_readf(pdu, proto_version, "bdq",
195 196 197 198 199 200 201 202
						      &qid->type, &qid->version,
						      &qid->path);
			}
			break;
		case 'S':{
				struct p9_wstat *stbuf =
				    va_arg(ap, struct p9_wstat *);

203
				memset(stbuf, 0, sizeof(struct p9_wstat));
204 205 206
				stbuf->n_uid = stbuf->n_muid = INVALID_UID;
				stbuf->n_gid = INVALID_GID;

207
				errcode =
208
				    p9pdu_readf(pdu, proto_version,
209
						"wwdQdddqssss?sugu",
210 211 212 213 214 215 216 217 218 219 220 221 222 223
						&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':{
M
M. Mohan Kumar 已提交
224
				uint32_t *count = va_arg(ap, uint32_t *);
225 226 227
				void **data = va_arg(ap, void **);

				errcode =
228
				    p9pdu_readf(pdu, proto_version, "d", count);
229 230
				if (!errcode) {
					*count =
M
M. Mohan Kumar 已提交
231
					    min_t(uint32_t, *count,
232
						  pdu->size - pdu->offset);
233 234 235 236 237
					*data = &pdu->sdata[pdu->offset];
				}
			}
			break;
		case 'T':{
238
				uint16_t *nwname = va_arg(ap, uint16_t *);
239 240
				char ***wnames = va_arg(ap, char ***);

241 242
				errcode = p9pdu_readf(pdu, proto_version,
								"w", nwname);
243 244 245
				if (!errcode) {
					*wnames =
					    kmalloc(sizeof(char *) * *nwname,
246
						    GFP_NOFS);
247 248 249 250 251 252 253 254 255
					if (!*wnames)
						errcode = -ENOMEM;
				}

				if (!errcode) {
					int i;

					for (i = 0; i < *nwname; i++) {
						errcode =
256 257
						    p9pdu_readf(pdu,
								proto_version,
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
								"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':{
278
				uint16_t *nwqid = va_arg(ap, uint16_t *);
279 280 281 282 283 284
				struct p9_qid **wqids =
				    va_arg(ap, struct p9_qid **);

				*wqids = NULL;

				errcode =
285
				    p9pdu_readf(pdu, proto_version, "w", nwqid);
286 287 288 289
				if (!errcode) {
					*wqids =
					    kmalloc(*nwqid *
						    sizeof(struct p9_qid),
290
						    GFP_NOFS);
291 292 293 294 295 296 297 298 299
					if (*wqids == NULL)
						errcode = -ENOMEM;
				}

				if (!errcode) {
					int i;

					for (i = 0; i < *nwqid; i++) {
						errcode =
300 301
						    p9pdu_readf(pdu,
								proto_version,
302 303 304 305 306 307 308 309 310 311 312 313 314
								"Q",
								&(*wqids)[i]);
						if (errcode)
							break;
					}
				}

				if (errcode) {
					kfree(*wqids);
					*wqids = NULL;
				}
			}
			break;
315 316 317 318 319 320 321
		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,
322
					"qQdugqqqqqqqqqqqqqqq",
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
					&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;
342
		case '?':
343 344
			if ((proto_version != p9_proto_2000u) &&
				(proto_version != p9_proto_2000L))
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
				return 0;
			break;
		default:
			BUG();
			break;
		}

		if (errcode)
			break;
	}

	return errcode;
}

int
360 361
p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
	va_list ap)
362 363 364 365 366 367 368 369 370 371 372 373 374
{
	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':{
375
				__le16 val = cpu_to_le16(va_arg(ap, int));
376 377 378 379 380
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 'd':{
381
				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
382 383 384 385 386
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 'q':{
387
				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
388 389 390 391 392
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 's':{
E
Eric Van Hensbergen 已提交
393
				const char *sptr = va_arg(ap, const char *);
M
M. Mohan Kumar 已提交
394
				uint16_t len = 0;
E
Eric Van Hensbergen 已提交
395
				if (sptr)
396
					len = min_t(size_t, strlen(sptr),
M
M. Mohan Kumar 已提交
397
								USHRT_MAX);
398

399 400
				errcode = p9pdu_writef(pdu, proto_version,
								"w", len);
E
Eric Van Hensbergen 已提交
401
				if (!errcode && pdu_write(pdu, sptr, len))
402 403 404
					errcode = -EFAULT;
			}
			break;
405 406 407 408 409 410 411 412 413 414 415 416 417 418
		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;
419 420 421 422
		case 'Q':{
				const struct p9_qid *qid =
				    va_arg(ap, const struct p9_qid *);
				errcode =
423
				    p9pdu_writef(pdu, proto_version, "bdq",
424 425 426 427 428 429 430
						 qid->type, qid->version,
						 qid->path);
			} break;
		case 'S':{
				const struct p9_wstat *stbuf =
				    va_arg(ap, const struct p9_wstat *);
				errcode =
431
				    p9pdu_writef(pdu, proto_version,
432
						 "wwdQdddqssss?sugu",
433
						 stbuf->size, stbuf->type,
434
						 stbuf->dev, &stbuf->qid,
435 436 437 438 439 440 441
						 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;
442
		case 'V':{
443
				uint32_t count = va_arg(ap, uint32_t);
444 445
				struct iov_iter *from =
						va_arg(ap, struct iov_iter *);
446 447
				errcode = p9pdu_writef(pdu, proto_version, "d",
									count);
448
				if (!errcode && pdu_write_u(pdu, from, count))
449 450 451
					errcode = -EFAULT;
			}
			break;
452
		case 'T':{
453
				uint16_t nwname = va_arg(ap, int);
454 455
				const char **wnames = va_arg(ap, const char **);

456 457
				errcode = p9pdu_writef(pdu, proto_version, "w",
									nwname);
458 459 460 461 462
				if (!errcode) {
					int i;

					for (i = 0; i < nwname; i++) {
						errcode =
463 464
						    p9pdu_writef(pdu,
								proto_version,
465 466 467 468 469 470 471 472 473
								 "s",
								 wnames[i]);
						if (errcode)
							break;
					}
				}
			}
			break;
		case 'R':{
474
				uint16_t nwqid = va_arg(ap, int);
475 476 477
				struct p9_qid *wqids =
				    va_arg(ap, struct p9_qid *);

478 479
				errcode = p9pdu_writef(pdu, proto_version, "w",
									nwqid);
480 481 482 483 484
				if (!errcode) {
					int i;

					for (i = 0; i < nwqid; i++) {
						errcode =
485 486
						    p9pdu_writef(pdu,
								proto_version,
487 488 489 490 491 492 493 494
								 "Q",
								 &wqids[i]);
						if (errcode)
							break;
					}
				}
			}
			break;
495 496 497 498 499
		case 'I':{
				struct p9_iattr_dotl *p9attr = va_arg(ap,
							struct p9_iattr_dotl *);

				errcode = p9pdu_writef(pdu, proto_version,
500
							"ddugqqqqq",
501 502 503 504 505 506 507 508 509 510 511
							p9attr->valid,
							p9attr->mode,
							p9attr->uid,
							p9attr->gid,
							p9attr->size,
							p9attr->atime_sec,
							p9attr->atime_nsec,
							p9attr->mtime_sec,
							p9attr->mtime_nsec);
			}
			break;
512
		case '?':
513 514
			if ((proto_version != p9_proto_2000u) &&
				(proto_version != p9_proto_2000L))
515 516 517 518 519 520 521 522 523 524 525 526 527 528
				return 0;
			break;
		default:
			BUG();
			break;
		}

		if (errcode)
			break;
	}

	return errcode;
}

529
int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
530 531 532 533 534
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
535
	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
536 537 538 539 540 541
	va_end(ap);

	return ret;
}

static int
542
p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
543 544 545 546 547
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
548
	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
549 550 551 552
	va_end(ap);

	return ret;
}
553

554
int p9stat_read(struct p9_client *clnt, char *buf, int len, struct p9_wstat *st)
555 556
{
	struct p9_fcall fake_pdu;
E
Eric Van Hensbergen 已提交
557
	int ret;
558 559 560 561 562 563

	fake_pdu.size = len;
	fake_pdu.capacity = len;
	fake_pdu.sdata = buf;
	fake_pdu.offset = 0;

564
	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "S", st);
E
Eric Van Hensbergen 已提交
565
	if (ret) {
566
		p9_debug(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
567
		trace_9p_protocol_dump(clnt, &fake_pdu);
E
Eric Van Hensbergen 已提交
568 569 570
	}

	return ret;
571 572 573
}
EXPORT_SYMBOL(p9stat_read);

574 575
int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
{
576
	pdu->id = type;
577 578 579
	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
}

580
int p9pdu_finalize(struct p9_client *clnt, struct p9_fcall *pdu)
581 582 583 584 585 586 587 588
{
	int size = pdu->size;
	int err;

	pdu->size = 0;
	err = p9pdu_writef(pdu, 0, "d", size);
	pdu->size = size;

589
	trace_9p_protocol_dump(clnt, pdu);
590 591
	p9_debug(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n",
		 pdu->size, pdu->id, pdu->tag);
E
Eric Van Hensbergen 已提交
592

593 594 595 596 597 598 599 600
	return err;
}

void p9pdu_reset(struct p9_fcall *pdu)
{
	pdu->offset = 0;
	pdu->size = 0;
}
601

602 603
int p9dirent_read(struct p9_client *clnt, char *buf, int len,
		  struct p9_dirent *dirent)
604 605 606 607 608 609 610 611 612 613
{
	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;

614 615
	ret = p9pdu_readf(&fake_pdu, clnt->proto_version, "Qqbs", &dirent->qid,
			  &dirent->d_off, &dirent->d_type, &nameptr);
616
	if (ret) {
617
		p9_debug(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
618
		trace_9p_protocol_dump(clnt, &fake_pdu);
619 620 621 622
		goto out;
	}

	strcpy(dirent->d_name, nameptr);
623
	kfree(nameptr);
624 625 626 627 628

out:
	return fake_pdu.offset;
}
EXPORT_SYMBOL(p9dirent_read);