trans_fd.c 29.5 KB
Newer Older
1 2 3 4 5 6 7
/*
 * linux/fs/9p/trans_fd.c
 *
 * Fd transport layer.  Includes deprecated socket layer.
 *
 *  Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
 *  Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
8
 *  Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *  Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
 *
 *  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/in.h>
#include <linux/module.h>
#include <linux/net.h>
#include <linux/ipv6.h>
32
#include <linux/kthread.h>
33 34 35 36 37 38 39
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/un.h>
#include <linux/uaccess.h>
#include <linux/inet.h>
#include <linux/idr.h>
#include <linux/file.h>
40
#include <linux/parser.h>
41
#include <net/9p/9p.h>
42
#include <net/9p/client.h>
43 44 45
#include <net/9p/transport.h>

#define P9_PORT 564
46
#define MAX_SOCK_BUF (64*1024)
47
#define MAXPOLLWADDR	2
48

49 50 51 52 53 54 55 56
/**
 * struct p9_fd_opts - per-transport options
 * @rfd: file descriptor for reading (trans=fd)
 * @wfd: file descriptor for writing (trans=fd)
 * @port: port to connect to (trans=tcp)
 *
 */

57 58 59 60 61
struct p9_fd_opts {
	int rfd;
	int wfd;
	u16 port;
};
62

63 64 65 66 67 68 69 70
/**
 * struct p9_trans_fd - transport state
 * @rd: reference to file to read from
 * @wr: reference of file to write to
 * @conn: connection state reference
 *
 */

71 72 73
struct p9_trans_fd {
	struct file *rd;
	struct file *wr;
74
	struct p9_conn *conn;
75 76
};

77 78 79 80
/*
  * Option Parsing (code inspired by NFS code)
  *  - a little lazy - parse all fd-transport options
  */
81

82 83
enum {
	/* Options that take integer arguments */
84
	Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
85
};
86

87
static const match_table_t tokens = {
88 89 90
	{Opt_port, "port=%u"},
	{Opt_rfdno, "rfdno=%u"},
	{Opt_wfdno, "wfdno=%u"},
91
	{Opt_err, NULL},
92
};
93

94 95 96 97 98 99 100
enum {
	Rworksched = 1,		/* read work scheduled or running */
	Rpending = 2,		/* can read */
	Wworksched = 4,		/* write work scheduled or running */
	Wpending = 8,		/* can write */
};

Tejun Heo's avatar
Tejun Heo committed
101 102 103 104
struct p9_poll_wait {
	struct p9_conn *conn;
	wait_queue_t wait;
	wait_queue_head_t *wait_addr;
105 106 107 108 109
};

/**
 * struct p9_conn - fd mux connection state information
 * @mux_list: list link for mux to manage multiple connections (?)
110
 * @client: reference to client instance for this connection
111 112 113
 * @err: error state
 * @req_list: accounting for requests which have been sent
 * @unsent_req_list: accounting for requests that haven't been sent
114 115 116
 * @req: current request being processed (if any)
 * @tmp_buf: temporary buffer to read in header
 * @rsize: amount to read for current frame
117 118 119 120 121 122 123 124 125 126 127 128 129
 * @rpos: read position in current frame
 * @rbuf: current read buffer
 * @wpos: write position for current frame
 * @wsize: amount of data to write for current frame
 * @wbuf: current write buffer
 * @poll_wait: array of wait_q's for various worker threads
 * @poll_waddr: ????
 * @pt: poll state
 * @rq: current read work
 * @wq: current write work
 * @wsched: ????
 *
 */
130 131 132

struct p9_conn {
	struct list_head mux_list;
133
	struct p9_client *client;
134 135 136
	int err;
	struct list_head req_list;
	struct list_head unsent_req_list;
137 138 139
	struct p9_req_t *req;
	char tmp_buf[7];
	int rsize;
140 141 142 143 144
	int rpos;
	char *rbuf;
	int wpos;
	int wsize;
	char *wbuf;
Tejun Heo's avatar
Tejun Heo committed
145 146
	struct list_head poll_pending_link;
	struct p9_poll_wait poll_wait[MAXPOLLWADDR];
147 148 149 150 151 152
	poll_table pt;
	struct work_struct rq;
	struct work_struct wq;
	unsigned long wsched;
};

Tejun Heo's avatar
Tejun Heo committed
153 154
static DEFINE_SPINLOCK(p9_poll_lock);
static LIST_HEAD(p9_poll_pending_list);
155
static struct workqueue_struct *p9_mux_wq;
Tejun Heo's avatar
Tejun Heo committed
156
static struct task_struct *p9_poll_task;
157

Tejun Heo's avatar
Tejun Heo committed
158
static void p9_mux_poll_stop(struct p9_conn *m)
159
{
Tejun Heo's avatar
Tejun Heo committed
160 161
	unsigned long flags;
	int i;
162

Tejun Heo's avatar
Tejun Heo committed
163 164
	for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
		struct p9_poll_wait *pwait = &m->poll_wait[i];
165

Tejun Heo's avatar
Tejun Heo committed
166 167 168
		if (pwait->wait_addr) {
			remove_wait_queue(pwait->wait_addr, &pwait->wait);
			pwait->wait_addr = NULL;
169 170 171
		}
	}

Tejun Heo's avatar
Tejun Heo committed
172 173 174
	spin_lock_irqsave(&p9_poll_lock, flags);
	list_del_init(&m->poll_pending_link);
	spin_unlock_irqrestore(&p9_poll_lock, flags);
175 176
}

177
static void p9_conn_rpc_cb(struct p9_client *, struct p9_req_t *);
178

179
static void p9_mux_flush_cb(struct p9_client *client, struct p9_req_t *freq)
180
{
181 182
	struct p9_conn *m = client->trans;
	struct p9_req_t *req;
183 184

	P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
185 186
		freq->tc, freq->rc, freq->t_err,
		freq->tc->params.tflush.oldtag);
187

188
	req = p9_tag_lookup(client, freq->tc->params.tflush.oldtag);
189
	if (req) {
190 191 192
		req->status = REQ_STATUS_FLSHD;
		list_del(&req->req_list);
		p9_conn_rpc_cb(client, req);
193 194
	}

195
	p9_free_req(client, freq);
196 197
}

198
static void p9_conn_rpc_cb(struct p9_client *client, struct p9_req_t *req)
199 200 201
{
	P9_DPRINTK(P9_DEBUG_MUX, "req %p\n", req);

202
	if (req->tc->id == P9_TFLUSH) { /* flush callback */
203
		P9_DPRINTK(P9_DEBUG_MUX, "flush req %p\n", req);
204
		p9_mux_flush_cb(client, req);
205 206
	} else {			/* normal wakeup path */
		P9_DPRINTK(P9_DEBUG_MUX, "normal req %p\n", req);
207 208 209
		if (!req->t_err && (req->status == REQ_STATUS_FLSHD ||
				 req->status == REQ_STATUS_FLSH))
			req->t_err = -ERESTARTSYS;
210

211
		wake_up(req->wq);
212 213 214
	}
}

215
/**
216 217 218
 * p9_conn_cancel - cancel all pending requests with error
 * @m: mux data
 * @err: error code
219 220
 *
 */
221

222
void p9_conn_cancel(struct p9_conn *m, int err)
223
{
224
	struct p9_req_t *req, *rtmp;
225
	LIST_HEAD(cancel_list);
226

227 228
	P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
	m->err = err;
229
	spin_lock(&m->client->lock);
230
	list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
231 232 233
		req->status = REQ_STATUS_ERROR;
		if (!req->t_err)
			req->t_err = err;
234 235 236
		list_move(&req->req_list, &cancel_list);
	}
	list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
237 238 239
		req->status = REQ_STATUS_ERROR;
		if (!req->t_err)
			req->t_err = err;
240
		list_move(&req->req_list, &cancel_list);
241
	}
242
	spin_unlock(&m->client->lock);
243

244 245
	list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
		list_del(&req->req_list);
246
		p9_conn_rpc_cb(m->client, req);
247
	}
248
}
249

250
static void process_request(struct p9_conn *m, struct p9_req_t *req)
251 252 253
{
	int ecode;
	struct p9_str *ename;
254

255 256 257
	if (!req->t_err && req->rc->id == P9_RERROR) {
		ecode = req->rc->params.rerror.errno;
		ename = &req->rc->params.rerror.error;
258 259 260 261 262

		P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
								ename->str);

		if (m->client->dotu)
263
			req->t_err = -ecode;
264

265 266
		if (!req->t_err) {
			req->t_err = p9_errstr2errno(ename->str, ename->len);
267 268

			/* string match failed */
269 270 271
			if (!req->t_err) {
				PRINT_FCALL_ERROR("unknown error", req->rc);
				req->t_err = -ESERVERFAULT;
272
			}
273
		}
274
	} else if (req->tc && req->rc->id != req->tc->id + 1) {
275 276
		P9_DPRINTK(P9_DEBUG_ERROR,
				"fcall mismatch: expected %d, got %d\n",
277 278 279
				req->tc->id + 1, req->rc->id);
		if (!req->t_err)
			req->t_err = -EIO;
280 281 282
	}
}

283 284
static unsigned int
p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
285
{
286 287
	int ret, n;
	struct p9_trans_fd *ts = NULL;
288

289 290
	if (client && client->status == Connected)
		ts = client->trans;
291

292 293
	if (!ts)
		return -EREMOTEIO;
294

295 296
	if (!ts->rd->f_op || !ts->rd->f_op->poll)
		return -EIO;
297

298 299
	if (!ts->wr->f_op || !ts->wr->f_op->poll)
		return -EIO;
Tejun Heo's avatar
Tejun Heo committed
300

301 302 303
	ret = ts->rd->f_op->poll(ts->rd, pt);
	if (ret < 0)
		return ret;
Tejun Heo's avatar
Tejun Heo committed
304

305 306 307 308 309 310 311 312
	if (ts->rd != ts->wr) {
		n = ts->wr->f_op->poll(ts->wr, pt);
		if (n < 0)
			return n;
		ret = (ret & ~POLLOUT) | (n & ~POLLIN);
	}

	return ret;
Tejun Heo's avatar
Tejun Heo committed
313 314
}

315
/**
316 317 318 319
 * p9_fd_read- read from a fd
 * @client: client instance
 * @v: buffer to receive data into
 * @len: size of receive buffer
320
 *
321
 */
322

323
static int p9_fd_read(struct p9_client *client, void *v, int len)
324
{
325 326
	int ret;
	struct p9_trans_fd *ts = NULL;
327

328 329
	if (client && client->status != Disconnected)
		ts = client->trans;
330

331 332
	if (!ts)
		return -EREMOTEIO;
333

334 335
	if (!(ts->rd->f_flags & O_NONBLOCK))
		P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
336

337 338 339 340
	ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
	if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
		client->status = Disconnected;
	return ret;
341 342 343
}

/**
344 345
 * p9_read_work - called when there is some data to be read from a transport
 * @work: container of work to be done
346
 *
347
 */
348

349
static void p9_read_work(struct work_struct *work)
350
{
351 352 353 354
	int n, err;
	struct p9_conn *m;

	m = container_of(work, struct p9_conn, rq);
355 356 357 358

	if (m->err < 0)
		return;

359
	P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
360

361 362
	if (!m->rbuf) {
		m->rbuf = m->tmp_buf;
363
		m->rpos = 0;
364
		m->rsize = 7; /* start by reading header */
365 366
	}

367
	clear_bit(Rpending, &m->wsched);
368 369
	P9_DPRINTK(P9_DEBUG_MUX, "read mux %p pos %d size: %d = %d\n", m,
					m->rpos, m->rsize, m->rsize-m->rpos);
370
	err = p9_fd_read(m->client, m->rbuf + m->rpos,
371
						m->rsize - m->rpos);
372 373 374 375
	P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
	if (err == -EAGAIN) {
		clear_bit(Rworksched, &m->wsched);
		return;
376 377
	}

378 379 380 381
	if (err <= 0)
		goto error;

	m->rpos += err;
382 383 384 385 386 387

	if ((!m->req) && (m->rpos == m->rsize)) { /* header read in */
		u16 tag;
		P9_DPRINTK(P9_DEBUG_MUX, "got new header\n");

		n = le32_to_cpu(*(__le32 *) m->rbuf); /* read packet size */
388 389 390 391 392 393 394
		if (n >= m->client->msize) {
			P9_DPRINTK(P9_DEBUG_ERROR,
				"requested packet size too big: %d\n", n);
			err = -EIO;
			goto error;
		}

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
		tag = le16_to_cpu(*(__le16 *) (m->rbuf+5)); /* read tag */
		P9_DPRINTK(P9_DEBUG_MUX, "mux %p pkt: size: %d bytes tag: %d\n",
								 m, n, tag);

		m->req = p9_tag_lookup(m->client, tag);
		if (!m->req) {
			P9_DPRINTK(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
								 tag);
			err = -EIO;
			goto error;
		}

		if (m->req->rc == NULL) {
			m->req->rc = kmalloc(sizeof(struct p9_fcall) +
						m->client->msize, GFP_KERNEL);
			if (!m->req->rc) {
				m->req = NULL;
				err = -ENOMEM;
				goto error;
			}
		}
		m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall);
		memcpy(m->rbuf, m->tmp_buf, m->rsize);
		m->rsize = n;
	}
420

421 422 423 424 425 426 427 428
	/* not an else because some packets (like clunk) have no payload */
	if ((m->req) && (m->rpos == m->rsize)) { /* packet is read in */
		P9_DPRINTK(P9_DEBUG_MUX, "got new packet\n");
		m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall);
		err = p9_deserialize_fcall(m->rbuf, m->rsize, m->req->rc,
							m->client->dotu);
		if (err < 0) {
			m->req = NULL;
429
			goto error;
430
		}
431 432 433 434 435

#ifdef CONFIG_NET_9P_DEBUG
		if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
			char buf[150];

436
			p9_printfcall(buf, sizeof(buf), m->req->rc,
437 438 439 440 441
				m->client->dotu);
			printk(KERN_NOTICE ">>> %p %s\n", m, buf);
		}
#endif

442 443
		P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
					m->req->rc->id, m->req->rc->tag);
444

445 446 447
		m->rbuf = NULL;
		m->rpos = 0;
		m->rsize = 0;
448

449 450 451 452
		if (m->req->status != REQ_STATUS_FLSH) {
			list_del(&m->req->req_list);
			m->req->status = REQ_STATUS_RCVD;
		}
453

454
		process_request(m, m->req);
455

456 457
		if (m->req->status != REQ_STATUS_FLSH)
			p9_conn_rpc_cb(m->client, m->req);
458

459
		m->req = NULL;
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	}

	if (!list_empty(&m->req_list)) {
		if (test_and_clear_bit(Rpending, &m->wsched))
			n = POLLIN;
		else
			n = p9_fd_poll(m->client, NULL);

		if (n & POLLIN) {
			P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
			queue_work(p9_mux_wq, &m->rq);
		} else
			clear_bit(Rworksched, &m->wsched);
	} else
		clear_bit(Rworksched, &m->wsched);

	return;
error:
	p9_conn_cancel(m, err);
	clear_bit(Rworksched, &m->wsched);
}

/**
 * p9_fd_write - write to a socket
 * @client: client instance
 * @v: buffer to send data from
 * @len: size of send buffer
487
 *
488
 */
489

490
static int p9_fd_write(struct p9_client *client, void *v, int len)
491
{
492 493 494
	int ret;
	mm_segment_t oldfs;
	struct p9_trans_fd *ts = NULL;
495

496 497
	if (client && client->status != Disconnected)
		ts = client->trans;
498

499 500
	if (!ts)
		return -EREMOTEIO;
501

502 503
	if (!(ts->wr->f_flags & O_NONBLOCK))
		P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
Tejun Heo's avatar
Tejun Heo committed
504

505 506 507 508 509
	oldfs = get_fs();
	set_fs(get_ds());
	/* The cast to a user pointer is valid due to the set_fs() */
	ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
	set_fs(oldfs);
Tejun Heo's avatar
Tejun Heo committed
510

511 512 513
	if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
		client->status = Disconnected;
	return ret;
514 515 516 517
}

/**
 * p9_write_work - called when a transport can send some data
518 519
 * @work: container for work to be done
 *
520
 */
521

522 523 524 525
static void p9_write_work(struct work_struct *work)
{
	int n, err;
	struct p9_conn *m;
526
	struct p9_req_t *req;
527 528 529 530 531 532 533 534 535 536 537 538 539 540

	m = container_of(work, struct p9_conn, wq);

	if (m->err < 0) {
		clear_bit(Wworksched, &m->wsched);
		return;
	}

	if (!m->wsize) {
		if (list_empty(&m->unsent_req_list)) {
			clear_bit(Wworksched, &m->wsched);
			return;
		}

541 542
		spin_lock(&m->client->lock);
		req = list_entry(m->unsent_req_list.next, struct p9_req_t,
543
			       req_list);
544
		req->status = REQ_STATUS_SENT;
545 546
		list_move_tail(&req->req_list, &m->req_list);

547 548
		m->wbuf = req->tc->sdata;
		m->wsize = req->tc->size;
549
		m->wpos = 0;
550
		spin_unlock(&m->client->lock);
551 552 553 554 555
	}

	P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
								m->wsize);
	clear_bit(Wpending, &m->wsched);
556
	err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
	if (err == -EAGAIN) {
		clear_bit(Wworksched, &m->wsched);
		return;
	}

	if (err < 0)
		goto error;
	else if (err == 0) {
		err = -EREMOTEIO;
		goto error;
	}

	m->wpos += err;
	if (m->wpos == m->wsize)
		m->wpos = m->wsize = 0;

	if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
		if (test_and_clear_bit(Wpending, &m->wsched))
			n = POLLOUT;
		else
578
			n = p9_fd_poll(m->client, NULL);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

		if (n & POLLOUT) {
			P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
			queue_work(p9_mux_wq, &m->wq);
		} else
			clear_bit(Wworksched, &m->wsched);
	} else
		clear_bit(Wworksched, &m->wsched);

	return;

error:
	p9_conn_cancel(m, err);
	clear_bit(Wworksched, &m->wsched);
}

595
static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
596
{
597 598 599 600 601
	struct p9_poll_wait *pwait =
		container_of(wait, struct p9_poll_wait, wait);
	struct p9_conn *m = pwait->conn;
	unsigned long flags;
	DECLARE_WAITQUEUE(dummy_wait, p9_poll_task);
602

603 604 605 606
	spin_lock_irqsave(&p9_poll_lock, flags);
	if (list_empty(&m->poll_pending_link))
		list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
	spin_unlock_irqrestore(&p9_poll_lock, flags);
607

608 609
	/* perform the default wake up operation */
	return default_wake_function(&dummy_wait, mode, sync, key);
610 611 612
}

/**
613 614 615 616
 * p9_pollwait - add poll task to the wait queue
 * @filp: file pointer being polled
 * @wait_address: wait_q to block on
 * @p: poll state
617
 *
618
 * called by files poll operation to add v9fs-poll task to files wait queue
619
 */
620

621 622
static void
p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
623
{
624 625 626
	struct p9_conn *m = container_of(p, struct p9_conn, pt);
	struct p9_poll_wait *pwait = NULL;
	int i;
627

628 629 630 631
	for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
		if (m->poll_wait[i].wait_addr == NULL) {
			pwait = &m->poll_wait[i];
			break;
632 633 634
		}
	}

635 636
	if (!pwait) {
		P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
637 638 639
		return;
	}

640 641 642 643 644
	if (!wait_address) {
		P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
		pwait->wait_addr = ERR_PTR(-EIO);
		return;
	}
645

646 647 648 649 650
	pwait->conn = m;
	pwait->wait_addr = wait_address;
	init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
	add_wait_queue(wait_address, &pwait->wait);
}
651

652 653 654 655 656 657
/**
 * p9_conn_create - allocate and initialize the per-session mux data
 * @client: client instance
 *
 * Note: Creates the polling task if this is the first session.
 */
658

659 660 661 662
static struct p9_conn *p9_conn_create(struct p9_client *client)
{
	int i, n;
	struct p9_conn *m;
663

664 665 666 667
	P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize);
	m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
	if (!m)
		return ERR_PTR(-ENOMEM);
668

669 670
	INIT_LIST_HEAD(&m->mux_list);
	m->client = client;
671

672 673 674 675 676 677
	INIT_LIST_HEAD(&m->req_list);
	INIT_LIST_HEAD(&m->unsent_req_list);
	INIT_WORK(&m->rq, p9_read_work);
	INIT_WORK(&m->wq, p9_write_work);
	INIT_LIST_HEAD(&m->poll_pending_link);
	init_poll_funcptr(&m->pt, p9_pollwait);
678

679 680 681 682 683
	n = p9_fd_poll(client, &m->pt);
	if (n & POLLIN) {
		P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
		set_bit(Rpending, &m->wsched);
	}
684

685 686 687 688 689 690 691 692 693 694 695
	if (n & POLLOUT) {
		P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
		set_bit(Wpending, &m->wsched);
	}

	for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
		if (IS_ERR(m->poll_wait[i].wait_addr)) {
			p9_mux_poll_stop(m);
			kfree(m);
			/* return the error code */
			return (void *)m->poll_wait[i].wait_addr;
696 697 698
		}
	}

699 700
	return m;
}
701

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
/**
 * p9_poll_mux - polls a mux and schedules read or write works if necessary
 * @m: connection to poll
 *
 */

static void p9_poll_mux(struct p9_conn *m)
{
	int n;

	if (m->err < 0)
		return;

	n = p9_fd_poll(m->client, NULL);
	if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
		P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
		if (n >= 0)
			n = -ECONNRESET;
		p9_conn_cancel(m, n);
	}

	if (n & POLLIN) {
		set_bit(Rpending, &m->wsched);
		P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
		if (!test_and_set_bit(Rworksched, &m->wsched)) {
727 728
			P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
			queue_work(p9_mux_wq, &m->rq);
729 730
		}
	}
731

732 733 734 735 736 737 738 739 740
	if (n & POLLOUT) {
		set_bit(Wpending, &m->wsched);
		P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
		if ((m->wsize || !list_empty(&m->unsent_req_list))
		    && !test_and_set_bit(Wworksched, &m->wsched)) {
			P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
			queue_work(p9_mux_wq, &m->wq);
		}
	}
741 742 743 744 745 746 747 748 749 750 751
}

/**
 * p9_send_request - send 9P request
 * The function can sleep until the request is scheduled for sending.
 * The function can be interrupted. Return from the function is not
 * a guarantee that the request is sent successfully. Can return errors
 * that can be retrieved by PTR_ERR macros.
 *
 * @m: mux data
 * @tc: request to be sent
752
 *
753
 */
754

755
static struct p9_req_t *p9_send_request(struct p9_conn *m, struct p9_fcall *tc)
756
{
757
	int tag;
758
	int n;
759
	struct p9_req_t *req;
760 761 762 763 764 765

	P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
		tc, tc->id);
	if (m->err < 0)
		return ERR_PTR(m->err);

766
	tag = P9_NOTAG;
767
	if (tc->id != P9_TVERSION) {
768 769
		tag = p9_idpool_get(m->client->tagpool);
		if (tag < 0)
770
			return ERR_PTR(-ENOMEM);
Julia Lawall's avatar
Julia Lawall committed
771
	}
772

773 774 775
	p9_set_tag(tc, tag);

	req = p9_tag_alloc(m->client, tag);
776 777 778 779 780

#ifdef CONFIG_NET_9P_DEBUG
	if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
		char buf[150];

781
		p9_printfcall(buf, sizeof(buf), tc, m->client->dotu);
782 783 784 785
		printk(KERN_NOTICE "<<< %p %s\n", m, buf);
	}
#endif

786 787 788 789 790
	req->tag = tag;
	req->tc = tc;
	req->rc = NULL;
	req->t_err = 0;
	req->status = REQ_STATUS_UNSENT;
791

792
	spin_lock(&m->client->lock);
793
	list_add_tail(&req->req_list, &m->unsent_req_list);
794
	spin_unlock(&m->client->lock);
795 796 797 798

	if (test_and_clear_bit(Wpending, &m->wsched))
		n = POLLOUT;
	else
799
		n = p9_fd_poll(m->client, NULL);
800 801 802 803 804 805 806 807

	if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
		queue_work(p9_mux_wq, &m->wq);

	return req;
}

static int
808
p9_mux_flush_request(struct p9_conn *m, struct p9_req_t *req)
809 810
{
	struct p9_fcall *fc;
811
	struct p9_req_t *rreq, *rptr;
812 813 814 815

	P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);

	/* if a response was received for a request, do nothing */
816
	if (req->rc || req->t_err) {
817 818 819 820 821
		P9_DPRINTK(P9_DEBUG_MUX,
			"mux %p req %p response already received\n", m, req);
		return 0;
	}

822
	req->status = REQ_STATUS_FLSH;
823

824
	spin_lock(&m->client->lock);
825 826 827 828 829 830
	/* if the request is not sent yet, just remove it from the list */
	list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
		if (rreq->tag == req->tag) {
			P9_DPRINTK(P9_DEBUG_MUX,
			   "mux %p req %p request is not sent yet\n", m, req);
			list_del(&rreq->req_list);
831 832 833
			req->status = REQ_STATUS_FLSHD;
			spin_unlock(&m->client->lock);
			p9_conn_rpc_cb(m->client, req);
834 835 836
			return 0;
		}
	}
837
	spin_unlock(&m->client->lock);
838 839 840

	clear_thread_flag(TIF_SIGPENDING);
	fc = p9_create_tflush(req->tag);
841
	p9_send_request(m, fc);
842 843 844 845 846 847
	return 1;
}

/**
 * p9_fd_rpc- sends 9P request and waits until a response is available.
 *	The function can be interrupted.
848
 * @client: client instance
849 850
 * @tc: request to be sent
 * @rc: pointer where a pointer to the response is stored
851
 *
852
 */
853

854
int
855
p9_fd_rpc(struct p9_client *client, struct p9_fcall *tc, struct p9_fcall **rc)
856
{
857
	struct p9_trans_fd *p = client->trans;
858 859 860
	struct p9_conn *m = p->conn;
	int err, sigpending;
	unsigned long flags;
861
	struct p9_req_t *req;
862 863 864 865 866 867 868 869 870 871

	if (rc)
		*rc = NULL;

	sigpending = 0;
	if (signal_pending(current)) {
		sigpending = 1;
		clear_thread_flag(TIF_SIGPENDING);
	}

872
	req = p9_send_request(m, tc);
873 874 875 876 877 878
	if (IS_ERR(req)) {
		err = PTR_ERR(req);
		P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
		return err;
	}

879 880 881 882
	err = wait_event_interruptible(*req->wq, req->rc != NULL ||
								req->t_err < 0);
	if (req->t_err < 0)
		err = req->t_err;
883

884
	if (err == -ERESTARTSYS && client->status == Connected
885 886 887 888 889
							&& m->err == 0) {
		if (p9_mux_flush_request(m, req)) {
			/* wait until we get response of the flush message */
			do {
				clear_thread_flag(TIF_SIGPENDING);
890 891 892
				err = wait_event_interruptible(*req->wq,
					req->rc || req->t_err);
			} while (!req->rc && !req->t_err &&
893 894
					err == -ERESTARTSYS &&
					client->status == Connected && !m->err);
895 896 897 898 899 900 901 902 903 904 905 906 907

			err = -ERESTARTSYS;
		}
		sigpending = 1;
	}

	if (sigpending) {
		spin_lock_irqsave(&current->sighand->siglock, flags);
		recalc_sigpending();
		spin_unlock_irqrestore(&current->sighand->siglock, flags);
	}

	if (rc)
908
		*rc = req->rc;
909
	else
910
		kfree(req->rc);
911

912
	p9_free_req(client, req);
913 914 915 916 917 918
	if (err > 0)
		err = -EIO;

	return err;
}

919
/**
920
 * parse_options - parse mount options into session structure
921
 * @options: options string passed from mount
922
 * @opts: transport-specific structure to parse options into
923
 *
924
 * Returns 0 upon success, -ERRNO upon failure
925
 */
926

927
static int parse_opts(char *params, struct p9_fd_opts *opts)
928
{
929 930 931
	char *p;
	substring_t args[MAX_OPT_ARGS];
	int option;
932
	char *options;
933
	int ret;
Latchesar Ionkov's avatar