• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/fs/9p/trans_fd.c
3  *
4  * Fd transport layer.  Includes deprecated socket layer.
5  *
6  *  Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
7  *  Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
8  *  Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
9  *  Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 2
13  *  as published by the Free Software Foundation.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to:
22  *  Free Software Foundation
23  *  51 Franklin Street, Fifth Floor
24  *  Boston, MA  02111-1301  USA
25  *
26  */
27 
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 
30 #include <linux/in.h>
31 #include <linux/module.h>
32 #include <linux/net.h>
33 #include <linux/ipv6.h>
34 #include <linux/kthread.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/un.h>
38 #include <linux/uaccess.h>
39 #include <linux/inet.h>
40 #include <linux/idr.h>
41 #include <linux/file.h>
42 #include <linux/parser.h>
43 #include <linux/slab.h>
44 #include <linux/seq_file.h>
45 #include <net/9p/9p.h>
46 #include <net/9p/client.h>
47 #include <net/9p/transport.h>
48 
49 #include <linux/syscalls.h> /* killme */
50 
51 #define P9_PORT 564
52 #define MAX_SOCK_BUF (64*1024)
53 #define MAXPOLLWADDR	2
54 
55 static struct p9_trans_module p9_tcp_trans;
56 static struct p9_trans_module p9_fd_trans;
57 
58 /**
59  * struct p9_fd_opts - per-transport options
60  * @rfd: file descriptor for reading (trans=fd)
61  * @wfd: file descriptor for writing (trans=fd)
62  * @port: port to connect to (trans=tcp)
63  *
64  */
65 
66 struct p9_fd_opts {
67 	int rfd;
68 	int wfd;
69 	u16 port;
70 	bool privport;
71 };
72 
73 /*
74   * Option Parsing (code inspired by NFS code)
75   *  - a little lazy - parse all fd-transport options
76   */
77 
78 enum {
79 	/* Options that take integer arguments */
80 	Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
81 	/* Options that take no arguments */
82 	Opt_privport,
83 };
84 
85 static const match_table_t tokens = {
86 	{Opt_port, "port=%u"},
87 	{Opt_rfdno, "rfdno=%u"},
88 	{Opt_wfdno, "wfdno=%u"},
89 	{Opt_privport, "privport"},
90 	{Opt_err, NULL},
91 };
92 
93 enum {
94 	Rworksched = 1,		/* read work scheduled or running */
95 	Rpending = 2,		/* can read */
96 	Wworksched = 4,		/* write work scheduled or running */
97 	Wpending = 8,		/* can write */
98 };
99 
100 struct p9_poll_wait {
101 	struct p9_conn *conn;
102 	wait_queue_entry_t wait;
103 	wait_queue_head_t *wait_addr;
104 };
105 
106 /**
107  * struct p9_conn - fd mux connection state information
108  * @mux_list: list link for mux to manage multiple connections (?)
109  * @client: reference to client instance for this connection
110  * @err: error state
111  * @req_list: accounting for requests which have been sent
112  * @unsent_req_list: accounting for requests that haven't been sent
113  * @req: current request being processed (if any)
114  * @tmp_buf: temporary buffer to read in header
115  * @rc: temporary fcall for reading current frame
116  * @wpos: write position for current frame
117  * @wsize: amount of data to write for current frame
118  * @wbuf: current write buffer
119  * @poll_pending_link: pending links to be polled per conn
120  * @poll_wait: array of wait_q's for various worker threads
121  * @pt: poll state
122  * @rq: current read work
123  * @wq: current write work
124  * @wsched: ????
125  *
126  */
127 
128 struct p9_conn {
129 	struct list_head mux_list;
130 	struct p9_client *client;
131 	int err;
132 	struct list_head req_list;
133 	struct list_head unsent_req_list;
134 	struct p9_req_t *req;
135 	char tmp_buf[7];
136 	struct p9_fcall rc;
137 	int wpos;
138 	int wsize;
139 	char *wbuf;
140 	struct list_head poll_pending_link;
141 	struct p9_poll_wait poll_wait[MAXPOLLWADDR];
142 	poll_table pt;
143 	struct work_struct rq;
144 	struct work_struct wq;
145 	unsigned long wsched;
146 };
147 
148 /**
149  * struct p9_trans_fd - transport state
150  * @rd: reference to file to read from
151  * @wr: reference of file to write to
152  * @conn: connection state reference
153  *
154  */
155 
156 struct p9_trans_fd {
157 	struct file *rd;
158 	struct file *wr;
159 	struct p9_conn conn;
160 };
161 
162 static void p9_poll_workfn(struct work_struct *work);
163 
164 static DEFINE_SPINLOCK(p9_poll_lock);
165 static LIST_HEAD(p9_poll_pending_list);
166 static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
167 
168 static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
169 static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
170 
p9_mux_poll_stop(struct p9_conn * m)171 static void p9_mux_poll_stop(struct p9_conn *m)
172 {
173 	unsigned long flags;
174 	int i;
175 
176 	for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
177 		struct p9_poll_wait *pwait = &m->poll_wait[i];
178 
179 		if (pwait->wait_addr) {
180 			remove_wait_queue(pwait->wait_addr, &pwait->wait);
181 			pwait->wait_addr = NULL;
182 		}
183 	}
184 
185 	spin_lock_irqsave(&p9_poll_lock, flags);
186 	list_del_init(&m->poll_pending_link);
187 	spin_unlock_irqrestore(&p9_poll_lock, flags);
188 
189 	flush_work(&p9_poll_work);
190 }
191 
192 /**
193  * p9_conn_cancel - cancel all pending requests with error
194  * @m: mux data
195  * @err: error code
196  *
197  */
198 
p9_conn_cancel(struct p9_conn * m,int err)199 static void p9_conn_cancel(struct p9_conn *m, int err)
200 {
201 	struct p9_req_t *req, *rtmp;
202 	LIST_HEAD(cancel_list);
203 
204 	p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
205 
206 	spin_lock(&m->client->lock);
207 
208 	if (m->err) {
209 		spin_unlock(&m->client->lock);
210 		return;
211 	}
212 
213 	m->err = err;
214 
215 	list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
216 		list_move(&req->req_list, &cancel_list);
217 	}
218 	list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
219 		list_move(&req->req_list, &cancel_list);
220 	}
221 
222 	list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
223 		p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
224 		list_del(&req->req_list);
225 		if (!req->t_err)
226 			req->t_err = err;
227 		p9_client_cb(m->client, req, REQ_STATUS_ERROR);
228 	}
229 	spin_unlock(&m->client->lock);
230 }
231 
232 static int
p9_fd_poll(struct p9_client * client,struct poll_table_struct * pt)233 p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
234 {
235 	int ret, n;
236 	struct p9_trans_fd *ts = NULL;
237 
238 	if (client && client->status == Connected)
239 		ts = client->trans;
240 
241 	if (!ts)
242 		return -EREMOTEIO;
243 
244 	if (!ts->rd->f_op->poll)
245 		return -EIO;
246 
247 	if (!ts->wr->f_op->poll)
248 		return -EIO;
249 
250 	ret = ts->rd->f_op->poll(ts->rd, pt);
251 	if (ret < 0)
252 		return ret;
253 
254 	if (ts->rd != ts->wr) {
255 		n = ts->wr->f_op->poll(ts->wr, pt);
256 		if (n < 0)
257 			return n;
258 		ret = (ret & ~POLLOUT) | (n & ~POLLIN);
259 	}
260 
261 	return ret;
262 }
263 
264 /**
265  * p9_fd_read- read from a fd
266  * @client: client instance
267  * @v: buffer to receive data into
268  * @len: size of receive buffer
269  *
270  */
271 
p9_fd_read(struct p9_client * client,void * v,int len)272 static int p9_fd_read(struct p9_client *client, void *v, int len)
273 {
274 	int ret;
275 	struct p9_trans_fd *ts = NULL;
276 	loff_t pos;
277 
278 	if (client && client->status != Disconnected)
279 		ts = client->trans;
280 
281 	if (!ts)
282 		return -EREMOTEIO;
283 
284 	if (!(ts->rd->f_flags & O_NONBLOCK))
285 		p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
286 
287 	pos = ts->rd->f_pos;
288 	ret = kernel_read(ts->rd, v, len, &pos);
289 	if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
290 		client->status = Disconnected;
291 	return ret;
292 }
293 
294 /**
295  * p9_read_work - called when there is some data to be read from a transport
296  * @work: container of work to be done
297  *
298  */
299 
p9_read_work(struct work_struct * work)300 static void p9_read_work(struct work_struct *work)
301 {
302 	int n, err;
303 	struct p9_conn *m;
304 	int status = REQ_STATUS_ERROR;
305 
306 	m = container_of(work, struct p9_conn, rq);
307 
308 	if (m->err < 0)
309 		return;
310 
311 	p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset);
312 
313 	if (!m->rc.sdata) {
314 		m->rc.sdata = m->tmp_buf;
315 		m->rc.offset = 0;
316 		m->rc.capacity = 7; /* start by reading header */
317 	}
318 
319 	clear_bit(Rpending, &m->wsched);
320 	p9_debug(P9_DEBUG_TRANS, "read mux %p pos %zd size: %zd = %zd\n",
321 		 m, m->rc.offset, m->rc.capacity,
322 		 m->rc.capacity - m->rc.offset);
323 	err = p9_fd_read(m->client, m->rc.sdata + m->rc.offset,
324 			 m->rc.capacity - m->rc.offset);
325 	p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
326 	if (err == -EAGAIN)
327 		goto end_clear;
328 
329 	if (err <= 0)
330 		goto error;
331 
332 	m->rc.offset += err;
333 
334 	/* header read in */
335 	if ((!m->req) && (m->rc.offset == m->rc.capacity)) {
336 		p9_debug(P9_DEBUG_TRANS, "got new header\n");
337 
338 		err = p9_parse_header(&m->rc, NULL, NULL, NULL, 0);
339 		if (err) {
340 			p9_debug(P9_DEBUG_ERROR,
341 				 "error parsing header: %d\n", err);
342 			goto error;
343 		}
344 
345 		if (m->rc.size >= m->client->msize) {
346 			p9_debug(P9_DEBUG_ERROR,
347 				 "requested packet size too big: %d\n",
348 				 m->rc.size);
349 			err = -EIO;
350 			goto error;
351 		}
352 
353 		p9_debug(P9_DEBUG_TRANS,
354 			 "mux %p pkt: size: %d bytes tag: %d\n",
355 			 m, m->rc.size, m->rc.tag);
356 
357 		m->req = p9_tag_lookup(m->client, m->rc.tag);
358 		if (!m->req || (m->req->status != REQ_STATUS_SENT)) {
359 			p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
360 				 m->rc.tag);
361 			err = -EIO;
362 			goto error;
363 		}
364 
365 		if (m->req->rc == NULL) {
366 			p9_debug(P9_DEBUG_ERROR,
367 				 "No recv fcall for tag %d (req %p), disconnecting!\n",
368 				 m->rc.tag, m->req);
369 			m->req = NULL;
370 			err = -EIO;
371 			goto error;
372 		}
373 		m->rc.sdata = (char *)m->req->rc + sizeof(struct p9_fcall);
374 		memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity);
375 		m->rc.capacity = m->rc.size;
376 	}
377 
378 	/* packet is read in
379 	 * not an else because some packets (like clunk) have no payload
380 	 */
381 	if ((m->req) && (m->rc.offset == m->rc.capacity)) {
382 		p9_debug(P9_DEBUG_TRANS, "got new packet\n");
383 		spin_lock(&m->client->lock);
384 		if (m->req->status != REQ_STATUS_ERROR)
385 			status = REQ_STATUS_RCVD;
386 		list_del(&m->req->req_list);
387 		/* update req->status while holding client->lock  */
388 		p9_client_cb(m->client, m->req, status);
389 		spin_unlock(&m->client->lock);
390 		m->rc.sdata = NULL;
391 		m->rc.offset = 0;
392 		m->rc.capacity = 0;
393 		m->req = NULL;
394 	}
395 
396 end_clear:
397 	clear_bit(Rworksched, &m->wsched);
398 
399 	if (!list_empty(&m->req_list)) {
400 		if (test_and_clear_bit(Rpending, &m->wsched))
401 			n = POLLIN;
402 		else
403 			n = p9_fd_poll(m->client, NULL);
404 
405 		if ((n & POLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
406 			p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
407 			schedule_work(&m->rq);
408 		}
409 	}
410 
411 	return;
412 error:
413 	p9_conn_cancel(m, err);
414 	clear_bit(Rworksched, &m->wsched);
415 }
416 
417 /**
418  * p9_fd_write - write to a socket
419  * @client: client instance
420  * @v: buffer to send data from
421  * @len: size of send buffer
422  *
423  */
424 
p9_fd_write(struct p9_client * client,void * v,int len)425 static int p9_fd_write(struct p9_client *client, void *v, int len)
426 {
427 	ssize_t ret;
428 	struct p9_trans_fd *ts = NULL;
429 
430 	if (client && client->status != Disconnected)
431 		ts = client->trans;
432 
433 	if (!ts)
434 		return -EREMOTEIO;
435 
436 	if (!(ts->wr->f_flags & O_NONBLOCK))
437 		p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
438 
439 	ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos);
440 	if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
441 		client->status = Disconnected;
442 	return ret;
443 }
444 
445 /**
446  * p9_write_work - called when a transport can send some data
447  * @work: container for work to be done
448  *
449  */
450 
p9_write_work(struct work_struct * work)451 static void p9_write_work(struct work_struct *work)
452 {
453 	int n, err;
454 	struct p9_conn *m;
455 	struct p9_req_t *req;
456 
457 	m = container_of(work, struct p9_conn, wq);
458 
459 	if (m->err < 0) {
460 		clear_bit(Wworksched, &m->wsched);
461 		return;
462 	}
463 
464 	if (!m->wsize) {
465 		spin_lock(&m->client->lock);
466 		if (list_empty(&m->unsent_req_list)) {
467 			clear_bit(Wworksched, &m->wsched);
468 			spin_unlock(&m->client->lock);
469 			return;
470 		}
471 
472 		req = list_entry(m->unsent_req_list.next, struct p9_req_t,
473 			       req_list);
474 		req->status = REQ_STATUS_SENT;
475 		p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
476 		list_move_tail(&req->req_list, &m->req_list);
477 
478 		m->wbuf = req->tc->sdata;
479 		m->wsize = req->tc->size;
480 		m->wpos = 0;
481 		spin_unlock(&m->client->lock);
482 	}
483 
484 	p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
485 		 m, m->wpos, m->wsize);
486 	clear_bit(Wpending, &m->wsched);
487 	err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
488 	p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
489 	if (err == -EAGAIN)
490 		goto end_clear;
491 
492 
493 	if (err < 0)
494 		goto error;
495 	else if (err == 0) {
496 		err = -EREMOTEIO;
497 		goto error;
498 	}
499 
500 	m->wpos += err;
501 	if (m->wpos == m->wsize)
502 		m->wpos = m->wsize = 0;
503 
504 end_clear:
505 	clear_bit(Wworksched, &m->wsched);
506 
507 	if (m->wsize || !list_empty(&m->unsent_req_list)) {
508 		if (test_and_clear_bit(Wpending, &m->wsched))
509 			n = POLLOUT;
510 		else
511 			n = p9_fd_poll(m->client, NULL);
512 
513 		if ((n & POLLOUT) &&
514 		   !test_and_set_bit(Wworksched, &m->wsched)) {
515 			p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
516 			schedule_work(&m->wq);
517 		}
518 	}
519 
520 	return;
521 
522 error:
523 	p9_conn_cancel(m, err);
524 	clear_bit(Wworksched, &m->wsched);
525 }
526 
p9_pollwake(wait_queue_entry_t * wait,unsigned int mode,int sync,void * key)527 static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key)
528 {
529 	struct p9_poll_wait *pwait =
530 		container_of(wait, struct p9_poll_wait, wait);
531 	struct p9_conn *m = pwait->conn;
532 	unsigned long flags;
533 
534 	spin_lock_irqsave(&p9_poll_lock, flags);
535 	if (list_empty(&m->poll_pending_link))
536 		list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
537 	spin_unlock_irqrestore(&p9_poll_lock, flags);
538 
539 	schedule_work(&p9_poll_work);
540 	return 1;
541 }
542 
543 /**
544  * p9_pollwait - add poll task to the wait queue
545  * @filp: file pointer being polled
546  * @wait_address: wait_q to block on
547  * @p: poll state
548  *
549  * called by files poll operation to add v9fs-poll task to files wait queue
550  */
551 
552 static void
p9_pollwait(struct file * filp,wait_queue_head_t * wait_address,poll_table * p)553 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
554 {
555 	struct p9_conn *m = container_of(p, struct p9_conn, pt);
556 	struct p9_poll_wait *pwait = NULL;
557 	int i;
558 
559 	for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
560 		if (m->poll_wait[i].wait_addr == NULL) {
561 			pwait = &m->poll_wait[i];
562 			break;
563 		}
564 	}
565 
566 	if (!pwait) {
567 		p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
568 		return;
569 	}
570 
571 	pwait->conn = m;
572 	pwait->wait_addr = wait_address;
573 	init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
574 	add_wait_queue(wait_address, &pwait->wait);
575 }
576 
577 /**
578  * p9_conn_create - initialize the per-session mux data
579  * @client: client instance
580  *
581  * Note: Creates the polling task if this is the first session.
582  */
583 
p9_conn_create(struct p9_client * client)584 static void p9_conn_create(struct p9_client *client)
585 {
586 	int n;
587 	struct p9_trans_fd *ts = client->trans;
588 	struct p9_conn *m = &ts->conn;
589 
590 	p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
591 
592 	INIT_LIST_HEAD(&m->mux_list);
593 	m->client = client;
594 
595 	INIT_LIST_HEAD(&m->req_list);
596 	INIT_LIST_HEAD(&m->unsent_req_list);
597 	INIT_WORK(&m->rq, p9_read_work);
598 	INIT_WORK(&m->wq, p9_write_work);
599 	INIT_LIST_HEAD(&m->poll_pending_link);
600 	init_poll_funcptr(&m->pt, p9_pollwait);
601 
602 	n = p9_fd_poll(client, &m->pt);
603 	if (n & POLLIN) {
604 		p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
605 		set_bit(Rpending, &m->wsched);
606 	}
607 
608 	if (n & POLLOUT) {
609 		p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
610 		set_bit(Wpending, &m->wsched);
611 	}
612 }
613 
614 /**
615  * p9_poll_mux - polls a mux and schedules read or write works if necessary
616  * @m: connection to poll
617  *
618  */
619 
p9_poll_mux(struct p9_conn * m)620 static void p9_poll_mux(struct p9_conn *m)
621 {
622 	int n;
623 
624 	if (m->err < 0)
625 		return;
626 
627 	n = p9_fd_poll(m->client, NULL);
628 	if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
629 		p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
630 		if (n >= 0)
631 			n = -ECONNRESET;
632 		p9_conn_cancel(m, n);
633 	}
634 
635 	if (n & POLLIN) {
636 		set_bit(Rpending, &m->wsched);
637 		p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
638 		if (!test_and_set_bit(Rworksched, &m->wsched)) {
639 			p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
640 			schedule_work(&m->rq);
641 		}
642 	}
643 
644 	if (n & POLLOUT) {
645 		set_bit(Wpending, &m->wsched);
646 		p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
647 		if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
648 		    !test_and_set_bit(Wworksched, &m->wsched)) {
649 			p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
650 			schedule_work(&m->wq);
651 		}
652 	}
653 }
654 
655 /**
656  * p9_fd_request - send 9P request
657  * The function can sleep until the request is scheduled for sending.
658  * The function can be interrupted. Return from the function is not
659  * a guarantee that the request is sent successfully.
660  *
661  * @client: client instance
662  * @req: request to be sent
663  *
664  */
665 
p9_fd_request(struct p9_client * client,struct p9_req_t * req)666 static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
667 {
668 	int n;
669 	struct p9_trans_fd *ts = client->trans;
670 	struct p9_conn *m = &ts->conn;
671 
672 	p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
673 		 m, current, req->tc, req->tc->id);
674 	if (m->err < 0)
675 		return m->err;
676 
677 	spin_lock(&client->lock);
678 	req->status = REQ_STATUS_UNSENT;
679 	list_add_tail(&req->req_list, &m->unsent_req_list);
680 	spin_unlock(&client->lock);
681 
682 	if (test_and_clear_bit(Wpending, &m->wsched))
683 		n = POLLOUT;
684 	else
685 		n = p9_fd_poll(m->client, NULL);
686 
687 	if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
688 		schedule_work(&m->wq);
689 
690 	return 0;
691 }
692 
p9_fd_cancel(struct p9_client * client,struct p9_req_t * req)693 static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
694 {
695 	int ret = 1;
696 
697 	p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
698 
699 	spin_lock(&client->lock);
700 
701 	if (req->status == REQ_STATUS_UNSENT) {
702 		list_del(&req->req_list);
703 		req->status = REQ_STATUS_FLSHD;
704 		ret = 0;
705 	}
706 	spin_unlock(&client->lock);
707 
708 	return ret;
709 }
710 
p9_fd_cancelled(struct p9_client * client,struct p9_req_t * req)711 static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
712 {
713 	p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
714 
715 	/* we haven't received a response for oldreq,
716 	 * remove it from the list.
717 	 */
718 	spin_lock(&client->lock);
719 	list_del(&req->req_list);
720 	spin_unlock(&client->lock);
721 
722 	return 0;
723 }
724 
p9_fd_show_options(struct seq_file * m,struct p9_client * clnt)725 static int p9_fd_show_options(struct seq_file *m, struct p9_client *clnt)
726 {
727 	if (clnt->trans_mod == &p9_tcp_trans) {
728 		if (clnt->trans_opts.tcp.port != P9_PORT)
729 			seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port);
730 	} else if (clnt->trans_mod == &p9_fd_trans) {
731 		if (clnt->trans_opts.fd.rfd != ~0)
732 			seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd);
733 		if (clnt->trans_opts.fd.wfd != ~0)
734 			seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd);
735 	}
736 	return 0;
737 }
738 
739 /**
740  * parse_opts - parse mount options into p9_fd_opts structure
741  * @params: options string passed from mount
742  * @opts: fd transport-specific structure to parse options into
743  *
744  * Returns 0 upon success, -ERRNO upon failure
745  */
746 
parse_opts(char * params,struct p9_fd_opts * opts)747 static int parse_opts(char *params, struct p9_fd_opts *opts)
748 {
749 	char *p;
750 	substring_t args[MAX_OPT_ARGS];
751 	int option;
752 	char *options, *tmp_options;
753 
754 	opts->port = P9_PORT;
755 	opts->rfd = ~0;
756 	opts->wfd = ~0;
757 	opts->privport = false;
758 
759 	if (!params)
760 		return 0;
761 
762 	tmp_options = kstrdup(params, GFP_KERNEL);
763 	if (!tmp_options) {
764 		p9_debug(P9_DEBUG_ERROR,
765 			 "failed to allocate copy of option string\n");
766 		return -ENOMEM;
767 	}
768 	options = tmp_options;
769 
770 	while ((p = strsep(&options, ",")) != NULL) {
771 		int token;
772 		int r;
773 		if (!*p)
774 			continue;
775 		token = match_token(p, tokens, args);
776 		if ((token != Opt_err) && (token != Opt_privport)) {
777 			r = match_int(&args[0], &option);
778 			if (r < 0) {
779 				p9_debug(P9_DEBUG_ERROR,
780 					 "integer field, but no integer?\n");
781 				continue;
782 			}
783 		}
784 		switch (token) {
785 		case Opt_port:
786 			opts->port = option;
787 			break;
788 		case Opt_rfdno:
789 			opts->rfd = option;
790 			break;
791 		case Opt_wfdno:
792 			opts->wfd = option;
793 			break;
794 		case Opt_privport:
795 			opts->privport = true;
796 			break;
797 		default:
798 			continue;
799 		}
800 	}
801 
802 	kfree(tmp_options);
803 	return 0;
804 }
805 
p9_fd_open(struct p9_client * client,int rfd,int wfd)806 static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
807 {
808 	struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
809 					   GFP_KERNEL);
810 	if (!ts)
811 		return -ENOMEM;
812 
813 	ts->rd = fget(rfd);
814 	ts->wr = fget(wfd);
815 	if (!ts->rd || !ts->wr) {
816 		if (ts->rd)
817 			fput(ts->rd);
818 		if (ts->wr)
819 			fput(ts->wr);
820 		kfree(ts);
821 		return -EIO;
822 	}
823 
824 	client->trans = ts;
825 	client->status = Connected;
826 
827 	return 0;
828 }
829 
p9_socket_open(struct p9_client * client,struct socket * csocket)830 static int p9_socket_open(struct p9_client *client, struct socket *csocket)
831 {
832 	struct p9_trans_fd *p;
833 	struct file *file;
834 
835 	p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
836 	if (!p)
837 		return -ENOMEM;
838 
839 	csocket->sk->sk_allocation = GFP_NOIO;
840 	file = sock_alloc_file(csocket, 0, NULL);
841 	if (IS_ERR(file)) {
842 		pr_err("%s (%d): failed to map fd\n",
843 		       __func__, task_pid_nr(current));
844 		sock_release(csocket);
845 		kfree(p);
846 		return PTR_ERR(file);
847 	}
848 
849 	get_file(file);
850 	p->wr = p->rd = file;
851 	client->trans = p;
852 	client->status = Connected;
853 
854 	p->rd->f_flags |= O_NONBLOCK;
855 
856 	p9_conn_create(client);
857 	return 0;
858 }
859 
860 /**
861  * p9_mux_destroy - cancels all pending requests of mux
862  * @m: mux to destroy
863  *
864  */
865 
p9_conn_destroy(struct p9_conn * m)866 static void p9_conn_destroy(struct p9_conn *m)
867 {
868 	p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
869 		 m, m->mux_list.prev, m->mux_list.next);
870 
871 	p9_mux_poll_stop(m);
872 	cancel_work_sync(&m->rq);
873 	cancel_work_sync(&m->wq);
874 
875 	p9_conn_cancel(m, -ECONNRESET);
876 
877 	m->client = NULL;
878 }
879 
880 /**
881  * p9_fd_close - shutdown file descriptor transport
882  * @client: client instance
883  *
884  */
885 
p9_fd_close(struct p9_client * client)886 static void p9_fd_close(struct p9_client *client)
887 {
888 	struct p9_trans_fd *ts;
889 
890 	if (!client)
891 		return;
892 
893 	ts = client->trans;
894 	if (!ts)
895 		return;
896 
897 	client->status = Disconnected;
898 
899 	p9_conn_destroy(&ts->conn);
900 
901 	if (ts->rd)
902 		fput(ts->rd);
903 	if (ts->wr)
904 		fput(ts->wr);
905 
906 	kfree(ts);
907 }
908 
909 /*
910  * stolen from NFS - maybe should be made a generic function?
911  */
valid_ipaddr4(const char * buf)912 static inline int valid_ipaddr4(const char *buf)
913 {
914 	int rc, count, in[4];
915 
916 	rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
917 	if (rc != 4)
918 		return -EINVAL;
919 	for (count = 0; count < 4; count++) {
920 		if (in[count] > 255)
921 			return -EINVAL;
922 	}
923 	return 0;
924 }
925 
p9_bind_privport(struct socket * sock)926 static int p9_bind_privport(struct socket *sock)
927 {
928 	struct sockaddr_in cl;
929 	int port, err = -EINVAL;
930 
931 	memset(&cl, 0, sizeof(cl));
932 	cl.sin_family = AF_INET;
933 	cl.sin_addr.s_addr = INADDR_ANY;
934 	for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
935 		cl.sin_port = htons((ushort)port);
936 		err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
937 		if (err != -EADDRINUSE)
938 			break;
939 	}
940 	return err;
941 }
942 
943 
944 static int
p9_fd_create_tcp(struct p9_client * client,const char * addr,char * args)945 p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
946 {
947 	int err;
948 	struct socket *csocket;
949 	struct sockaddr_in sin_server;
950 	struct p9_fd_opts opts;
951 
952 	err = parse_opts(args, &opts);
953 	if (err < 0)
954 		return err;
955 
956 	if (addr == NULL || valid_ipaddr4(addr) < 0)
957 		return -EINVAL;
958 
959 	csocket = NULL;
960 
961 	client->trans_opts.tcp.port = opts.port;
962 	client->trans_opts.tcp.privport = opts.privport;
963 	sin_server.sin_family = AF_INET;
964 	sin_server.sin_addr.s_addr = in_aton(addr);
965 	sin_server.sin_port = htons(opts.port);
966 	err = __sock_create(current->nsproxy->net_ns, PF_INET,
967 			    SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
968 	if (err) {
969 		pr_err("%s (%d): problem creating socket\n",
970 		       __func__, task_pid_nr(current));
971 		return err;
972 	}
973 
974 	if (opts.privport) {
975 		err = p9_bind_privport(csocket);
976 		if (err < 0) {
977 			pr_err("%s (%d): problem binding to privport\n",
978 			       __func__, task_pid_nr(current));
979 			sock_release(csocket);
980 			return err;
981 		}
982 	}
983 
984 	err = csocket->ops->connect(csocket,
985 				    (struct sockaddr *)&sin_server,
986 				    sizeof(struct sockaddr_in), 0);
987 	if (err < 0) {
988 		pr_err("%s (%d): problem connecting socket to %s\n",
989 		       __func__, task_pid_nr(current), addr);
990 		sock_release(csocket);
991 		return err;
992 	}
993 
994 	return p9_socket_open(client, csocket);
995 }
996 
997 static int
p9_fd_create_unix(struct p9_client * client,const char * addr,char * args)998 p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
999 {
1000 	int err;
1001 	struct socket *csocket;
1002 	struct sockaddr_un sun_server;
1003 
1004 	csocket = NULL;
1005 
1006 	if (addr == NULL)
1007 		return -EINVAL;
1008 
1009 	if (strlen(addr) >= UNIX_PATH_MAX) {
1010 		pr_err("%s (%d): address too long: %s\n",
1011 		       __func__, task_pid_nr(current), addr);
1012 		return -ENAMETOOLONG;
1013 	}
1014 
1015 	sun_server.sun_family = PF_UNIX;
1016 	strcpy(sun_server.sun_path, addr);
1017 	err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
1018 			    SOCK_STREAM, 0, &csocket, 1);
1019 	if (err < 0) {
1020 		pr_err("%s (%d): problem creating socket\n",
1021 		       __func__, task_pid_nr(current));
1022 
1023 		return err;
1024 	}
1025 	err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1026 			sizeof(struct sockaddr_un) - 1, 0);
1027 	if (err < 0) {
1028 		pr_err("%s (%d): problem connecting socket: %s: %d\n",
1029 		       __func__, task_pid_nr(current), addr, err);
1030 		sock_release(csocket);
1031 		return err;
1032 	}
1033 
1034 	return p9_socket_open(client, csocket);
1035 }
1036 
1037 static int
p9_fd_create(struct p9_client * client,const char * addr,char * args)1038 p9_fd_create(struct p9_client *client, const char *addr, char *args)
1039 {
1040 	int err;
1041 	struct p9_fd_opts opts;
1042 
1043 	parse_opts(args, &opts);
1044 	client->trans_opts.fd.rfd = opts.rfd;
1045 	client->trans_opts.fd.wfd = opts.wfd;
1046 
1047 	if (opts.rfd == ~0 || opts.wfd == ~0) {
1048 		pr_err("Insufficient options for proto=fd\n");
1049 		return -ENOPROTOOPT;
1050 	}
1051 
1052 	err = p9_fd_open(client, opts.rfd, opts.wfd);
1053 	if (err < 0)
1054 		return err;
1055 
1056 	p9_conn_create(client);
1057 
1058 	return 0;
1059 }
1060 
1061 static struct p9_trans_module p9_tcp_trans = {
1062 	.name = "tcp",
1063 	.maxsize = MAX_SOCK_BUF,
1064 	.def = 0,
1065 	.create = p9_fd_create_tcp,
1066 	.close = p9_fd_close,
1067 	.request = p9_fd_request,
1068 	.cancel = p9_fd_cancel,
1069 	.cancelled = p9_fd_cancelled,
1070 	.show_options = p9_fd_show_options,
1071 	.owner = THIS_MODULE,
1072 };
1073 
1074 static struct p9_trans_module p9_unix_trans = {
1075 	.name = "unix",
1076 	.maxsize = MAX_SOCK_BUF,
1077 	.def = 0,
1078 	.create = p9_fd_create_unix,
1079 	.close = p9_fd_close,
1080 	.request = p9_fd_request,
1081 	.cancel = p9_fd_cancel,
1082 	.cancelled = p9_fd_cancelled,
1083 	.show_options = p9_fd_show_options,
1084 	.owner = THIS_MODULE,
1085 };
1086 
1087 static struct p9_trans_module p9_fd_trans = {
1088 	.name = "fd",
1089 	.maxsize = MAX_SOCK_BUF,
1090 	.def = 0,
1091 	.create = p9_fd_create,
1092 	.close = p9_fd_close,
1093 	.request = p9_fd_request,
1094 	.cancel = p9_fd_cancel,
1095 	.cancelled = p9_fd_cancelled,
1096 	.show_options = p9_fd_show_options,
1097 	.owner = THIS_MODULE,
1098 };
1099 
1100 /**
1101  * p9_poll_proc - poll worker thread
1102  * @a: thread state and arguments
1103  *
1104  * polls all v9fs transports for new events and queues the appropriate
1105  * work to the work queue
1106  *
1107  */
1108 
p9_poll_workfn(struct work_struct * work)1109 static void p9_poll_workfn(struct work_struct *work)
1110 {
1111 	unsigned long flags;
1112 
1113 	p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
1114 
1115 	spin_lock_irqsave(&p9_poll_lock, flags);
1116 	while (!list_empty(&p9_poll_pending_list)) {
1117 		struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1118 							struct p9_conn,
1119 							poll_pending_link);
1120 		list_del_init(&conn->poll_pending_link);
1121 		spin_unlock_irqrestore(&p9_poll_lock, flags);
1122 
1123 		p9_poll_mux(conn);
1124 
1125 		spin_lock_irqsave(&p9_poll_lock, flags);
1126 	}
1127 	spin_unlock_irqrestore(&p9_poll_lock, flags);
1128 
1129 	p9_debug(P9_DEBUG_TRANS, "finish\n");
1130 }
1131 
p9_trans_fd_init(void)1132 int p9_trans_fd_init(void)
1133 {
1134 	v9fs_register_trans(&p9_tcp_trans);
1135 	v9fs_register_trans(&p9_unix_trans);
1136 	v9fs_register_trans(&p9_fd_trans);
1137 
1138 	return 0;
1139 }
1140 
p9_trans_fd_exit(void)1141 void p9_trans_fd_exit(void)
1142 {
1143 	flush_work(&p9_poll_work);
1144 	v9fs_unregister_trans(&p9_tcp_trans);
1145 	v9fs_unregister_trans(&p9_unix_trans);
1146 	v9fs_unregister_trans(&p9_fd_trans);
1147 }
1148