blob: f4dd0456beaf4847fca04a1f55a9e9a1cae28c78 [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05002/*
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 Hensbergen8a0dc952008-02-06 19:25:03 -06009 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050010 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050011 */
12
Joe Perches5d385152011-11-28 10:40:46 -080013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050015#include <linux/in.h>
16#include <linux/module.h>
17#include <linux/net.h>
18#include <linux/ipv6.h>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060019#include <linux/kthread.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050020#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 Hensbergena80d9232007-10-17 14:31:07 -050027#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
David Howellsc4fac912017-07-05 16:25:37 +010029#include <linux/seq_file.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050030#include <net/9p/9p.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050031#include <net/9p/client.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050032#include <net/9p/transport.h>
33
Al Viro6b186622009-08-06 02:02:43 +040034#include <linux/syscalls.h> /* killme */
35
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050036#define P9_PORT 564
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050037#define MAX_SOCK_BUF (64*1024)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060038#define MAXPOLLWADDR 2
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050039
David Howellsc4fac912017-07-05 16:25:37 +010040static struct p9_trans_module p9_tcp_trans;
41static struct p9_trans_module p9_fd_trans;
42
Eric Van Hensbergenee443992008-03-05 07:08:09 -060043/**
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 Lunn760b3d62020-10-31 19:26:55 +010048 * @privport: port is privileged
Eric Van Hensbergenee443992008-03-05 07:08:09 -060049 */
50
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050051struct p9_fd_opts {
52 int rfd;
53 int wfd;
54 u16 port;
David Howellsc4fac912017-07-05 16:25:37 +010055 bool privport;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050056};
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050057
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050058/*
59 * Option Parsing (code inspired by NFS code)
60 * - a little lazy - parse all fd-transport options
61 */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050062
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050063enum {
64 /* Options that take integer arguments */
Latchesar Ionkov55762692007-11-06 08:02:53 -060065 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
Jim Garlick2f28c8b2013-05-29 12:15:07 -070066 /* Options that take no arguments */
67 Opt_privport,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050068};
69
Steven Whitehousea447c092008-10-13 10:46:57 +010070static const match_table_t tokens = {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050071 {Opt_port, "port=%u"},
72 {Opt_rfdno, "rfdno=%u"},
73 {Opt_wfdno, "wfdno=%u"},
Jim Garlick2f28c8b2013-05-29 12:15:07 -070074 {Opt_privport, "privport"},
Latchesar Ionkov55762692007-11-06 08:02:53 -060075 {Opt_err, NULL},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050076};
77
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060078enum {
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 Heo992b3f12008-10-13 18:45:25 -050085struct p9_poll_wait {
86 struct p9_conn *conn;
Ingo Molnarac6424b2017-06-20 12:06:13 +020087 wait_queue_entry_t wait;
Tejun Heo992b3f12008-10-13 18:45:25 -050088 wait_queue_head_t *wait_addr;
Eric Van Hensbergenee443992008-03-05 07:08:09 -060089};
90
91/**
92 * struct p9_conn - fd mux connection state information
Eric Van Hensbergenee443992008-03-05 07:08:09 -060093 * @mux_list: list link for mux to manage multiple connections (?)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050094 * @client: reference to client instance for this connection
Eric Van Hensbergenee443992008-03-05 07:08:09 -060095 * @err: error state
Eric Van Hensbergenee443992008-03-05 07:08:09 -060096 * @req_list: accounting for requests which have been sent
97 * @unsent_req_list: accounting for requests that haven't been sent
Andrew Lunn760b3d62020-10-31 19:26:55 +010098 * @rreq: read request
99 * @wreq: write request
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500100 * @req: current request being processed (if any)
101 * @tmp_buf: temporary buffer to read in header
Dominique Martinet947867a2015-09-03 11:28:20 +0200102 * @rc: temporary fcall for reading current frame
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600103 * @wpos: write position for current frame
104 * @wsize: amount of data to write for current frame
105 * @wbuf: current write buffer
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600106 * @poll_pending_link: pending links to be polled per conn
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600107 * @poll_wait: array of wait_q's for various worker threads
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600108 * @pt: poll state
109 * @rq: current read work
110 * @wq: current write work
111 * @wsched: ????
112 *
113 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600114
115struct p9_conn {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600116 struct list_head mux_list;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500117 struct p9_client *client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600118 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600119 struct list_head req_list;
120 struct list_head unsent_req_list;
Tomas Bortoli6d351902018-09-03 18:03:21 +0200121 struct p9_req_t *rreq;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200122 struct p9_req_t *wreq;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500123 char tmp_buf[7];
Dominique Martinet947867a2015-09-03 11:28:20 +0200124 struct p9_fcall rc;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600125 int wpos;
126 int wsize;
127 char *wbuf;
Tejun Heo992b3f12008-10-13 18:45:25 -0500128 struct list_head poll_pending_link;
129 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600130 poll_table pt;
131 struct work_struct rq;
132 struct work_struct wq;
133 unsigned long wsched;
134};
135
Simon Derr263c5822014-03-11 10:13:09 +0100136/**
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
144struct p9_trans_fd {
145 struct file *rd;
146 struct file *wr;
147 struct p9_conn conn;
148};
149
Tejun Heoaa70c582011-02-01 11:42:43 +0100150static void p9_poll_workfn(struct work_struct *work);
151
Tejun Heo992b3f12008-10-13 18:45:25 -0500152static DEFINE_SPINLOCK(p9_poll_lock);
153static LIST_HEAD(p9_poll_pending_list);
Tejun Heoaa70c582011-02-01 11:42:43 +0100154static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600155
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700156static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
157static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
158
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600159static void p9_mux_poll_stop(struct p9_conn *m)
160{
Tejun Heo992b3f12008-10-13 18:45:25 -0500161 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600162 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600163
Tejun Heo992b3f12008-10-13 18:45:25 -0500164 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600170 }
171 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500172
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 Bortoli430ac662018-07-20 11:27:30 +0200176
177 flush_work(&p9_poll_work);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600178}
179
180/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500181 * p9_conn_cancel - cancel all pending requests with error
182 * @m: mux data
183 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600184 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600185 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600186
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500187static void p9_conn_cancel(struct p9_conn *m, int err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600188{
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500189 struct p9_req_t *req, *rtmp;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500190 LIST_HEAD(cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600191
Joe Perches5d385152011-11-28 10:40:46 -0800192 p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500193
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200194 spin_lock(&m->client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500195
196 if (m->err) {
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200197 spin_unlock(&m->client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500198 return;
199 }
200
201 m->err = err;
202
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500203 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
204 list_move(&req->req_list, &cancel_list);
Tejun Heo992b3f12008-10-13 18:45:25 -0500205 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500206 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
207 list_move(&req->req_list, &cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600208 }
209
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500210 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
Joe Perches5d385152011-11-28 10:40:46 -0800211 p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500212 list_del(&req->req_list);
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100213 if (!req->t_err)
214 req->t_err = err;
215 p9_client_cb(m->client, req, REQ_STATUS_ERROR);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600216 }
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200217 spin_unlock(&m->client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600218}
219
Al Viro7594bf32017-07-16 22:53:08 -0400220static __poll_t
221p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt, int *err)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500222{
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800223 __poll_t ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500224 struct p9_trans_fd *ts = NULL;
225
226 if (client && client->status == Connected)
227 ts = client->trans;
228
Al Viro7594bf32017-07-16 22:53:08 -0400229 if (!ts) {
230 if (err)
231 *err = -EREMOTEIO;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800232 return EPOLLERR;
Al Viro7594bf32017-07-16 22:53:08 -0400233 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500234
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800235 ret = vfs_poll(ts->rd, pt);
236 if (ts->rd != ts->wr)
237 ret = (ret & ~EPOLLOUT) | (vfs_poll(ts->wr, pt) & ~EPOLLIN);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500238 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
249static int p9_fd_read(struct p9_client *client, void *v, int len)
250{
251 int ret;
252 struct p9_trans_fd *ts = NULL;
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200253 loff_t pos;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500254
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 Perches5d385152011-11-28 10:40:46 -0800262 p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500263
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200264 pos = ts->rd->f_pos;
265 ret = kernel_read(ts->rd, v, len, &pos);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500266 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
267 client->status = Disconnected;
268 return ret;
269}
270
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600271/**
272 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600273 * @work: container of work to be done
274 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600275 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600276
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600277static void p9_read_work(struct work_struct *work)
278{
Al Viro7594bf32017-07-16 22:53:08 -0400279 __poll_t n;
280 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600281 struct p9_conn *m;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600282
283 m = container_of(work, struct p9_conn, rq);
284
285 if (m->err < 0)
286 return;
287
Dominique Martinet947867a2015-09-03 11:28:20 +0200288 p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600289
Dominique Martinet947867a2015-09-03 11:28:20 +0200290 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600294 }
295
296 clear_bit(Rpending, &m->wsched);
Dominique Martinet947867a2015-09-03 11:28:20 +0200297 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 Perches5d385152011-11-28 10:40:46 -0800302 p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
Dominique Martinet947867a2015-09-03 11:28:20 +0200303 if (err == -EAGAIN)
Simon Derr04621942012-09-17 15:16:28 +0200304 goto end_clear;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600305
306 if (err <= 0)
307 goto error;
308
Dominique Martinet947867a2015-09-03 11:28:20 +0200309 m->rc.offset += err;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500310
Dominique Martinet947867a2015-09-03 11:28:20 +0200311 /* header read in */
Tomas Bortoli6d351902018-09-03 18:03:21 +0200312 if ((!m->rreq) && (m->rc.offset == m->rc.capacity)) {
Joe Perches5d385152011-11-28 10:40:46 -0800313 p9_debug(P9_DEBUG_TRANS, "got new header\n");
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500314
Tomas Bortolif9845792018-07-23 17:44:04 +0200315 /* Header size */
316 m->rc.size = 7;
317 err = p9_parse_header(&m->rc, &m->rc.size, NULL, NULL, 0);
Dominique Martinet947867a2015-09-03 11:28:20 +0200318 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800319 p9_debug(P9_DEBUG_ERROR,
Dominique Martinet947867a2015-09-03 11:28:20 +0200320 "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 Hensbergen8a0dc952008-02-06 19:25:03 -0600328 err = -EIO;
329 goto error;
330 }
331
Joe Perches5d385152011-11-28 10:40:46 -0800332 p9_debug(P9_DEBUG_TRANS,
Dominique Martinet947867a2015-09-03 11:28:20 +0200333 "mux %p pkt: size: %d bytes tag: %d\n",
334 m, m->rc.size, m->rc.tag);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600335
Tomas Bortoli6d351902018-09-03 18:03:21 +0200336 m->rreq = p9_tag_lookup(m->client, m->rc.tag);
337 if (!m->rreq || (m->rreq->status != REQ_STATUS_SENT)) {
Joe Perches5d385152011-11-28 10:40:46 -0800338 p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
Dominique Martinet947867a2015-09-03 11:28:20 +0200339 m->rc.tag);
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500340 err = -EIO;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600341 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500342 }
343
Tomas Bortoli6d351902018-09-03 18:03:21 +0200344 if (!m->rreq->rc.sdata) {
Dominique Martinet30536002015-09-07 17:06:54 +0200345 p9_debug(P9_DEBUG_ERROR,
346 "No recv fcall for tag %d (req %p), disconnecting!\n",
Tomas Bortoli6d351902018-09-03 18:03:21 +0200347 m->rc.tag, m->rreq);
348 m->rreq = NULL;
Dominique Martinet30536002015-09-07 17:06:54 +0200349 err = -EIO;
350 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500351 }
Tomas Bortoli6d351902018-09-03 18:03:21 +0200352 m->rc.sdata = m->rreq->rc.sdata;
Dominique Martinet947867a2015-09-03 11:28:20 +0200353 memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity);
354 m->rc.capacity = m->rc.size;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500355 }
356
Dominique Martinet947867a2015-09-03 11:28:20 +0200357 /* packet is read in
358 * not an else because some packets (like clunk) have no payload
359 */
Tomas Bortoli6d351902018-09-03 18:03:21 +0200360 if ((m->rreq) && (m->rc.offset == m->rc.capacity)) {
Joe Perches5d385152011-11-28 10:40:46 -0800361 p9_debug(P9_DEBUG_TRANS, "got new packet\n");
Tomas Bortoli6d351902018-09-03 18:03:21 +0200362 m->rreq->rc.size = m->rc.offset;
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500363 spin_lock(&m->client->lock);
Dominique Martinete4ca13f2018-10-09 11:18:52 +0900364 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 Hai74d6a5d2020-06-12 17:08:33 +0800367 } 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 Martinete4ca13f2018-10-09 11:18:52 +0900371 } 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 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;
Tomas Bortoli6d351902018-09-03 18:03:21 +0200383 p9_req_put(m->rreq);
384 m->rreq = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600385 }
386
Simon Derr04621942012-09-17 15:16:28 +0200387end_clear:
388 clear_bit(Rworksched, &m->wsched);
389
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600390 if (!list_empty(&m->req_list)) {
391 if (test_and_clear_bit(Rpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800392 n = EPOLLIN;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600393 else
Al Viro7594bf32017-07-16 22:53:08 -0400394 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600395
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800396 if ((n & EPOLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800397 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100398 schedule_work(&m->rq);
Simon Derr04621942012-09-17 15:16:28 +0200399 }
400 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600401
402 return;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600403error:
404 p9_conn_cancel(m, err);
405 clear_bit(Rworksched, &m->wsched);
406}
407
408/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500409 * 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
416static int p9_fd_write(struct p9_client *client, void *v, int len)
417{
Christoph Hellwig670986e2017-09-01 17:39:18 +0200418 ssize_t ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500419 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 Perches5d385152011-11-28 10:40:46 -0800428 p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500429
Christoph Hellwig670986e2017-09-01 17:39:18 +0200430 ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500431 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
442static void p9_write_work(struct work_struct *work)
443{
Al Viro7594bf32017-07-16 22:53:08 -0400444 __poll_t n;
445 int err;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500446 struct p9_conn *m;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500447 struct p9_req_t *req;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500448
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 Derr759f4292012-09-17 15:16:31 +0200457 spin_lock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500458 if (list_empty(&m->unsent_req_list)) {
459 clear_bit(Wworksched, &m->wsched);
Simon Derr759f4292012-09-17 15:16:31 +0200460 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500461 return;
462 }
463
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500464 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500465 req_list);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500466 req->status = REQ_STATUS_SENT;
Joe Perches5d385152011-11-28 10:40:46 -0800467 p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500468 list_move_tail(&req->req_list, &m->req_list);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500469
Dominique Martinet523adb62018-07-30 05:55:19 +0000470 m->wbuf = req->tc.sdata;
471 m->wsize = req->tc.size;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500472 m->wpos = 0;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200473 p9_req_get(req);
474 m->wreq = req;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500475 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500476 }
477
Joe Perches5d385152011-11-28 10:40:46 -0800478 p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
479 m, m->wpos, m->wsize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500480 clear_bit(Wpending, &m->wsched);
481 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
Joe Perches5d385152011-11-28 10:40:46 -0800482 p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
Simon Derr584a8c12012-09-17 15:16:30 +0200483 if (err == -EAGAIN)
484 goto end_clear;
485
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500486
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 Bortoli728356d2018-08-14 19:43:42 +0200495 if (m->wpos == m->wsize) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500496 m->wpos = m->wsize = 0;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200497 p9_req_put(m->wreq);
498 m->wreq = NULL;
499 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500500
Simon Derr584a8c12012-09-17 15:16:30 +0200501end_clear:
502 clear_bit(Wworksched, &m->wsched);
503
Simon Derr1957b3a2012-09-17 15:16:29 +0200504 if (m->wsize || !list_empty(&m->unsent_req_list)) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500505 if (test_and_clear_bit(Wpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800506 n = EPOLLOUT;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500507 else
Al Viro7594bf32017-07-16 22:53:08 -0400508 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500509
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800510 if ((n & EPOLLOUT) &&
Simon Derr584a8c12012-09-17 15:16:30 +0200511 !test_and_set_bit(Wworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800512 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100513 schedule_work(&m->wq);
Simon Derr584a8c12012-09-17 15:16:30 +0200514 }
515 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500516
517 return;
518
519error:
520 p9_conn_cancel(m, err);
521 clear_bit(Wworksched, &m->wsched);
522}
523
Ingo Molnarac6424b2017-06-20 12:06:13 +0200524static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500525{
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 Hensbergen5503ac52008-10-13 18:45:24 -0500530
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 Heoaa70c582011-02-01 11:42:43 +0100536 schedule_work(&p9_poll_work);
537 return 1;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500538}
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
549static void
550p9_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 Perches5d385152011-11-28 10:40:46 -0800564 p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500565 return;
566 }
567
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500568 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 Derr263c5822014-03-11 10:13:09 +0100575 * p9_conn_create - initialize the per-session mux data
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500576 * @client: client instance
577 *
578 * Note: Creates the polling task if this is the first session.
579 */
580
Simon Derr263c5822014-03-11 10:13:09 +0100581static void p9_conn_create(struct p9_client *client)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500582{
Al Viro7594bf32017-07-16 22:53:08 -0400583 __poll_t n;
Simon Derr263c5822014-03-11 10:13:09 +0100584 struct p9_trans_fd *ts = client->trans;
585 struct p9_conn *m = &ts->conn;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500586
Joe Perches5d385152011-11-28 10:40:46 -0800587 p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500588
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500589 INIT_LIST_HEAD(&m->mux_list);
590 m->client = client;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500591
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 Viro7594bf32017-07-16 22:53:08 -0400599 n = p9_fd_poll(client, &m->pt, NULL);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800600 if (n & EPOLLIN) {
Joe Perches5d385152011-11-28 10:40:46 -0800601 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500602 set_bit(Rpending, &m->wsched);
603 }
604
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800605 if (n & EPOLLOUT) {
Joe Perches5d385152011-11-28 10:40:46 -0800606 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500607 set_bit(Wpending, &m->wsched);
608 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500609}
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
617static void p9_poll_mux(struct p9_conn *m)
618{
Al Viro7594bf32017-07-16 22:53:08 -0400619 __poll_t n;
620 int err = -ECONNRESET;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500621
622 if (m->err < 0)
623 return;
624
Al Viro7594bf32017-07-16 22:53:08 -0400625 n = p9_fd_poll(m->client, NULL, &err);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800626 if (n & (EPOLLERR | EPOLLHUP | EPOLLNVAL)) {
Joe Perches5d385152011-11-28 10:40:46 -0800627 p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
Al Viro7594bf32017-07-16 22:53:08 -0400628 p9_conn_cancel(m, err);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500629 }
630
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800631 if (n & EPOLLIN) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500632 set_bit(Rpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800633 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500634 if (!test_and_set_bit(Rworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800635 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100636 schedule_work(&m->rq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500637 }
638 }
639
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800640 if (n & EPOLLOUT) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500641 set_bit(Wpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800642 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800643 if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
644 !test_and_set_bit(Wworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800645 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100646 schedule_work(&m->wq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500647 }
648 }
649}
650
651/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500652 * p9_fd_request - send 9P request
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600653 * 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 Hensbergen91b85342008-10-13 18:45:21 -0500655 * a guarantee that the request is sent successfully.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600656 *
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500657 * @client: client instance
658 * @req: request to be sent
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600659 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600660 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600661
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500662static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600663{
Al Viro7594bf32017-07-16 22:53:08 -0400664 __poll_t n;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500665 struct p9_trans_fd *ts = client->trans;
Simon Derr263c5822014-03-11 10:13:09 +0100666 struct p9_conn *m = &ts->conn;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600667
Joe Perches5d385152011-11-28 10:40:46 -0800668 p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
Dominique Martinet523adb62018-07-30 05:55:19 +0000669 m, current, &req->tc, req->tc.id);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600670 if (m->err < 0)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500671 return m->err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600672
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500673 spin_lock(&client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500674 req->status = REQ_STATUS_UNSENT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600675 list_add_tail(&req->req_list, &m->unsent_req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500676 spin_unlock(&client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600677
678 if (test_and_clear_bit(Wpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800679 n = EPOLLOUT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600680 else
Al Viro7594bf32017-07-16 22:53:08 -0400681 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600682
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800683 if (n & EPOLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
Tejun Heo61edeee2011-02-01 11:42:43 +0100684 schedule_work(&m->wq);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600685
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500686 return 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600687}
688
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500689static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600690{
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500691 int ret = 1;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600692
Joe Perches5d385152011-11-28 10:40:46 -0800693 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600694
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500695 spin_lock(&client->lock);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500696
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500697 if (req->status == REQ_STATUS_UNSENT) {
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500698 list_del(&req->req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500699 req->status = REQ_STATUS_FLSHD;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200700 p9_req_put(req);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500701 ret = 0;
Simon Derr0bfd6842014-03-10 16:38:52 +0100702 }
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500703 spin_unlock(&client->lock);
704
705 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600706}
707
Simon Derrafd8d652014-03-10 16:38:49 +0100708static 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 Hai74d6a5d2020-06-12 17:08:33 +0800712 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 Derrafd8d652014-03-10 16:38:49 +0100721 /* we haven't received a response for oldreq,
722 * remove it from the list.
723 */
Simon Derrafd8d652014-03-10 16:38:49 +0100724 list_del(&req->req_list);
Wang Hai74d6a5d2020-06-12 17:08:33 +0800725 req->status = REQ_STATUS_FLSHD;
Simon Derrafd8d652014-03-10 16:38:49 +0100726 spin_unlock(&client->lock);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200727 p9_req_put(req);
Simon Derrafd8d652014-03-10 16:38:49 +0100728
729 return 0;
730}
731
David Howellsc4fac912017-07-05 16:25:37 +0100732static 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 Tynkkynen61b272c2017-11-19 11:28:43 +0200736 seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port);
David Howellsc4fac912017-07-05 16:25:37 +0100737 } else if (clnt->trans_mod == &p9_fd_trans) {
738 if (clnt->trans_opts.fd.rfd != ~0)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200739 seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd);
David Howellsc4fac912017-07-05 16:25:37 +0100740 if (clnt->trans_opts.fd.wfd != ~0)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200741 seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd);
David Howellsc4fac912017-07-05 16:25:37 +0100742 }
743 return 0;
744}
745
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600746/**
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600747 * 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 Hensbergena80d9232007-10-17 14:31:07 -0500750 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600751 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500752 */
753
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600754static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500755{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500756 char *p;
757 substring_t args[MAX_OPT_ARGS];
758 int option;
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600759 char *options, *tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500760
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500761 opts->port = P9_PORT;
762 opts->rfd = ~0;
763 opts->wfd = ~0;
David Howellsc4fac912017-07-05 16:25:37 +0100764 opts->privport = false;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500765
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600766 if (!params)
767 return 0;
768
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600769 tmp_options = kstrdup(params, GFP_KERNEL);
770 if (!tmp_options) {
Joe Perches5d385152011-11-28 10:40:46 -0800771 p9_debug(P9_DEBUG_ERROR,
772 "failed to allocate copy of option string\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600773 return -ENOMEM;
774 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600775 options = tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500776
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500777 while ((p = strsep(&options, ",")) != NULL) {
778 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600779 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500780 if (!*p)
781 continue;
782 token = match_token(p, tokens, args);
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700783 if ((token != Opt_err) && (token != Opt_privport)) {
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000784 r = match_int(&args[0], &option);
785 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800786 p9_debug(P9_DEBUG_ERROR,
787 "integer field, but no integer?\n");
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000788 continue;
789 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500790 }
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 Garlick2f28c8b2013-05-29 12:15:07 -0700801 case Opt_privport:
David Howellsc4fac912017-07-05 16:25:37 +0100802 opts->privport = true;
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700803 break;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500804 default:
805 continue;
806 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500807 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600808
809 kfree(tmp_options);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600810 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500811}
812
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500813static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500814{
Simon Derr263c5822014-03-11 10:13:09 +0100815 struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500816 GFP_KERNEL);
817 if (!ts)
818 return -ENOMEM;
819
820 ts->rd = fget(rfd);
Christoph Hellwiga39c4602020-07-10 10:57:22 +0200821 if (!ts->rd)
822 goto out_free_ts;
823 if (!(ts->rd->f_mode & FMODE_READ))
824 goto out_put_rd;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500825 ts->wr = fget(wfd);
Christoph Hellwiga39c4602020-07-10 10:57:22 +0200826 if (!ts->wr)
827 goto out_put_rd;
828 if (!(ts->wr->f_mode & FMODE_WRITE))
829 goto out_put_wr;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500830
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500831 client->trans = ts;
832 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500833
834 return 0;
Christoph Hellwiga39c4602020-07-10 10:57:22 +0200835
836out_put_wr:
837 fput(ts->wr);
838out_put_rd:
839 fput(ts->rd);
840out_free_ts:
841 kfree(ts);
842 return -EIO;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500843}
844
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500845static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500846{
Al Viro6b186622009-08-06 02:02:43 +0400847 struct p9_trans_fd *p;
Al Viro56b31d12012-08-18 00:25:51 -0400848 struct file *file;
Al Viro6b186622009-08-06 02:02:43 +0400849
Simon Derr263c5822014-03-11 10:13:09 +0100850 p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
Al Viro6b186622009-08-06 02:02:43 +0400851 if (!p)
852 return -ENOMEM;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500853
854 csocket->sk->sk_allocation = GFP_NOIO;
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700855 file = sock_alloc_file(csocket, 0, NULL);
Al Viro56b31d12012-08-18 00:25:51 -0400856 if (IS_ERR(file)) {
Joe Perches5d385152011-11-28 10:40:46 -0800857 pr_err("%s (%d): failed to map fd\n",
858 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400859 kfree(p);
Al Viro56b31d12012-08-18 00:25:51 -0400860 return PTR_ERR(file);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500861 }
862
Al Viro56b31d12012-08-18 00:25:51 -0400863 get_file(file);
864 p->wr = p->rd = file;
Al Viro6b186622009-08-06 02:02:43 +0400865 client->trans = p;
866 client->status = Connected;
867
Al Viro6b186622009-08-06 02:02:43 +0400868 p->rd->f_flags |= O_NONBLOCK;
869
Simon Derr263c5822014-03-11 10:13:09 +0100870 p9_conn_create(client);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500871 return 0;
872}
873
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500874/**
Xiongfeng Wang54e625e2021-03-27 16:15:53 +0800875 * p9_conn_destroy - cancels all pending requests of mux
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500876 * @m: mux to destroy
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500877 *
878 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600879
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500880static void p9_conn_destroy(struct p9_conn *m)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500881{
Joe Perches5d385152011-11-28 10:40:46 -0800882 p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
883 m, m->mux_list.prev, m->mux_list.next);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500884
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500885 p9_mux_poll_stop(m);
886 cancel_work_sync(&m->rq);
Dominique Martinetfb488fc2018-10-09 11:38:00 +0900887 if (m->rreq) {
888 p9_req_put(m->rreq);
889 m->rreq = NULL;
890 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500891 cancel_work_sync(&m->wq);
Dominique Martinetfb488fc2018-10-09 11:38:00 +0900892 if (m->wreq) {
893 p9_req_put(m->wreq);
894 m->wreq = NULL;
895 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500896
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500897 p9_conn_cancel(m, -ECONNRESET);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500898
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500899 m->client = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500900}
901
902/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500903 * p9_fd_close - shutdown file descriptor transport
904 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500905 *
906 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600907
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500908static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500909{
910 struct p9_trans_fd *ts;
911
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500912 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500913 return;
914
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500915 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500916 if (!ts)
917 return;
918
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500919 client->status = Disconnected;
920
Simon Derr263c5822014-03-11 10:13:09 +0100921 p9_conn_destroy(&ts->conn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600922
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500923 if (ts->rd)
924 fput(ts->rd);
925 if (ts->wr)
926 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500927
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500928 kfree(ts);
929}
930
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500931/*
932 * stolen from NFS - maybe should be made a generic function?
933 */
934static 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 Garlick2f28c8b2013-05-29 12:15:07 -0700948static 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 Kapshuk6db6ea72020-06-18 21:34:17 +0300955 cl.sin_addr.s_addr = htonl(INADDR_ANY);
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700956 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 Hensbergen8b81ef52008-10-13 18:45:25 -0500966static int
967p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500968{
969 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500970 struct socket *csocket;
971 struct sockaddr_in sin_server;
972 struct p9_fd_opts opts;
973
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600974 err = parse_opts(args, &opts);
975 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500976 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500977
Tomas Bortoli10aa1452018-07-27 13:05:58 +0200978 if (addr == NULL || valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500979 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500980
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500981 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500982
David Howellsc4fac912017-07-05 16:25:37 +0100983 client->trans_opts.tcp.port = opts.port;
984 client->trans_opts.tcp.privport = opts.privport;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500985 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. Biederman0c5c9fb2015-03-11 23:06:44 -0500988 err = __sock_create(current->nsproxy->net_ns, PF_INET,
Rob Landleye75762f2011-05-23 21:46:56 -0500989 SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +0400990 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800991 pr_err("%s (%d): problem creating socket\n",
992 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400993 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500994 }
995
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700996 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 Hensbergena80d9232007-10-17 14:31:07 -05001006 err = csocket->ops->connect(csocket,
1007 (struct sockaddr *)&sin_server,
1008 sizeof(struct sockaddr_in), 0);
1009 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001010 pr_err("%s (%d): problem connecting socket to %s\n",
1011 __func__, task_pid_nr(current), addr);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001012 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +04001013 return err;
1014 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001015
Al Viro6b186622009-08-06 02:02:43 +04001016 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001017}
1018
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001019static int
1020p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001021{
1022 int err;
1023 struct socket *csocket;
1024 struct sockaddr_un sun_server;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001025
1026 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001027
Anant Thazhemadam7ca1db22020-10-12 09:54:04 +05301028 if (!addr || !strlen(addr))
Tomas Bortoli10aa1452018-07-27 13:05:58 +02001029 return -EINVAL;
1030
Dan Carpentercff6b8a2010-07-10 11:51:54 +02001031 if (strlen(addr) >= UNIX_PATH_MAX) {
Joe Perches5d385152011-11-28 10:40:46 -08001032 pr_err("%s (%d): address too long: %s\n",
1033 __func__, task_pid_nr(current), addr);
Al Viro6b186622009-08-06 02:02:43 +04001034 return -ENAMETOOLONG;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001035 }
1036
1037 sun_server.sun_family = PF_UNIX;
1038 strcpy(sun_server.sun_path, addr);
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -05001039 err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
Rob Landleye75762f2011-05-23 21:46:56 -05001040 SOCK_STREAM, 0, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +04001041 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001042 pr_err("%s (%d): problem creating socket\n",
1043 __func__, task_pid_nr(current));
1044
Al Viro6b186622009-08-06 02:02:43 +04001045 return err;
1046 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001047 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1048 sizeof(struct sockaddr_un) - 1, 0);
1049 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001050 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1051 __func__, task_pid_nr(current), addr, err);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001052 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +04001053 return err;
1054 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001055
Al Viro6b186622009-08-06 02:02:43 +04001056 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001057}
1058
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001059static int
1060p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001061{
1062 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001063 struct p9_fd_opts opts;
1064
1065 parse_opts(args, &opts);
David Howellsc4fac912017-07-05 16:25:37 +01001066 client->trans_opts.fd.rfd = opts.rfd;
1067 client->trans_opts.fd.wfd = opts.wfd;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001068
1069 if (opts.rfd == ~0 || opts.wfd == ~0) {
Joe Perches5d385152011-11-28 10:40:46 -08001070 pr_err("Insufficient options for proto=fd\n");
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001071 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001072 }
1073
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001074 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001075 if (err < 0)
Al Viro6b186622009-08-06 02:02:43 +04001076 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001077
Simon Derr263c5822014-03-11 10:13:09 +01001078 p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001079
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001080 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001081}
1082
1083static struct p9_trans_module p9_tcp_trans = {
1084 .name = "tcp",
1085 .maxsize = MAX_SOCK_BUF,
Eric Van Hensbergenf94741f2013-11-12 10:20:03 -06001086 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001087 .create = p9_fd_create_tcp,
1088 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001089 .request = p9_fd_request,
1090 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001091 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001092 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001093 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001094};
1095
1096static struct p9_trans_module p9_unix_trans = {
1097 .name = "unix",
1098 .maxsize = MAX_SOCK_BUF,
1099 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001100 .create = p9_fd_create_unix,
1101 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001102 .request = p9_fd_request,
1103 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001104 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001105 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001106 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001107};
1108
1109static struct p9_trans_module p9_fd_trans = {
1110 .name = "fd",
1111 .maxsize = MAX_SOCK_BUF,
1112 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001113 .create = p9_fd_create,
1114 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001115 .request = p9_fd_request,
1116 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001117 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001118 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001119 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001120};
1121
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001122/**
Sun Lianwen4a026da2018-05-08 09:49:38 +08001123 * p9_poll_workfn - poll worker thread
1124 * @work: work queue
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001125 *
1126 * polls all v9fs transports for new events and queues the appropriate
1127 * work to the work queue
1128 *
1129 */
1130
Tejun Heoaa70c582011-02-01 11:42:43 +01001131static void p9_poll_workfn(struct work_struct *work)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001132{
1133 unsigned long flags;
1134
Joe Perches5d385152011-11-28 10:40:46 -08001135 p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
Tejun Heoaa70c582011-02-01 11:42:43 +01001136
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001137 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 Perches5d385152011-11-28 10:40:46 -08001151 p9_debug(P9_DEBUG_TRANS, "finish\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001152}
1153
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001154int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001155{
1156 v9fs_register_trans(&p9_tcp_trans);
1157 v9fs_register_trans(&p9_unix_trans);
1158 v9fs_register_trans(&p9_fd_trans);
1159
Andrew Morton3387b802008-03-28 14:15:57 -07001160 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001161}
Tejun Heo72029fe2008-09-24 16:22:23 -05001162
1163void p9_trans_fd_exit(void)
1164{
Tejun Heo43829732012-08-20 14:51:24 -07001165 flush_work(&p9_poll_work);
Tejun Heo72029fe2008-09-24 16:22:23 -05001166 v9fs_unregister_trans(&p9_tcp_trans);
1167 v9fs_unregister_trans(&p9_unix_trans);
1168 v9fs_unregister_trans(&p9_fd_trans);
1169}