protocol.c 14.3 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 37 38 39 40
#include <net/9p/9p.h>
#include <net/9p/client.h>
#include "protocol.h"

static int
41
p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...);
42

E
Eric Van Hensbergen 已提交
43
#ifdef CONFIG_NET_9P_DEBUG
44 45 46
void
p9pdu_dump(int way, struct p9_fcall *pdu)
{
47 48 49 50 51 52 53 54 55 56
	int len = pdu->size;

	if ((p9_debug_level & P9_DEBUG_VPKT) != P9_DEBUG_VPKT) {
		if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT) {
			if (len > 32)
				len = 32;
		} else {
			/* shouldn't happen */
			return;
		}
57 58 59
	}

	if (way)
60 61
		print_hex_dump_bytes("[9P] ", DUMP_PREFIX_OFFSET, pdu->sdata,
									len);
62
	else
63 64
		print_hex_dump_bytes("]9P[ ", DUMP_PREFIX_OFFSET, pdu->sdata,
									len);
65
}
E
Eric Van Hensbergen 已提交
66 67 68 69 70 71
#else
void
p9pdu_dump(int way, struct p9_fcall *pdu)
{
}
#endif
72 73
EXPORT_SYMBOL(p9pdu_dump);

74 75 76 77 78 79 80 81 82 83 84 85
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);

static size_t pdu_read(struct p9_fcall *pdu, void *data, size_t size)
{
86
	size_t len = min(pdu->size - pdu->offset, size);
87 88 89 90 91 92 93
	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)
{
94
	size_t len = min(pdu->capacity - pdu->size, size);
95 96 97 98 99
	memcpy(&pdu->sdata[pdu->size], data, len);
	pdu->size += len;
	return size - len;
}

100 101 102
static size_t
pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
{
103
	size_t len = min(pdu->capacity - pdu->size, size);
104 105
	if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
		len = 0;
106 107 108 109 110

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

111 112 113 114 115 116 117 118 119 120 121
static size_t
pdu_write_urw(struct p9_fcall *pdu, const char *kdata, const char __user *udata,
		size_t size)
{
	BUG_ON(pdu->size > P9_IOHDRSZ);
	pdu->pubuf = (char __user *)udata;
	pdu->pkbuf = (char *)kdata;
	pdu->pbuf_size = size;
	return 0;
}

122 123 124 125 126 127 128 129 130
static size_t
pdu_write_readdir(struct p9_fcall *pdu, const char *kdata, size_t size)
{
	BUG_ON(pdu->size > P9_READDIRHDRSZ);
	pdu->pkbuf = (char *)kdata;
	pdu->pbuf_size = size;
	return 0;
}

131 132 133 134 135 136 137 138 139 140 141
/*
	b - int8_t
	w - int16_t
	d - int32_t
	q - int64_t
	s - string
	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)
142
	A - stat for 9p2000.L (p9_stat_dotl)
143 144 145 146
	? - if optional = 1, continue parsing
*/

static int
147 148
p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
	va_list ap)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
{
	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 *);
165 166
				__le16 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
167 168 169
					errcode = -EFAULT;
					break;
				}
170
				*val = le16_to_cpu(le_val);
171 172 173 174
			}
			break;
		case 'd':{
				int32_t *val = va_arg(ap, int32_t *);
175 176
				__le32 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
177 178 179
					errcode = -EFAULT;
					break;
				}
180
				*val = le32_to_cpu(le_val);
181 182 183 184
			}
			break;
		case 'q':{
				int64_t *val = va_arg(ap, int64_t *);
185 186
				__le64 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
187 188 189
					errcode = -EFAULT;
					break;
				}
190
				*val = le64_to_cpu(le_val);
191 192 193
			}
			break;
		case 's':{
E
Eric Van Hensbergen 已提交
194
				char **sptr = va_arg(ap, char **);
M
M. Mohan Kumar 已提交
195
				uint16_t len;
196

197 198
				errcode = p9pdu_readf(pdu, proto_version,
								"w", &len);
199 200 201
				if (errcode)
					break;

202
				*sptr = kmalloc(len + 1, GFP_NOFS);
E
Eric Van Hensbergen 已提交
203
				if (*sptr == NULL) {
204 205 206
					errcode = -EFAULT;
					break;
				}
M
M. Mohan Kumar 已提交
207
				if (pdu_read(pdu, *sptr, len)) {
208
					errcode = -EFAULT;
E
Eric Van Hensbergen 已提交
209 210
					kfree(*sptr);
					*sptr = NULL;
211
				} else
M
M. Mohan Kumar 已提交
212
					(*sptr)[len] = 0;
213 214 215 216 217 218
			}
			break;
		case 'Q':{
				struct p9_qid *qid =
				    va_arg(ap, struct p9_qid *);

219
				errcode = p9pdu_readf(pdu, proto_version, "bdq",
220 221 222 223 224 225 226 227
						      &qid->type, &qid->version,
						      &qid->path);
			}
			break;
		case 'S':{
				struct p9_wstat *stbuf =
				    va_arg(ap, struct p9_wstat *);

228
				memset(stbuf, 0, sizeof(struct p9_wstat));
229
				stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
230
									-1;
231
				errcode =
232
				    p9pdu_readf(pdu, proto_version,
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
						"wwdQdddqssss?sddd",
						&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 已提交
248
				uint32_t *count = va_arg(ap, uint32_t *);
249 250 251
				void **data = va_arg(ap, void **);

				errcode =
252
				    p9pdu_readf(pdu, proto_version, "d", count);
253 254
				if (!errcode) {
					*count =
M
M. Mohan Kumar 已提交
255
					    min_t(uint32_t, *count,
256
						  pdu->size - pdu->offset);
257 258 259 260 261
					*data = &pdu->sdata[pdu->offset];
				}
			}
			break;
		case 'T':{
262
				uint16_t *nwname = va_arg(ap, uint16_t *);
263 264
				char ***wnames = va_arg(ap, char ***);

265 266
				errcode = p9pdu_readf(pdu, proto_version,
								"w", nwname);
267 268 269
				if (!errcode) {
					*wnames =
					    kmalloc(sizeof(char *) * *nwname,
270
						    GFP_NOFS);
271 272 273 274 275 276 277 278 279
					if (!*wnames)
						errcode = -ENOMEM;
				}

				if (!errcode) {
					int i;

					for (i = 0; i < *nwname; i++) {
						errcode =
280 281
						    p9pdu_readf(pdu,
								proto_version,
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
								"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':{
				int16_t *nwqid = va_arg(ap, int16_t *);
				struct p9_qid **wqids =
				    va_arg(ap, struct p9_qid **);

				*wqids = NULL;

				errcode =
309
				    p9pdu_readf(pdu, proto_version, "w", nwqid);
310 311 312 313
				if (!errcode) {
					*wqids =
					    kmalloc(*nwqid *
						    sizeof(struct p9_qid),
314
						    GFP_NOFS);
315 316 317 318 319 320 321 322 323
					if (*wqids == NULL)
						errcode = -ENOMEM;
				}

				if (!errcode) {
					int i;

					for (i = 0; i < *nwqid; i++) {
						errcode =
324 325
						    p9pdu_readf(pdu,
								proto_version,
326 327 328 329 330 331 332 333 334 335 336 337 338
								"Q",
								&(*wqids)[i]);
						if (errcode)
							break;
					}
				}

				if (errcode) {
					kfree(*wqids);
					*wqids = NULL;
				}
			}
			break;
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
		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,
					"qQdddqqqqqqqqqqqqqqq",
					&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;
366
		case '?':
367 368
			if ((proto_version != p9_proto_2000u) &&
				(proto_version != p9_proto_2000L))
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
				return 0;
			break;
		default:
			BUG();
			break;
		}

		if (errcode)
			break;
	}

	return errcode;
}

int
384 385
p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
	va_list ap)
386 387 388 389 390 391 392 393 394 395 396 397 398
{
	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':{
399
				__le16 val = cpu_to_le16(va_arg(ap, int));
400 401 402 403 404
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 'd':{
405
				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
406 407 408 409 410
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 'q':{
411
				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
412 413 414 415 416
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 's':{
E
Eric Van Hensbergen 已提交
417
				const char *sptr = va_arg(ap, const char *);
M
M. Mohan Kumar 已提交
418
				uint16_t len = 0;
E
Eric Van Hensbergen 已提交
419
				if (sptr)
M
M. Mohan Kumar 已提交
420 421
					len = min_t(uint16_t, strlen(sptr),
								USHRT_MAX);
422

423 424
				errcode = p9pdu_writef(pdu, proto_version,
								"w", len);
E
Eric Van Hensbergen 已提交
425
				if (!errcode && pdu_write(pdu, sptr, len))
426 427 428 429 430 431 432
					errcode = -EFAULT;
			}
			break;
		case 'Q':{
				const struct p9_qid *qid =
				    va_arg(ap, const struct p9_qid *);
				errcode =
433
				    p9pdu_writef(pdu, proto_version, "bdq",
434 435 436 437 438 439 440
						 qid->type, qid->version,
						 qid->path);
			} break;
		case 'S':{
				const struct p9_wstat *stbuf =
				    va_arg(ap, const struct p9_wstat *);
				errcode =
441
				    p9pdu_writef(pdu, proto_version,
442 443
						 "wwdQdddqssss?sddd",
						 stbuf->size, stbuf->type,
444
						 stbuf->dev, &stbuf->qid,
445 446 447 448 449 450 451 452
						 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;
		case 'D':{
M
M. Mohan Kumar 已提交
453
				uint32_t count = va_arg(ap, uint32_t);
454 455
				const void *data = va_arg(ap, const void *);

456 457
				errcode = p9pdu_writef(pdu, proto_version, "d",
									count);
458 459 460 461
				if (!errcode && pdu_write(pdu, data, count))
					errcode = -EFAULT;
			}
			break;
462 463 464
		case 'E':{
				 int32_t cnt = va_arg(ap, int32_t);
				 const char *k = va_arg(ap, const void *);
A
Aneesh Kumar K.V 已提交
465 466
				 const char __user *u = va_arg(ap,
							const void __user *);
467 468 469 470 471 472
				 errcode = p9pdu_writef(pdu, proto_version, "d",
						 cnt);
				 if (!errcode && pdu_write_urw(pdu, k, u, cnt))
					errcode = -EFAULT;
			 }
			 break;
473 474 475 476 477 478 479 480 481
		case 'F':{
				 int32_t cnt = va_arg(ap, int32_t);
				 const char *k = va_arg(ap, const void *);
				 errcode = p9pdu_writef(pdu, proto_version, "d",
						 cnt);
				 if (!errcode && pdu_write_readdir(pdu, k, cnt))
					errcode = -EFAULT;
			 }
			 break;
482 483 484
		case 'U':{
				int32_t count = va_arg(ap, int32_t);
				const char __user *udata =
E
Eric Van Hensbergen 已提交
485
						va_arg(ap, const void __user *);
486 487
				errcode = p9pdu_writef(pdu, proto_version, "d",
									count);
488 489 490 491
				if (!errcode && pdu_write_u(pdu, udata, count))
					errcode = -EFAULT;
			}
			break;
492
		case 'T':{
493
				uint16_t nwname = va_arg(ap, int);
494 495
				const char **wnames = va_arg(ap, const char **);

496 497
				errcode = p9pdu_writef(pdu, proto_version, "w",
									nwname);
498 499 500 501 502
				if (!errcode) {
					int i;

					for (i = 0; i < nwname; i++) {
						errcode =
503 504
						    p9pdu_writef(pdu,
								proto_version,
505 506 507 508 509 510 511 512 513 514 515 516 517
								 "s",
								 wnames[i]);
						if (errcode)
							break;
					}
				}
			}
			break;
		case 'R':{
				int16_t nwqid = va_arg(ap, int);
				struct p9_qid *wqids =
				    va_arg(ap, struct p9_qid *);

518 519
				errcode = p9pdu_writef(pdu, proto_version, "w",
									nwqid);
520 521 522 523 524
				if (!errcode) {
					int i;

					for (i = 0; i < nwqid; i++) {
						errcode =
525 526
						    p9pdu_writef(pdu,
								proto_version,
527 528 529 530 531 532 533 534
								 "Q",
								 &wqids[i]);
						if (errcode)
							break;
					}
				}
			}
			break;
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
		case 'I':{
				struct p9_iattr_dotl *p9attr = va_arg(ap,
							struct p9_iattr_dotl *);

				errcode = p9pdu_writef(pdu, proto_version,
							"ddddqqqqq",
							p9attr->valid,
							p9attr->mode,
							p9attr->uid,
							p9attr->gid,
							p9attr->size,
							p9attr->atime_sec,
							p9attr->atime_nsec,
							p9attr->mtime_sec,
							p9attr->mtime_nsec);
			}
			break;
552
		case '?':
553 554
			if ((proto_version != p9_proto_2000u) &&
				(proto_version != p9_proto_2000L))
555 556 557 558 559 560 561 562 563 564 565 566 567 568
				return 0;
			break;
		default:
			BUG();
			break;
		}

		if (errcode)
			break;
	}

	return errcode;
}

569
int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
570 571 572 573 574
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
575
	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
576 577 578 579 580 581
	va_end(ap);

	return ret;
}

static int
582
p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
583 584 585 586 587
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
588
	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
589 590 591 592
	va_end(ap);

	return ret;
}
593

594
int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
595 596
{
	struct p9_fcall fake_pdu;
E
Eric Van Hensbergen 已提交
597
	int ret;
598 599 600 601 602 603

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

604
	ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
E
Eric Van Hensbergen 已提交
605 606
	if (ret) {
		P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
607
		P9_DUMP_PKT(0, &fake_pdu);
E
Eric Van Hensbergen 已提交
608 609 610
	}

	return ret;
611 612 613
}
EXPORT_SYMBOL(p9stat_read);

614 615
int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
{
616
	pdu->id = type;
617 618 619 620 621 622 623 624 625 626 627 628
	return p9pdu_writef(pdu, 0, "dbw", 0, type, tag);
}

int p9pdu_finalize(struct p9_fcall *pdu)
{
	int size = pdu->size;
	int err;

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

629
	P9_DUMP_PKT(0, pdu);
E
Eric Van Hensbergen 已提交
630 631 632
	P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
							pdu->id, pdu->tag);

633 634 635 636 637 638 639
	return err;
}

void p9pdu_reset(struct p9_fcall *pdu)
{
	pdu->offset = 0;
	pdu->size = 0;
640 641 642 643
	pdu->private = NULL;
	pdu->pubuf = NULL;
	pdu->pkbuf = NULL;
	pdu->pbuf_size = 0;
644
}
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

int p9dirent_read(char *buf, int len, struct p9_dirent *dirent,
						int proto_version)
{
	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;

	ret = p9pdu_readf(&fake_pdu, proto_version, "Qqbs", &dirent->qid,
			&dirent->d_off, &dirent->d_type, &nameptr);
	if (ret) {
		P9_DPRINTK(P9_DEBUG_9P, "<<< p9dirent_read failed: %d\n", ret);
662
		P9_DUMP_PKT(1, &fake_pdu);
663 664 665 666
		goto out;
	}

	strcpy(dirent->d_name, nameptr);
667
	kfree(nameptr);
668 669 670 671 672

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