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