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)
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_list: accounting for requests which have been sent
97 * @unsent_req_list: accounting for requests that haven't been sent
99 * @wreq: write request
100 * @req: current request being processed (if any)
101 * @tmp_buf: temporary buffer to read in header
102 * @rc: temporary fcall for reading current frame
103 * @wpos: write position for current frame
104 * @wsize: amount of data to write for current frame
105 * @wbuf: current write buffer
106 * @poll_pending_link: pending links to be polled per conn
107 * @poll_wait: array of wait_q's for various worker threads
109 * @rq: current read work
110 * @wq: current write work
116 struct list_head mux_list
;
117 struct p9_client
*client
;
119 struct list_head req_list
;
120 struct list_head unsent_req_list
;
121 struct p9_req_t
*rreq
;
122 struct p9_req_t
*wreq
;
128 struct list_head poll_pending_link
;
129 struct p9_poll_wait poll_wait
[MAXPOLLWADDR
];
131 struct work_struct rq
;
132 struct work_struct wq
;
133 unsigned long wsched
;
137 * struct p9_trans_fd - transport state
138 * @rd: reference to file to read from
139 * @wr: reference of file to write to
140 * @conn: connection state reference
150 static void p9_poll_workfn(struct work_struct
*work
);
152 static DEFINE_SPINLOCK(p9_poll_lock
);
153 static LIST_HEAD(p9_poll_pending_list
);
154 static DECLARE_WORK(p9_poll_work
, p9_poll_workfn
);
156 static unsigned int p9_ipport_resv_min
= P9_DEF_MIN_RESVPORT
;
157 static unsigned int p9_ipport_resv_max
= P9_DEF_MAX_RESVPORT
;
159 static void p9_mux_poll_stop(struct p9_conn
*m
)
164 for (i
= 0; i
< ARRAY_SIZE(m
->poll_wait
); i
++) {
165 struct p9_poll_wait
*pwait
= &m
->poll_wait
[i
];
167 if (pwait
->wait_addr
) {
168 remove_wait_queue(pwait
->wait_addr
, &pwait
->wait
);
169 pwait
->wait_addr
= NULL
;
173 spin_lock_irqsave(&p9_poll_lock
, flags
);
174 list_del_init(&m
->poll_pending_link
);
175 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
177 flush_work(&p9_poll_work
);
181 * p9_conn_cancel - cancel all pending requests with error
187 static void p9_conn_cancel(struct p9_conn
*m
, int err
)
189 struct p9_req_t
*req
, *rtmp
;
190 LIST_HEAD(cancel_list
);
192 p9_debug(P9_DEBUG_ERROR
, "mux %p err %d\n", m
, err
);
194 spin_lock(&m
->client
->lock
);
197 spin_unlock(&m
->client
->lock
);
203 list_for_each_entry_safe(req
, rtmp
, &m
->req_list
, req_list
) {
204 list_move(&req
->req_list
, &cancel_list
);
206 list_for_each_entry_safe(req
, rtmp
, &m
->unsent_req_list
, req_list
) {
207 list_move(&req
->req_list
, &cancel_list
);
210 list_for_each_entry_safe(req
, rtmp
, &cancel_list
, req_list
) {
211 p9_debug(P9_DEBUG_ERROR
, "call back req %p\n", req
);
212 list_del(&req
->req_list
);
215 p9_client_cb(m
->client
, req
, REQ_STATUS_ERROR
);
217 spin_unlock(&m
->client
->lock
);
221 p9_fd_poll(struct p9_client
*client
, struct poll_table_struct
*pt
, int *err
)
224 struct p9_trans_fd
*ts
= NULL
;
226 if (client
&& client
->status
== Connected
)
235 ret
= vfs_poll(ts
->rd
, pt
);
236 if (ts
->rd
!= ts
->wr
)
237 ret
= (ret
& ~EPOLLOUT
) | (vfs_poll(ts
->wr
, pt
) & ~EPOLLIN
);
242 * p9_fd_read- read from a fd
243 * @client: client instance
244 * @v: buffer to receive data into
245 * @len: size of receive buffer
249 static int p9_fd_read(struct p9_client
*client
, void *v
, int len
)
252 struct p9_trans_fd
*ts
= NULL
;
255 if (client
&& client
->status
!= Disconnected
)
261 if (!(ts
->rd
->f_flags
& O_NONBLOCK
))
262 p9_debug(P9_DEBUG_ERROR
, "blocking read ...\n");
265 ret
= kernel_read(ts
->rd
, v
, len
, &pos
);
266 if (ret
<= 0 && ret
!= -ERESTARTSYS
&& ret
!= -EAGAIN
)
267 client
->status
= Disconnected
;
272 * p9_read_work - called when there is some data to be read from a transport
273 * @work: container of work to be done
277 static void p9_read_work(struct work_struct
*work
)
283 m
= container_of(work
, struct p9_conn
, rq
);
288 p9_debug(P9_DEBUG_TRANS
, "start mux %p pos %zd\n", m
, m
->rc
.offset
);
291 m
->rc
.sdata
= m
->tmp_buf
;
293 m
->rc
.capacity
= 7; /* start by reading header */
296 clear_bit(Rpending
, &m
->wsched
);
297 p9_debug(P9_DEBUG_TRANS
, "read mux %p pos %zd size: %zd = %zd\n",
298 m
, m
->rc
.offset
, m
->rc
.capacity
,
299 m
->rc
.capacity
- m
->rc
.offset
);
300 err
= p9_fd_read(m
->client
, m
->rc
.sdata
+ m
->rc
.offset
,
301 m
->rc
.capacity
- m
->rc
.offset
);
302 p9_debug(P9_DEBUG_TRANS
, "mux %p got %d bytes\n", m
, err
);
312 if ((!m
->rreq
) && (m
->rc
.offset
== m
->rc
.capacity
)) {
313 p9_debug(P9_DEBUG_TRANS
, "got new header\n");
317 err
= p9_parse_header(&m
->rc
, &m
->rc
.size
, NULL
, NULL
, 0);
319 p9_debug(P9_DEBUG_ERROR
,
320 "error parsing header: %d\n", err
);
324 if (m
->rc
.size
>= m
->client
->msize
) {
325 p9_debug(P9_DEBUG_ERROR
,
326 "requested packet size too big: %d\n",
332 p9_debug(P9_DEBUG_TRANS
,
333 "mux %p pkt: size: %d bytes tag: %d\n",
334 m
, m
->rc
.size
, m
->rc
.tag
);
336 m
->rreq
= p9_tag_lookup(m
->client
, m
->rc
.tag
);
337 if (!m
->rreq
|| (m
->rreq
->status
!= REQ_STATUS_SENT
)) {
338 p9_debug(P9_DEBUG_ERROR
, "Unexpected packet tag %d\n",
344 if (!m
->rreq
->rc
.sdata
) {
345 p9_debug(P9_DEBUG_ERROR
,
346 "No recv fcall for tag %d (req %p), disconnecting!\n",
352 m
->rc
.sdata
= m
->rreq
->rc
.sdata
;
353 memcpy(m
->rc
.sdata
, m
->tmp_buf
, m
->rc
.capacity
);
354 m
->rc
.capacity
= m
->rc
.size
;
358 * not an else because some packets (like clunk) have no payload
360 if ((m
->rreq
) && (m
->rc
.offset
== m
->rc
.capacity
)) {
361 p9_debug(P9_DEBUG_TRANS
, "got new packet\n");
362 m
->rreq
->rc
.size
= m
->rc
.offset
;
363 spin_lock(&m
->client
->lock
);
364 if (m
->rreq
->status
== REQ_STATUS_SENT
) {
365 list_del(&m
->rreq
->req_list
);
366 p9_client_cb(m
->client
, m
->rreq
, REQ_STATUS_RCVD
);
367 } else if (m
->rreq
->status
== REQ_STATUS_FLSHD
) {
368 /* Ignore replies associated with a cancelled request. */
369 p9_debug(P9_DEBUG_TRANS
,
370 "Ignore replies associated with a cancelled request\n");
372 spin_unlock(&m
->client
->lock
);
373 p9_debug(P9_DEBUG_ERROR
,
374 "Request tag %d errored out while we were reading the reply\n",
379 spin_unlock(&m
->client
->lock
);
388 clear_bit(Rworksched
, &m
->wsched
);
390 if (!list_empty(&m
->req_list
)) {
391 if (test_and_clear_bit(Rpending
, &m
->wsched
))
394 n
= p9_fd_poll(m
->client
, NULL
, NULL
);
396 if ((n
& EPOLLIN
) && !test_and_set_bit(Rworksched
, &m
->wsched
)) {
397 p9_debug(P9_DEBUG_TRANS
, "sched read work %p\n", m
);
398 schedule_work(&m
->rq
);
404 p9_conn_cancel(m
, err
);
405 clear_bit(Rworksched
, &m
->wsched
);
409 * p9_fd_write - write to a socket
410 * @client: client instance
411 * @v: buffer to send data from
412 * @len: size of send buffer
416 static int p9_fd_write(struct p9_client
*client
, void *v
, int len
)
419 struct p9_trans_fd
*ts
= NULL
;
421 if (client
&& client
->status
!= Disconnected
)
427 if (!(ts
->wr
->f_flags
& O_NONBLOCK
))
428 p9_debug(P9_DEBUG_ERROR
, "blocking write ...\n");
430 ret
= kernel_write(ts
->wr
, v
, len
, &ts
->wr
->f_pos
);
431 if (ret
<= 0 && ret
!= -ERESTARTSYS
&& ret
!= -EAGAIN
)
432 client
->status
= Disconnected
;
437 * p9_write_work - called when a transport can send some data
438 * @work: container for work to be done
442 static void p9_write_work(struct work_struct
*work
)
447 struct p9_req_t
*req
;
449 m
= container_of(work
, struct p9_conn
, wq
);
452 clear_bit(Wworksched
, &m
->wsched
);
457 spin_lock(&m
->client
->lock
);
458 if (list_empty(&m
->unsent_req_list
)) {
459 clear_bit(Wworksched
, &m
->wsched
);
460 spin_unlock(&m
->client
->lock
);
464 req
= list_entry(m
->unsent_req_list
.next
, struct p9_req_t
,
466 req
->status
= REQ_STATUS_SENT
;
467 p9_debug(P9_DEBUG_TRANS
, "move req %p\n", req
);
468 list_move_tail(&req
->req_list
, &m
->req_list
);
470 m
->wbuf
= req
->tc
.sdata
;
471 m
->wsize
= req
->tc
.size
;
475 spin_unlock(&m
->client
->lock
);
478 p9_debug(P9_DEBUG_TRANS
, "mux %p pos %d size %d\n",
479 m
, m
->wpos
, m
->wsize
);
480 clear_bit(Wpending
, &m
->wsched
);
481 err
= p9_fd_write(m
->client
, m
->wbuf
+ m
->wpos
, m
->wsize
- m
->wpos
);
482 p9_debug(P9_DEBUG_TRANS
, "mux %p sent %d bytes\n", m
, err
);
495 if (m
->wpos
== m
->wsize
) {
496 m
->wpos
= m
->wsize
= 0;
502 clear_bit(Wworksched
, &m
->wsched
);
504 if (m
->wsize
|| !list_empty(&m
->unsent_req_list
)) {
505 if (test_and_clear_bit(Wpending
, &m
->wsched
))
508 n
= p9_fd_poll(m
->client
, NULL
, NULL
);
510 if ((n
& EPOLLOUT
) &&
511 !test_and_set_bit(Wworksched
, &m
->wsched
)) {
512 p9_debug(P9_DEBUG_TRANS
, "sched write work %p\n", m
);
513 schedule_work(&m
->wq
);
520 p9_conn_cancel(m
, err
);
521 clear_bit(Wworksched
, &m
->wsched
);
524 static int p9_pollwake(wait_queue_entry_t
*wait
, unsigned int mode
, int sync
, void *key
)
526 struct p9_poll_wait
*pwait
=
527 container_of(wait
, struct p9_poll_wait
, wait
);
528 struct p9_conn
*m
= pwait
->conn
;
531 spin_lock_irqsave(&p9_poll_lock
, flags
);
532 if (list_empty(&m
->poll_pending_link
))
533 list_add_tail(&m
->poll_pending_link
, &p9_poll_pending_list
);
534 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
536 schedule_work(&p9_poll_work
);
541 * p9_pollwait - add poll task to the wait queue
542 * @filp: file pointer being polled
543 * @wait_address: wait_q to block on
546 * called by files poll operation to add v9fs-poll task to files wait queue
550 p9_pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
, poll_table
*p
)
552 struct p9_conn
*m
= container_of(p
, struct p9_conn
, pt
);
553 struct p9_poll_wait
*pwait
= NULL
;
556 for (i
= 0; i
< ARRAY_SIZE(m
->poll_wait
); i
++) {
557 if (m
->poll_wait
[i
].wait_addr
== NULL
) {
558 pwait
= &m
->poll_wait
[i
];
564 p9_debug(P9_DEBUG_ERROR
, "not enough wait_address slots\n");
569 pwait
->wait_addr
= wait_address
;
570 init_waitqueue_func_entry(&pwait
->wait
, p9_pollwake
);
571 add_wait_queue(wait_address
, &pwait
->wait
);
575 * p9_conn_create - initialize the per-session mux data
576 * @client: client instance
578 * Note: Creates the polling task if this is the first session.
581 static void p9_conn_create(struct p9_client
*client
)
584 struct p9_trans_fd
*ts
= client
->trans
;
585 struct p9_conn
*m
= &ts
->conn
;
587 p9_debug(P9_DEBUG_TRANS
, "client %p msize %d\n", client
, client
->msize
);
589 INIT_LIST_HEAD(&m
->mux_list
);
592 INIT_LIST_HEAD(&m
->req_list
);
593 INIT_LIST_HEAD(&m
->unsent_req_list
);
594 INIT_WORK(&m
->rq
, p9_read_work
);
595 INIT_WORK(&m
->wq
, p9_write_work
);
596 INIT_LIST_HEAD(&m
->poll_pending_link
);
597 init_poll_funcptr(&m
->pt
, p9_pollwait
);
599 n
= p9_fd_poll(client
, &m
->pt
, NULL
);
601 p9_debug(P9_DEBUG_TRANS
, "mux %p can read\n", m
);
602 set_bit(Rpending
, &m
->wsched
);
606 p9_debug(P9_DEBUG_TRANS
, "mux %p can write\n", m
);
607 set_bit(Wpending
, &m
->wsched
);
612 * p9_poll_mux - polls a mux and schedules read or write works if necessary
613 * @m: connection to poll
617 static void p9_poll_mux(struct p9_conn
*m
)
620 int err
= -ECONNRESET
;
625 n
= p9_fd_poll(m
->client
, NULL
, &err
);
626 if (n
& (EPOLLERR
| EPOLLHUP
| EPOLLNVAL
)) {
627 p9_debug(P9_DEBUG_TRANS
, "error mux %p err %d\n", m
, n
);
628 p9_conn_cancel(m
, err
);
632 set_bit(Rpending
, &m
->wsched
);
633 p9_debug(P9_DEBUG_TRANS
, "mux %p can read\n", m
);
634 if (!test_and_set_bit(Rworksched
, &m
->wsched
)) {
635 p9_debug(P9_DEBUG_TRANS
, "sched read work %p\n", m
);
636 schedule_work(&m
->rq
);
641 set_bit(Wpending
, &m
->wsched
);
642 p9_debug(P9_DEBUG_TRANS
, "mux %p can write\n", m
);
643 if ((m
->wsize
|| !list_empty(&m
->unsent_req_list
)) &&
644 !test_and_set_bit(Wworksched
, &m
->wsched
)) {
645 p9_debug(P9_DEBUG_TRANS
, "sched write work %p\n", m
);
646 schedule_work(&m
->wq
);
652 * p9_fd_request - send 9P request
653 * The function can sleep until the request is scheduled for sending.
654 * The function can be interrupted. Return from the function is not
655 * a guarantee that the request is sent successfully.
657 * @client: client instance
658 * @req: request to be sent
662 static int p9_fd_request(struct p9_client
*client
, struct p9_req_t
*req
)
665 struct p9_trans_fd
*ts
= client
->trans
;
666 struct p9_conn
*m
= &ts
->conn
;
668 p9_debug(P9_DEBUG_TRANS
, "mux %p task %p tcall %p id %d\n",
669 m
, current
, &req
->tc
, req
->tc
.id
);
673 spin_lock(&client
->lock
);
674 req
->status
= REQ_STATUS_UNSENT
;
675 list_add_tail(&req
->req_list
, &m
->unsent_req_list
);
676 spin_unlock(&client
->lock
);
678 if (test_and_clear_bit(Wpending
, &m
->wsched
))
681 n
= p9_fd_poll(m
->client
, NULL
, NULL
);
683 if (n
& EPOLLOUT
&& !test_and_set_bit(Wworksched
, &m
->wsched
))
684 schedule_work(&m
->wq
);
689 static int p9_fd_cancel(struct p9_client
*client
, struct p9_req_t
*req
)
693 p9_debug(P9_DEBUG_TRANS
, "client %p req %p\n", client
, req
);
695 spin_lock(&client
->lock
);
697 if (req
->status
== REQ_STATUS_UNSENT
) {
698 list_del(&req
->req_list
);
699 req
->status
= REQ_STATUS_FLSHD
;
703 spin_unlock(&client
->lock
);
708 static int p9_fd_cancelled(struct p9_client
*client
, struct p9_req_t
*req
)
710 p9_debug(P9_DEBUG_TRANS
, "client %p req %p\n", client
, req
);
712 spin_lock(&client
->lock
);
713 /* Ignore cancelled request if message has been received
716 if (req
->status
== REQ_STATUS_RCVD
) {
717 spin_unlock(&client
->lock
);
721 /* we haven't received a response for oldreq,
722 * remove it from the list.
724 list_del(&req
->req_list
);
725 req
->status
= REQ_STATUS_FLSHD
;
726 spin_unlock(&client
->lock
);
732 static int p9_fd_show_options(struct seq_file
*m
, struct p9_client
*clnt
)
734 if (clnt
->trans_mod
== &p9_tcp_trans
) {
735 if (clnt
->trans_opts
.tcp
.port
!= P9_PORT
)
736 seq_printf(m
, ",port=%u", clnt
->trans_opts
.tcp
.port
);
737 } else if (clnt
->trans_mod
== &p9_fd_trans
) {
738 if (clnt
->trans_opts
.fd
.rfd
!= ~0)
739 seq_printf(m
, ",rfd=%u", clnt
->trans_opts
.fd
.rfd
);
740 if (clnt
->trans_opts
.fd
.wfd
!= ~0)
741 seq_printf(m
, ",wfd=%u", clnt
->trans_opts
.fd
.wfd
);
747 * parse_opts - parse mount options into p9_fd_opts structure
748 * @params: options string passed from mount
749 * @opts: fd transport-specific structure to parse options into
751 * Returns 0 upon success, -ERRNO upon failure
754 static int parse_opts(char *params
, struct p9_fd_opts
*opts
)
757 substring_t args
[MAX_OPT_ARGS
];
759 char *options
, *tmp_options
;
761 opts
->port
= P9_PORT
;
764 opts
->privport
= false;
769 tmp_options
= kstrdup(params
, GFP_KERNEL
);
771 p9_debug(P9_DEBUG_ERROR
,
772 "failed to allocate copy of option string\n");
775 options
= tmp_options
;
777 while ((p
= strsep(&options
, ",")) != NULL
) {
782 token
= match_token(p
, tokens
, args
);
783 if ((token
!= Opt_err
) && (token
!= Opt_privport
)) {
784 r
= match_int(&args
[0], &option
);
786 p9_debug(P9_DEBUG_ERROR
,
787 "integer field, but no integer?\n");
802 opts
->privport
= true;
813 static int p9_fd_open(struct p9_client
*client
, int rfd
, int wfd
)
815 struct p9_trans_fd
*ts
= kzalloc(sizeof(struct p9_trans_fd
),
823 if (!(ts
->rd
->f_mode
& FMODE_READ
))
828 if (!(ts
->wr
->f_mode
& FMODE_WRITE
))
832 client
->status
= Connected
;
845 static int p9_socket_open(struct p9_client
*client
, struct socket
*csocket
)
847 struct p9_trans_fd
*p
;
850 p
= kzalloc(sizeof(struct p9_trans_fd
), GFP_KERNEL
);
854 csocket
->sk
->sk_allocation
= GFP_NOIO
;
855 file
= sock_alloc_file(csocket
, 0, NULL
);
857 pr_err("%s (%d): failed to map fd\n",
858 __func__
, task_pid_nr(current
));
860 return PTR_ERR(file
);
864 p
->wr
= p
->rd
= file
;
866 client
->status
= Connected
;
868 p
->rd
->f_flags
|= O_NONBLOCK
;
870 p9_conn_create(client
);
875 * p9_mux_destroy - cancels all pending requests of mux
880 static void p9_conn_destroy(struct p9_conn
*m
)
882 p9_debug(P9_DEBUG_TRANS
, "mux %p prev %p next %p\n",
883 m
, m
->mux_list
.prev
, m
->mux_list
.next
);
886 cancel_work_sync(&m
->rq
);
891 cancel_work_sync(&m
->wq
);
897 p9_conn_cancel(m
, -ECONNRESET
);
903 * p9_fd_close - shutdown file descriptor transport
904 * @client: client instance
908 static void p9_fd_close(struct p9_client
*client
)
910 struct p9_trans_fd
*ts
;
919 client
->status
= Disconnected
;
921 p9_conn_destroy(&ts
->conn
);
932 * stolen from NFS - maybe should be made a generic function?
934 static inline int valid_ipaddr4(const char *buf
)
936 int rc
, count
, in
[4];
938 rc
= sscanf(buf
, "%d.%d.%d.%d", &in
[0], &in
[1], &in
[2], &in
[3]);
941 for (count
= 0; count
< 4; count
++) {
948 static int p9_bind_privport(struct socket
*sock
)
950 struct sockaddr_in cl
;
951 int port
, err
= -EINVAL
;
953 memset(&cl
, 0, sizeof(cl
));
954 cl
.sin_family
= AF_INET
;
955 cl
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
956 for (port
= p9_ipport_resv_max
; port
>= p9_ipport_resv_min
; port
--) {
957 cl
.sin_port
= htons((ushort
)port
);
958 err
= kernel_bind(sock
, (struct sockaddr
*)&cl
, sizeof(cl
));
959 if (err
!= -EADDRINUSE
)
967 p9_fd_create_tcp(struct p9_client
*client
, const char *addr
, char *args
)
970 struct socket
*csocket
;
971 struct sockaddr_in sin_server
;
972 struct p9_fd_opts opts
;
974 err
= parse_opts(args
, &opts
);
978 if (addr
== NULL
|| valid_ipaddr4(addr
) < 0)
983 client
->trans_opts
.tcp
.port
= opts
.port
;
984 client
->trans_opts
.tcp
.privport
= opts
.privport
;
985 sin_server
.sin_family
= AF_INET
;
986 sin_server
.sin_addr
.s_addr
= in_aton(addr
);
987 sin_server
.sin_port
= htons(opts
.port
);
988 err
= __sock_create(current
->nsproxy
->net_ns
, PF_INET
,
989 SOCK_STREAM
, IPPROTO_TCP
, &csocket
, 1);
991 pr_err("%s (%d): problem creating socket\n",
992 __func__
, task_pid_nr(current
));
997 err
= p9_bind_privport(csocket
);
999 pr_err("%s (%d): problem binding to privport\n",
1000 __func__
, task_pid_nr(current
));
1001 sock_release(csocket
);
1006 err
= csocket
->ops
->connect(csocket
,
1007 (struct sockaddr
*)&sin_server
,
1008 sizeof(struct sockaddr_in
), 0);
1010 pr_err("%s (%d): problem connecting socket to %s\n",
1011 __func__
, task_pid_nr(current
), addr
);
1012 sock_release(csocket
);
1016 return p9_socket_open(client
, csocket
);
1020 p9_fd_create_unix(struct p9_client
*client
, const char *addr
, char *args
)
1023 struct socket
*csocket
;
1024 struct sockaddr_un sun_server
;
1028 if (!addr
|| !strlen(addr
))
1031 if (strlen(addr
) >= UNIX_PATH_MAX
) {
1032 pr_err("%s (%d): address too long: %s\n",
1033 __func__
, task_pid_nr(current
), addr
);
1034 return -ENAMETOOLONG
;
1037 sun_server
.sun_family
= PF_UNIX
;
1038 strcpy(sun_server
.sun_path
, addr
);
1039 err
= __sock_create(current
->nsproxy
->net_ns
, PF_UNIX
,
1040 SOCK_STREAM
, 0, &csocket
, 1);
1042 pr_err("%s (%d): problem creating socket\n",
1043 __func__
, task_pid_nr(current
));
1047 err
= csocket
->ops
->connect(csocket
, (struct sockaddr
*)&sun_server
,
1048 sizeof(struct sockaddr_un
) - 1, 0);
1050 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1051 __func__
, task_pid_nr(current
), addr
, err
);
1052 sock_release(csocket
);
1056 return p9_socket_open(client
, csocket
);
1060 p9_fd_create(struct p9_client
*client
, const char *addr
, char *args
)
1063 struct p9_fd_opts opts
;
1065 parse_opts(args
, &opts
);
1066 client
->trans_opts
.fd
.rfd
= opts
.rfd
;
1067 client
->trans_opts
.fd
.wfd
= opts
.wfd
;
1069 if (opts
.rfd
== ~0 || opts
.wfd
== ~0) {
1070 pr_err("Insufficient options for proto=fd\n");
1071 return -ENOPROTOOPT
;
1074 err
= p9_fd_open(client
, opts
.rfd
, opts
.wfd
);
1078 p9_conn_create(client
);
1083 static struct p9_trans_module p9_tcp_trans
= {
1085 .maxsize
= MAX_SOCK_BUF
,
1087 .create
= p9_fd_create_tcp
,
1088 .close
= p9_fd_close
,
1089 .request
= p9_fd_request
,
1090 .cancel
= p9_fd_cancel
,
1091 .cancelled
= p9_fd_cancelled
,
1092 .show_options
= p9_fd_show_options
,
1093 .owner
= THIS_MODULE
,
1096 static struct p9_trans_module p9_unix_trans
= {
1098 .maxsize
= MAX_SOCK_BUF
,
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
,
1109 static struct p9_trans_module p9_fd_trans
= {
1111 .maxsize
= MAX_SOCK_BUF
,
1113 .create
= p9_fd_create
,
1114 .close
= p9_fd_close
,
1115 .request
= p9_fd_request
,
1116 .cancel
= p9_fd_cancel
,
1117 .cancelled
= p9_fd_cancelled
,
1118 .show_options
= p9_fd_show_options
,
1119 .owner
= THIS_MODULE
,
1123 * p9_poll_workfn - poll worker thread
1126 * polls all v9fs transports for new events and queues the appropriate
1127 * work to the work queue
1131 static void p9_poll_workfn(struct work_struct
*work
)
1133 unsigned long flags
;
1135 p9_debug(P9_DEBUG_TRANS
, "start %p\n", current
);
1137 spin_lock_irqsave(&p9_poll_lock
, flags
);
1138 while (!list_empty(&p9_poll_pending_list
)) {
1139 struct p9_conn
*conn
= list_first_entry(&p9_poll_pending_list
,
1142 list_del_init(&conn
->poll_pending_link
);
1143 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
1147 spin_lock_irqsave(&p9_poll_lock
, flags
);
1149 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
1151 p9_debug(P9_DEBUG_TRANS
, "finish\n");
1154 int p9_trans_fd_init(void)
1156 v9fs_register_trans(&p9_tcp_trans
);
1157 v9fs_register_trans(&p9_unix_trans
);
1158 v9fs_register_trans(&p9_fd_trans
);
1163 void p9_trans_fd_exit(void)
1165 flush_work(&p9_poll_work
);
1166 v9fs_unregister_trans(&p9_tcp_trans
);
1167 v9fs_unregister_trans(&p9_unix_trans
);
1168 v9fs_unregister_trans(&p9_fd_trans
);