blob: cce41b20a709a2f4bc2dce037b099ebc3f359f41 [file] [log] [blame]
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001/*
2 * linux/fs/9p/trans_fd.c
3 *
4 * Fd transport layer. Includes deprecated socket layer.
5 *
6 * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
7 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06008 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05009 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to:
22 * Free Software Foundation
23 * 51 Franklin Street, Fifth Floor
24 * Boston, MA 02111-1301 USA
25 *
26 */
27
Joe Perches5d385152011-11-28 10:40:46 -080028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050030#include <linux/in.h>
31#include <linux/module.h>
32#include <linux/net.h>
33#include <linux/ipv6.h>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060034#include <linux/kthread.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050035#include <linux/errno.h>
36#include <linux/kernel.h>
37#include <linux/un.h>
38#include <linux/uaccess.h>
39#include <linux/inet.h>
40#include <linux/idr.h>
41#include <linux/file.h>
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050042#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
David Howellsc4fac912017-07-05 16:25:37 +010044#include <linux/seq_file.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050045#include <net/9p/9p.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050046#include <net/9p/client.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050047#include <net/9p/transport.h>
48
Al Viro6b186622009-08-06 02:02:43 +040049#include <linux/syscalls.h> /* killme */
50
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050051#define P9_PORT 564
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050052#define MAX_SOCK_BUF (64*1024)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060053#define MAXPOLLWADDR 2
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050054
David Howellsc4fac912017-07-05 16:25:37 +010055static struct p9_trans_module p9_tcp_trans;
56static struct p9_trans_module p9_fd_trans;
57
Eric Van Hensbergenee443992008-03-05 07:08:09 -060058/**
59 * struct p9_fd_opts - per-transport options
60 * @rfd: file descriptor for reading (trans=fd)
61 * @wfd: file descriptor for writing (trans=fd)
62 * @port: port to connect to (trans=tcp)
63 *
64 */
65
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050066struct p9_fd_opts {
67 int rfd;
68 int wfd;
69 u16 port;
David Howellsc4fac912017-07-05 16:25:37 +010070 bool privport;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050071};
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050072
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050073/*
74 * Option Parsing (code inspired by NFS code)
75 * - a little lazy - parse all fd-transport options
76 */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050077
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050078enum {
79 /* Options that take integer arguments */
Latchesar Ionkov55762692007-11-06 08:02:53 -060080 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
Jim Garlick2f28c8b2013-05-29 12:15:07 -070081 /* Options that take no arguments */
82 Opt_privport,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050083};
84
Steven Whitehousea447c092008-10-13 10:46:57 +010085static const match_table_t tokens = {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050086 {Opt_port, "port=%u"},
87 {Opt_rfdno, "rfdno=%u"},
88 {Opt_wfdno, "wfdno=%u"},
Jim Garlick2f28c8b2013-05-29 12:15:07 -070089 {Opt_privport, "privport"},
Latchesar Ionkov55762692007-11-06 08:02:53 -060090 {Opt_err, NULL},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050091};
92
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060093enum {
94 Rworksched = 1, /* read work scheduled or running */
95 Rpending = 2, /* can read */
96 Wworksched = 4, /* write work scheduled or running */
97 Wpending = 8, /* can write */
98};
99
Tejun Heo992b3f12008-10-13 18:45:25 -0500100struct p9_poll_wait {
101 struct p9_conn *conn;
Ingo Molnarac6424b2017-06-20 12:06:13 +0200102 wait_queue_entry_t wait;
Tejun Heo992b3f12008-10-13 18:45:25 -0500103 wait_queue_head_t *wait_addr;
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600104};
105
106/**
107 * struct p9_conn - fd mux connection state information
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600108 * @mux_list: list link for mux to manage multiple connections (?)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500109 * @client: reference to client instance for this connection
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600110 * @err: error state
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600111 * @req_list: accounting for requests which have been sent
112 * @unsent_req_list: accounting for requests that haven't been sent
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500113 * @req: current request being processed (if any)
114 * @tmp_buf: temporary buffer to read in header
Dominique Martinet947867a2015-09-03 11:28:20 +0200115 * @rc: temporary fcall for reading current frame
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600116 * @wpos: write position for current frame
117 * @wsize: amount of data to write for current frame
118 * @wbuf: current write buffer
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600119 * @poll_pending_link: pending links to be polled per conn
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600120 * @poll_wait: array of wait_q's for various worker threads
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600121 * @pt: poll state
122 * @rq: current read work
123 * @wq: current write work
124 * @wsched: ????
125 *
126 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600127
128struct p9_conn {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600129 struct list_head mux_list;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500130 struct p9_client *client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600131 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600132 struct list_head req_list;
133 struct list_head unsent_req_list;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500134 struct p9_req_t *req;
135 char tmp_buf[7];
Dominique Martinet947867a2015-09-03 11:28:20 +0200136 struct p9_fcall rc;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600137 int wpos;
138 int wsize;
139 char *wbuf;
Tejun Heo992b3f12008-10-13 18:45:25 -0500140 struct list_head poll_pending_link;
141 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600142 poll_table pt;
143 struct work_struct rq;
144 struct work_struct wq;
145 unsigned long wsched;
146};
147
Simon Derr263c5822014-03-11 10:13:09 +0100148/**
149 * struct p9_trans_fd - transport state
150 * @rd: reference to file to read from
151 * @wr: reference of file to write to
152 * @conn: connection state reference
153 *
154 */
155
156struct p9_trans_fd {
157 struct file *rd;
158 struct file *wr;
159 struct p9_conn conn;
160};
161
Tejun Heoaa70c582011-02-01 11:42:43 +0100162static void p9_poll_workfn(struct work_struct *work);
163
Tejun Heo992b3f12008-10-13 18:45:25 -0500164static DEFINE_SPINLOCK(p9_poll_lock);
165static LIST_HEAD(p9_poll_pending_list);
Tejun Heoaa70c582011-02-01 11:42:43 +0100166static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600167
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700168static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
169static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
170
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600171static void p9_mux_poll_stop(struct p9_conn *m)
172{
Tejun Heo992b3f12008-10-13 18:45:25 -0500173 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600174 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600175
Tejun Heo992b3f12008-10-13 18:45:25 -0500176 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
177 struct p9_poll_wait *pwait = &m->poll_wait[i];
178
179 if (pwait->wait_addr) {
180 remove_wait_queue(pwait->wait_addr, &pwait->wait);
181 pwait->wait_addr = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600182 }
183 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500184
185 spin_lock_irqsave(&p9_poll_lock, flags);
186 list_del_init(&m->poll_pending_link);
187 spin_unlock_irqrestore(&p9_poll_lock, flags);
Tomas Bortoli430ac662018-07-20 11:27:30 +0200188
189 flush_work(&p9_poll_work);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600190}
191
192/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500193 * p9_conn_cancel - cancel all pending requests with error
194 * @m: mux data
195 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600196 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600197 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600198
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500199static void p9_conn_cancel(struct p9_conn *m, int err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600200{
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500201 struct p9_req_t *req, *rtmp;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500202 LIST_HEAD(cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600203
Joe Perches5d385152011-11-28 10:40:46 -0800204 p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500205
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200206 spin_lock(&m->client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500207
208 if (m->err) {
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200209 spin_unlock(&m->client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500210 return;
211 }
212
213 m->err = err;
214
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500215 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
216 list_move(&req->req_list, &cancel_list);
Tejun Heo992b3f12008-10-13 18:45:25 -0500217 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500218 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
219 list_move(&req->req_list, &cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600220 }
221
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500222 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
Joe Perches5d385152011-11-28 10:40:46 -0800223 p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500224 list_del(&req->req_list);
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100225 if (!req->t_err)
226 req->t_err = err;
227 p9_client_cb(m->client, req, REQ_STATUS_ERROR);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600228 }
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200229 spin_unlock(&m->client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600230}
231
Al Viro7594bf32017-07-16 22:53:08 -0400232static __poll_t
233p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt, int *err)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500234{
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800235 __poll_t ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500236 struct p9_trans_fd *ts = NULL;
237
238 if (client && client->status == Connected)
239 ts = client->trans;
240
Al Viro7594bf32017-07-16 22:53:08 -0400241 if (!ts) {
242 if (err)
243 *err = -EREMOTEIO;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800244 return EPOLLERR;
Al Viro7594bf32017-07-16 22:53:08 -0400245 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500246
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800247 ret = vfs_poll(ts->rd, pt);
248 if (ts->rd != ts->wr)
249 ret = (ret & ~EPOLLOUT) | (vfs_poll(ts->wr, pt) & ~EPOLLIN);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500250 return ret;
251}
252
253/**
254 * p9_fd_read- read from a fd
255 * @client: client instance
256 * @v: buffer to receive data into
257 * @len: size of receive buffer
258 *
259 */
260
261static int p9_fd_read(struct p9_client *client, void *v, int len)
262{
263 int ret;
264 struct p9_trans_fd *ts = NULL;
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200265 loff_t pos;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500266
267 if (client && client->status != Disconnected)
268 ts = client->trans;
269
270 if (!ts)
271 return -EREMOTEIO;
272
273 if (!(ts->rd->f_flags & O_NONBLOCK))
Joe Perches5d385152011-11-28 10:40:46 -0800274 p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500275
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200276 pos = ts->rd->f_pos;
277 ret = kernel_read(ts->rd, v, len, &pos);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500278 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
279 client->status = Disconnected;
280 return ret;
281}
282
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600283/**
284 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600285 * @work: container of work to be done
286 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600287 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600288
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600289static void p9_read_work(struct work_struct *work)
290{
Al Viro7594bf32017-07-16 22:53:08 -0400291 __poll_t n;
292 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600293 struct p9_conn *m;
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100294 int status = REQ_STATUS_ERROR;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600295
296 m = container_of(work, struct p9_conn, rq);
297
298 if (m->err < 0)
299 return;
300
Dominique Martinet947867a2015-09-03 11:28:20 +0200301 p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600302
Dominique Martinet947867a2015-09-03 11:28:20 +0200303 if (!m->rc.sdata) {
304 m->rc.sdata = m->tmp_buf;
305 m->rc.offset = 0;
306 m->rc.capacity = 7; /* start by reading header */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600307 }
308
309 clear_bit(Rpending, &m->wsched);
Dominique Martinet947867a2015-09-03 11:28:20 +0200310 p9_debug(P9_DEBUG_TRANS, "read mux %p pos %zd size: %zd = %zd\n",
311 m, m->rc.offset, m->rc.capacity,
312 m->rc.capacity - m->rc.offset);
313 err = p9_fd_read(m->client, m->rc.sdata + m->rc.offset,
314 m->rc.capacity - m->rc.offset);
Joe Perches5d385152011-11-28 10:40:46 -0800315 p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
Dominique Martinet947867a2015-09-03 11:28:20 +0200316 if (err == -EAGAIN)
Simon Derr04621942012-09-17 15:16:28 +0200317 goto end_clear;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600318
319 if (err <= 0)
320 goto error;
321
Dominique Martinet947867a2015-09-03 11:28:20 +0200322 m->rc.offset += err;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500323
Dominique Martinet947867a2015-09-03 11:28:20 +0200324 /* header read in */
325 if ((!m->req) && (m->rc.offset == m->rc.capacity)) {
Joe Perches5d385152011-11-28 10:40:46 -0800326 p9_debug(P9_DEBUG_TRANS, "got new header\n");
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500327
Dominique Martinet947867a2015-09-03 11:28:20 +0200328 err = p9_parse_header(&m->rc, NULL, NULL, NULL, 0);
329 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800330 p9_debug(P9_DEBUG_ERROR,
Dominique Martinet947867a2015-09-03 11:28:20 +0200331 "error parsing header: %d\n", err);
332 goto error;
333 }
334
335 if (m->rc.size >= m->client->msize) {
336 p9_debug(P9_DEBUG_ERROR,
337 "requested packet size too big: %d\n",
338 m->rc.size);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600339 err = -EIO;
340 goto error;
341 }
342
Joe Perches5d385152011-11-28 10:40:46 -0800343 p9_debug(P9_DEBUG_TRANS,
Dominique Martinet947867a2015-09-03 11:28:20 +0200344 "mux %p pkt: size: %d bytes tag: %d\n",
345 m, m->rc.size, m->rc.tag);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600346
Dominique Martinet947867a2015-09-03 11:28:20 +0200347 m->req = p9_tag_lookup(m->client, m->rc.tag);
Simon Derr0bfd6842014-03-10 16:38:52 +0100348 if (!m->req || (m->req->status != REQ_STATUS_SENT)) {
Joe Perches5d385152011-11-28 10:40:46 -0800349 p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
Dominique Martinet947867a2015-09-03 11:28:20 +0200350 m->rc.tag);
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500351 err = -EIO;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600352 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500353 }
354
355 if (m->req->rc == NULL) {
Dominique Martinet30536002015-09-07 17:06:54 +0200356 p9_debug(P9_DEBUG_ERROR,
357 "No recv fcall for tag %d (req %p), disconnecting!\n",
358 m->rc.tag, m->req);
359 m->req = NULL;
360 err = -EIO;
361 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500362 }
Dominique Martinet947867a2015-09-03 11:28:20 +0200363 m->rc.sdata = (char *)m->req->rc + sizeof(struct p9_fcall);
364 memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity);
365 m->rc.capacity = m->rc.size;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500366 }
367
Dominique Martinet947867a2015-09-03 11:28:20 +0200368 /* packet is read in
369 * not an else because some packets (like clunk) have no payload
370 */
371 if ((m->req) && (m->rc.offset == m->rc.capacity)) {
Joe Perches5d385152011-11-28 10:40:46 -0800372 p9_debug(P9_DEBUG_TRANS, "got new packet\n");
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500373 spin_lock(&m->client->lock);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500374 if (m->req->status != REQ_STATUS_ERROR)
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100375 status = REQ_STATUS_RCVD;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500376 list_del(&m->req->req_list);
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200377 /* update req->status while holding client->lock */
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100378 p9_client_cb(m->client, m->req, status);
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200379 spin_unlock(&m->client->lock);
Dominique Martinet947867a2015-09-03 11:28:20 +0200380 m->rc.sdata = NULL;
381 m->rc.offset = 0;
382 m->rc.capacity = 0;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500383 m->req = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600384 }
385
Simon Derr04621942012-09-17 15:16:28 +0200386end_clear:
387 clear_bit(Rworksched, &m->wsched);
388
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600389 if (!list_empty(&m->req_list)) {
390 if (test_and_clear_bit(Rpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800391 n = EPOLLIN;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600392 else
Al Viro7594bf32017-07-16 22:53:08 -0400393 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600394
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800395 if ((n & EPOLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800396 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100397 schedule_work(&m->rq);
Simon Derr04621942012-09-17 15:16:28 +0200398 }
399 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600400
401 return;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600402error:
403 p9_conn_cancel(m, err);
404 clear_bit(Rworksched, &m->wsched);
405}
406
407/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500408 * p9_fd_write - write to a socket
409 * @client: client instance
410 * @v: buffer to send data from
411 * @len: size of send buffer
412 *
413 */
414
415static int p9_fd_write(struct p9_client *client, void *v, int len)
416{
Christoph Hellwig670986e2017-09-01 17:39:18 +0200417 ssize_t ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500418 struct p9_trans_fd *ts = NULL;
419
420 if (client && client->status != Disconnected)
421 ts = client->trans;
422
423 if (!ts)
424 return -EREMOTEIO;
425
426 if (!(ts->wr->f_flags & O_NONBLOCK))
Joe Perches5d385152011-11-28 10:40:46 -0800427 p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500428
Christoph Hellwig670986e2017-09-01 17:39:18 +0200429 ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500430 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
431 client->status = Disconnected;
432 return ret;
433}
434
435/**
436 * p9_write_work - called when a transport can send some data
437 * @work: container for work to be done
438 *
439 */
440
441static void p9_write_work(struct work_struct *work)
442{
Al Viro7594bf32017-07-16 22:53:08 -0400443 __poll_t n;
444 int err;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500445 struct p9_conn *m;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500446 struct p9_req_t *req;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500447
448 m = container_of(work, struct p9_conn, wq);
449
450 if (m->err < 0) {
451 clear_bit(Wworksched, &m->wsched);
452 return;
453 }
454
455 if (!m->wsize) {
Simon Derr759f4292012-09-17 15:16:31 +0200456 spin_lock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500457 if (list_empty(&m->unsent_req_list)) {
458 clear_bit(Wworksched, &m->wsched);
Simon Derr759f4292012-09-17 15:16:31 +0200459 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500460 return;
461 }
462
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500463 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500464 req_list);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500465 req->status = REQ_STATUS_SENT;
Joe Perches5d385152011-11-28 10:40:46 -0800466 p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500467 list_move_tail(&req->req_list, &m->req_list);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500468
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500469 m->wbuf = req->tc->sdata;
470 m->wsize = req->tc->size;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500471 m->wpos = 0;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500472 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500473 }
474
Joe Perches5d385152011-11-28 10:40:46 -0800475 p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
476 m, m->wpos, m->wsize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500477 clear_bit(Wpending, &m->wsched);
478 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
Joe Perches5d385152011-11-28 10:40:46 -0800479 p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
Simon Derr584a8c12012-09-17 15:16:30 +0200480 if (err == -EAGAIN)
481 goto end_clear;
482
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500483
484 if (err < 0)
485 goto error;
486 else if (err == 0) {
487 err = -EREMOTEIO;
488 goto error;
489 }
490
491 m->wpos += err;
492 if (m->wpos == m->wsize)
493 m->wpos = m->wsize = 0;
494
Simon Derr584a8c12012-09-17 15:16:30 +0200495end_clear:
496 clear_bit(Wworksched, &m->wsched);
497
Simon Derr1957b3a2012-09-17 15:16:29 +0200498 if (m->wsize || !list_empty(&m->unsent_req_list)) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500499 if (test_and_clear_bit(Wpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800500 n = EPOLLOUT;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500501 else
Al Viro7594bf32017-07-16 22:53:08 -0400502 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500503
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800504 if ((n & EPOLLOUT) &&
Simon Derr584a8c12012-09-17 15:16:30 +0200505 !test_and_set_bit(Wworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800506 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100507 schedule_work(&m->wq);
Simon Derr584a8c12012-09-17 15:16:30 +0200508 }
509 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500510
511 return;
512
513error:
514 p9_conn_cancel(m, err);
515 clear_bit(Wworksched, &m->wsched);
516}
517
Ingo Molnarac6424b2017-06-20 12:06:13 +0200518static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500519{
520 struct p9_poll_wait *pwait =
521 container_of(wait, struct p9_poll_wait, wait);
522 struct p9_conn *m = pwait->conn;
523 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500524
525 spin_lock_irqsave(&p9_poll_lock, flags);
526 if (list_empty(&m->poll_pending_link))
527 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
528 spin_unlock_irqrestore(&p9_poll_lock, flags);
529
Tejun Heoaa70c582011-02-01 11:42:43 +0100530 schedule_work(&p9_poll_work);
531 return 1;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500532}
533
534/**
535 * p9_pollwait - add poll task to the wait queue
536 * @filp: file pointer being polled
537 * @wait_address: wait_q to block on
538 * @p: poll state
539 *
540 * called by files poll operation to add v9fs-poll task to files wait queue
541 */
542
543static void
544p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
545{
546 struct p9_conn *m = container_of(p, struct p9_conn, pt);
547 struct p9_poll_wait *pwait = NULL;
548 int i;
549
550 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
551 if (m->poll_wait[i].wait_addr == NULL) {
552 pwait = &m->poll_wait[i];
553 break;
554 }
555 }
556
557 if (!pwait) {
Joe Perches5d385152011-11-28 10:40:46 -0800558 p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500559 return;
560 }
561
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500562 pwait->conn = m;
563 pwait->wait_addr = wait_address;
564 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
565 add_wait_queue(wait_address, &pwait->wait);
566}
567
568/**
Simon Derr263c5822014-03-11 10:13:09 +0100569 * p9_conn_create - initialize the per-session mux data
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500570 * @client: client instance
571 *
572 * Note: Creates the polling task if this is the first session.
573 */
574
Simon Derr263c5822014-03-11 10:13:09 +0100575static void p9_conn_create(struct p9_client *client)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500576{
Al Viro7594bf32017-07-16 22:53:08 -0400577 __poll_t n;
Simon Derr263c5822014-03-11 10:13:09 +0100578 struct p9_trans_fd *ts = client->trans;
579 struct p9_conn *m = &ts->conn;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500580
Joe Perches5d385152011-11-28 10:40:46 -0800581 p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500582
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500583 INIT_LIST_HEAD(&m->mux_list);
584 m->client = client;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500585
586 INIT_LIST_HEAD(&m->req_list);
587 INIT_LIST_HEAD(&m->unsent_req_list);
588 INIT_WORK(&m->rq, p9_read_work);
589 INIT_WORK(&m->wq, p9_write_work);
590 INIT_LIST_HEAD(&m->poll_pending_link);
591 init_poll_funcptr(&m->pt, p9_pollwait);
592
Al Viro7594bf32017-07-16 22:53:08 -0400593 n = p9_fd_poll(client, &m->pt, NULL);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800594 if (n & EPOLLIN) {
Joe Perches5d385152011-11-28 10:40:46 -0800595 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500596 set_bit(Rpending, &m->wsched);
597 }
598
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800599 if (n & EPOLLOUT) {
Joe Perches5d385152011-11-28 10:40:46 -0800600 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500601 set_bit(Wpending, &m->wsched);
602 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500603}
604
605/**
606 * p9_poll_mux - polls a mux and schedules read or write works if necessary
607 * @m: connection to poll
608 *
609 */
610
611static void p9_poll_mux(struct p9_conn *m)
612{
Al Viro7594bf32017-07-16 22:53:08 -0400613 __poll_t n;
614 int err = -ECONNRESET;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500615
616 if (m->err < 0)
617 return;
618
Al Viro7594bf32017-07-16 22:53:08 -0400619 n = p9_fd_poll(m->client, NULL, &err);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800620 if (n & (EPOLLERR | EPOLLHUP | EPOLLNVAL)) {
Joe Perches5d385152011-11-28 10:40:46 -0800621 p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
Al Viro7594bf32017-07-16 22:53:08 -0400622 p9_conn_cancel(m, err);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500623 }
624
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800625 if (n & EPOLLIN) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500626 set_bit(Rpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800627 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500628 if (!test_and_set_bit(Rworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800629 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100630 schedule_work(&m->rq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500631 }
632 }
633
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800634 if (n & EPOLLOUT) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500635 set_bit(Wpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800636 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800637 if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
638 !test_and_set_bit(Wworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800639 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100640 schedule_work(&m->wq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500641 }
642 }
643}
644
645/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500646 * p9_fd_request - send 9P request
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600647 * The function can sleep until the request is scheduled for sending.
648 * The function can be interrupted. Return from the function is not
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500649 * a guarantee that the request is sent successfully.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600650 *
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500651 * @client: client instance
652 * @req: request to be sent
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600653 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600654 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600655
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500656static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600657{
Al Viro7594bf32017-07-16 22:53:08 -0400658 __poll_t n;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500659 struct p9_trans_fd *ts = client->trans;
Simon Derr263c5822014-03-11 10:13:09 +0100660 struct p9_conn *m = &ts->conn;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600661
Joe Perches5d385152011-11-28 10:40:46 -0800662 p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
663 m, current, req->tc, req->tc->id);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600664 if (m->err < 0)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500665 return m->err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600666
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500667 spin_lock(&client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500668 req->status = REQ_STATUS_UNSENT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600669 list_add_tail(&req->req_list, &m->unsent_req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500670 spin_unlock(&client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600671
672 if (test_and_clear_bit(Wpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800673 n = EPOLLOUT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600674 else
Al Viro7594bf32017-07-16 22:53:08 -0400675 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600676
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800677 if (n & EPOLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
Tejun Heo61edeee2011-02-01 11:42:43 +0100678 schedule_work(&m->wq);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600679
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500680 return 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600681}
682
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500683static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600684{
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500685 int ret = 1;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600686
Joe Perches5d385152011-11-28 10:40:46 -0800687 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600688
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500689 spin_lock(&client->lock);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500690
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500691 if (req->status == REQ_STATUS_UNSENT) {
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500692 list_del(&req->req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500693 req->status = REQ_STATUS_FLSHD;
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500694 ret = 0;
Simon Derr0bfd6842014-03-10 16:38:52 +0100695 }
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500696 spin_unlock(&client->lock);
697
698 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600699}
700
Simon Derrafd8d652014-03-10 16:38:49 +0100701static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
702{
703 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
704
705 /* we haven't received a response for oldreq,
706 * remove it from the list.
707 */
708 spin_lock(&client->lock);
709 list_del(&req->req_list);
710 spin_unlock(&client->lock);
711
712 return 0;
713}
714
David Howellsc4fac912017-07-05 16:25:37 +0100715static int p9_fd_show_options(struct seq_file *m, struct p9_client *clnt)
716{
717 if (clnt->trans_mod == &p9_tcp_trans) {
718 if (clnt->trans_opts.tcp.port != P9_PORT)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200719 seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port);
David Howellsc4fac912017-07-05 16:25:37 +0100720 } else if (clnt->trans_mod == &p9_fd_trans) {
721 if (clnt->trans_opts.fd.rfd != ~0)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200722 seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd);
David Howellsc4fac912017-07-05 16:25:37 +0100723 if (clnt->trans_opts.fd.wfd != ~0)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200724 seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd);
David Howellsc4fac912017-07-05 16:25:37 +0100725 }
726 return 0;
727}
728
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600729/**
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600730 * parse_opts - parse mount options into p9_fd_opts structure
731 * @params: options string passed from mount
732 * @opts: fd transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500733 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600734 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500735 */
736
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600737static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500738{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500739 char *p;
740 substring_t args[MAX_OPT_ARGS];
741 int option;
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600742 char *options, *tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500743
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500744 opts->port = P9_PORT;
745 opts->rfd = ~0;
746 opts->wfd = ~0;
David Howellsc4fac912017-07-05 16:25:37 +0100747 opts->privport = false;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500748
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600749 if (!params)
750 return 0;
751
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600752 tmp_options = kstrdup(params, GFP_KERNEL);
753 if (!tmp_options) {
Joe Perches5d385152011-11-28 10:40:46 -0800754 p9_debug(P9_DEBUG_ERROR,
755 "failed to allocate copy of option string\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600756 return -ENOMEM;
757 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600758 options = tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500759
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500760 while ((p = strsep(&options, ",")) != NULL) {
761 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600762 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500763 if (!*p)
764 continue;
765 token = match_token(p, tokens, args);
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700766 if ((token != Opt_err) && (token != Opt_privport)) {
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000767 r = match_int(&args[0], &option);
768 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800769 p9_debug(P9_DEBUG_ERROR,
770 "integer field, but no integer?\n");
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000771 continue;
772 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500773 }
774 switch (token) {
775 case Opt_port:
776 opts->port = option;
777 break;
778 case Opt_rfdno:
779 opts->rfd = option;
780 break;
781 case Opt_wfdno:
782 opts->wfd = option;
783 break;
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700784 case Opt_privport:
David Howellsc4fac912017-07-05 16:25:37 +0100785 opts->privport = true;
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700786 break;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500787 default:
788 continue;
789 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500790 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600791
792 kfree(tmp_options);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600793 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500794}
795
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500796static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500797{
Simon Derr263c5822014-03-11 10:13:09 +0100798 struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500799 GFP_KERNEL);
800 if (!ts)
801 return -ENOMEM;
802
803 ts->rd = fget(rfd);
804 ts->wr = fget(wfd);
805 if (!ts->rd || !ts->wr) {
806 if (ts->rd)
807 fput(ts->rd);
808 if (ts->wr)
809 fput(ts->wr);
810 kfree(ts);
811 return -EIO;
812 }
813
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500814 client->trans = ts;
815 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500816
817 return 0;
818}
819
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500820static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500821{
Al Viro6b186622009-08-06 02:02:43 +0400822 struct p9_trans_fd *p;
Al Viro56b31d12012-08-18 00:25:51 -0400823 struct file *file;
Al Viro6b186622009-08-06 02:02:43 +0400824
Simon Derr263c5822014-03-11 10:13:09 +0100825 p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
Al Viro6b186622009-08-06 02:02:43 +0400826 if (!p)
827 return -ENOMEM;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500828
829 csocket->sk->sk_allocation = GFP_NOIO;
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700830 file = sock_alloc_file(csocket, 0, NULL);
Al Viro56b31d12012-08-18 00:25:51 -0400831 if (IS_ERR(file)) {
Joe Perches5d385152011-11-28 10:40:46 -0800832 pr_err("%s (%d): failed to map fd\n",
833 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400834 kfree(p);
Al Viro56b31d12012-08-18 00:25:51 -0400835 return PTR_ERR(file);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500836 }
837
Al Viro56b31d12012-08-18 00:25:51 -0400838 get_file(file);
839 p->wr = p->rd = file;
Al Viro6b186622009-08-06 02:02:43 +0400840 client->trans = p;
841 client->status = Connected;
842
Al Viro6b186622009-08-06 02:02:43 +0400843 p->rd->f_flags |= O_NONBLOCK;
844
Simon Derr263c5822014-03-11 10:13:09 +0100845 p9_conn_create(client);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500846 return 0;
847}
848
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500849/**
Simon Derr263c5822014-03-11 10:13:09 +0100850 * p9_mux_destroy - cancels all pending requests of mux
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500851 * @m: mux to destroy
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500852 *
853 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600854
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500855static void p9_conn_destroy(struct p9_conn *m)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500856{
Joe Perches5d385152011-11-28 10:40:46 -0800857 p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
858 m, m->mux_list.prev, m->mux_list.next);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500859
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500860 p9_mux_poll_stop(m);
861 cancel_work_sync(&m->rq);
862 cancel_work_sync(&m->wq);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500863
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500864 p9_conn_cancel(m, -ECONNRESET);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500865
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500866 m->client = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500867}
868
869/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500870 * p9_fd_close - shutdown file descriptor transport
871 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500872 *
873 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600874
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500875static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500876{
877 struct p9_trans_fd *ts;
878
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500879 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500880 return;
881
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500882 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500883 if (!ts)
884 return;
885
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500886 client->status = Disconnected;
887
Simon Derr263c5822014-03-11 10:13:09 +0100888 p9_conn_destroy(&ts->conn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600889
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500890 if (ts->rd)
891 fput(ts->rd);
892 if (ts->wr)
893 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500894
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500895 kfree(ts);
896}
897
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500898/*
899 * stolen from NFS - maybe should be made a generic function?
900 */
901static inline int valid_ipaddr4(const char *buf)
902{
903 int rc, count, in[4];
904
905 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
906 if (rc != 4)
907 return -EINVAL;
908 for (count = 0; count < 4; count++) {
909 if (in[count] > 255)
910 return -EINVAL;
911 }
912 return 0;
913}
914
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700915static int p9_bind_privport(struct socket *sock)
916{
917 struct sockaddr_in cl;
918 int port, err = -EINVAL;
919
920 memset(&cl, 0, sizeof(cl));
921 cl.sin_family = AF_INET;
922 cl.sin_addr.s_addr = INADDR_ANY;
923 for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
924 cl.sin_port = htons((ushort)port);
925 err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
926 if (err != -EADDRINUSE)
927 break;
928 }
929 return err;
930}
931
932
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500933static int
934p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500935{
936 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500937 struct socket *csocket;
938 struct sockaddr_in sin_server;
939 struct p9_fd_opts opts;
940
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600941 err = parse_opts(args, &opts);
942 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500943 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500944
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500945 if (valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500946 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500947
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500948 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500949
David Howellsc4fac912017-07-05 16:25:37 +0100950 client->trans_opts.tcp.port = opts.port;
951 client->trans_opts.tcp.privport = opts.privport;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500952 sin_server.sin_family = AF_INET;
953 sin_server.sin_addr.s_addr = in_aton(addr);
954 sin_server.sin_port = htons(opts.port);
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -0500955 err = __sock_create(current->nsproxy->net_ns, PF_INET,
Rob Landleye75762f2011-05-23 21:46:56 -0500956 SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +0400957 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800958 pr_err("%s (%d): problem creating socket\n",
959 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400960 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500961 }
962
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700963 if (opts.privport) {
964 err = p9_bind_privport(csocket);
965 if (err < 0) {
966 pr_err("%s (%d): problem binding to privport\n",
967 __func__, task_pid_nr(current));
968 sock_release(csocket);
969 return err;
970 }
971 }
972
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500973 err = csocket->ops->connect(csocket,
974 (struct sockaddr *)&sin_server,
975 sizeof(struct sockaddr_in), 0);
976 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800977 pr_err("%s (%d): problem connecting socket to %s\n",
978 __func__, task_pid_nr(current), addr);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500979 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +0400980 return err;
981 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500982
Al Viro6b186622009-08-06 02:02:43 +0400983 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500984}
985
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500986static int
987p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500988{
989 int err;
990 struct socket *csocket;
991 struct sockaddr_un sun_server;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500992
993 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500994
Dan Carpentercff6b8a2010-07-10 11:51:54 +0200995 if (strlen(addr) >= UNIX_PATH_MAX) {
Joe Perches5d385152011-11-28 10:40:46 -0800996 pr_err("%s (%d): address too long: %s\n",
997 __func__, task_pid_nr(current), addr);
Al Viro6b186622009-08-06 02:02:43 +0400998 return -ENAMETOOLONG;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500999 }
1000
1001 sun_server.sun_family = PF_UNIX;
1002 strcpy(sun_server.sun_path, addr);
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -05001003 err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
Rob Landleye75762f2011-05-23 21:46:56 -05001004 SOCK_STREAM, 0, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +04001005 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001006 pr_err("%s (%d): problem creating socket\n",
1007 __func__, task_pid_nr(current));
1008
Al Viro6b186622009-08-06 02:02:43 +04001009 return err;
1010 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001011 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1012 sizeof(struct sockaddr_un) - 1, 0);
1013 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001014 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1015 __func__, task_pid_nr(current), addr, err);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001016 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +04001017 return err;
1018 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001019
Al Viro6b186622009-08-06 02:02:43 +04001020 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001021}
1022
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001023static int
1024p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001025{
1026 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001027 struct p9_fd_opts opts;
1028
1029 parse_opts(args, &opts);
David Howellsc4fac912017-07-05 16:25:37 +01001030 client->trans_opts.fd.rfd = opts.rfd;
1031 client->trans_opts.fd.wfd = opts.wfd;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001032
1033 if (opts.rfd == ~0 || opts.wfd == ~0) {
Joe Perches5d385152011-11-28 10:40:46 -08001034 pr_err("Insufficient options for proto=fd\n");
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001035 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001036 }
1037
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001038 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001039 if (err < 0)
Al Viro6b186622009-08-06 02:02:43 +04001040 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001041
Simon Derr263c5822014-03-11 10:13:09 +01001042 p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001043
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001044 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001045}
1046
1047static struct p9_trans_module p9_tcp_trans = {
1048 .name = "tcp",
1049 .maxsize = MAX_SOCK_BUF,
Eric Van Hensbergenf94741f2013-11-12 10:20:03 -06001050 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001051 .create = p9_fd_create_tcp,
1052 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001053 .request = p9_fd_request,
1054 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001055 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001056 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001057 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001058};
1059
1060static struct p9_trans_module p9_unix_trans = {
1061 .name = "unix",
1062 .maxsize = MAX_SOCK_BUF,
1063 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001064 .create = p9_fd_create_unix,
1065 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001066 .request = p9_fd_request,
1067 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001068 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001069 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001070 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001071};
1072
1073static struct p9_trans_module p9_fd_trans = {
1074 .name = "fd",
1075 .maxsize = MAX_SOCK_BUF,
1076 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001077 .create = p9_fd_create,
1078 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001079 .request = p9_fd_request,
1080 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001081 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001082 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001083 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001084};
1085
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001086/**
Sun Lianwen4a026da2018-05-08 09:49:38 +08001087 * p9_poll_workfn - poll worker thread
1088 * @work: work queue
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001089 *
1090 * polls all v9fs transports for new events and queues the appropriate
1091 * work to the work queue
1092 *
1093 */
1094
Tejun Heoaa70c582011-02-01 11:42:43 +01001095static void p9_poll_workfn(struct work_struct *work)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001096{
1097 unsigned long flags;
1098
Joe Perches5d385152011-11-28 10:40:46 -08001099 p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
Tejun Heoaa70c582011-02-01 11:42:43 +01001100
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001101 spin_lock_irqsave(&p9_poll_lock, flags);
1102 while (!list_empty(&p9_poll_pending_list)) {
1103 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1104 struct p9_conn,
1105 poll_pending_link);
1106 list_del_init(&conn->poll_pending_link);
1107 spin_unlock_irqrestore(&p9_poll_lock, flags);
1108
1109 p9_poll_mux(conn);
1110
1111 spin_lock_irqsave(&p9_poll_lock, flags);
1112 }
1113 spin_unlock_irqrestore(&p9_poll_lock, flags);
1114
Joe Perches5d385152011-11-28 10:40:46 -08001115 p9_debug(P9_DEBUG_TRANS, "finish\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001116}
1117
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001118int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001119{
1120 v9fs_register_trans(&p9_tcp_trans);
1121 v9fs_register_trans(&p9_unix_trans);
1122 v9fs_register_trans(&p9_fd_trans);
1123
Andrew Morton3387b802008-03-28 14:15:57 -07001124 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001125}
Tejun Heo72029fe2008-09-24 16:22:23 -05001126
1127void p9_trans_fd_exit(void)
1128{
Tejun Heo43829732012-08-20 14:51:24 -07001129 flush_work(&p9_poll_work);
Tejun Heo72029fe2008-09-24 16:22:23 -05001130 v9fs_unregister_trans(&p9_tcp_trans);
1131 v9fs_unregister_trans(&p9_unix_trans);
1132 v9fs_unregister_trans(&p9_fd_trans);
1133}