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 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
void
p9pdu_dump(int way, struct p9_fcall *pdu)
{
	int i, n;
	u8 *data = pdu->sdata;
	int datalen = pdu->size;
	char buf[255];
	int buflen = 255;

	i = n = 0;
	if (datalen > (buflen-16))
		datalen = buflen-16;
	while (i < datalen) {
		n += scnprintf(buf + n, buflen - n, "%02x ", data[i]);
		if (i%4 == 3)
			n += scnprintf(buf + n, buflen - n, " ");
		if (i%32 == 31)
			n += scnprintf(buf + n, buflen - n, "\n");

		i++;
	}
	n += scnprintf(buf + n, buflen - n, "\n");

	if (way)
E
Eric Van Hensbergen 已提交
68
		P9_DPRINTK(P9_DEBUG_PKT, "[[[(%d) %s\n", datalen, buf);
69
	else
E
Eric Van Hensbergen 已提交
70
		P9_DPRINTK(P9_DEBUG_PKT, "]]](%d) %s\n", datalen, buf);
71
}
E
Eric Van Hensbergen 已提交
72 73 74 75 76 77
#else
void
p9pdu_dump(int way, struct p9_fcall *pdu)
{
}
#endif
78 79
EXPORT_SYMBOL(p9pdu_dump);

80 81 82 83 84 85 86 87 88 89 90 91
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)
{
92
	size_t len = min(pdu->size - pdu->offset, size);
93 94 95 96 97 98 99
	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)
{
100
	size_t len = min(pdu->capacity - pdu->size, size);
101 102 103 104 105
	memcpy(&pdu->sdata[pdu->size], data, len);
	pdu->size += len;
	return size - len;
}

106 107 108
static size_t
pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size)
{
109
	size_t len = min(pdu->capacity - pdu->size, size);
110 111
	if (copy_from_user(&pdu->sdata[pdu->size], udata, len))
		len = 0;
112 113 114 115 116

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

117 118 119 120 121 122 123 124 125 126 127
/*
	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)
128
	A - stat for 9p2000.L (p9_stat_dotl)
129 130 131 132
	? - if optional = 1, continue parsing
*/

static int
133 134
p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
	va_list ap)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
{
	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 *);
151 152
				__le16 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
153 154 155
					errcode = -EFAULT;
					break;
				}
156
				*val = le16_to_cpu(le_val);
157 158 159 160
			}
			break;
		case 'd':{
				int32_t *val = va_arg(ap, int32_t *);
161 162
				__le32 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
163 164 165
					errcode = -EFAULT;
					break;
				}
166
				*val = le32_to_cpu(le_val);
167 168 169 170
			}
			break;
		case 'q':{
				int64_t *val = va_arg(ap, int64_t *);
171 172
				__le64 le_val;
				if (pdu_read(pdu, &le_val, sizeof(le_val))) {
173 174 175
					errcode = -EFAULT;
					break;
				}
176
				*val = le64_to_cpu(le_val);
177 178 179
			}
			break;
		case 's':{
E
Eric Van Hensbergen 已提交
180
				char **sptr = va_arg(ap, char **);
181 182 183
				int16_t len;
				int size;

184 185
				errcode = p9pdu_readf(pdu, proto_version,
								"w", &len);
186 187 188
				if (errcode)
					break;

189
				size = max_t(int16_t, len, 0);
190

E
Eric Van Hensbergen 已提交
191 192
				*sptr = kmalloc(size + 1, GFP_KERNEL);
				if (*sptr == NULL) {
193 194 195
					errcode = -EFAULT;
					break;
				}
E
Eric Van Hensbergen 已提交
196
				if (pdu_read(pdu, *sptr, size)) {
197
					errcode = -EFAULT;
E
Eric Van Hensbergen 已提交
198 199
					kfree(*sptr);
					*sptr = NULL;
200
				} else
E
Eric Van Hensbergen 已提交
201
					(*sptr)[size] = 0;
202 203 204 205 206 207
			}
			break;
		case 'Q':{
				struct p9_qid *qid =
				    va_arg(ap, struct p9_qid *);

208
				errcode = p9pdu_readf(pdu, proto_version, "bdq",
209 210 211 212 213 214 215 216
						      &qid->type, &qid->version,
						      &qid->path);
			}
			break;
		case 'S':{
				struct p9_wstat *stbuf =
				    va_arg(ap, struct p9_wstat *);

217
				memset(stbuf, 0, sizeof(struct p9_wstat));
218
				stbuf->n_uid = stbuf->n_gid = stbuf->n_muid =
219
									-1;
220
				errcode =
221
				    p9pdu_readf(pdu, proto_version,
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
						"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':{
				int32_t *count = va_arg(ap, int32_t *);
				void **data = va_arg(ap, void **);

				errcode =
241
				    p9pdu_readf(pdu, proto_version, "d", count);
242 243
				if (!errcode) {
					*count =
244 245
					    min_t(int32_t, *count,
						  pdu->size - pdu->offset);
246 247 248 249 250 251 252 253
					*data = &pdu->sdata[pdu->offset];
				}
			}
			break;
		case 'T':{
				int16_t *nwname = va_arg(ap, int16_t *);
				char ***wnames = va_arg(ap, char ***);

254 255
				errcode = p9pdu_readf(pdu, proto_version,
								"w", nwname);
256 257 258 259 260 261 262 263 264 265 266 267 268
				if (!errcode) {
					*wnames =
					    kmalloc(sizeof(char *) * *nwname,
						    GFP_KERNEL);
					if (!*wnames)
						errcode = -ENOMEM;
				}

				if (!errcode) {
					int i;

					for (i = 0; i < *nwname; i++) {
						errcode =
269 270
						    p9pdu_readf(pdu,
								proto_version,
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
								"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 =
298
				    p9pdu_readf(pdu, proto_version, "w", nwqid);
299 300 301 302 303 304 305 306 307 308 309 310 311 312
				if (!errcode) {
					*wqids =
					    kmalloc(*nwqid *
						    sizeof(struct p9_qid),
						    GFP_KERNEL);
					if (*wqids == NULL)
						errcode = -ENOMEM;
				}

				if (!errcode) {
					int i;

					for (i = 0; i < *nwqid; i++) {
						errcode =
313 314
						    p9pdu_readf(pdu,
								proto_version,
315 316 317 318 319 320 321 322 323 324 325 326 327
								"Q",
								&(*wqids)[i]);
						if (errcode)
							break;
					}
				}

				if (errcode) {
					kfree(*wqids);
					*wqids = NULL;
				}
			}
			break;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		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;
355
		case '?':
356 357
			if ((proto_version != p9_proto_2000u) &&
				(proto_version != p9_proto_2000L))
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
				return 0;
			break;
		default:
			BUG();
			break;
		}

		if (errcode)
			break;
	}

	return errcode;
}

int
373 374
p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
	va_list ap)
375 376 377 378 379 380 381 382 383 384 385 386 387
{
	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':{
388
				__le16 val = cpu_to_le16(va_arg(ap, int));
389 390 391 392 393
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 'd':{
394
				__le32 val = cpu_to_le32(va_arg(ap, int32_t));
395 396 397 398 399
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 'q':{
400
				__le64 val = cpu_to_le64(va_arg(ap, int64_t));
401 402 403 404 405
				if (pdu_write(pdu, &val, sizeof(val)))
					errcode = -EFAULT;
			}
			break;
		case 's':{
E
Eric Van Hensbergen 已提交
406
				const char *sptr = va_arg(ap, const char *);
407
				int16_t len = 0;
E
Eric Van Hensbergen 已提交
408
				if (sptr)
409
					len = min_t(int16_t, strlen(sptr), USHRT_MAX);
410

411 412
				errcode = p9pdu_writef(pdu, proto_version,
								"w", len);
E
Eric Van Hensbergen 已提交
413
				if (!errcode && pdu_write(pdu, sptr, len))
414 415 416 417 418 419 420
					errcode = -EFAULT;
			}
			break;
		case 'Q':{
				const struct p9_qid *qid =
				    va_arg(ap, const struct p9_qid *);
				errcode =
421
				    p9pdu_writef(pdu, proto_version, "bdq",
422 423 424 425 426 427 428
						 qid->type, qid->version,
						 qid->path);
			} break;
		case 'S':{
				const struct p9_wstat *stbuf =
				    va_arg(ap, const struct p9_wstat *);
				errcode =
429
				    p9pdu_writef(pdu, proto_version,
430 431
						 "wwdQdddqssss?sddd",
						 stbuf->size, stbuf->type,
432
						 stbuf->dev, &stbuf->qid,
433 434 435 436 437 438 439 440 441 442 443
						 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':{
				int32_t count = va_arg(ap, int32_t);
				const void *data = va_arg(ap, const void *);

444 445
				errcode = p9pdu_writef(pdu, proto_version, "d",
									count);
446 447 448 449
				if (!errcode && pdu_write(pdu, data, count))
					errcode = -EFAULT;
			}
			break;
450 451 452
		case 'U':{
				int32_t count = va_arg(ap, int32_t);
				const char __user *udata =
E
Eric Van Hensbergen 已提交
453
						va_arg(ap, const void __user *);
454 455
				errcode = p9pdu_writef(pdu, proto_version, "d",
									count);
456 457 458 459
				if (!errcode && pdu_write_u(pdu, udata, count))
					errcode = -EFAULT;
			}
			break;
460 461 462 463
		case 'T':{
				int16_t nwname = va_arg(ap, int);
				const char **wnames = va_arg(ap, const char **);

464 465
				errcode = p9pdu_writef(pdu, proto_version, "w",
									nwname);
466 467 468 469 470
				if (!errcode) {
					int i;

					for (i = 0; i < nwname; i++) {
						errcode =
471 472
						    p9pdu_writef(pdu,
								proto_version,
473 474 475 476 477 478 479 480 481 482 483 484 485
								 "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 *);

486 487
				errcode = p9pdu_writef(pdu, proto_version, "w",
									nwqid);
488 489 490 491 492
				if (!errcode) {
					int i;

					for (i = 0; i < nwqid; i++) {
						errcode =
493 494
						    p9pdu_writef(pdu,
								proto_version,
495 496 497 498 499 500 501 502
								 "Q",
								 &wqids[i]);
						if (errcode)
							break;
					}
				}
			}
			break;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		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;
520
		case '?':
521 522
			if ((proto_version != p9_proto_2000u) &&
				(proto_version != p9_proto_2000L))
523 524 525 526 527 528 529 530 531 532 533 534 535 536
				return 0;
			break;
		default:
			BUG();
			break;
		}

		if (errcode)
			break;
	}

	return errcode;
}

537
int p9pdu_readf(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
538 539 540 541 542
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
543
	ret = p9pdu_vreadf(pdu, proto_version, fmt, ap);
544 545 546 547 548 549
	va_end(ap);

	return ret;
}

static int
550
p9pdu_writef(struct p9_fcall *pdu, int proto_version, const char *fmt, ...)
551 552 553 554 555
{
	va_list ap;
	int ret;

	va_start(ap, fmt);
556
	ret = p9pdu_vwritef(pdu, proto_version, fmt, ap);
557 558 559 560
	va_end(ap);

	return ret;
}
561

562
int p9stat_read(char *buf, int len, struct p9_wstat *st, int proto_version)
563 564
{
	struct p9_fcall fake_pdu;
E
Eric Van Hensbergen 已提交
565
	int ret;
566 567 568 569 570 571

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

572
	ret = p9pdu_readf(&fake_pdu, proto_version, "S", st);
E
Eric Van Hensbergen 已提交
573 574 575 576 577 578
	if (ret) {
		P9_DPRINTK(P9_DEBUG_9P, "<<< p9stat_read failed: %d\n", ret);
		p9pdu_dump(1, &fake_pdu);
	}

	return ret;
579 580 581
}
EXPORT_SYMBOL(p9stat_read);

582 583 584 585 586 587 588 589 590 591 592 593 594 595
int p9pdu_prepare(struct p9_fcall *pdu, int16_t tag, int8_t type)
{
	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;

E
Eric Van Hensbergen 已提交
596
#ifdef CONFIG_NET_9P_DEBUG
E
Eric Van Hensbergen 已提交
597
	if ((p9_debug_level & P9_DEBUG_PKT) == P9_DEBUG_PKT)
598
		p9pdu_dump(0, pdu);
E
Eric Van Hensbergen 已提交
599
#endif
600

E
Eric Van Hensbergen 已提交
601 602 603
	P9_DPRINTK(P9_DEBUG_9P, ">>> size=%d type: %d tag: %d\n", pdu->size,
							pdu->id, pdu->tag);

604 605 606 607 608 609 610 611
	return err;
}

void p9pdu_reset(struct p9_fcall *pdu)
{
	pdu->offset = 0;
	pdu->size = 0;
}
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638

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);
		p9pdu_dump(1, &fake_pdu);
		goto out;
	}

	strcpy(dirent->d_name, nameptr);

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