1 // SPDX-License-Identifier: GPL-2.0-only
3 * Fd transport layer. Includes deprecated socket layer.
5 * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
6 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
7 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/net.h>
16 #include <linux/ipv6.h>
17 #include <linux/kthread.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
21 #include <linux/uaccess.h>
22 #include <linux/inet.h>
23 #include <linux/file.h>
24 #include <linux/parser.h>
25 #include <linux/slab.h>
26 #include <linux/seq_file.h>
27 #include <net/9p/9p.h>
28 #include <net/9p/client.h>
29 #include <net/9p/transport.h>
31 #include <linux/syscalls.h> /* killme */
34 #define MAX_SOCK_BUF (1024*1024)
35 #define MAXPOLLWADDR 2
37 static struct p9_trans_module p9_tcp_trans
;
38 static struct p9_trans_module p9_fd_trans
;
41 * struct p9_fd_opts - per-transport options
42 * @rfd: file descriptor for reading (trans=fd)
43 * @wfd: file descriptor for writing (trans=fd)
44 * @port: port to connect to (trans=tcp)
45 * @privport: port is privileged
56 * Option Parsing (code inspired by NFS code)
57 * - a little lazy - parse all fd-transport options
61 /* Options that take integer arguments */
62 Opt_port
, Opt_rfdno
, Opt_wfdno
, Opt_err
,
63 /* Options that take no arguments */
67 static const match_table_t tokens
= {
68 {Opt_port
, "port=%u"},
69 {Opt_rfdno
, "rfdno=%u"},
70 {Opt_wfdno
, "wfdno=%u"},
71 {Opt_privport
, "privport"},
76 Rworksched
= 1, /* read work scheduled or running */
77 Rpending
= 2, /* can read */
78 Wworksched
= 4, /* write work scheduled or running */
79 Wpending
= 8, /* can write */
84 wait_queue_entry_t wait
;
85 wait_queue_head_t
*wait_addr
;
89 * struct p9_conn - fd mux connection state information
90 * @mux_list: list link for mux to manage multiple connections (?)
91 * @client: reference to client instance for this connection
93 * @req_lock: lock protecting req_list and requests statuses
94 * @req_list: accounting for requests which have been sent
95 * @unsent_req_list: accounting for requests that haven't been sent
97 * @wreq: write request
98 * @tmp_buf: temporary buffer to read in header
99 * @rc: temporary fcall for reading current frame
100 * @wpos: write position for current frame
101 * @wsize: amount of data to write for current frame
102 * @wbuf: current write buffer
103 * @poll_pending_link: pending links to be polled per conn
104 * @poll_wait: array of wait_q's for various worker threads
106 * @rq: current read work
107 * @wq: current write work
113 struct list_head mux_list
;
114 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
;
121 char tmp_buf
[P9_HDRSZ
];
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
->req_lock
);
195 spin_unlock(&m
->req_lock
);
201 list_for_each_entry_safe(req
, rtmp
, &m
->req_list
, req_list
) {
202 list_move(&req
->req_list
, &cancel_list
);
203 WRITE_ONCE(req
->status
, REQ_STATUS_ERROR
);
205 list_for_each_entry_safe(req
, rtmp
, &m
->unsent_req_list
, req_list
) {
206 list_move(&req
->req_list
, &cancel_list
);
207 WRITE_ONCE(req
->status
, REQ_STATUS_ERROR
);
210 spin_unlock(&m
->req_lock
);
212 list_for_each_entry_safe(req
, rtmp
, &cancel_list
, req_list
) {
213 p9_debug(P9_DEBUG_ERROR
, "call back req %p\n", req
);
214 list_del(&req
->req_list
);
217 p9_client_cb(m
->client
, req
, REQ_STATUS_ERROR
);
222 p9_fd_poll(struct p9_client
*client
, struct poll_table_struct
*pt
, int *err
)
225 struct p9_trans_fd
*ts
= NULL
;
227 if (client
&& client
->status
== Connected
)
236 ret
= vfs_poll(ts
->rd
, pt
);
237 if (ts
->rd
!= ts
->wr
)
238 ret
= (ret
& ~EPOLLOUT
) | (vfs_poll(ts
->wr
, pt
) & ~EPOLLIN
);
243 * p9_fd_read- read from a fd
244 * @client: client instance
245 * @v: buffer to receive data into
246 * @len: size of receive buffer
250 static int p9_fd_read(struct p9_client
*client
, void *v
, int len
)
253 struct p9_trans_fd
*ts
= NULL
;
256 if (client
&& client
->status
!= Disconnected
)
262 if (!(ts
->rd
->f_flags
& O_NONBLOCK
))
263 p9_debug(P9_DEBUG_ERROR
, "blocking read ...\n");
266 ret
= kernel_read(ts
->rd
, v
, len
, &pos
);
267 if (ret
<= 0 && ret
!= -ERESTARTSYS
&& ret
!= -EAGAIN
)
268 client
->status
= Disconnected
;
273 * p9_read_work - called when there is some data to be read from a transport
274 * @work: container of work to be done
278 static void p9_read_work(struct work_struct
*work
)
284 m
= container_of(work
, struct p9_conn
, rq
);
289 p9_debug(P9_DEBUG_TRANS
, "start mux %p pos %zd\n", m
, m
->rc
.offset
);
292 m
->rc
.sdata
= m
->tmp_buf
;
294 m
->rc
.capacity
= P9_HDRSZ
; /* start by reading header */
297 clear_bit(Rpending
, &m
->wsched
);
298 p9_debug(P9_DEBUG_TRANS
, "read mux %p pos %zd size: %zd = %zd\n",
299 m
, m
->rc
.offset
, m
->rc
.capacity
,
300 m
->rc
.capacity
- m
->rc
.offset
);
301 err
= p9_fd_read(m
->client
, m
->rc
.sdata
+ m
->rc
.offset
,
302 m
->rc
.capacity
- m
->rc
.offset
);
303 p9_debug(P9_DEBUG_TRANS
, "mux %p got %d bytes\n", m
, err
);
313 if ((!m
->rreq
) && (m
->rc
.offset
== m
->rc
.capacity
)) {
314 p9_debug(P9_DEBUG_TRANS
, "got new header\n");
317 m
->rc
.size
= P9_HDRSZ
;
318 err
= p9_parse_header(&m
->rc
, &m
->rc
.size
, NULL
, NULL
, 0);
320 p9_debug(P9_DEBUG_ERROR
,
321 "error parsing header: %d\n", err
);
325 p9_debug(P9_DEBUG_TRANS
,
326 "mux %p pkt: size: %d bytes tag: %d\n",
327 m
, m
->rc
.size
, m
->rc
.tag
);
329 m
->rreq
= p9_tag_lookup(m
->client
, m
->rc
.tag
);
330 if (!m
->rreq
|| (m
->rreq
->status
!= REQ_STATUS_SENT
)) {
331 p9_debug(P9_DEBUG_ERROR
, "Unexpected packet tag %d\n",
337 if (m
->rc
.size
> m
->rreq
->rc
.capacity
) {
338 p9_debug(P9_DEBUG_ERROR
,
339 "requested packet size too big: %d for tag %d with capacity %zd\n",
340 m
->rc
.size
, m
->rc
.tag
, m
->rreq
->rc
.capacity
);
345 if (!m
->rreq
->rc
.sdata
) {
346 p9_debug(P9_DEBUG_ERROR
,
347 "No recv fcall for tag %d (req %p), disconnecting!\n",
349 p9_req_put(m
->client
, m
->rreq
);
354 m
->rc
.sdata
= m
->rreq
->rc
.sdata
;
355 memcpy(m
->rc
.sdata
, m
->tmp_buf
, m
->rc
.capacity
);
356 m
->rc
.capacity
= m
->rc
.size
;
360 * not an else because some packets (like clunk) have no payload
362 if ((m
->rreq
) && (m
->rc
.offset
== m
->rc
.capacity
)) {
363 p9_debug(P9_DEBUG_TRANS
, "got new packet\n");
364 m
->rreq
->rc
.size
= m
->rc
.offset
;
365 spin_lock(&m
->req_lock
);
366 if (m
->rreq
->status
== REQ_STATUS_SENT
) {
367 list_del(&m
->rreq
->req_list
);
368 p9_client_cb(m
->client
, m
->rreq
, REQ_STATUS_RCVD
);
369 } else if (m
->rreq
->status
== REQ_STATUS_FLSHD
) {
370 /* Ignore replies associated with a cancelled request. */
371 p9_debug(P9_DEBUG_TRANS
,
372 "Ignore replies associated with a cancelled request\n");
374 spin_unlock(&m
->req_lock
);
375 p9_debug(P9_DEBUG_ERROR
,
376 "Request tag %d errored out while we were reading the reply\n",
381 spin_unlock(&m
->req_lock
);
385 p9_req_put(m
->client
, m
->rreq
);
390 clear_bit(Rworksched
, &m
->wsched
);
392 if (!list_empty(&m
->req_list
)) {
393 if (test_and_clear_bit(Rpending
, &m
->wsched
))
396 n
= p9_fd_poll(m
->client
, NULL
, NULL
);
398 if ((n
& EPOLLIN
) && !test_and_set_bit(Rworksched
, &m
->wsched
)) {
399 p9_debug(P9_DEBUG_TRANS
, "sched read work %p\n", m
);
400 schedule_work(&m
->rq
);
406 p9_conn_cancel(m
, err
);
407 clear_bit(Rworksched
, &m
->wsched
);
411 * p9_fd_write - write to a socket
412 * @client: client instance
413 * @v: buffer to send data from
414 * @len: size of send buffer
418 static int p9_fd_write(struct p9_client
*client
, void *v
, int len
)
421 struct p9_trans_fd
*ts
= NULL
;
423 if (client
&& client
->status
!= Disconnected
)
429 if (!(ts
->wr
->f_flags
& O_NONBLOCK
))
430 p9_debug(P9_DEBUG_ERROR
, "blocking write ...\n");
432 ret
= kernel_write(ts
->wr
, v
, len
, &ts
->wr
->f_pos
);
433 if (ret
<= 0 && ret
!= -ERESTARTSYS
&& ret
!= -EAGAIN
)
434 client
->status
= Disconnected
;
439 * p9_write_work - called when a transport can send some data
440 * @work: container for work to be done
444 static void p9_write_work(struct work_struct
*work
)
449 struct p9_req_t
*req
;
451 m
= container_of(work
, struct p9_conn
, wq
);
454 clear_bit(Wworksched
, &m
->wsched
);
459 spin_lock(&m
->req_lock
);
460 if (list_empty(&m
->unsent_req_list
)) {
461 clear_bit(Wworksched
, &m
->wsched
);
462 spin_unlock(&m
->req_lock
);
466 req
= list_entry(m
->unsent_req_list
.next
, struct p9_req_t
,
468 WRITE_ONCE(req
->status
, REQ_STATUS_SENT
);
469 p9_debug(P9_DEBUG_TRANS
, "move req %p\n", req
);
470 list_move_tail(&req
->req_list
, &m
->req_list
);
472 m
->wbuf
= req
->tc
.sdata
;
473 m
->wsize
= req
->tc
.size
;
477 spin_unlock(&m
->req_lock
);
480 p9_debug(P9_DEBUG_TRANS
, "mux %p pos %d size %d\n",
481 m
, m
->wpos
, m
->wsize
);
482 clear_bit(Wpending
, &m
->wsched
);
483 err
= p9_fd_write(m
->client
, m
->wbuf
+ m
->wpos
, m
->wsize
- m
->wpos
);
484 p9_debug(P9_DEBUG_TRANS
, "mux %p sent %d bytes\n", m
, err
);
497 if (m
->wpos
== m
->wsize
) {
498 m
->wpos
= m
->wsize
= 0;
499 p9_req_put(m
->client
, m
->wreq
);
504 clear_bit(Wworksched
, &m
->wsched
);
506 if (m
->wsize
|| !list_empty(&m
->unsent_req_list
)) {
507 if (test_and_clear_bit(Wpending
, &m
->wsched
))
510 n
= p9_fd_poll(m
->client
, NULL
, NULL
);
512 if ((n
& EPOLLOUT
) &&
513 !test_and_set_bit(Wworksched
, &m
->wsched
)) {
514 p9_debug(P9_DEBUG_TRANS
, "sched write work %p\n", m
);
515 schedule_work(&m
->wq
);
522 p9_conn_cancel(m
, err
);
523 clear_bit(Wworksched
, &m
->wsched
);
526 static int p9_pollwake(wait_queue_entry_t
*wait
, unsigned int mode
, int sync
, void *key
)
528 struct p9_poll_wait
*pwait
=
529 container_of(wait
, struct p9_poll_wait
, wait
);
530 struct p9_conn
*m
= pwait
->conn
;
533 spin_lock_irqsave(&p9_poll_lock
, flags
);
534 if (list_empty(&m
->poll_pending_link
))
535 list_add_tail(&m
->poll_pending_link
, &p9_poll_pending_list
);
536 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
538 schedule_work(&p9_poll_work
);
543 * p9_pollwait - add poll task to the wait queue
544 * @filp: file pointer being polled
545 * @wait_address: wait_q to block on
548 * called by files poll operation to add v9fs-poll task to files wait queue
552 p9_pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
, poll_table
*p
)
554 struct p9_conn
*m
= container_of(p
, struct p9_conn
, pt
);
555 struct p9_poll_wait
*pwait
= NULL
;
558 for (i
= 0; i
< ARRAY_SIZE(m
->poll_wait
); i
++) {
559 if (m
->poll_wait
[i
].wait_addr
== NULL
) {
560 pwait
= &m
->poll_wait
[i
];
566 p9_debug(P9_DEBUG_ERROR
, "not enough wait_address slots\n");
571 pwait
->wait_addr
= wait_address
;
572 init_waitqueue_func_entry(&pwait
->wait
, p9_pollwake
);
573 add_wait_queue(wait_address
, &pwait
->wait
);
577 * p9_conn_create - initialize the per-session mux data
578 * @client: client instance
580 * Note: Creates the polling task if this is the first session.
583 static void p9_conn_create(struct p9_client
*client
)
586 struct p9_trans_fd
*ts
= client
->trans
;
587 struct p9_conn
*m
= &ts
->conn
;
589 p9_debug(P9_DEBUG_TRANS
, "client %p msize %d\n", client
, client
->msize
);
591 INIT_LIST_HEAD(&m
->mux_list
);
594 spin_lock_init(&m
->req_lock
);
595 INIT_LIST_HEAD(&m
->req_list
);
596 INIT_LIST_HEAD(&m
->unsent_req_list
);
597 INIT_WORK(&m
->rq
, p9_read_work
);
598 INIT_WORK(&m
->wq
, p9_write_work
);
599 INIT_LIST_HEAD(&m
->poll_pending_link
);
600 init_poll_funcptr(&m
->pt
, p9_pollwait
);
602 n
= p9_fd_poll(client
, &m
->pt
, NULL
);
604 p9_debug(P9_DEBUG_TRANS
, "mux %p can read\n", m
);
605 set_bit(Rpending
, &m
->wsched
);
609 p9_debug(P9_DEBUG_TRANS
, "mux %p can write\n", m
);
610 set_bit(Wpending
, &m
->wsched
);
615 * p9_poll_mux - polls a mux and schedules read or write works if necessary
616 * @m: connection to poll
620 static void p9_poll_mux(struct p9_conn
*m
)
623 int err
= -ECONNRESET
;
628 n
= p9_fd_poll(m
->client
, NULL
, &err
);
629 if (n
& (EPOLLERR
| EPOLLHUP
| EPOLLNVAL
)) {
630 p9_debug(P9_DEBUG_TRANS
, "error mux %p err %d\n", m
, n
);
631 p9_conn_cancel(m
, err
);
635 set_bit(Rpending
, &m
->wsched
);
636 p9_debug(P9_DEBUG_TRANS
, "mux %p can read\n", m
);
637 if (!test_and_set_bit(Rworksched
, &m
->wsched
)) {
638 p9_debug(P9_DEBUG_TRANS
, "sched read work %p\n", m
);
639 schedule_work(&m
->rq
);
644 set_bit(Wpending
, &m
->wsched
);
645 p9_debug(P9_DEBUG_TRANS
, "mux %p can write\n", m
);
646 if ((m
->wsize
|| !list_empty(&m
->unsent_req_list
)) &&
647 !test_and_set_bit(Wworksched
, &m
->wsched
)) {
648 p9_debug(P9_DEBUG_TRANS
, "sched write work %p\n", m
);
649 schedule_work(&m
->wq
);
655 * p9_fd_request - send 9P request
656 * The function can sleep until the request is scheduled for sending.
657 * The function can be interrupted. Return from the function is not
658 * a guarantee that the request is sent successfully.
660 * @client: client instance
661 * @req: request to be sent
665 static int p9_fd_request(struct p9_client
*client
, struct p9_req_t
*req
)
668 struct p9_trans_fd
*ts
= client
->trans
;
669 struct p9_conn
*m
= &ts
->conn
;
671 p9_debug(P9_DEBUG_TRANS
, "mux %p task %p tcall %p id %d\n",
672 m
, current
, &req
->tc
, req
->tc
.id
);
674 spin_lock(&m
->req_lock
);
677 spin_unlock(&m
->req_lock
);
681 WRITE_ONCE(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 WRITE_ONCE(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 WRITE_ONCE(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 * It's technically possible for userspace or concurrent mounts to
840 * modify this flag concurrently, which will likely result in a
841 * broken filesystem. However, just having bad flags here should
842 * not crash the kernel or cause any other sort of bug, so mark this
843 * particular data race as intentional so that tooling (like KCSAN)
844 * can allow it and detect further problems.
846 data_race(ts
->rd
->f_flags
|= O_NONBLOCK
);
850 if (!(ts
->wr
->f_mode
& FMODE_WRITE
))
852 data_race(ts
->wr
->f_flags
|= O_NONBLOCK
);
855 client
->status
= Connected
;
868 static int p9_socket_open(struct p9_client
*client
, struct socket
*csocket
)
870 struct p9_trans_fd
*p
;
873 p
= kzalloc(sizeof(struct p9_trans_fd
), GFP_KERNEL
);
875 sock_release(csocket
);
879 csocket
->sk
->sk_allocation
= GFP_NOIO
;
880 csocket
->sk
->sk_use_task_frag
= false;
881 file
= sock_alloc_file(csocket
, 0, NULL
);
883 pr_err("%s (%d): failed to map fd\n",
884 __func__
, task_pid_nr(current
));
886 return PTR_ERR(file
);
890 p
->wr
= p
->rd
= file
;
892 client
->status
= Connected
;
894 p
->rd
->f_flags
|= O_NONBLOCK
;
896 p9_conn_create(client
);
901 * p9_conn_destroy - cancels all pending requests of mux
906 static void p9_conn_destroy(struct p9_conn
*m
)
908 p9_debug(P9_DEBUG_TRANS
, "mux %p prev %p next %p\n",
909 m
, m
->mux_list
.prev
, m
->mux_list
.next
);
912 cancel_work_sync(&m
->rq
);
914 p9_req_put(m
->client
, m
->rreq
);
917 cancel_work_sync(&m
->wq
);
919 p9_req_put(m
->client
, m
->wreq
);
923 p9_conn_cancel(m
, -ECONNRESET
);
929 * p9_fd_close - shutdown file descriptor transport
930 * @client: client instance
934 static void p9_fd_close(struct p9_client
*client
)
936 struct p9_trans_fd
*ts
;
945 client
->status
= Disconnected
;
947 p9_conn_destroy(&ts
->conn
);
958 * stolen from NFS - maybe should be made a generic function?
960 static inline int valid_ipaddr4(const char *buf
)
962 int rc
, count
, in
[4];
964 rc
= sscanf(buf
, "%d.%d.%d.%d", &in
[0], &in
[1], &in
[2], &in
[3]);
967 for (count
= 0; count
< 4; count
++) {
974 static int p9_bind_privport(struct socket
*sock
)
976 struct sockaddr_in cl
;
977 int port
, err
= -EINVAL
;
979 memset(&cl
, 0, sizeof(cl
));
980 cl
.sin_family
= AF_INET
;
981 cl
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
982 for (port
= p9_ipport_resv_max
; port
>= p9_ipport_resv_min
; port
--) {
983 cl
.sin_port
= htons((ushort
)port
);
984 err
= kernel_bind(sock
, (struct sockaddr
*)&cl
, sizeof(cl
));
985 if (err
!= -EADDRINUSE
)
993 p9_fd_create_tcp(struct p9_client
*client
, const char *addr
, char *args
)
996 struct socket
*csocket
;
997 struct sockaddr_in sin_server
;
998 struct p9_fd_opts opts
;
1000 err
= parse_opts(args
, &opts
);
1004 if (addr
== NULL
|| valid_ipaddr4(addr
) < 0)
1009 client
->trans_opts
.tcp
.port
= opts
.port
;
1010 client
->trans_opts
.tcp
.privport
= opts
.privport
;
1011 sin_server
.sin_family
= AF_INET
;
1012 sin_server
.sin_addr
.s_addr
= in_aton(addr
);
1013 sin_server
.sin_port
= htons(opts
.port
);
1014 err
= __sock_create(current
->nsproxy
->net_ns
, PF_INET
,
1015 SOCK_STREAM
, IPPROTO_TCP
, &csocket
, 1);
1017 pr_err("%s (%d): problem creating socket\n",
1018 __func__
, task_pid_nr(current
));
1022 if (opts
.privport
) {
1023 err
= p9_bind_privport(csocket
);
1025 pr_err("%s (%d): problem binding to privport\n",
1026 __func__
, task_pid_nr(current
));
1027 sock_release(csocket
);
1032 err
= READ_ONCE(csocket
->ops
)->connect(csocket
,
1033 (struct sockaddr
*)&sin_server
,
1034 sizeof(struct sockaddr_in
), 0);
1036 pr_err("%s (%d): problem connecting socket to %s\n",
1037 __func__
, task_pid_nr(current
), addr
);
1038 sock_release(csocket
);
1042 return p9_socket_open(client
, csocket
);
1046 p9_fd_create_unix(struct p9_client
*client
, const char *addr
, char *args
)
1049 struct socket
*csocket
;
1050 struct sockaddr_un sun_server
;
1054 if (!addr
|| !strlen(addr
))
1057 if (strlen(addr
) >= UNIX_PATH_MAX
) {
1058 pr_err("%s (%d): address too long: %s\n",
1059 __func__
, task_pid_nr(current
), addr
);
1060 return -ENAMETOOLONG
;
1063 sun_server
.sun_family
= PF_UNIX
;
1064 strcpy(sun_server
.sun_path
, addr
);
1065 err
= __sock_create(current
->nsproxy
->net_ns
, PF_UNIX
,
1066 SOCK_STREAM
, 0, &csocket
, 1);
1068 pr_err("%s (%d): problem creating socket\n",
1069 __func__
, task_pid_nr(current
));
1073 err
= READ_ONCE(csocket
->ops
)->connect(csocket
, (struct sockaddr
*)&sun_server
,
1074 sizeof(struct sockaddr_un
) - 1, 0);
1076 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1077 __func__
, task_pid_nr(current
), addr
, err
);
1078 sock_release(csocket
);
1082 return p9_socket_open(client
, csocket
);
1086 p9_fd_create(struct p9_client
*client
, const char *addr
, char *args
)
1089 struct p9_fd_opts opts
;
1091 err
= parse_opts(args
, &opts
);
1094 client
->trans_opts
.fd
.rfd
= opts
.rfd
;
1095 client
->trans_opts
.fd
.wfd
= opts
.wfd
;
1097 if (opts
.rfd
== ~0 || opts
.wfd
== ~0) {
1098 pr_err("Insufficient options for proto=fd\n");
1099 return -ENOPROTOOPT
;
1102 err
= p9_fd_open(client
, opts
.rfd
, opts
.wfd
);
1106 p9_conn_create(client
);
1111 static struct p9_trans_module p9_tcp_trans
= {
1113 .maxsize
= MAX_SOCK_BUF
,
1114 .pooled_rbuffers
= false,
1116 .create
= p9_fd_create_tcp
,
1117 .close
= p9_fd_close
,
1118 .request
= p9_fd_request
,
1119 .cancel
= p9_fd_cancel
,
1120 .cancelled
= p9_fd_cancelled
,
1121 .show_options
= p9_fd_show_options
,
1122 .owner
= THIS_MODULE
,
1124 MODULE_ALIAS_9P("tcp");
1126 static struct p9_trans_module p9_unix_trans
= {
1128 .maxsize
= MAX_SOCK_BUF
,
1130 .create
= p9_fd_create_unix
,
1131 .close
= p9_fd_close
,
1132 .request
= p9_fd_request
,
1133 .cancel
= p9_fd_cancel
,
1134 .cancelled
= p9_fd_cancelled
,
1135 .show_options
= p9_fd_show_options
,
1136 .owner
= THIS_MODULE
,
1138 MODULE_ALIAS_9P("unix");
1140 static struct p9_trans_module p9_fd_trans
= {
1142 .maxsize
= MAX_SOCK_BUF
,
1144 .create
= p9_fd_create
,
1145 .close
= p9_fd_close
,
1146 .request
= p9_fd_request
,
1147 .cancel
= p9_fd_cancel
,
1148 .cancelled
= p9_fd_cancelled
,
1149 .show_options
= p9_fd_show_options
,
1150 .owner
= THIS_MODULE
,
1152 MODULE_ALIAS_9P("fd");
1155 * p9_poll_workfn - poll worker thread
1158 * polls all v9fs transports for new events and queues the appropriate
1159 * work to the work queue
1163 static void p9_poll_workfn(struct work_struct
*work
)
1165 unsigned long flags
;
1167 p9_debug(P9_DEBUG_TRANS
, "start %p\n", current
);
1169 spin_lock_irqsave(&p9_poll_lock
, flags
);
1170 while (!list_empty(&p9_poll_pending_list
)) {
1171 struct p9_conn
*conn
= list_first_entry(&p9_poll_pending_list
,
1174 list_del_init(&conn
->poll_pending_link
);
1175 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
1179 spin_lock_irqsave(&p9_poll_lock
, flags
);
1181 spin_unlock_irqrestore(&p9_poll_lock
, flags
);
1183 p9_debug(P9_DEBUG_TRANS
, "finish\n");
1186 static int __init
p9_trans_fd_init(void)
1188 v9fs_register_trans(&p9_tcp_trans
);
1189 v9fs_register_trans(&p9_unix_trans
);
1190 v9fs_register_trans(&p9_fd_trans
);
1195 static void __exit
p9_trans_fd_exit(void)
1197 flush_work(&p9_poll_work
);
1198 v9fs_unregister_trans(&p9_tcp_trans
);
1199 v9fs_unregister_trans(&p9_unix_trans
);
1200 v9fs_unregister_trans(&p9_fd_trans
);
1203 module_init(p9_trans_fd_init
);
1204 module_exit(p9_trans_fd_exit
);
1206 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
1207 MODULE_DESCRIPTION("Filedescriptor Transport for 9P");
1208 MODULE_LICENSE("GPL");