Linux 5.7.7
[linux/fpc-iii.git] / net / 9p / trans_fd.c
blob13cd683a658ab67b38aa26bd0195f23113707e29
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/9p/trans_fd.c
5 * Fd transport layer. Includes deprecated socket layer.
7 * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
8 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
9 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
10 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/in.h>
16 #include <linux/module.h>
17 #include <linux/net.h>
18 #include <linux/ipv6.h>
19 #include <linux/kthread.h>
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/un.h>
23 #include <linux/uaccess.h>
24 #include <linux/inet.h>
25 #include <linux/idr.h>
26 #include <linux/file.h>
27 #include <linux/parser.h>
28 #include <linux/slab.h>
29 #include <linux/seq_file.h>
30 #include <net/9p/9p.h>
31 #include <net/9p/client.h>
32 #include <net/9p/transport.h>
34 #include <linux/syscalls.h> /* killme */
36 #define P9_PORT 564
37 #define MAX_SOCK_BUF (64*1024)
38 #define MAXPOLLWADDR 2
40 static struct p9_trans_module p9_tcp_trans;
41 static struct p9_trans_module p9_fd_trans;
43 /**
44 * struct p9_fd_opts - per-transport options
45 * @rfd: file descriptor for reading (trans=fd)
46 * @wfd: file descriptor for writing (trans=fd)
47 * @port: port to connect to (trans=tcp)
51 struct p9_fd_opts {
52 int rfd;
53 int wfd;
54 u16 port;
55 bool privport;
59 * Option Parsing (code inspired by NFS code)
60 * - a little lazy - parse all fd-transport options
63 enum {
64 /* Options that take integer arguments */
65 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
66 /* Options that take no arguments */
67 Opt_privport,
70 static const match_table_t tokens = {
71 {Opt_port, "port=%u"},
72 {Opt_rfdno, "rfdno=%u"},
73 {Opt_wfdno, "wfdno=%u"},
74 {Opt_privport, "privport"},
75 {Opt_err, NULL},
78 enum {
79 Rworksched = 1, /* read work scheduled or running */
80 Rpending = 2, /* can read */
81 Wworksched = 4, /* write work scheduled or running */
82 Wpending = 8, /* can write */
85 struct p9_poll_wait {
86 struct p9_conn *conn;
87 wait_queue_entry_t wait;
88 wait_queue_head_t *wait_addr;
91 /**
92 * struct p9_conn - fd mux connection state information
93 * @mux_list: list link for mux to manage multiple connections (?)
94 * @client: reference to client instance for this connection
95 * @err: error state
96 * @req_list: accounting for requests which have been sent
97 * @unsent_req_list: accounting for requests that haven't been sent
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: ????
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;
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
142 struct p9_trans_fd {
143 struct file *rd;
144 struct file *wr;
145 struct p9_conn conn;
148 static void p9_poll_workfn(struct work_struct *work);
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);
154 static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
155 static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
157 static void p9_mux_poll_stop(struct p9_conn *m)
159 unsigned long flags;
160 int i;
162 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
163 struct p9_poll_wait *pwait = &m->poll_wait[i];
165 if (pwait->wait_addr) {
166 remove_wait_queue(pwait->wait_addr, &pwait->wait);
167 pwait->wait_addr = NULL;
171 spin_lock_irqsave(&p9_poll_lock, flags);
172 list_del_init(&m->poll_pending_link);
173 spin_unlock_irqrestore(&p9_poll_lock, flags);
175 flush_work(&p9_poll_work);
179 * p9_conn_cancel - cancel all pending requests with error
180 * @m: mux data
181 * @err: error code
185 static void p9_conn_cancel(struct p9_conn *m, int err)
187 struct p9_req_t *req, *rtmp;
188 LIST_HEAD(cancel_list);
190 p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
192 spin_lock(&m->client->lock);
194 if (m->err) {
195 spin_unlock(&m->client->lock);
196 return;
199 m->err = err;
201 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
202 list_move(&req->req_list, &cancel_list);
204 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
205 list_move(&req->req_list, &cancel_list);
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);
215 spin_unlock(&m->client->lock);
218 static __poll_t
219 p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt, int *err)
221 __poll_t ret;
222 struct p9_trans_fd *ts = NULL;
224 if (client && client->status == Connected)
225 ts = client->trans;
227 if (!ts) {
228 if (err)
229 *err = -EREMOTEIO;
230 return EPOLLERR;
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;
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
247 static int p9_fd_read(struct p9_client *client, void *v, int len)
249 int ret;
250 struct p9_trans_fd *ts = NULL;
251 loff_t pos;
253 if (client && client->status != Disconnected)
254 ts = client->trans;
256 if (!ts)
257 return -EREMOTEIO;
259 if (!(ts->rd->f_flags & O_NONBLOCK))
260 p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
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;
270 * p9_read_work - called when there is some data to be read from a transport
271 * @work: container of work to be done
275 static void p9_read_work(struct work_struct *work)
277 __poll_t n;
278 int err;
279 struct p9_conn *m;
281 m = container_of(work, struct p9_conn, rq);
283 if (m->err < 0)
284 return;
286 p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset);
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 */
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;
304 if (err <= 0)
305 goto error;
307 m->rc.offset += err;
309 /* header read in */
310 if ((!m->rreq) && (m->rc.offset == m->rc.capacity)) {
311 p9_debug(P9_DEBUG_TRANS, "got new header\n");
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;
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;
330 p9_debug(P9_DEBUG_TRANS,
331 "mux %p pkt: size: %d bytes tag: %d\n",
332 m, m->rc.size, m->rc.tag);
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;
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 m->rreq = NULL;
347 err = -EIO;
348 goto error;
350 m->rc.sdata = m->rreq->rc.sdata;
351 memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity);
352 m->rc.capacity = m->rc.size;
355 /* packet is read in
356 * not an else because some packets (like clunk) have no payload
358 if ((m->rreq) && (m->rc.offset == m->rc.capacity)) {
359 p9_debug(P9_DEBUG_TRANS, "got new packet\n");
360 m->rreq->rc.size = m->rc.offset;
361 spin_lock(&m->client->lock);
362 if (m->rreq->status == REQ_STATUS_SENT) {
363 list_del(&m->rreq->req_list);
364 p9_client_cb(m->client, m->rreq, REQ_STATUS_RCVD);
365 } else {
366 spin_unlock(&m->client->lock);
367 p9_debug(P9_DEBUG_ERROR,
368 "Request tag %d errored out while we were reading the reply\n",
369 m->rc.tag);
370 err = -EIO;
371 goto error;
373 spin_unlock(&m->client->lock);
374 m->rc.sdata = NULL;
375 m->rc.offset = 0;
376 m->rc.capacity = 0;
377 p9_req_put(m->rreq);
378 m->rreq = NULL;
381 end_clear:
382 clear_bit(Rworksched, &m->wsched);
384 if (!list_empty(&m->req_list)) {
385 if (test_and_clear_bit(Rpending, &m->wsched))
386 n = EPOLLIN;
387 else
388 n = p9_fd_poll(m->client, NULL, NULL);
390 if ((n & EPOLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
391 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
392 schedule_work(&m->rq);
396 return;
397 error:
398 p9_conn_cancel(m, err);
399 clear_bit(Rworksched, &m->wsched);
403 * p9_fd_write - write to a socket
404 * @client: client instance
405 * @v: buffer to send data from
406 * @len: size of send buffer
410 static int p9_fd_write(struct p9_client *client, void *v, int len)
412 ssize_t ret;
413 struct p9_trans_fd *ts = NULL;
415 if (client && client->status != Disconnected)
416 ts = client->trans;
418 if (!ts)
419 return -EREMOTEIO;
421 if (!(ts->wr->f_flags & O_NONBLOCK))
422 p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
424 ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos);
425 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
426 client->status = Disconnected;
427 return ret;
431 * p9_write_work - called when a transport can send some data
432 * @work: container for work to be done
436 static void p9_write_work(struct work_struct *work)
438 __poll_t n;
439 int err;
440 struct p9_conn *m;
441 struct p9_req_t *req;
443 m = container_of(work, struct p9_conn, wq);
445 if (m->err < 0) {
446 clear_bit(Wworksched, &m->wsched);
447 return;
450 if (!m->wsize) {
451 spin_lock(&m->client->lock);
452 if (list_empty(&m->unsent_req_list)) {
453 clear_bit(Wworksched, &m->wsched);
454 spin_unlock(&m->client->lock);
455 return;
458 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
459 req_list);
460 req->status = REQ_STATUS_SENT;
461 p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
462 list_move_tail(&req->req_list, &m->req_list);
464 m->wbuf = req->tc.sdata;
465 m->wsize = req->tc.size;
466 m->wpos = 0;
467 p9_req_get(req);
468 m->wreq = req;
469 spin_unlock(&m->client->lock);
472 p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
473 m, m->wpos, m->wsize);
474 clear_bit(Wpending, &m->wsched);
475 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
476 p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
477 if (err == -EAGAIN)
478 goto end_clear;
481 if (err < 0)
482 goto error;
483 else if (err == 0) {
484 err = -EREMOTEIO;
485 goto error;
488 m->wpos += err;
489 if (m->wpos == m->wsize) {
490 m->wpos = m->wsize = 0;
491 p9_req_put(m->wreq);
492 m->wreq = NULL;
495 end_clear:
496 clear_bit(Wworksched, &m->wsched);
498 if (m->wsize || !list_empty(&m->unsent_req_list)) {
499 if (test_and_clear_bit(Wpending, &m->wsched))
500 n = EPOLLOUT;
501 else
502 n = p9_fd_poll(m->client, NULL, NULL);
504 if ((n & EPOLLOUT) &&
505 !test_and_set_bit(Wworksched, &m->wsched)) {
506 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
507 schedule_work(&m->wq);
511 return;
513 error:
514 p9_conn_cancel(m, err);
515 clear_bit(Wworksched, &m->wsched);
518 static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key)
520 struct p9_poll_wait *pwait =
521 container_of(wait, struct p9_poll_wait, wait);
522 struct p9_conn *m = pwait->conn;
523 unsigned long flags;
525 spin_lock_irqsave(&p9_poll_lock, flags);
526 if (list_empty(&m->poll_pending_link))
527 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
528 spin_unlock_irqrestore(&p9_poll_lock, flags);
530 schedule_work(&p9_poll_work);
531 return 1;
535 * p9_pollwait - add poll task to the wait queue
536 * @filp: file pointer being polled
537 * @wait_address: wait_q to block on
538 * @p: poll state
540 * called by files poll operation to add v9fs-poll task to files wait queue
543 static void
544 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
546 struct p9_conn *m = container_of(p, struct p9_conn, pt);
547 struct p9_poll_wait *pwait = NULL;
548 int i;
550 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
551 if (m->poll_wait[i].wait_addr == NULL) {
552 pwait = &m->poll_wait[i];
553 break;
557 if (!pwait) {
558 p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
559 return;
562 pwait->conn = m;
563 pwait->wait_addr = wait_address;
564 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
565 add_wait_queue(wait_address, &pwait->wait);
569 * p9_conn_create - initialize the per-session mux data
570 * @client: client instance
572 * Note: Creates the polling task if this is the first session.
575 static void p9_conn_create(struct p9_client *client)
577 __poll_t n;
578 struct p9_trans_fd *ts = client->trans;
579 struct p9_conn *m = &ts->conn;
581 p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
583 INIT_LIST_HEAD(&m->mux_list);
584 m->client = client;
586 INIT_LIST_HEAD(&m->req_list);
587 INIT_LIST_HEAD(&m->unsent_req_list);
588 INIT_WORK(&m->rq, p9_read_work);
589 INIT_WORK(&m->wq, p9_write_work);
590 INIT_LIST_HEAD(&m->poll_pending_link);
591 init_poll_funcptr(&m->pt, p9_pollwait);
593 n = p9_fd_poll(client, &m->pt, NULL);
594 if (n & EPOLLIN) {
595 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
596 set_bit(Rpending, &m->wsched);
599 if (n & EPOLLOUT) {
600 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
601 set_bit(Wpending, &m->wsched);
606 * p9_poll_mux - polls a mux and schedules read or write works if necessary
607 * @m: connection to poll
611 static void p9_poll_mux(struct p9_conn *m)
613 __poll_t n;
614 int err = -ECONNRESET;
616 if (m->err < 0)
617 return;
619 n = p9_fd_poll(m->client, NULL, &err);
620 if (n & (EPOLLERR | EPOLLHUP | EPOLLNVAL)) {
621 p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
622 p9_conn_cancel(m, err);
625 if (n & EPOLLIN) {
626 set_bit(Rpending, &m->wsched);
627 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
628 if (!test_and_set_bit(Rworksched, &m->wsched)) {
629 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
630 schedule_work(&m->rq);
634 if (n & EPOLLOUT) {
635 set_bit(Wpending, &m->wsched);
636 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
637 if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
638 !test_and_set_bit(Wworksched, &m->wsched)) {
639 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
640 schedule_work(&m->wq);
646 * p9_fd_request - send 9P request
647 * The function can sleep until the request is scheduled for sending.
648 * The function can be interrupted. Return from the function is not
649 * a guarantee that the request is sent successfully.
651 * @client: client instance
652 * @req: request to be sent
656 static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
658 __poll_t n;
659 struct p9_trans_fd *ts = client->trans;
660 struct p9_conn *m = &ts->conn;
662 p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
663 m, current, &req->tc, req->tc.id);
664 if (m->err < 0)
665 return m->err;
667 spin_lock(&client->lock);
668 req->status = REQ_STATUS_UNSENT;
669 list_add_tail(&req->req_list, &m->unsent_req_list);
670 spin_unlock(&client->lock);
672 if (test_and_clear_bit(Wpending, &m->wsched))
673 n = EPOLLOUT;
674 else
675 n = p9_fd_poll(m->client, NULL, NULL);
677 if (n & EPOLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
678 schedule_work(&m->wq);
680 return 0;
683 static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
685 int ret = 1;
687 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
689 spin_lock(&client->lock);
691 if (req->status == REQ_STATUS_UNSENT) {
692 list_del(&req->req_list);
693 req->status = REQ_STATUS_FLSHD;
694 p9_req_put(req);
695 ret = 0;
697 spin_unlock(&client->lock);
699 return ret;
702 static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
704 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
706 /* we haven't received a response for oldreq,
707 * remove it from the list.
709 spin_lock(&client->lock);
710 list_del(&req->req_list);
711 spin_unlock(&client->lock);
712 p9_req_put(req);
714 return 0;
717 static int p9_fd_show_options(struct seq_file *m, struct p9_client *clnt)
719 if (clnt->trans_mod == &p9_tcp_trans) {
720 if (clnt->trans_opts.tcp.port != P9_PORT)
721 seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port);
722 } else if (clnt->trans_mod == &p9_fd_trans) {
723 if (clnt->trans_opts.fd.rfd != ~0)
724 seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd);
725 if (clnt->trans_opts.fd.wfd != ~0)
726 seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd);
728 return 0;
732 * parse_opts - parse mount options into p9_fd_opts structure
733 * @params: options string passed from mount
734 * @opts: fd transport-specific structure to parse options into
736 * Returns 0 upon success, -ERRNO upon failure
739 static int parse_opts(char *params, struct p9_fd_opts *opts)
741 char *p;
742 substring_t args[MAX_OPT_ARGS];
743 int option;
744 char *options, *tmp_options;
746 opts->port = P9_PORT;
747 opts->rfd = ~0;
748 opts->wfd = ~0;
749 opts->privport = false;
751 if (!params)
752 return 0;
754 tmp_options = kstrdup(params, GFP_KERNEL);
755 if (!tmp_options) {
756 p9_debug(P9_DEBUG_ERROR,
757 "failed to allocate copy of option string\n");
758 return -ENOMEM;
760 options = tmp_options;
762 while ((p = strsep(&options, ",")) != NULL) {
763 int token;
764 int r;
765 if (!*p)
766 continue;
767 token = match_token(p, tokens, args);
768 if ((token != Opt_err) && (token != Opt_privport)) {
769 r = match_int(&args[0], &option);
770 if (r < 0) {
771 p9_debug(P9_DEBUG_ERROR,
772 "integer field, but no integer?\n");
773 continue;
776 switch (token) {
777 case Opt_port:
778 opts->port = option;
779 break;
780 case Opt_rfdno:
781 opts->rfd = option;
782 break;
783 case Opt_wfdno:
784 opts->wfd = option;
785 break;
786 case Opt_privport:
787 opts->privport = true;
788 break;
789 default:
790 continue;
794 kfree(tmp_options);
795 return 0;
798 static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
800 struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
801 GFP_KERNEL);
802 if (!ts)
803 return -ENOMEM;
805 ts->rd = fget(rfd);
806 ts->wr = fget(wfd);
807 if (!ts->rd || !ts->wr) {
808 if (ts->rd)
809 fput(ts->rd);
810 if (ts->wr)
811 fput(ts->wr);
812 kfree(ts);
813 return -EIO;
816 client->trans = ts;
817 client->status = Connected;
819 return 0;
822 static int p9_socket_open(struct p9_client *client, struct socket *csocket)
824 struct p9_trans_fd *p;
825 struct file *file;
827 p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
828 if (!p)
829 return -ENOMEM;
831 csocket->sk->sk_allocation = GFP_NOIO;
832 file = sock_alloc_file(csocket, 0, NULL);
833 if (IS_ERR(file)) {
834 pr_err("%s (%d): failed to map fd\n",
835 __func__, task_pid_nr(current));
836 kfree(p);
837 return PTR_ERR(file);
840 get_file(file);
841 p->wr = p->rd = file;
842 client->trans = p;
843 client->status = Connected;
845 p->rd->f_flags |= O_NONBLOCK;
847 p9_conn_create(client);
848 return 0;
852 * p9_mux_destroy - cancels all pending requests of mux
853 * @m: mux to destroy
857 static void p9_conn_destroy(struct p9_conn *m)
859 p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
860 m, m->mux_list.prev, m->mux_list.next);
862 p9_mux_poll_stop(m);
863 cancel_work_sync(&m->rq);
864 if (m->rreq) {
865 p9_req_put(m->rreq);
866 m->rreq = NULL;
868 cancel_work_sync(&m->wq);
869 if (m->wreq) {
870 p9_req_put(m->wreq);
871 m->wreq = NULL;
874 p9_conn_cancel(m, -ECONNRESET);
876 m->client = NULL;
880 * p9_fd_close - shutdown file descriptor transport
881 * @client: client instance
885 static void p9_fd_close(struct p9_client *client)
887 struct p9_trans_fd *ts;
889 if (!client)
890 return;
892 ts = client->trans;
893 if (!ts)
894 return;
896 client->status = Disconnected;
898 p9_conn_destroy(&ts->conn);
900 if (ts->rd)
901 fput(ts->rd);
902 if (ts->wr)
903 fput(ts->wr);
905 kfree(ts);
909 * stolen from NFS - maybe should be made a generic function?
911 static inline int valid_ipaddr4(const char *buf)
913 int rc, count, in[4];
915 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
916 if (rc != 4)
917 return -EINVAL;
918 for (count = 0; count < 4; count++) {
919 if (in[count] > 255)
920 return -EINVAL;
922 return 0;
925 static int p9_bind_privport(struct socket *sock)
927 struct sockaddr_in cl;
928 int port, err = -EINVAL;
930 memset(&cl, 0, sizeof(cl));
931 cl.sin_family = AF_INET;
932 cl.sin_addr.s_addr = INADDR_ANY;
933 for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
934 cl.sin_port = htons((ushort)port);
935 err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
936 if (err != -EADDRINUSE)
937 break;
939 return err;
943 static int
944 p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
946 int err;
947 struct socket *csocket;
948 struct sockaddr_in sin_server;
949 struct p9_fd_opts opts;
951 err = parse_opts(args, &opts);
952 if (err < 0)
953 return err;
955 if (addr == NULL || valid_ipaddr4(addr) < 0)
956 return -EINVAL;
958 csocket = NULL;
960 client->trans_opts.tcp.port = opts.port;
961 client->trans_opts.tcp.privport = opts.privport;
962 sin_server.sin_family = AF_INET;
963 sin_server.sin_addr.s_addr = in_aton(addr);
964 sin_server.sin_port = htons(opts.port);
965 err = __sock_create(current->nsproxy->net_ns, PF_INET,
966 SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
967 if (err) {
968 pr_err("%s (%d): problem creating socket\n",
969 __func__, task_pid_nr(current));
970 return err;
973 if (opts.privport) {
974 err = p9_bind_privport(csocket);
975 if (err < 0) {
976 pr_err("%s (%d): problem binding to privport\n",
977 __func__, task_pid_nr(current));
978 sock_release(csocket);
979 return err;
983 err = csocket->ops->connect(csocket,
984 (struct sockaddr *)&sin_server,
985 sizeof(struct sockaddr_in), 0);
986 if (err < 0) {
987 pr_err("%s (%d): problem connecting socket to %s\n",
988 __func__, task_pid_nr(current), addr);
989 sock_release(csocket);
990 return err;
993 return p9_socket_open(client, csocket);
996 static int
997 p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
999 int err;
1000 struct socket *csocket;
1001 struct sockaddr_un sun_server;
1003 csocket = NULL;
1005 if (addr == NULL)
1006 return -EINVAL;
1008 if (strlen(addr) >= UNIX_PATH_MAX) {
1009 pr_err("%s (%d): address too long: %s\n",
1010 __func__, task_pid_nr(current), addr);
1011 return -ENAMETOOLONG;
1014 sun_server.sun_family = PF_UNIX;
1015 strcpy(sun_server.sun_path, addr);
1016 err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
1017 SOCK_STREAM, 0, &csocket, 1);
1018 if (err < 0) {
1019 pr_err("%s (%d): problem creating socket\n",
1020 __func__, task_pid_nr(current));
1022 return err;
1024 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1025 sizeof(struct sockaddr_un) - 1, 0);
1026 if (err < 0) {
1027 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1028 __func__, task_pid_nr(current), addr, err);
1029 sock_release(csocket);
1030 return err;
1033 return p9_socket_open(client, csocket);
1036 static int
1037 p9_fd_create(struct p9_client *client, const char *addr, char *args)
1039 int err;
1040 struct p9_fd_opts opts;
1042 parse_opts(args, &opts);
1043 client->trans_opts.fd.rfd = opts.rfd;
1044 client->trans_opts.fd.wfd = opts.wfd;
1046 if (opts.rfd == ~0 || opts.wfd == ~0) {
1047 pr_err("Insufficient options for proto=fd\n");
1048 return -ENOPROTOOPT;
1051 err = p9_fd_open(client, opts.rfd, opts.wfd);
1052 if (err < 0)
1053 return err;
1055 p9_conn_create(client);
1057 return 0;
1060 static struct p9_trans_module p9_tcp_trans = {
1061 .name = "tcp",
1062 .maxsize = MAX_SOCK_BUF,
1063 .def = 0,
1064 .create = p9_fd_create_tcp,
1065 .close = p9_fd_close,
1066 .request = p9_fd_request,
1067 .cancel = p9_fd_cancel,
1068 .cancelled = p9_fd_cancelled,
1069 .show_options = p9_fd_show_options,
1070 .owner = THIS_MODULE,
1073 static struct p9_trans_module p9_unix_trans = {
1074 .name = "unix",
1075 .maxsize = MAX_SOCK_BUF,
1076 .def = 0,
1077 .create = p9_fd_create_unix,
1078 .close = p9_fd_close,
1079 .request = p9_fd_request,
1080 .cancel = p9_fd_cancel,
1081 .cancelled = p9_fd_cancelled,
1082 .show_options = p9_fd_show_options,
1083 .owner = THIS_MODULE,
1086 static struct p9_trans_module p9_fd_trans = {
1087 .name = "fd",
1088 .maxsize = MAX_SOCK_BUF,
1089 .def = 0,
1090 .create = p9_fd_create,
1091 .close = p9_fd_close,
1092 .request = p9_fd_request,
1093 .cancel = p9_fd_cancel,
1094 .cancelled = p9_fd_cancelled,
1095 .show_options = p9_fd_show_options,
1096 .owner = THIS_MODULE,
1100 * p9_poll_workfn - poll worker thread
1101 * @work: work queue
1103 * polls all v9fs transports for new events and queues the appropriate
1104 * work to the work queue
1108 static void p9_poll_workfn(struct work_struct *work)
1110 unsigned long flags;
1112 p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
1114 spin_lock_irqsave(&p9_poll_lock, flags);
1115 while (!list_empty(&p9_poll_pending_list)) {
1116 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1117 struct p9_conn,
1118 poll_pending_link);
1119 list_del_init(&conn->poll_pending_link);
1120 spin_unlock_irqrestore(&p9_poll_lock, flags);
1122 p9_poll_mux(conn);
1124 spin_lock_irqsave(&p9_poll_lock, flags);
1126 spin_unlock_irqrestore(&p9_poll_lock, flags);
1128 p9_debug(P9_DEBUG_TRANS, "finish\n");
1131 int p9_trans_fd_init(void)
1133 v9fs_register_trans(&p9_tcp_trans);
1134 v9fs_register_trans(&p9_unix_trans);
1135 v9fs_register_trans(&p9_fd_trans);
1137 return 0;
1140 void p9_trans_fd_exit(void)
1142 flush_work(&p9_poll_work);
1143 v9fs_unregister_trans(&p9_tcp_trans);
1144 v9fs_unregister_trans(&p9_unix_trans);
1145 v9fs_unregister_trans(&p9_fd_trans);