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 (64*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)
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_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
107 * @rq: current read work
108 * @wq: current write work
114 struct list_head mux_list
;
115 struct p9_client
*client
;
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
;
126 struct list_head poll_pending_link
;
127 struct p9_poll_wait poll_wait
[MAXPOLLWADDR
];
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
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
)
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
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
);
195 spin_unlock(&m
->client
->lock
);
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
);
213 p9_client_cb(m
->client
, req
, REQ_STATUS_ERROR
);
215 spin_unlock(&m
->client
->lock
);
219 p9_fd_poll(struct p9_client
*client
, struct poll_table_struct
*pt
, int *err
)
222 struct p9_trans_fd
*ts
= NULL
;
224 if (client
&& client
->status
== Connected
)
233 ret
= vfs_poll(ts
->rd
, pt
);
234 if (ts
->rd
!= ts
->wr
)
235 ret
= (ret
& ~EPOLLOUT
) | (vfs_poll(ts
->wr
, pt
) & ~EPOLLIN
);
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
)
250 struct p9_trans_fd
*ts
= NULL
;
253 if (client
&& client
->status
!= Disconnected
)
259 if (!(ts
->rd
->f_flags
& O_NONBLOCK
))
260 p9_debug(P9_DEBUG_ERROR
, "blocking read ...\n");
263 ret
= kernel_read(ts
->rd
, v
, len
, &pos
);
264 if (ret
<= 0 && ret
!= -ERESTARTSYS
&& ret
!= -EAGAIN
)
265 client
->status
= Disconnected
;
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
)
281 m
= container_of(work
, struct p9_conn
, rq
);
286 p9_debug(P9_DEBUG_TRANS
, "start mux %p pos %zd\n", m
, m
->rc
.offset
);
289 m
->rc
.sdata
= m
->tmp_buf
;
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
);
310 if ((!m
->rreq
) && (m
->rc
.offset
== m
->rc
.capacity
)) {
311 p9_debug(P9_DEBUG_TRANS
, "got new header\n");
315 err
= p9_parse_header(&m
->rc
, &m
->rc
.size
, NULL
, NULL
, 0);
317 p9_debug(P9_DEBUG_ERROR
,
318 "error parsing header: %d\n", err
);
322 if (m
->rc
.size
>= m
->client
->msize
) {
323 p9_debug(P9_DEBUG_ERROR
,
324 "requested packet size too big: %d\n",
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",
342 if (!m
->rreq
->rc
.sdata
) {
343 p9_debug(P9_DEBUG_ERROR
,
344 "No recv fcall for tag %d (req %p), disconnecting!\n",
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
;
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
);
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",
373 spin_unlock(&m
->client
->lock
);
382 clear_bit(Rworksched
, &m
->wsched
);
384 if (!list_empty(&m
->req_list
)) {
385 if (test_and_clear_bit(Rpending
, &m
->wsched
))
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
);
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
)
413 struct p9_trans_fd
*ts
= NULL
;
415 if (client
&& client
->status
!= Disconnected
)
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
;
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
)
441 struct p9_req_t
*req
;
443 m
= container_of(work
, struct p9_conn
, wq
);
446 clear_bit(Wworksched
, &m
->wsched
);
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
);
458 req
= list_entry(m
->unsent_req_list
.next
, struct p9_req_t
,
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
;
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
);
489 if (m
->wpos
== m
->wsize
) {
490 m
->wpos
= m
->wsize
= 0;
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
))
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
);
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
;
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
);
535 * p9_pollwait - add poll task to the wait queue
536 * @filp: file pointer being polled
537 * @wait_address: wait_q to block on
540 * called by files poll operation to add v9fs-poll task to files wait queue
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
;
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
];
558 p9_debug(P9_DEBUG_ERROR
, "not enough wait_address slots\n");
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
)
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
);
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
);
595 p9_debug(P9_DEBUG_TRANS
, "mux %p can read\n", m
);
596 set_bit(Rpending
, &m
->wsched
);
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
)
614 int err
= -ECONNRESET
;
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
);
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
);
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
)
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
);
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
))
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
);
683 static int p9_fd_cancel(struct p9_client
*client
, struct p9_req_t
*req
)
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
;
697 spin_unlock(&client
->lock
);
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
);
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
);
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
)
742 substring_t args
[MAX_OPT_ARGS
];
744 char *options
, *tmp_options
;
746 opts
->port
= P9_PORT
;
749 opts
->privport
= false;
754 tmp_options
= kstrdup(params
, GFP_KERNEL
);
756 p9_debug(P9_DEBUG_ERROR
,
757 "failed to allocate copy of option string\n");
760 options
= tmp_options
;
762 while ((p
= strsep(&options
, ",")) != NULL
) {
767 token
= match_token(p
, tokens
, args
);
768 if ((token
!= Opt_err
) && (token
!= Opt_privport
)) {
769 r
= match_int(&args
[0], &option
);
771 p9_debug(P9_DEBUG_ERROR
,
772 "integer field, but no integer?\n");
787 opts
->privport
= true;
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
),
807 if (!ts
->rd
|| !ts
->wr
) {
817 client
->status
= Connected
;
822 static int p9_socket_open(struct p9_client
*client
, struct socket
*csocket
)
824 struct p9_trans_fd
*p
;
827 p
= kzalloc(sizeof(struct p9_trans_fd
), GFP_KERNEL
);
831 csocket
->sk
->sk_allocation
= GFP_NOIO
;
832 file
= sock_alloc_file(csocket
, 0, NULL
);
834 pr_err("%s (%d): failed to map fd\n",
835 __func__
, task_pid_nr(current
));
837 return PTR_ERR(file
);
841 p
->wr
= p
->rd
= file
;
843 client
->status
= Connected
;
845 p
->rd
->f_flags
|= O_NONBLOCK
;
847 p9_conn_create(client
);
852 * p9_mux_destroy - cancels all pending requests of mux
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
);
863 cancel_work_sync(&m
->rq
);
868 cancel_work_sync(&m
->wq
);
874 p9_conn_cancel(m
, -ECONNRESET
);
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
;
896 client
->status
= Disconnected
;
898 p9_conn_destroy(&ts
->conn
);
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]);
918 for (count
= 0; count
< 4; count
++) {
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
)
944 p9_fd_create_tcp(struct p9_client
*client
, const char *addr
, char *args
)
947 struct socket
*csocket
;
948 struct sockaddr_in sin_server
;
949 struct p9_fd_opts opts
;
951 err
= parse_opts(args
, &opts
);
955 if (addr
== NULL
|| valid_ipaddr4(addr
) < 0)
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);
968 pr_err("%s (%d): problem creating socket\n",
969 __func__
, task_pid_nr(current
));
974 err
= p9_bind_privport(csocket
);
976 pr_err("%s (%d): problem binding to privport\n",
977 __func__
, task_pid_nr(current
));
978 sock_release(csocket
);
983 err
= csocket
->ops
->connect(csocket
,
984 (struct sockaddr
*)&sin_server
,
985 sizeof(struct sockaddr_in
), 0);
987 pr_err("%s (%d): problem connecting socket to %s\n",
988 __func__
, task_pid_nr(current
), addr
);
989 sock_release(csocket
);
993 return p9_socket_open(client
, csocket
);
997 p9_fd_create_unix(struct p9_client
*client
, const char *addr
, char *args
)
1000 struct socket
*csocket
;
1001 struct sockaddr_un sun_server
;
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);
1019 pr_err("%s (%d): problem creating socket\n",
1020 __func__
, task_pid_nr(current
));
1024 err
= csocket
->ops
->connect(csocket
, (struct sockaddr
*)&sun_server
,
1025 sizeof(struct sockaddr_un
) - 1, 0);
1027 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1028 __func__
, task_pid_nr(current
), addr
, err
);
1029 sock_release(csocket
);
1033 return p9_socket_open(client
, csocket
);
1037 p9_fd_create(struct p9_client
*client
, const char *addr
, char *args
)
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
);
1055 p9_conn_create(client
);
1060 static struct p9_trans_module p9_tcp_trans
= {
1062 .maxsize
= MAX_SOCK_BUF
,
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
= {
1075 .maxsize
= MAX_SOCK_BUF
,
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
= {
1088 .maxsize
= MAX_SOCK_BUF
,
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
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
,
1119 list_del_init(&conn
->poll_pending_link
);
1120 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
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
);
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
);