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