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