Thomas Gleixner | 1f32761 | 2019-05-28 09:57:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/9p/trans_fd.c |
| 4 | * |
| 5 | * Fd transport layer. Includes deprecated socket layer. |
| 6 | * |
| 7 | * Copyright (C) 2006 by Russ Cox <rsc@swtch.com> |
| 8 | * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net> |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 9 | * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 10 | * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 11 | */ |
| 12 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 15 | #include <linux/in.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/net.h> |
| 18 | #include <linux/ipv6.h> |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 19 | #include <linux/kthread.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 20 | #include <linux/errno.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/un.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/inet.h> |
| 25 | #include <linux/idr.h> |
| 26 | #include <linux/file.h> |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 27 | #include <linux/parser.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 29 | #include <linux/seq_file.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 30 | #include <net/9p/9p.h> |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 31 | #include <net/9p/client.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 32 | #include <net/9p/transport.h> |
| 33 | |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 34 | #include <linux/syscalls.h> /* killme */ |
| 35 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 36 | #define P9_PORT 564 |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 37 | #define MAX_SOCK_BUF (64*1024) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 38 | #define MAXPOLLWADDR 2 |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 39 | |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 40 | static struct p9_trans_module p9_tcp_trans; |
| 41 | static struct p9_trans_module p9_fd_trans; |
| 42 | |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 43 | /** |
| 44 | * struct p9_fd_opts - per-transport options |
| 45 | * @rfd: file descriptor for reading (trans=fd) |
| 46 | * @wfd: file descriptor for writing (trans=fd) |
| 47 | * @port: port to connect to (trans=tcp) |
| 48 | * |
| 49 | */ |
| 50 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 51 | struct p9_fd_opts { |
| 52 | int rfd; |
| 53 | int wfd; |
| 54 | u16 port; |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 55 | bool privport; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 56 | }; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 57 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 58 | /* |
| 59 | * Option Parsing (code inspired by NFS code) |
| 60 | * - a little lazy - parse all fd-transport options |
| 61 | */ |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 62 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 63 | enum { |
| 64 | /* Options that take integer arguments */ |
Latchesar Ionkov | 5576269 | 2007-11-06 08:02:53 -0600 | [diff] [blame] | 65 | Opt_port, Opt_rfdno, Opt_wfdno, Opt_err, |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 66 | /* Options that take no arguments */ |
| 67 | Opt_privport, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 68 | }; |
| 69 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 70 | static const match_table_t tokens = { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 71 | {Opt_port, "port=%u"}, |
| 72 | {Opt_rfdno, "rfdno=%u"}, |
| 73 | {Opt_wfdno, "wfdno=%u"}, |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 74 | {Opt_privport, "privport"}, |
Latchesar Ionkov | 5576269 | 2007-11-06 08:02:53 -0600 | [diff] [blame] | 75 | {Opt_err, NULL}, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 78 | enum { |
| 79 | Rworksched = 1, /* read work scheduled or running */ |
| 80 | Rpending = 2, /* can read */ |
| 81 | Wworksched = 4, /* write work scheduled or running */ |
| 82 | Wpending = 8, /* can write */ |
| 83 | }; |
| 84 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 85 | struct p9_poll_wait { |
| 86 | struct p9_conn *conn; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 87 | wait_queue_entry_t wait; |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 88 | wait_queue_head_t *wait_addr; |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /** |
| 92 | * struct p9_conn - fd mux connection state information |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 93 | * @mux_list: list link for mux to manage multiple connections (?) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 94 | * @client: reference to client instance for this connection |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 95 | * @err: error state |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 96 | * @req_list: accounting for requests which have been sent |
| 97 | * @unsent_req_list: accounting for requests that haven't been sent |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 98 | * @req: current request being processed (if any) |
| 99 | * @tmp_buf: temporary buffer to read in header |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 100 | * @rc: temporary fcall for reading current frame |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 101 | * @wpos: write position for current frame |
| 102 | * @wsize: amount of data to write for current frame |
| 103 | * @wbuf: current write buffer |
Abhishek Kulkarni | 0e15597 | 2009-07-19 13:41:55 -0600 | [diff] [blame] | 104 | * @poll_pending_link: pending links to be polled per conn |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 105 | * @poll_wait: array of wait_q's for various worker threads |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 106 | * @pt: poll state |
| 107 | * @rq: current read work |
| 108 | * @wq: current write work |
| 109 | * @wsched: ???? |
| 110 | * |
| 111 | */ |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 112 | |
| 113 | struct p9_conn { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 114 | struct list_head mux_list; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 115 | struct p9_client *client; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 116 | int err; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 117 | struct list_head req_list; |
| 118 | struct list_head unsent_req_list; |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 119 | struct p9_req_t *rreq; |
Tomas Bortoli | 728356d | 2018-08-14 19:43:42 +0200 | [diff] [blame] | 120 | struct p9_req_t *wreq; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 121 | char tmp_buf[7]; |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 122 | struct p9_fcall rc; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 123 | int wpos; |
| 124 | int wsize; |
| 125 | char *wbuf; |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 126 | struct list_head poll_pending_link; |
| 127 | struct p9_poll_wait poll_wait[MAXPOLLWADDR]; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 128 | poll_table pt; |
| 129 | struct work_struct rq; |
| 130 | struct work_struct wq; |
| 131 | unsigned long wsched; |
| 132 | }; |
| 133 | |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 134 | /** |
| 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 |
| 139 | * |
| 140 | */ |
| 141 | |
| 142 | struct p9_trans_fd { |
| 143 | struct file *rd; |
| 144 | struct file *wr; |
| 145 | struct p9_conn conn; |
| 146 | }; |
| 147 | |
Tejun Heo | aa70c58 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 148 | static void p9_poll_workfn(struct work_struct *work); |
| 149 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 150 | static DEFINE_SPINLOCK(p9_poll_lock); |
| 151 | static LIST_HEAD(p9_poll_pending_list); |
Tejun Heo | aa70c58 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 152 | static DECLARE_WORK(p9_poll_work, p9_poll_workfn); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 153 | |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 154 | static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT; |
| 155 | static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT; |
| 156 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 157 | static void p9_mux_poll_stop(struct p9_conn *m) |
| 158 | { |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 159 | unsigned long flags; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 160 | int i; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 161 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 162 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 163 | struct p9_poll_wait *pwait = &m->poll_wait[i]; |
| 164 | |
| 165 | if (pwait->wait_addr) { |
| 166 | remove_wait_queue(pwait->wait_addr, &pwait->wait); |
| 167 | pwait->wait_addr = NULL; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 168 | } |
| 169 | } |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 170 | |
| 171 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 172 | list_del_init(&m->poll_pending_link); |
| 173 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
Tomas Bortoli | 430ac66 | 2018-07-20 11:27:30 +0200 | [diff] [blame] | 174 | |
| 175 | flush_work(&p9_poll_work); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /** |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 179 | * p9_conn_cancel - cancel all pending requests with error |
| 180 | * @m: mux data |
| 181 | * @err: error code |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 182 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 183 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 184 | |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 185 | static void p9_conn_cancel(struct p9_conn *m, int err) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 186 | { |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 187 | struct p9_req_t *req, *rtmp; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 188 | LIST_HEAD(cancel_list); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 189 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 190 | p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err); |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 191 | |
Tomas Bortoli | 9f476d7 | 2018-07-23 20:42:53 +0200 | [diff] [blame] | 192 | spin_lock(&m->client->lock); |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 193 | |
| 194 | if (m->err) { |
Tomas Bortoli | 9f476d7 | 2018-07-23 20:42:53 +0200 | [diff] [blame] | 195 | spin_unlock(&m->client->lock); |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | |
| 199 | m->err = err; |
| 200 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 201 | list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) { |
| 202 | list_move(&req->req_list, &cancel_list); |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 203 | } |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 204 | list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) { |
| 205 | list_move(&req->req_list, &cancel_list); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 206 | } |
| 207 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 208 | list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 209 | p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req); |
Latchesar Ionkov | 1bab88b | 2009-04-05 16:28:59 -0500 | [diff] [blame] | 210 | list_del(&req->req_list); |
Dominique Martinet | 2b6e72e | 2014-01-17 18:31:00 +0100 | [diff] [blame] | 211 | if (!req->t_err) |
| 212 | req->t_err = err; |
| 213 | p9_client_cb(m->client, req, REQ_STATUS_ERROR); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 214 | } |
Tomas Bortoli | 9f476d7 | 2018-07-23 20:42:53 +0200 | [diff] [blame] | 215 | spin_unlock(&m->client->lock); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 216 | } |
| 217 | |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 218 | static __poll_t |
| 219 | p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt, int *err) |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 220 | { |
Christoph Hellwig | 9965ed17 | 2018-03-05 07:26:05 -0800 | [diff] [blame] | 221 | __poll_t ret; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 222 | struct p9_trans_fd *ts = NULL; |
| 223 | |
| 224 | if (client && client->status == Connected) |
| 225 | ts = client->trans; |
| 226 | |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 227 | if (!ts) { |
| 228 | if (err) |
| 229 | *err = -EREMOTEIO; |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 230 | return EPOLLERR; |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 231 | } |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 232 | |
Christoph Hellwig | 9965ed17 | 2018-03-05 07:26:05 -0800 | [diff] [blame] | 233 | ret = vfs_poll(ts->rd, pt); |
| 234 | if (ts->rd != ts->wr) |
| 235 | ret = (ret & ~EPOLLOUT) | (vfs_poll(ts->wr, pt) & ~EPOLLIN); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | /** |
| 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 |
| 244 | * |
| 245 | */ |
| 246 | |
| 247 | static int p9_fd_read(struct p9_client *client, void *v, int len) |
| 248 | { |
| 249 | int ret; |
| 250 | struct p9_trans_fd *ts = NULL; |
Christoph Hellwig | bdd1d2d | 2017-09-01 17:39:13 +0200 | [diff] [blame] | 251 | loff_t pos; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 252 | |
| 253 | if (client && client->status != Disconnected) |
| 254 | ts = client->trans; |
| 255 | |
| 256 | if (!ts) |
| 257 | return -EREMOTEIO; |
| 258 | |
| 259 | if (!(ts->rd->f_flags & O_NONBLOCK)) |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 260 | p9_debug(P9_DEBUG_ERROR, "blocking read ...\n"); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 261 | |
Christoph Hellwig | bdd1d2d | 2017-09-01 17:39:13 +0200 | [diff] [blame] | 262 | pos = ts->rd->f_pos; |
| 263 | ret = kernel_read(ts->rd, v, len, &pos); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 264 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
| 265 | client->status = Disconnected; |
| 266 | return ret; |
| 267 | } |
| 268 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 269 | /** |
| 270 | * p9_read_work - called when there is some data to be read from a transport |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 271 | * @work: container of work to be done |
| 272 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 273 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 274 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 275 | static void p9_read_work(struct work_struct *work) |
| 276 | { |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 277 | __poll_t n; |
| 278 | int err; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 279 | struct p9_conn *m; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 280 | |
| 281 | m = container_of(work, struct p9_conn, rq); |
| 282 | |
| 283 | if (m->err < 0) |
| 284 | return; |
| 285 | |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 286 | p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 287 | |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 288 | if (!m->rc.sdata) { |
| 289 | m->rc.sdata = m->tmp_buf; |
| 290 | m->rc.offset = 0; |
| 291 | m->rc.capacity = 7; /* start by reading header */ |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | clear_bit(Rpending, &m->wsched); |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 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); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 300 | p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err); |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 301 | if (err == -EAGAIN) |
Simon Derr | 0462194 | 2012-09-17 15:16:28 +0200 | [diff] [blame] | 302 | goto end_clear; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 303 | |
| 304 | if (err <= 0) |
| 305 | goto error; |
| 306 | |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 307 | m->rc.offset += err; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 308 | |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 309 | /* header read in */ |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 310 | if ((!m->rreq) && (m->rc.offset == m->rc.capacity)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 311 | p9_debug(P9_DEBUG_TRANS, "got new header\n"); |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 312 | |
Tomas Bortoli | f984579 | 2018-07-23 17:44:04 +0200 | [diff] [blame] | 313 | /* Header size */ |
| 314 | m->rc.size = 7; |
| 315 | err = p9_parse_header(&m->rc, &m->rc.size, NULL, NULL, 0); |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 316 | if (err) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 317 | p9_debug(P9_DEBUG_ERROR, |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 318 | "error parsing header: %d\n", err); |
| 319 | goto error; |
| 320 | } |
| 321 | |
| 322 | if (m->rc.size >= m->client->msize) { |
| 323 | p9_debug(P9_DEBUG_ERROR, |
| 324 | "requested packet size too big: %d\n", |
| 325 | m->rc.size); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 326 | err = -EIO; |
| 327 | goto error; |
| 328 | } |
| 329 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 330 | p9_debug(P9_DEBUG_TRANS, |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 331 | "mux %p pkt: size: %d bytes tag: %d\n", |
| 332 | m, m->rc.size, m->rc.tag); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 333 | |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 334 | m->rreq = p9_tag_lookup(m->client, m->rc.tag); |
| 335 | if (!m->rreq || (m->rreq->status != REQ_STATUS_SENT)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 336 | p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n", |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 337 | m->rc.tag); |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 338 | err = -EIO; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 339 | goto error; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 340 | } |
| 341 | |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 342 | if (!m->rreq->rc.sdata) { |
Dominique Martinet | 3053600 | 2015-09-07 17:06:54 +0200 | [diff] [blame] | 343 | p9_debug(P9_DEBUG_ERROR, |
| 344 | "No recv fcall for tag %d (req %p), disconnecting!\n", |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 345 | m->rc.tag, m->rreq); |
| 346 | m->rreq = NULL; |
Dominique Martinet | 3053600 | 2015-09-07 17:06:54 +0200 | [diff] [blame] | 347 | err = -EIO; |
| 348 | goto error; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 349 | } |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 350 | m->rc.sdata = m->rreq->rc.sdata; |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 351 | memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity); |
| 352 | m->rc.capacity = m->rc.size; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 355 | /* packet is read in |
| 356 | * not an else because some packets (like clunk) have no payload |
| 357 | */ |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 358 | if ((m->rreq) && (m->rc.offset == m->rc.capacity)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 359 | p9_debug(P9_DEBUG_TRANS, "got new packet\n"); |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 360 | m->rreq->rc.size = m->rc.offset; |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 361 | spin_lock(&m->client->lock); |
Dominique Martinet | e4ca13f | 2018-10-09 11:18:52 +0900 | [diff] [blame] | 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); |
Wang Hai | 74d6a5d | 2020-06-12 17:08:33 +0800 | [diff] [blame] | 365 | } else if (m->rreq->status == REQ_STATUS_FLSHD) { |
| 366 | /* Ignore replies associated with a cancelled request. */ |
| 367 | p9_debug(P9_DEBUG_TRANS, |
| 368 | "Ignore replies associated with a cancelled request\n"); |
Dominique Martinet | e4ca13f | 2018-10-09 11:18:52 +0900 | [diff] [blame] | 369 | } else { |
| 370 | spin_unlock(&m->client->lock); |
| 371 | p9_debug(P9_DEBUG_ERROR, |
| 372 | "Request tag %d errored out while we were reading the reply\n", |
| 373 | m->rc.tag); |
| 374 | err = -EIO; |
| 375 | goto error; |
| 376 | } |
Tomas Bortoli | 9f476d7 | 2018-07-23 20:42:53 +0200 | [diff] [blame] | 377 | spin_unlock(&m->client->lock); |
Dominique Martinet | 947867a | 2015-09-03 11:28:20 +0200 | [diff] [blame] | 378 | m->rc.sdata = NULL; |
| 379 | m->rc.offset = 0; |
| 380 | m->rc.capacity = 0; |
Tomas Bortoli | 6d35190 | 2018-09-03 18:03:21 +0200 | [diff] [blame] | 381 | p9_req_put(m->rreq); |
| 382 | m->rreq = NULL; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 383 | } |
| 384 | |
Simon Derr | 0462194 | 2012-09-17 15:16:28 +0200 | [diff] [blame] | 385 | end_clear: |
| 386 | clear_bit(Rworksched, &m->wsched); |
| 387 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 388 | if (!list_empty(&m->req_list)) { |
| 389 | if (test_and_clear_bit(Rpending, &m->wsched)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 390 | n = EPOLLIN; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 391 | else |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 392 | n = p9_fd_poll(m->client, NULL, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 393 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 394 | if ((n & EPOLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 395 | p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m); |
Tejun Heo | 61edeee | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 396 | schedule_work(&m->rq); |
Simon Derr | 0462194 | 2012-09-17 15:16:28 +0200 | [diff] [blame] | 397 | } |
| 398 | } |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 399 | |
| 400 | return; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 401 | error: |
| 402 | p9_conn_cancel(m, err); |
| 403 | clear_bit(Rworksched, &m->wsched); |
| 404 | } |
| 405 | |
| 406 | /** |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 407 | * p9_fd_write - write to a socket |
| 408 | * @client: client instance |
| 409 | * @v: buffer to send data from |
| 410 | * @len: size of send buffer |
| 411 | * |
| 412 | */ |
| 413 | |
| 414 | static int p9_fd_write(struct p9_client *client, void *v, int len) |
| 415 | { |
Christoph Hellwig | 670986e | 2017-09-01 17:39:18 +0200 | [diff] [blame] | 416 | ssize_t ret; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 417 | struct p9_trans_fd *ts = NULL; |
| 418 | |
| 419 | if (client && client->status != Disconnected) |
| 420 | ts = client->trans; |
| 421 | |
| 422 | if (!ts) |
| 423 | return -EREMOTEIO; |
| 424 | |
| 425 | if (!(ts->wr->f_flags & O_NONBLOCK)) |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 426 | p9_debug(P9_DEBUG_ERROR, "blocking write ...\n"); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 427 | |
Christoph Hellwig | 670986e | 2017-09-01 17:39:18 +0200 | [diff] [blame] | 428 | ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 429 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
| 430 | client->status = Disconnected; |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * p9_write_work - called when a transport can send some data |
| 436 | * @work: container for work to be done |
| 437 | * |
| 438 | */ |
| 439 | |
| 440 | static void p9_write_work(struct work_struct *work) |
| 441 | { |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 442 | __poll_t n; |
| 443 | int err; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 444 | struct p9_conn *m; |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 445 | struct p9_req_t *req; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 446 | |
| 447 | m = container_of(work, struct p9_conn, wq); |
| 448 | |
| 449 | if (m->err < 0) { |
| 450 | clear_bit(Wworksched, &m->wsched); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | if (!m->wsize) { |
Simon Derr | 759f429 | 2012-09-17 15:16:31 +0200 | [diff] [blame] | 455 | spin_lock(&m->client->lock); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 456 | if (list_empty(&m->unsent_req_list)) { |
| 457 | clear_bit(Wworksched, &m->wsched); |
Simon Derr | 759f429 | 2012-09-17 15:16:31 +0200 | [diff] [blame] | 458 | spin_unlock(&m->client->lock); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 462 | req = list_entry(m->unsent_req_list.next, struct p9_req_t, |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 463 | req_list); |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 464 | req->status = REQ_STATUS_SENT; |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 465 | p9_debug(P9_DEBUG_TRANS, "move req %p\n", req); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 466 | list_move_tail(&req->req_list, &m->req_list); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 467 | |
Dominique Martinet | 523adb6 | 2018-07-30 05:55:19 +0000 | [diff] [blame] | 468 | m->wbuf = req->tc.sdata; |
| 469 | m->wsize = req->tc.size; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 470 | m->wpos = 0; |
Tomas Bortoli | 728356d | 2018-08-14 19:43:42 +0200 | [diff] [blame] | 471 | p9_req_get(req); |
| 472 | m->wreq = req; |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 473 | spin_unlock(&m->client->lock); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 476 | p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n", |
| 477 | m, m->wpos, m->wsize); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 478 | clear_bit(Wpending, &m->wsched); |
| 479 | err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 480 | p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err); |
Simon Derr | 584a8c1 | 2012-09-17 15:16:30 +0200 | [diff] [blame] | 481 | if (err == -EAGAIN) |
| 482 | goto end_clear; |
| 483 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 484 | |
| 485 | if (err < 0) |
| 486 | goto error; |
| 487 | else if (err == 0) { |
| 488 | err = -EREMOTEIO; |
| 489 | goto error; |
| 490 | } |
| 491 | |
| 492 | m->wpos += err; |
Tomas Bortoli | 728356d | 2018-08-14 19:43:42 +0200 | [diff] [blame] | 493 | if (m->wpos == m->wsize) { |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 494 | m->wpos = m->wsize = 0; |
Tomas Bortoli | 728356d | 2018-08-14 19:43:42 +0200 | [diff] [blame] | 495 | p9_req_put(m->wreq); |
| 496 | m->wreq = NULL; |
| 497 | } |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 498 | |
Simon Derr | 584a8c1 | 2012-09-17 15:16:30 +0200 | [diff] [blame] | 499 | end_clear: |
| 500 | clear_bit(Wworksched, &m->wsched); |
| 501 | |
Simon Derr | 1957b3a | 2012-09-17 15:16:29 +0200 | [diff] [blame] | 502 | if (m->wsize || !list_empty(&m->unsent_req_list)) { |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 503 | if (test_and_clear_bit(Wpending, &m->wsched)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 504 | n = EPOLLOUT; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 505 | else |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 506 | n = p9_fd_poll(m->client, NULL, NULL); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 507 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 508 | if ((n & EPOLLOUT) && |
Simon Derr | 584a8c1 | 2012-09-17 15:16:30 +0200 | [diff] [blame] | 509 | !test_and_set_bit(Wworksched, &m->wsched)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 510 | p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m); |
Tejun Heo | 61edeee | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 511 | schedule_work(&m->wq); |
Simon Derr | 584a8c1 | 2012-09-17 15:16:30 +0200 | [diff] [blame] | 512 | } |
| 513 | } |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 514 | |
| 515 | return; |
| 516 | |
| 517 | error: |
| 518 | p9_conn_cancel(m, err); |
| 519 | clear_bit(Wworksched, &m->wsched); |
| 520 | } |
| 521 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 522 | static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key) |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 523 | { |
| 524 | struct p9_poll_wait *pwait = |
| 525 | container_of(wait, struct p9_poll_wait, wait); |
| 526 | struct p9_conn *m = pwait->conn; |
| 527 | unsigned long flags; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 528 | |
| 529 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 530 | if (list_empty(&m->poll_pending_link)) |
| 531 | list_add_tail(&m->poll_pending_link, &p9_poll_pending_list); |
| 532 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
| 533 | |
Tejun Heo | aa70c58 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 534 | schedule_work(&p9_poll_work); |
| 535 | return 1; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /** |
| 539 | * p9_pollwait - add poll task to the wait queue |
| 540 | * @filp: file pointer being polled |
| 541 | * @wait_address: wait_q to block on |
| 542 | * @p: poll state |
| 543 | * |
| 544 | * called by files poll operation to add v9fs-poll task to files wait queue |
| 545 | */ |
| 546 | |
| 547 | static void |
| 548 | p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p) |
| 549 | { |
| 550 | struct p9_conn *m = container_of(p, struct p9_conn, pt); |
| 551 | struct p9_poll_wait *pwait = NULL; |
| 552 | int i; |
| 553 | |
| 554 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 555 | if (m->poll_wait[i].wait_addr == NULL) { |
| 556 | pwait = &m->poll_wait[i]; |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (!pwait) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 562 | p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n"); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 563 | return; |
| 564 | } |
| 565 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 566 | pwait->conn = m; |
| 567 | pwait->wait_addr = wait_address; |
| 568 | init_waitqueue_func_entry(&pwait->wait, p9_pollwake); |
| 569 | add_wait_queue(wait_address, &pwait->wait); |
| 570 | } |
| 571 | |
| 572 | /** |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 573 | * p9_conn_create - initialize the per-session mux data |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 574 | * @client: client instance |
| 575 | * |
| 576 | * Note: Creates the polling task if this is the first session. |
| 577 | */ |
| 578 | |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 579 | static void p9_conn_create(struct p9_client *client) |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 580 | { |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 581 | __poll_t n; |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 582 | struct p9_trans_fd *ts = client->trans; |
| 583 | struct p9_conn *m = &ts->conn; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 584 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 585 | p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 586 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 587 | INIT_LIST_HEAD(&m->mux_list); |
| 588 | m->client = client; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 589 | |
| 590 | INIT_LIST_HEAD(&m->req_list); |
| 591 | INIT_LIST_HEAD(&m->unsent_req_list); |
| 592 | INIT_WORK(&m->rq, p9_read_work); |
| 593 | INIT_WORK(&m->wq, p9_write_work); |
| 594 | INIT_LIST_HEAD(&m->poll_pending_link); |
| 595 | init_poll_funcptr(&m->pt, p9_pollwait); |
| 596 | |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 597 | n = p9_fd_poll(client, &m->pt, NULL); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 598 | if (n & EPOLLIN) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 599 | p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 600 | set_bit(Rpending, &m->wsched); |
| 601 | } |
| 602 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 603 | if (n & EPOLLOUT) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 604 | p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 605 | set_bit(Wpending, &m->wsched); |
| 606 | } |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /** |
| 610 | * p9_poll_mux - polls a mux and schedules read or write works if necessary |
| 611 | * @m: connection to poll |
| 612 | * |
| 613 | */ |
| 614 | |
| 615 | static void p9_poll_mux(struct p9_conn *m) |
| 616 | { |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 617 | __poll_t n; |
| 618 | int err = -ECONNRESET; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 619 | |
| 620 | if (m->err < 0) |
| 621 | return; |
| 622 | |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 623 | n = p9_fd_poll(m->client, NULL, &err); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 624 | if (n & (EPOLLERR | EPOLLHUP | EPOLLNVAL)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 625 | p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n); |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 626 | p9_conn_cancel(m, err); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 627 | } |
| 628 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 629 | if (n & EPOLLIN) { |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 630 | set_bit(Rpending, &m->wsched); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 631 | p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 632 | if (!test_and_set_bit(Rworksched, &m->wsched)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 633 | p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m); |
Tejun Heo | 61edeee | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 634 | schedule_work(&m->rq); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 638 | if (n & EPOLLOUT) { |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 639 | set_bit(Wpending, &m->wsched); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 640 | p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m); |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 641 | if ((m->wsize || !list_empty(&m->unsent_req_list)) && |
| 642 | !test_and_set_bit(Wworksched, &m->wsched)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 643 | p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m); |
Tejun Heo | 61edeee | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 644 | schedule_work(&m->wq); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /** |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 650 | * p9_fd_request - send 9P request |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 651 | * The function can sleep until the request is scheduled for sending. |
| 652 | * The function can be interrupted. Return from the function is not |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 653 | * a guarantee that the request is sent successfully. |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 654 | * |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 655 | * @client: client instance |
| 656 | * @req: request to be sent |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 657 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 658 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 659 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 660 | static int p9_fd_request(struct p9_client *client, struct p9_req_t *req) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 661 | { |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 662 | __poll_t n; |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 663 | struct p9_trans_fd *ts = client->trans; |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 664 | struct p9_conn *m = &ts->conn; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 665 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 666 | p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n", |
Dominique Martinet | 523adb6 | 2018-07-30 05:55:19 +0000 | [diff] [blame] | 667 | m, current, &req->tc, req->tc.id); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 668 | if (m->err < 0) |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 669 | return m->err; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 670 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 671 | spin_lock(&client->lock); |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 672 | req->status = REQ_STATUS_UNSENT; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 673 | list_add_tail(&req->req_list, &m->unsent_req_list); |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 674 | spin_unlock(&client->lock); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 675 | |
| 676 | if (test_and_clear_bit(Wpending, &m->wsched)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 677 | n = EPOLLOUT; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 678 | else |
Al Viro | 7594bf3 | 2017-07-16 22:53:08 -0400 | [diff] [blame] | 679 | n = p9_fd_poll(m->client, NULL, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 680 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 681 | if (n & EPOLLOUT && !test_and_set_bit(Wworksched, &m->wsched)) |
Tejun Heo | 61edeee | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 682 | schedule_work(&m->wq); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 683 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 684 | return 0; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 685 | } |
| 686 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 687 | static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 688 | { |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 689 | int ret = 1; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 690 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 691 | p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 692 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 693 | spin_lock(&client->lock); |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 694 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 695 | if (req->status == REQ_STATUS_UNSENT) { |
Latchesar Ionkov | 1bab88b | 2009-04-05 16:28:59 -0500 | [diff] [blame] | 696 | list_del(&req->req_list); |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 697 | req->status = REQ_STATUS_FLSHD; |
Tomas Bortoli | 728356d | 2018-08-14 19:43:42 +0200 | [diff] [blame] | 698 | p9_req_put(req); |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 699 | ret = 0; |
Simon Derr | 0bfd684 | 2014-03-10 16:38:52 +0100 | [diff] [blame] | 700 | } |
Eric Van Hensbergen | 7eb923b | 2008-10-17 12:45:40 -0500 | [diff] [blame] | 701 | spin_unlock(&client->lock); |
| 702 | |
| 703 | return ret; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 704 | } |
| 705 | |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 706 | static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req) |
| 707 | { |
| 708 | p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req); |
| 709 | |
Wang Hai | 74d6a5d | 2020-06-12 17:08:33 +0800 | [diff] [blame] | 710 | spin_lock(&client->lock); |
| 711 | /* Ignore cancelled request if message has been received |
| 712 | * before lock. |
| 713 | */ |
| 714 | if (req->status == REQ_STATUS_RCVD) { |
| 715 | spin_unlock(&client->lock); |
| 716 | return 0; |
| 717 | } |
| 718 | |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 719 | /* we haven't received a response for oldreq, |
| 720 | * remove it from the list. |
| 721 | */ |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 722 | list_del(&req->req_list); |
Wang Hai | 74d6a5d | 2020-06-12 17:08:33 +0800 | [diff] [blame] | 723 | req->status = REQ_STATUS_FLSHD; |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 724 | spin_unlock(&client->lock); |
Tomas Bortoli | 728356d | 2018-08-14 19:43:42 +0200 | [diff] [blame] | 725 | p9_req_put(req); |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 730 | static int p9_fd_show_options(struct seq_file *m, struct p9_client *clnt) |
| 731 | { |
| 732 | if (clnt->trans_mod == &p9_tcp_trans) { |
| 733 | if (clnt->trans_opts.tcp.port != P9_PORT) |
Tuomas Tynkkynen | 61b272c | 2017-11-19 11:28:43 +0200 | [diff] [blame] | 734 | seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port); |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 735 | } else if (clnt->trans_mod == &p9_fd_trans) { |
| 736 | if (clnt->trans_opts.fd.rfd != ~0) |
Tuomas Tynkkynen | 61b272c | 2017-11-19 11:28:43 +0200 | [diff] [blame] | 737 | seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd); |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 738 | if (clnt->trans_opts.fd.wfd != ~0) |
Tuomas Tynkkynen | 61b272c | 2017-11-19 11:28:43 +0200 | [diff] [blame] | 739 | seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd); |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 740 | } |
| 741 | return 0; |
| 742 | } |
| 743 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 744 | /** |
Abhishek Kulkarni | 0e15597 | 2009-07-19 13:41:55 -0600 | [diff] [blame] | 745 | * parse_opts - parse mount options into p9_fd_opts structure |
| 746 | * @params: options string passed from mount |
| 747 | * @opts: fd transport-specific structure to parse options into |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 748 | * |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 749 | * Returns 0 upon success, -ERRNO upon failure |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 750 | */ |
| 751 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 752 | static int parse_opts(char *params, struct p9_fd_opts *opts) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 753 | { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 754 | char *p; |
| 755 | substring_t args[MAX_OPT_ARGS]; |
| 756 | int option; |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 757 | char *options, *tmp_options; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 758 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 759 | opts->port = P9_PORT; |
| 760 | opts->rfd = ~0; |
| 761 | opts->wfd = ~0; |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 762 | opts->privport = false; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 763 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 764 | if (!params) |
| 765 | return 0; |
| 766 | |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 767 | tmp_options = kstrdup(params, GFP_KERNEL); |
| 768 | if (!tmp_options) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 769 | p9_debug(P9_DEBUG_ERROR, |
| 770 | "failed to allocate copy of option string\n"); |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 771 | return -ENOMEM; |
| 772 | } |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 773 | options = tmp_options; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 774 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 775 | while ((p = strsep(&options, ",")) != NULL) { |
| 776 | int token; |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 777 | int r; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 778 | if (!*p) |
| 779 | continue; |
| 780 | token = match_token(p, tokens, args); |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 781 | if ((token != Opt_err) && (token != Opt_privport)) { |
Abhishek Kulkarni | 15da4b1 | 2009-07-01 08:50:54 +0000 | [diff] [blame] | 782 | r = match_int(&args[0], &option); |
| 783 | if (r < 0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 784 | p9_debug(P9_DEBUG_ERROR, |
| 785 | "integer field, but no integer?\n"); |
Abhishek Kulkarni | 15da4b1 | 2009-07-01 08:50:54 +0000 | [diff] [blame] | 786 | continue; |
| 787 | } |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 788 | } |
| 789 | switch (token) { |
| 790 | case Opt_port: |
| 791 | opts->port = option; |
| 792 | break; |
| 793 | case Opt_rfdno: |
| 794 | opts->rfd = option; |
| 795 | break; |
| 796 | case Opt_wfdno: |
| 797 | opts->wfd = option; |
| 798 | break; |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 799 | case Opt_privport: |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 800 | opts->privport = true; |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 801 | break; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 802 | default: |
| 803 | continue; |
| 804 | } |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 805 | } |
Eric Van Hensbergen | d8c8a9e | 2010-02-08 16:23:23 -0600 | [diff] [blame] | 806 | |
| 807 | kfree(tmp_options); |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 808 | return 0; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 809 | } |
| 810 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 811 | static int p9_fd_open(struct p9_client *client, int rfd, int wfd) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 812 | { |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 813 | struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd), |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 814 | GFP_KERNEL); |
| 815 | if (!ts) |
| 816 | return -ENOMEM; |
| 817 | |
| 818 | ts->rd = fget(rfd); |
Christoph Hellwig | a39c460 | 2020-07-10 10:57:22 +0200 | [diff] [blame] | 819 | if (!ts->rd) |
| 820 | goto out_free_ts; |
| 821 | if (!(ts->rd->f_mode & FMODE_READ)) |
| 822 | goto out_put_rd; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 823 | ts->wr = fget(wfd); |
Christoph Hellwig | a39c460 | 2020-07-10 10:57:22 +0200 | [diff] [blame] | 824 | if (!ts->wr) |
| 825 | goto out_put_rd; |
| 826 | if (!(ts->wr->f_mode & FMODE_WRITE)) |
| 827 | goto out_put_wr; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 828 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 829 | client->trans = ts; |
| 830 | client->status = Connected; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 831 | |
| 832 | return 0; |
Christoph Hellwig | a39c460 | 2020-07-10 10:57:22 +0200 | [diff] [blame] | 833 | |
| 834 | out_put_wr: |
| 835 | fput(ts->wr); |
| 836 | out_put_rd: |
| 837 | fput(ts->rd); |
| 838 | out_free_ts: |
| 839 | kfree(ts); |
| 840 | return -EIO; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 841 | } |
| 842 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 843 | static int p9_socket_open(struct p9_client *client, struct socket *csocket) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 844 | { |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 845 | struct p9_trans_fd *p; |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 846 | struct file *file; |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 847 | |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 848 | p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 849 | if (!p) |
| 850 | return -ENOMEM; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 851 | |
| 852 | csocket->sk->sk_allocation = GFP_NOIO; |
Linus Torvalds | aab174f | 2012-10-02 20:25:04 -0700 | [diff] [blame] | 853 | file = sock_alloc_file(csocket, 0, NULL); |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 854 | if (IS_ERR(file)) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 855 | pr_err("%s (%d): failed to map fd\n", |
| 856 | __func__, task_pid_nr(current)); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 857 | kfree(p); |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 858 | return PTR_ERR(file); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 859 | } |
| 860 | |
Al Viro | 56b31d1 | 2012-08-18 00:25:51 -0400 | [diff] [blame] | 861 | get_file(file); |
| 862 | p->wr = p->rd = file; |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 863 | client->trans = p; |
| 864 | client->status = Connected; |
| 865 | |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 866 | p->rd->f_flags |= O_NONBLOCK; |
| 867 | |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 868 | p9_conn_create(client); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 872 | /** |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 873 | * p9_mux_destroy - cancels all pending requests of mux |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 874 | * @m: mux to destroy |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 875 | * |
| 876 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 877 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 878 | static void p9_conn_destroy(struct p9_conn *m) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 879 | { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 880 | p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n", |
| 881 | m, m->mux_list.prev, m->mux_list.next); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 882 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 883 | p9_mux_poll_stop(m); |
| 884 | cancel_work_sync(&m->rq); |
Dominique Martinet | fb488fc | 2018-10-09 11:38:00 +0900 | [diff] [blame] | 885 | if (m->rreq) { |
| 886 | p9_req_put(m->rreq); |
| 887 | m->rreq = NULL; |
| 888 | } |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 889 | cancel_work_sync(&m->wq); |
Dominique Martinet | fb488fc | 2018-10-09 11:38:00 +0900 | [diff] [blame] | 890 | if (m->wreq) { |
| 891 | p9_req_put(m->wreq); |
| 892 | m->wreq = NULL; |
| 893 | } |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 894 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 895 | p9_conn_cancel(m, -ECONNRESET); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 896 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 897 | m->client = NULL; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /** |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 901 | * p9_fd_close - shutdown file descriptor transport |
| 902 | * @client: client instance |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 903 | * |
| 904 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 905 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 906 | static void p9_fd_close(struct p9_client *client) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 907 | { |
| 908 | struct p9_trans_fd *ts; |
| 909 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 910 | if (!client) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 911 | return; |
| 912 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 913 | ts = client->trans; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 914 | if (!ts) |
| 915 | return; |
| 916 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 917 | client->status = Disconnected; |
| 918 | |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 919 | p9_conn_destroy(&ts->conn); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 920 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 921 | if (ts->rd) |
| 922 | fput(ts->rd); |
| 923 | if (ts->wr) |
| 924 | fput(ts->wr); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 925 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 926 | kfree(ts); |
| 927 | } |
| 928 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 929 | /* |
| 930 | * stolen from NFS - maybe should be made a generic function? |
| 931 | */ |
| 932 | static inline int valid_ipaddr4(const char *buf) |
| 933 | { |
| 934 | int rc, count, in[4]; |
| 935 | |
| 936 | rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]); |
| 937 | if (rc != 4) |
| 938 | return -EINVAL; |
| 939 | for (count = 0; count < 4; count++) { |
| 940 | if (in[count] > 255) |
| 941 | return -EINVAL; |
| 942 | } |
| 943 | return 0; |
| 944 | } |
| 945 | |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 946 | static int p9_bind_privport(struct socket *sock) |
| 947 | { |
| 948 | struct sockaddr_in cl; |
| 949 | int port, err = -EINVAL; |
| 950 | |
| 951 | memset(&cl, 0, sizeof(cl)); |
| 952 | cl.sin_family = AF_INET; |
Alexander Kapshuk | 6db6ea7 | 2020-06-18 21:34:17 +0300 | [diff] [blame^] | 953 | cl.sin_addr.s_addr = htonl(INADDR_ANY); |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 954 | for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) { |
| 955 | cl.sin_port = htons((ushort)port); |
| 956 | err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl)); |
| 957 | if (err != -EADDRINUSE) |
| 958 | break; |
| 959 | } |
| 960 | return err; |
| 961 | } |
| 962 | |
| 963 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 964 | static int |
| 965 | p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 966 | { |
| 967 | int err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 968 | struct socket *csocket; |
| 969 | struct sockaddr_in sin_server; |
| 970 | struct p9_fd_opts opts; |
| 971 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 972 | err = parse_opts(args, &opts); |
| 973 | if (err < 0) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 974 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 975 | |
Tomas Bortoli | 10aa145 | 2018-07-27 13:05:58 +0200 | [diff] [blame] | 976 | if (addr == NULL || valid_ipaddr4(addr) < 0) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 977 | return -EINVAL; |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 978 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 979 | csocket = NULL; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 980 | |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 981 | client->trans_opts.tcp.port = opts.port; |
| 982 | client->trans_opts.tcp.privport = opts.privport; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 983 | sin_server.sin_family = AF_INET; |
| 984 | sin_server.sin_addr.s_addr = in_aton(addr); |
| 985 | sin_server.sin_port = htons(opts.port); |
Eric W. Biederman | 0c5c9fb | 2015-03-11 23:06:44 -0500 | [diff] [blame] | 986 | err = __sock_create(current->nsproxy->net_ns, PF_INET, |
Rob Landley | e75762f | 2011-05-23 21:46:56 -0500 | [diff] [blame] | 987 | SOCK_STREAM, IPPROTO_TCP, &csocket, 1); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 988 | if (err) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 989 | pr_err("%s (%d): problem creating socket\n", |
| 990 | __func__, task_pid_nr(current)); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 991 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 992 | } |
| 993 | |
Jim Garlick | 2f28c8b | 2013-05-29 12:15:07 -0700 | [diff] [blame] | 994 | if (opts.privport) { |
| 995 | err = p9_bind_privport(csocket); |
| 996 | if (err < 0) { |
| 997 | pr_err("%s (%d): problem binding to privport\n", |
| 998 | __func__, task_pid_nr(current)); |
| 999 | sock_release(csocket); |
| 1000 | return err; |
| 1001 | } |
| 1002 | } |
| 1003 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1004 | err = csocket->ops->connect(csocket, |
| 1005 | (struct sockaddr *)&sin_server, |
| 1006 | sizeof(struct sockaddr_in), 0); |
| 1007 | if (err < 0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 1008 | pr_err("%s (%d): problem connecting socket to %s\n", |
| 1009 | __func__, task_pid_nr(current), addr); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1010 | sock_release(csocket); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1011 | return err; |
| 1012 | } |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1013 | |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1014 | return p9_socket_open(client, csocket); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1017 | static int |
| 1018 | p9_fd_create_unix(struct p9_client *client, const char *addr, char *args) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1019 | { |
| 1020 | int err; |
| 1021 | struct socket *csocket; |
| 1022 | struct sockaddr_un sun_server; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1023 | |
| 1024 | csocket = NULL; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1025 | |
Tomas Bortoli | 10aa145 | 2018-07-27 13:05:58 +0200 | [diff] [blame] | 1026 | if (addr == NULL) |
| 1027 | return -EINVAL; |
| 1028 | |
Dan Carpenter | cff6b8a | 2010-07-10 11:51:54 +0200 | [diff] [blame] | 1029 | if (strlen(addr) >= UNIX_PATH_MAX) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 1030 | pr_err("%s (%d): address too long: %s\n", |
| 1031 | __func__, task_pid_nr(current), addr); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1032 | return -ENAMETOOLONG; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | sun_server.sun_family = PF_UNIX; |
| 1036 | strcpy(sun_server.sun_path, addr); |
Eric W. Biederman | 0c5c9fb | 2015-03-11 23:06:44 -0500 | [diff] [blame] | 1037 | err = __sock_create(current->nsproxy->net_ns, PF_UNIX, |
Rob Landley | e75762f | 2011-05-23 21:46:56 -0500 | [diff] [blame] | 1038 | SOCK_STREAM, 0, &csocket, 1); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1039 | if (err < 0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 1040 | pr_err("%s (%d): problem creating socket\n", |
| 1041 | __func__, task_pid_nr(current)); |
| 1042 | |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1043 | return err; |
| 1044 | } |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1045 | err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, |
| 1046 | sizeof(struct sockaddr_un) - 1, 0); |
| 1047 | if (err < 0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 1048 | pr_err("%s (%d): problem connecting socket: %s: %d\n", |
| 1049 | __func__, task_pid_nr(current), addr, err); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1050 | sock_release(csocket); |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1051 | return err; |
| 1052 | } |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1053 | |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1054 | return p9_socket_open(client, csocket); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1055 | } |
| 1056 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1057 | static int |
| 1058 | p9_fd_create(struct p9_client *client, const char *addr, char *args) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1059 | { |
| 1060 | int err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1061 | struct p9_fd_opts opts; |
| 1062 | |
| 1063 | parse_opts(args, &opts); |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 1064 | client->trans_opts.fd.rfd = opts.rfd; |
| 1065 | client->trans_opts.fd.wfd = opts.wfd; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1066 | |
| 1067 | if (opts.rfd == ~0 || opts.wfd == ~0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 1068 | pr_err("Insufficient options for proto=fd\n"); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1069 | return -ENOPROTOOPT; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1070 | } |
| 1071 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1072 | err = p9_fd_open(client, opts.rfd, opts.wfd); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1073 | if (err < 0) |
Al Viro | 6b18662 | 2009-08-06 02:02:43 +0400 | [diff] [blame] | 1074 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1075 | |
Simon Derr | 263c582 | 2014-03-11 10:13:09 +0100 | [diff] [blame] | 1076 | p9_conn_create(client); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1077 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1078 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static struct p9_trans_module p9_tcp_trans = { |
| 1082 | .name = "tcp", |
| 1083 | .maxsize = MAX_SOCK_BUF, |
Eric Van Hensbergen | f94741f | 2013-11-12 10:20:03 -0600 | [diff] [blame] | 1084 | .def = 0, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1085 | .create = p9_fd_create_tcp, |
| 1086 | .close = p9_fd_close, |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 1087 | .request = p9_fd_request, |
| 1088 | .cancel = p9_fd_cancel, |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 1089 | .cancelled = p9_fd_cancelled, |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 1090 | .show_options = p9_fd_show_options, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1091 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1092 | }; |
| 1093 | |
| 1094 | static struct p9_trans_module p9_unix_trans = { |
| 1095 | .name = "unix", |
| 1096 | .maxsize = MAX_SOCK_BUF, |
| 1097 | .def = 0, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1098 | .create = p9_fd_create_unix, |
| 1099 | .close = p9_fd_close, |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 1100 | .request = p9_fd_request, |
| 1101 | .cancel = p9_fd_cancel, |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 1102 | .cancelled = p9_fd_cancelled, |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 1103 | .show_options = p9_fd_show_options, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1104 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1105 | }; |
| 1106 | |
| 1107 | static struct p9_trans_module p9_fd_trans = { |
| 1108 | .name = "fd", |
| 1109 | .maxsize = MAX_SOCK_BUF, |
| 1110 | .def = 0, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1111 | .create = p9_fd_create, |
| 1112 | .close = p9_fd_close, |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame] | 1113 | .request = p9_fd_request, |
| 1114 | .cancel = p9_fd_cancel, |
Simon Derr | afd8d65 | 2014-03-10 16:38:49 +0100 | [diff] [blame] | 1115 | .cancelled = p9_fd_cancelled, |
David Howells | c4fac91 | 2017-07-05 16:25:37 +0100 | [diff] [blame] | 1116 | .show_options = p9_fd_show_options, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1117 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1118 | }; |
| 1119 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 1120 | /** |
Sun Lianwen | 4a026da | 2018-05-08 09:49:38 +0800 | [diff] [blame] | 1121 | * p9_poll_workfn - poll worker thread |
| 1122 | * @work: work queue |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 1123 | * |
| 1124 | * polls all v9fs transports for new events and queues the appropriate |
| 1125 | * work to the work queue |
| 1126 | * |
| 1127 | */ |
| 1128 | |
Tejun Heo | aa70c58 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 1129 | static void p9_poll_workfn(struct work_struct *work) |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 1130 | { |
| 1131 | unsigned long flags; |
| 1132 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 1133 | p9_debug(P9_DEBUG_TRANS, "start %p\n", current); |
Tejun Heo | aa70c58 | 2011-02-01 11:42:43 +0100 | [diff] [blame] | 1134 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 1135 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 1136 | while (!list_empty(&p9_poll_pending_list)) { |
| 1137 | struct p9_conn *conn = list_first_entry(&p9_poll_pending_list, |
| 1138 | struct p9_conn, |
| 1139 | poll_pending_link); |
| 1140 | list_del_init(&conn->poll_pending_link); |
| 1141 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
| 1142 | |
| 1143 | p9_poll_mux(conn); |
| 1144 | |
| 1145 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 1146 | } |
| 1147 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
| 1148 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 1149 | p9_debug(P9_DEBUG_TRANS, "finish\n"); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 1152 | int p9_trans_fd_init(void) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1153 | { |
| 1154 | v9fs_register_trans(&p9_tcp_trans); |
| 1155 | v9fs_register_trans(&p9_unix_trans); |
| 1156 | v9fs_register_trans(&p9_fd_trans); |
| 1157 | |
Andrew Morton | 3387b80 | 2008-03-28 14:15:57 -0700 | [diff] [blame] | 1158 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1159 | } |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1160 | |
| 1161 | void p9_trans_fd_exit(void) |
| 1162 | { |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 1163 | flush_work(&p9_poll_work); |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1164 | v9fs_unregister_trans(&p9_tcp_trans); |
| 1165 | v9fs_unregister_trans(&p9_unix_trans); |
| 1166 | v9fs_unregister_trans(&p9_fd_trans); |
| 1167 | } |