1 // SPDX-License-Identifier: GPL-2.0-only
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
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>
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 */
37 #define MAX_SOCK_BUF (1024*1024)
38 #define MAXPOLLWADDR 2
40 static struct p9_trans_module p9_tcp_trans;
41 static struct p9_trans_module p9_fd_trans;
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)
48 * @privport: port is privileged
59 * Option Parsing (code inspired by NFS code)
60 * - a little lazy - parse all fd-transport options
64 /* Options that take integer arguments */
65 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
66 /* Options that take no arguments */
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"},
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 */
87 wait_queue_entry_t wait;
88 wait_queue_head_t *wait_addr;
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
96 * @req_lock: lock protecting req_list and requests statuses
97 * @req_list: accounting for requests which have been sent
98 * @unsent_req_list: accounting for requests that haven't been sent
100 * @wreq: write request
101 * @req: current request being processed (if any)
102 * @tmp_buf: temporary buffer to read in header
103 * @rc: temporary fcall for reading current frame
104 * @wpos: write position for current frame
105 * @wsize: amount of data to write for current frame
106 * @wbuf: current write buffer
107 * @poll_pending_link: pending links to be polled per conn
108 * @poll_wait: array of wait_q's for various worker threads
110 * @rq: current read work
111 * @wq: current write work
117 struct list_head mux_list;
118 struct p9_client *client;
121 struct list_head req_list;
122 struct list_head unsent_req_list;
123 struct p9_req_t *rreq;
124 struct p9_req_t *wreq;
125 char tmp_buf[P9_HDRSZ];
130 struct list_head poll_pending_link;
131 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
133 struct work_struct rq;
134 struct work_struct wq;
135 unsigned long wsched;
139 * struct p9_trans_fd - transport state
140 * @rd: reference to file to read from
141 * @wr: reference of file to write to
142 * @conn: connection state reference
152 static void p9_poll_workfn(struct work_struct *work);
154 static DEFINE_SPINLOCK(p9_poll_lock);
155 static LIST_HEAD(p9_poll_pending_list);
156 static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
158 static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
159 static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
161 static void p9_mux_poll_stop(struct p9_conn *m)
166 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
167 struct p9_poll_wait *pwait = &m->poll_wait[i];
169 if (pwait->wait_addr) {
170 remove_wait_queue(pwait->wait_addr, &pwait->wait);
171 pwait->wait_addr = NULL;
175 spin_lock_irqsave(&p9_poll_lock, flags);
176 list_del_init(&m->poll_pending_link);
177 spin_unlock_irqrestore(&p9_poll_lock, flags);
179 flush_work(&p9_poll_work);
183 * p9_conn_cancel - cancel all pending requests with error
189 static void p9_conn_cancel(struct p9_conn *m, int err)
191 struct p9_req_t *req, *rtmp;
192 LIST_HEAD(cancel_list);
194 p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
196 spin_lock(&m->req_lock);
199 spin_unlock(&m->req_lock);
205 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
206 list_move(&req->req_list, &cancel_list);
207 req->status = REQ_STATUS_ERROR;
209 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
210 list_move(&req->req_list, &cancel_list);
211 req->status = REQ_STATUS_ERROR;
214 spin_unlock(&m->req_lock);
216 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
217 p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
218 list_del(&req->req_list);
221 p9_client_cb(m->client, req, REQ_STATUS_ERROR);
226 p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt, int *err)
229 struct p9_trans_fd *ts = NULL;
231 if (client && client->status == Connected)
240 ret = vfs_poll(ts->rd, pt);
241 if (ts->rd != ts->wr)
242 ret = (ret & ~EPOLLOUT) | (vfs_poll(ts->wr, pt) & ~EPOLLIN);
247 * p9_fd_read- read from a fd
248 * @client: client instance
249 * @v: buffer to receive data into
250 * @len: size of receive buffer
254 static int p9_fd_read(struct p9_client *client, void *v, int len)
257 struct p9_trans_fd *ts = NULL;
260 if (client && client->status != Disconnected)
266 if (!(ts->rd->f_flags & O_NONBLOCK))
267 p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
270 ret = kernel_read(ts->rd, v, len, &pos);
271 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
272 client->status = Disconnected;
277 * p9_read_work - called when there is some data to be read from a transport
278 * @work: container of work to be done
282 static void p9_read_work(struct work_struct *work)
288 m = container_of(work, struct p9_conn, rq);
293 p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset);
296 m->rc.sdata = m->tmp_buf;
298 m->rc.capacity = P9_HDRSZ; /* start by reading header */
301 clear_bit(Rpending, &m->wsched);
302 p9_debug(P9_DEBUG_TRANS, "read mux %p pos %zd size: %zd = %zd\n",
303 m, m->rc.offset, m->rc.capacity,
304 m->rc.capacity - m->rc.offset);
305 err = p9_fd_read(m->client, m->rc.sdata + m->rc.offset,
306 m->rc.capacity - m->rc.offset);
307 p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
317 if ((!m->rreq) && (m->rc.offset == m->rc.capacity)) {
318 p9_debug(P9_DEBUG_TRANS, "got new header\n");
321 m->rc.size = P9_HDRSZ;
322 err = p9_parse_header(&m->rc, &m->rc.size, NULL, NULL, 0);
324 p9_debug(P9_DEBUG_ERROR,
325 "error parsing header: %d\n", err);
329 if (m->rc.size >= m->client->msize) {
330 p9_debug(P9_DEBUG_ERROR,
331 "requested packet size too big: %d\n",
337 p9_debug(P9_DEBUG_TRANS,
338 "mux %p pkt: size: %d bytes tag: %d\n",
339 m, m->rc.size, m->rc.tag);
341 m->rreq = p9_tag_lookup(m->client, m->rc.tag);
342 if (!m->rreq || (m->rreq->status != REQ_STATUS_SENT)) {
343 p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
349 if (!m->rreq->rc.sdata) {
350 p9_debug(P9_DEBUG_ERROR,
351 "No recv fcall for tag %d (req %p), disconnecting!\n",
353 p9_req_put(m->client, m->rreq);
358 m->rc.sdata = m->rreq->rc.sdata;
359 memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity);
360 m->rc.capacity = m->rc.size;
364 * not an else because some packets (like clunk) have no payload
366 if ((m->rreq) && (m->rc.offset == m->rc.capacity)) {
367 p9_debug(P9_DEBUG_TRANS, "got new packet\n");
368 m->rreq->rc.size = m->rc.offset;
369 spin_lock(&m->req_lock);
370 if (m->rreq->status == REQ_STATUS_SENT) {
371 list_del(&m->rreq->req_list);
372 p9_client_cb(m->client, m->rreq, REQ_STATUS_RCVD);
373 } else if (m->rreq->status == REQ_STATUS_FLSHD) {
374 /* Ignore replies associated with a cancelled request. */
375 p9_debug(P9_DEBUG_TRANS,
376 "Ignore replies associated with a cancelled request\n");
378 spin_unlock(&m->req_lock);
379 p9_debug(P9_DEBUG_ERROR,
380 "Request tag %d errored out while we were reading the reply\n",
385 spin_unlock(&m->req_lock);
389 p9_req_put(m->client, m->rreq);
394 clear_bit(Rworksched, &m->wsched);
396 if (!list_empty(&m->req_list)) {
397 if (test_and_clear_bit(Rpending, &m->wsched))
400 n = p9_fd_poll(m->client, NULL, NULL);
402 if ((n & EPOLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
403 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
404 schedule_work(&m->rq);
410 p9_conn_cancel(m, err);
411 clear_bit(Rworksched, &m->wsched);
415 * p9_fd_write - write to a socket
416 * @client: client instance
417 * @v: buffer to send data from
418 * @len: size of send buffer
422 static int p9_fd_write(struct p9_client *client, void *v, int len)
425 struct p9_trans_fd *ts = NULL;
427 if (client && client->status != Disconnected)
433 if (!(ts->wr->f_flags & O_NONBLOCK))
434 p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
436 ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos);
437 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
438 client->status = Disconnected;
443 * p9_write_work - called when a transport can send some data
444 * @work: container for work to be done
448 static void p9_write_work(struct work_struct *work)
453 struct p9_req_t *req;
455 m = container_of(work, struct p9_conn, wq);
458 clear_bit(Wworksched, &m->wsched);
463 spin_lock(&m->req_lock);
464 if (list_empty(&m->unsent_req_list)) {
465 clear_bit(Wworksched, &m->wsched);
466 spin_unlock(&m->req_lock);
470 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
472 req->status = REQ_STATUS_SENT;
473 p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
474 list_move_tail(&req->req_list, &m->req_list);
476 m->wbuf = req->tc.sdata;
477 m->wsize = req->tc.size;
481 spin_unlock(&m->req_lock);
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);
501 if (m->wpos == m->wsize) {
502 m->wpos = m->wsize = 0;
503 p9_req_put(m->client, m->wreq);
508 clear_bit(Wworksched, &m->wsched);
510 if (m->wsize || !list_empty(&m->unsent_req_list)) {
511 if (test_and_clear_bit(Wpending, &m->wsched))
514 n = p9_fd_poll(m->client, NULL, NULL);
516 if ((n & EPOLLOUT) &&
517 !test_and_set_bit(Wworksched, &m->wsched)) {
518 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
519 schedule_work(&m->wq);
526 p9_conn_cancel(m, err);
527 clear_bit(Wworksched, &m->wsched);
530 static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key)
532 struct p9_poll_wait *pwait =
533 container_of(wait, struct p9_poll_wait, wait);
534 struct p9_conn *m = pwait->conn;
537 spin_lock_irqsave(&p9_poll_lock, flags);
538 if (list_empty(&m->poll_pending_link))
539 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
540 spin_unlock_irqrestore(&p9_poll_lock, flags);
542 schedule_work(&p9_poll_work);
547 * p9_pollwait - add poll task to the wait queue
548 * @filp: file pointer being polled
549 * @wait_address: wait_q to block on
552 * called by files poll operation to add v9fs-poll task to files wait queue
556 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
558 struct p9_conn *m = container_of(p, struct p9_conn, pt);
559 struct p9_poll_wait *pwait = NULL;
562 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
563 if (m->poll_wait[i].wait_addr == NULL) {
564 pwait = &m->poll_wait[i];
570 p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
575 pwait->wait_addr = wait_address;
576 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
577 add_wait_queue(wait_address, &pwait->wait);
581 * p9_conn_create - initialize the per-session mux data
582 * @client: client instance
584 * Note: Creates the polling task if this is the first session.
587 static void p9_conn_create(struct p9_client *client)
590 struct p9_trans_fd *ts = client->trans;
591 struct p9_conn *m = &ts->conn;
593 p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
595 INIT_LIST_HEAD(&m->mux_list);
598 spin_lock_init(&m->req_lock);
599 INIT_LIST_HEAD(&m->req_list);
600 INIT_LIST_HEAD(&m->unsent_req_list);
601 INIT_WORK(&m->rq, p9_read_work);
602 INIT_WORK(&m->wq, p9_write_work);
603 INIT_LIST_HEAD(&m->poll_pending_link);
604 init_poll_funcptr(&m->pt, p9_pollwait);
606 n = p9_fd_poll(client, &m->pt, NULL);
608 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
609 set_bit(Rpending, &m->wsched);
613 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
614 set_bit(Wpending, &m->wsched);
619 * p9_poll_mux - polls a mux and schedules read or write works if necessary
620 * @m: connection to poll
624 static void p9_poll_mux(struct p9_conn *m)
627 int err = -ECONNRESET;
632 n = p9_fd_poll(m->client, NULL, &err);
633 if (n & (EPOLLERR | EPOLLHUP | EPOLLNVAL)) {
634 p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
635 p9_conn_cancel(m, err);
639 set_bit(Rpending, &m->wsched);
640 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
641 if (!test_and_set_bit(Rworksched, &m->wsched)) {
642 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
643 schedule_work(&m->rq);
648 set_bit(Wpending, &m->wsched);
649 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
650 if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
651 !test_and_set_bit(Wworksched, &m->wsched)) {
652 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
653 schedule_work(&m->wq);
659 * p9_fd_request - send 9P request
660 * The function can sleep until the request is scheduled for sending.
661 * The function can be interrupted. Return from the function is not
662 * a guarantee that the request is sent successfully.
664 * @client: client instance
665 * @req: request to be sent
669 static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
672 struct p9_trans_fd *ts = client->trans;
673 struct p9_conn *m = &ts->conn;
675 p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
676 m, current, &req->tc, req->tc.id);
680 spin_lock(&m->req_lock);
681 req->status = REQ_STATUS_UNSENT;
682 list_add_tail(&req->req_list, &m->unsent_req_list);
683 spin_unlock(&m->req_lock);
685 if (test_and_clear_bit(Wpending, &m->wsched))
688 n = p9_fd_poll(m->client, NULL, NULL);
690 if (n & EPOLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
691 schedule_work(&m->wq);
696 static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
698 struct p9_trans_fd *ts = client->trans;
699 struct p9_conn *m = &ts->conn;
702 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
704 spin_lock(&m->req_lock);
706 if (req->status == REQ_STATUS_UNSENT) {
707 list_del(&req->req_list);
708 req->status = REQ_STATUS_FLSHD;
709 p9_req_put(client, req);
712 spin_unlock(&m->req_lock);
717 static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
719 struct p9_trans_fd *ts = client->trans;
720 struct p9_conn *m = &ts->conn;
722 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
724 spin_lock(&m->req_lock);
725 /* Ignore cancelled request if message has been received
728 if (req->status == REQ_STATUS_RCVD) {
729 spin_unlock(&m->req_lock);
733 /* we haven't received a response for oldreq,
734 * remove it from the list.
736 list_del(&req->req_list);
737 req->status = REQ_STATUS_FLSHD;
738 spin_unlock(&m->req_lock);
740 p9_req_put(client, req);
745 static int p9_fd_show_options(struct seq_file *m, struct p9_client *clnt)
747 if (clnt->trans_mod == &p9_tcp_trans) {
748 if (clnt->trans_opts.tcp.port != P9_PORT)
749 seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port);
750 } else if (clnt->trans_mod == &p9_fd_trans) {
751 if (clnt->trans_opts.fd.rfd != ~0)
752 seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd);
753 if (clnt->trans_opts.fd.wfd != ~0)
754 seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd);
760 * parse_opts - parse mount options into p9_fd_opts structure
761 * @params: options string passed from mount
762 * @opts: fd transport-specific structure to parse options into
764 * Returns 0 upon success, -ERRNO upon failure
767 static int parse_opts(char *params, struct p9_fd_opts *opts)
770 substring_t args[MAX_OPT_ARGS];
772 char *options, *tmp_options;
774 opts->port = P9_PORT;
777 opts->privport = false;
782 tmp_options = kstrdup(params, GFP_KERNEL);
784 p9_debug(P9_DEBUG_ERROR,
785 "failed to allocate copy of option string\n");
788 options = tmp_options;
790 while ((p = strsep(&options, ",")) != NULL) {
795 token = match_token(p, tokens, args);
796 if ((token != Opt_err) && (token != Opt_privport)) {
797 r = match_int(&args[0], &option);
799 p9_debug(P9_DEBUG_ERROR,
800 "integer field, but no integer?\n");
815 opts->privport = true;
826 static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
828 struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
836 if (!(ts->rd->f_mode & FMODE_READ))
838 /* prevent workers from hanging on IO when fd is a pipe */
839 ts->rd->f_flags |= O_NONBLOCK;
843 if (!(ts->wr->f_mode & FMODE_WRITE))
845 ts->wr->f_flags |= O_NONBLOCK;
848 client->status = Connected;
861 static int p9_socket_open(struct p9_client *client, struct socket *csocket)
863 struct p9_trans_fd *p;
866 p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
868 sock_release(csocket);
872 csocket->sk->sk_allocation = GFP_NOIO;
873 file = sock_alloc_file(csocket, 0, NULL);
875 pr_err("%s (%d): failed to map fd\n",
876 __func__, task_pid_nr(current));
878 return PTR_ERR(file);
882 p->wr = p->rd = file;
884 client->status = Connected;
886 p->rd->f_flags |= O_NONBLOCK;
888 p9_conn_create(client);
893 * p9_conn_destroy - cancels all pending requests of mux
898 static void p9_conn_destroy(struct p9_conn *m)
900 p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
901 m, m->mux_list.prev, m->mux_list.next);
904 cancel_work_sync(&m->rq);
906 p9_req_put(m->client, m->rreq);
909 cancel_work_sync(&m->wq);
911 p9_req_put(m->client, m->wreq);
915 p9_conn_cancel(m, -ECONNRESET);
921 * p9_fd_close - shutdown file descriptor transport
922 * @client: client instance
926 static void p9_fd_close(struct p9_client *client)
928 struct p9_trans_fd *ts;
937 client->status = Disconnected;
939 p9_conn_destroy(&ts->conn);
950 * stolen from NFS - maybe should be made a generic function?
952 static inline int valid_ipaddr4(const char *buf)
954 int rc, count, in[4];
956 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
959 for (count = 0; count < 4; count++) {
966 static int p9_bind_privport(struct socket *sock)
968 struct sockaddr_in cl;
969 int port, err = -EINVAL;
971 memset(&cl, 0, sizeof(cl));
972 cl.sin_family = AF_INET;
973 cl.sin_addr.s_addr = htonl(INADDR_ANY);
974 for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
975 cl.sin_port = htons((ushort)port);
976 err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
977 if (err != -EADDRINUSE)
985 p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
988 struct socket *csocket;
989 struct sockaddr_in sin_server;
990 struct p9_fd_opts opts;
992 err = parse_opts(args, &opts);
996 if (addr == NULL || valid_ipaddr4(addr) < 0)
1001 client->trans_opts.tcp.port = opts.port;
1002 client->trans_opts.tcp.privport = opts.privport;
1003 sin_server.sin_family = AF_INET;
1004 sin_server.sin_addr.s_addr = in_aton(addr);
1005 sin_server.sin_port = htons(opts.port);
1006 err = __sock_create(current->nsproxy->net_ns, PF_INET,
1007 SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
1009 pr_err("%s (%d): problem creating socket\n",
1010 __func__, task_pid_nr(current));
1014 if (opts.privport) {
1015 err = p9_bind_privport(csocket);
1017 pr_err("%s (%d): problem binding to privport\n",
1018 __func__, task_pid_nr(current));
1019 sock_release(csocket);
1024 err = csocket->ops->connect(csocket,
1025 (struct sockaddr *)&sin_server,
1026 sizeof(struct sockaddr_in), 0);
1028 pr_err("%s (%d): problem connecting socket to %s\n",
1029 __func__, task_pid_nr(current), addr);
1030 sock_release(csocket);
1034 return p9_socket_open(client, csocket);
1038 p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
1041 struct socket *csocket;
1042 struct sockaddr_un sun_server;
1046 if (!addr || !strlen(addr))
1049 if (strlen(addr) >= UNIX_PATH_MAX) {
1050 pr_err("%s (%d): address too long: %s\n",
1051 __func__, task_pid_nr(current), addr);
1052 return -ENAMETOOLONG;
1055 sun_server.sun_family = PF_UNIX;
1056 strcpy(sun_server.sun_path, addr);
1057 err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
1058 SOCK_STREAM, 0, &csocket, 1);
1060 pr_err("%s (%d): problem creating socket\n",
1061 __func__, task_pid_nr(current));
1065 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1066 sizeof(struct sockaddr_un) - 1, 0);
1068 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1069 __func__, task_pid_nr(current), addr, err);
1070 sock_release(csocket);
1074 return p9_socket_open(client, csocket);
1078 p9_fd_create(struct p9_client *client, const char *addr, char *args)
1081 struct p9_fd_opts opts;
1083 parse_opts(args, &opts);
1084 client->trans_opts.fd.rfd = opts.rfd;
1085 client->trans_opts.fd.wfd = opts.wfd;
1087 if (opts.rfd == ~0 || opts.wfd == ~0) {
1088 pr_err("Insufficient options for proto=fd\n");
1089 return -ENOPROTOOPT;
1092 err = p9_fd_open(client, opts.rfd, opts.wfd);
1096 p9_conn_create(client);
1101 static struct p9_trans_module p9_tcp_trans = {
1103 .maxsize = MAX_SOCK_BUF,
1105 .create = p9_fd_create_tcp,
1106 .close = p9_fd_close,
1107 .request = p9_fd_request,
1108 .cancel = p9_fd_cancel,
1109 .cancelled = p9_fd_cancelled,
1110 .show_options = p9_fd_show_options,
1111 .owner = THIS_MODULE,
1114 static struct p9_trans_module p9_unix_trans = {
1116 .maxsize = MAX_SOCK_BUF,
1118 .create = p9_fd_create_unix,
1119 .close = p9_fd_close,
1120 .request = p9_fd_request,
1121 .cancel = p9_fd_cancel,
1122 .cancelled = p9_fd_cancelled,
1123 .show_options = p9_fd_show_options,
1124 .owner = THIS_MODULE,
1127 static struct p9_trans_module p9_fd_trans = {
1129 .maxsize = MAX_SOCK_BUF,
1131 .create = p9_fd_create,
1132 .close = p9_fd_close,
1133 .request = p9_fd_request,
1134 .cancel = p9_fd_cancel,
1135 .cancelled = p9_fd_cancelled,
1136 .show_options = p9_fd_show_options,
1137 .owner = THIS_MODULE,
1141 * p9_poll_workfn - poll worker thread
1144 * polls all v9fs transports for new events and queues the appropriate
1145 * work to the work queue
1149 static void p9_poll_workfn(struct work_struct *work)
1151 unsigned long flags;
1153 p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
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,
1160 list_del_init(&conn->poll_pending_link);
1161 spin_unlock_irqrestore(&p9_poll_lock, flags);
1165 spin_lock_irqsave(&p9_poll_lock, flags);
1167 spin_unlock_irqrestore(&p9_poll_lock, flags);
1169 p9_debug(P9_DEBUG_TRANS, "finish\n");
1172 int p9_trans_fd_init(void)
1174 v9fs_register_trans(&p9_tcp_trans);
1175 v9fs_register_trans(&p9_unix_trans);
1176 v9fs_register_trans(&p9_fd_trans);
1181 void p9_trans_fd_exit(void)
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);