blob: c0762a302162c9d2127143bf131c49d553706846 [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)
48 *
49 */
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
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -050098 * @req: current request being processed (if any)
99 * @tmp_buf: temporary buffer to read in header
Dominique Martinet947867a2015-09-03 11:28:20 +0200100 * @rc: temporary fcall for reading current frame
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600101 * @wpos: write position for current frame
102 * @wsize: amount of data to write for current frame
103 * @wbuf: current write buffer
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600104 * @poll_pending_link: pending links to be polled per conn
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600105 * @poll_wait: array of wait_q's for various worker threads
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600106 * @pt: poll state
107 * @rq: current read work
108 * @wq: current write work
109 * @wsched: ????
110 *
111 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600112
113struct p9_conn {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600114 struct list_head mux_list;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500115 struct p9_client *client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600116 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600117 struct list_head req_list;
118 struct list_head unsent_req_list;
Tomas Bortoli6d351902018-09-03 18:03:21 +0200119 struct p9_req_t *rreq;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200120 struct p9_req_t *wreq;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500121 char tmp_buf[7];
Dominique Martinet947867a2015-09-03 11:28:20 +0200122 struct p9_fcall rc;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600123 int wpos;
124 int wsize;
125 char *wbuf;
Tejun Heo992b3f12008-10-13 18:45:25 -0500126 struct list_head poll_pending_link;
127 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600128 poll_table pt;
129 struct work_struct rq;
130 struct work_struct wq;
131 unsigned long wsched;
132};
133
Simon Derr263c5822014-03-11 10:13:09 +0100134/**
135 * struct p9_trans_fd - transport state
136 * @rd: reference to file to read from
137 * @wr: reference of file to write to
138 * @conn: connection state reference
139 *
140 */
141
142struct p9_trans_fd {
143 struct file *rd;
144 struct file *wr;
145 struct p9_conn conn;
146};
147
Tejun Heoaa70c582011-02-01 11:42:43 +0100148static void p9_poll_workfn(struct work_struct *work);
149
Tejun Heo992b3f12008-10-13 18:45:25 -0500150static DEFINE_SPINLOCK(p9_poll_lock);
151static LIST_HEAD(p9_poll_pending_list);
Tejun Heoaa70c582011-02-01 11:42:43 +0100152static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600153
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700154static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
155static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
156
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600157static void p9_mux_poll_stop(struct p9_conn *m)
158{
Tejun Heo992b3f12008-10-13 18:45:25 -0500159 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600160 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600161
Tejun Heo992b3f12008-10-13 18:45:25 -0500162 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
163 struct p9_poll_wait *pwait = &m->poll_wait[i];
164
165 if (pwait->wait_addr) {
166 remove_wait_queue(pwait->wait_addr, &pwait->wait);
167 pwait->wait_addr = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600168 }
169 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500170
171 spin_lock_irqsave(&p9_poll_lock, flags);
172 list_del_init(&m->poll_pending_link);
173 spin_unlock_irqrestore(&p9_poll_lock, flags);
Tomas Bortoli430ac662018-07-20 11:27:30 +0200174
175 flush_work(&p9_poll_work);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600176}
177
178/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500179 * p9_conn_cancel - cancel all pending requests with error
180 * @m: mux data
181 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600182 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600183 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600184
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500185static void p9_conn_cancel(struct p9_conn *m, int err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600186{
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500187 struct p9_req_t *req, *rtmp;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500188 LIST_HEAD(cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600189
Joe Perches5d385152011-11-28 10:40:46 -0800190 p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500191
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200192 spin_lock(&m->client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500193
194 if (m->err) {
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200195 spin_unlock(&m->client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500196 return;
197 }
198
199 m->err = err;
200
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500201 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
202 list_move(&req->req_list, &cancel_list);
Tejun Heo992b3f12008-10-13 18:45:25 -0500203 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500204 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
205 list_move(&req->req_list, &cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600206 }
207
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500208 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
Joe Perches5d385152011-11-28 10:40:46 -0800209 p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500210 list_del(&req->req_list);
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100211 if (!req->t_err)
212 req->t_err = err;
213 p9_client_cb(m->client, req, REQ_STATUS_ERROR);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600214 }
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200215 spin_unlock(&m->client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600216}
217
Al Viro7594bf32017-07-16 22:53:08 -0400218static __poll_t
219p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt, int *err)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500220{
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800221 __poll_t ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500222 struct p9_trans_fd *ts = NULL;
223
224 if (client && client->status == Connected)
225 ts = client->trans;
226
Al Viro7594bf32017-07-16 22:53:08 -0400227 if (!ts) {
228 if (err)
229 *err = -EREMOTEIO;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800230 return EPOLLERR;
Al Viro7594bf32017-07-16 22:53:08 -0400231 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500232
Christoph Hellwig9965ed172018-03-05 07:26:05 -0800233 ret = vfs_poll(ts->rd, pt);
234 if (ts->rd != ts->wr)
235 ret = (ret & ~EPOLLOUT) | (vfs_poll(ts->wr, pt) & ~EPOLLIN);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500236 return ret;
237}
238
239/**
240 * p9_fd_read- read from a fd
241 * @client: client instance
242 * @v: buffer to receive data into
243 * @len: size of receive buffer
244 *
245 */
246
247static int p9_fd_read(struct p9_client *client, void *v, int len)
248{
249 int ret;
250 struct p9_trans_fd *ts = NULL;
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200251 loff_t pos;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500252
253 if (client && client->status != Disconnected)
254 ts = client->trans;
255
256 if (!ts)
257 return -EREMOTEIO;
258
259 if (!(ts->rd->f_flags & O_NONBLOCK))
Joe Perches5d385152011-11-28 10:40:46 -0800260 p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500261
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +0200262 pos = ts->rd->f_pos;
263 ret = kernel_read(ts->rd, v, len, &pos);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500264 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
265 client->status = Disconnected;
266 return ret;
267}
268
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600269/**
270 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600271 * @work: container of work to be done
272 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600273 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600274
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600275static void p9_read_work(struct work_struct *work)
276{
Al Viro7594bf32017-07-16 22:53:08 -0400277 __poll_t n;
278 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600279 struct p9_conn *m;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600280
281 m = container_of(work, struct p9_conn, rq);
282
283 if (m->err < 0)
284 return;
285
Dominique Martinet947867a2015-09-03 11:28:20 +0200286 p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600287
Dominique Martinet947867a2015-09-03 11:28:20 +0200288 if (!m->rc.sdata) {
289 m->rc.sdata = m->tmp_buf;
290 m->rc.offset = 0;
291 m->rc.capacity = 7; /* start by reading header */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600292 }
293
294 clear_bit(Rpending, &m->wsched);
Dominique Martinet947867a2015-09-03 11:28:20 +0200295 p9_debug(P9_DEBUG_TRANS, "read mux %p pos %zd size: %zd = %zd\n",
296 m, m->rc.offset, m->rc.capacity,
297 m->rc.capacity - m->rc.offset);
298 err = p9_fd_read(m->client, m->rc.sdata + m->rc.offset,
299 m->rc.capacity - m->rc.offset);
Joe Perches5d385152011-11-28 10:40:46 -0800300 p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
Dominique Martinet947867a2015-09-03 11:28:20 +0200301 if (err == -EAGAIN)
Simon Derr04621942012-09-17 15:16:28 +0200302 goto end_clear;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600303
304 if (err <= 0)
305 goto error;
306
Dominique Martinet947867a2015-09-03 11:28:20 +0200307 m->rc.offset += err;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500308
Dominique Martinet947867a2015-09-03 11:28:20 +0200309 /* header read in */
Tomas Bortoli6d351902018-09-03 18:03:21 +0200310 if ((!m->rreq) && (m->rc.offset == m->rc.capacity)) {
Joe Perches5d385152011-11-28 10:40:46 -0800311 p9_debug(P9_DEBUG_TRANS, "got new header\n");
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500312
Tomas Bortolif9845792018-07-23 17:44:04 +0200313 /* Header size */
314 m->rc.size = 7;
315 err = p9_parse_header(&m->rc, &m->rc.size, NULL, NULL, 0);
Dominique Martinet947867a2015-09-03 11:28:20 +0200316 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800317 p9_debug(P9_DEBUG_ERROR,
Dominique Martinet947867a2015-09-03 11:28:20 +0200318 "error parsing header: %d\n", err);
319 goto error;
320 }
321
322 if (m->rc.size >= m->client->msize) {
323 p9_debug(P9_DEBUG_ERROR,
324 "requested packet size too big: %d\n",
325 m->rc.size);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600326 err = -EIO;
327 goto error;
328 }
329
Joe Perches5d385152011-11-28 10:40:46 -0800330 p9_debug(P9_DEBUG_TRANS,
Dominique Martinet947867a2015-09-03 11:28:20 +0200331 "mux %p pkt: size: %d bytes tag: %d\n",
332 m, m->rc.size, m->rc.tag);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600333
Tomas Bortoli6d351902018-09-03 18:03:21 +0200334 m->rreq = p9_tag_lookup(m->client, m->rc.tag);
335 if (!m->rreq || (m->rreq->status != REQ_STATUS_SENT)) {
Joe Perches5d385152011-11-28 10:40:46 -0800336 p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
Dominique Martinet947867a2015-09-03 11:28:20 +0200337 m->rc.tag);
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500338 err = -EIO;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600339 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500340 }
341
Tomas Bortoli6d351902018-09-03 18:03:21 +0200342 if (!m->rreq->rc.sdata) {
Dominique Martinet30536002015-09-07 17:06:54 +0200343 p9_debug(P9_DEBUG_ERROR,
344 "No recv fcall for tag %d (req %p), disconnecting!\n",
Tomas Bortoli6d351902018-09-03 18:03:21 +0200345 m->rc.tag, m->rreq);
346 m->rreq = NULL;
Dominique Martinet30536002015-09-07 17:06:54 +0200347 err = -EIO;
348 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500349 }
Tomas Bortoli6d351902018-09-03 18:03:21 +0200350 m->rc.sdata = m->rreq->rc.sdata;
Dominique Martinet947867a2015-09-03 11:28:20 +0200351 memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity);
352 m->rc.capacity = m->rc.size;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500353 }
354
Dominique Martinet947867a2015-09-03 11:28:20 +0200355 /* packet is read in
356 * not an else because some packets (like clunk) have no payload
357 */
Tomas Bortoli6d351902018-09-03 18:03:21 +0200358 if ((m->rreq) && (m->rc.offset == m->rc.capacity)) {
Joe Perches5d385152011-11-28 10:40:46 -0800359 p9_debug(P9_DEBUG_TRANS, "got new packet\n");
Tomas Bortoli6d351902018-09-03 18:03:21 +0200360 m->rreq->rc.size = m->rc.offset;
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500361 spin_lock(&m->client->lock);
Dominique Martinete4ca13f2018-10-09 11:18:52 +0900362 if (m->rreq->status == REQ_STATUS_SENT) {
363 list_del(&m->rreq->req_list);
364 p9_client_cb(m->client, m->rreq, REQ_STATUS_RCVD);
Wang Hai74d6a5d2020-06-12 17:08:33 +0800365 } else if (m->rreq->status == REQ_STATUS_FLSHD) {
366 /* Ignore replies associated with a cancelled request. */
367 p9_debug(P9_DEBUG_TRANS,
368 "Ignore replies associated with a cancelled request\n");
Dominique Martinete4ca13f2018-10-09 11:18:52 +0900369 } else {
370 spin_unlock(&m->client->lock);
371 p9_debug(P9_DEBUG_ERROR,
372 "Request tag %d errored out while we were reading the reply\n",
373 m->rc.tag);
374 err = -EIO;
375 goto error;
376 }
Tomas Bortoli9f476d72018-07-23 20:42:53 +0200377 spin_unlock(&m->client->lock);
Dominique Martinet947867a2015-09-03 11:28:20 +0200378 m->rc.sdata = NULL;
379 m->rc.offset = 0;
380 m->rc.capacity = 0;
Tomas Bortoli6d351902018-09-03 18:03:21 +0200381 p9_req_put(m->rreq);
382 m->rreq = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600383 }
384
Simon Derr04621942012-09-17 15:16:28 +0200385end_clear:
386 clear_bit(Rworksched, &m->wsched);
387
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600388 if (!list_empty(&m->req_list)) {
389 if (test_and_clear_bit(Rpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800390 n = EPOLLIN;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600391 else
Al Viro7594bf32017-07-16 22:53:08 -0400392 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600393
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800394 if ((n & EPOLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800395 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100396 schedule_work(&m->rq);
Simon Derr04621942012-09-17 15:16:28 +0200397 }
398 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600399
400 return;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600401error:
402 p9_conn_cancel(m, err);
403 clear_bit(Rworksched, &m->wsched);
404}
405
406/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500407 * p9_fd_write - write to a socket
408 * @client: client instance
409 * @v: buffer to send data from
410 * @len: size of send buffer
411 *
412 */
413
414static int p9_fd_write(struct p9_client *client, void *v, int len)
415{
Christoph Hellwig670986e2017-09-01 17:39:18 +0200416 ssize_t ret;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500417 struct p9_trans_fd *ts = NULL;
418
419 if (client && client->status != Disconnected)
420 ts = client->trans;
421
422 if (!ts)
423 return -EREMOTEIO;
424
425 if (!(ts->wr->f_flags & O_NONBLOCK))
Joe Perches5d385152011-11-28 10:40:46 -0800426 p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500427
Christoph Hellwig670986e2017-09-01 17:39:18 +0200428 ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500429 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
430 client->status = Disconnected;
431 return ret;
432}
433
434/**
435 * p9_write_work - called when a transport can send some data
436 * @work: container for work to be done
437 *
438 */
439
440static void p9_write_work(struct work_struct *work)
441{
Al Viro7594bf32017-07-16 22:53:08 -0400442 __poll_t n;
443 int err;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500444 struct p9_conn *m;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500445 struct p9_req_t *req;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500446
447 m = container_of(work, struct p9_conn, wq);
448
449 if (m->err < 0) {
450 clear_bit(Wworksched, &m->wsched);
451 return;
452 }
453
454 if (!m->wsize) {
Simon Derr759f4292012-09-17 15:16:31 +0200455 spin_lock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500456 if (list_empty(&m->unsent_req_list)) {
457 clear_bit(Wworksched, &m->wsched);
Simon Derr759f4292012-09-17 15:16:31 +0200458 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500459 return;
460 }
461
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500462 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500463 req_list);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500464 req->status = REQ_STATUS_SENT;
Joe Perches5d385152011-11-28 10:40:46 -0800465 p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500466 list_move_tail(&req->req_list, &m->req_list);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500467
Dominique Martinet523adb62018-07-30 05:55:19 +0000468 m->wbuf = req->tc.sdata;
469 m->wsize = req->tc.size;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500470 m->wpos = 0;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200471 p9_req_get(req);
472 m->wreq = req;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500473 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500474 }
475
Joe Perches5d385152011-11-28 10:40:46 -0800476 p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
477 m, m->wpos, m->wsize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500478 clear_bit(Wpending, &m->wsched);
479 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
Joe Perches5d385152011-11-28 10:40:46 -0800480 p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
Simon Derr584a8c12012-09-17 15:16:30 +0200481 if (err == -EAGAIN)
482 goto end_clear;
483
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500484
485 if (err < 0)
486 goto error;
487 else if (err == 0) {
488 err = -EREMOTEIO;
489 goto error;
490 }
491
492 m->wpos += err;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200493 if (m->wpos == m->wsize) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500494 m->wpos = m->wsize = 0;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200495 p9_req_put(m->wreq);
496 m->wreq = NULL;
497 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500498
Simon Derr584a8c12012-09-17 15:16:30 +0200499end_clear:
500 clear_bit(Wworksched, &m->wsched);
501
Simon Derr1957b3a2012-09-17 15:16:29 +0200502 if (m->wsize || !list_empty(&m->unsent_req_list)) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500503 if (test_and_clear_bit(Wpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800504 n = EPOLLOUT;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500505 else
Al Viro7594bf32017-07-16 22:53:08 -0400506 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500507
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800508 if ((n & EPOLLOUT) &&
Simon Derr584a8c12012-09-17 15:16:30 +0200509 !test_and_set_bit(Wworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800510 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100511 schedule_work(&m->wq);
Simon Derr584a8c12012-09-17 15:16:30 +0200512 }
513 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500514
515 return;
516
517error:
518 p9_conn_cancel(m, err);
519 clear_bit(Wworksched, &m->wsched);
520}
521
Ingo Molnarac6424b2017-06-20 12:06:13 +0200522static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500523{
524 struct p9_poll_wait *pwait =
525 container_of(wait, struct p9_poll_wait, wait);
526 struct p9_conn *m = pwait->conn;
527 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500528
529 spin_lock_irqsave(&p9_poll_lock, flags);
530 if (list_empty(&m->poll_pending_link))
531 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
532 spin_unlock_irqrestore(&p9_poll_lock, flags);
533
Tejun Heoaa70c582011-02-01 11:42:43 +0100534 schedule_work(&p9_poll_work);
535 return 1;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500536}
537
538/**
539 * p9_pollwait - add poll task to the wait queue
540 * @filp: file pointer being polled
541 * @wait_address: wait_q to block on
542 * @p: poll state
543 *
544 * called by files poll operation to add v9fs-poll task to files wait queue
545 */
546
547static void
548p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
549{
550 struct p9_conn *m = container_of(p, struct p9_conn, pt);
551 struct p9_poll_wait *pwait = NULL;
552 int i;
553
554 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
555 if (m->poll_wait[i].wait_addr == NULL) {
556 pwait = &m->poll_wait[i];
557 break;
558 }
559 }
560
561 if (!pwait) {
Joe Perches5d385152011-11-28 10:40:46 -0800562 p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500563 return;
564 }
565
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500566 pwait->conn = m;
567 pwait->wait_addr = wait_address;
568 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
569 add_wait_queue(wait_address, &pwait->wait);
570}
571
572/**
Simon Derr263c5822014-03-11 10:13:09 +0100573 * p9_conn_create - initialize the per-session mux data
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500574 * @client: client instance
575 *
576 * Note: Creates the polling task if this is the first session.
577 */
578
Simon Derr263c5822014-03-11 10:13:09 +0100579static void p9_conn_create(struct p9_client *client)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500580{
Al Viro7594bf32017-07-16 22:53:08 -0400581 __poll_t n;
Simon Derr263c5822014-03-11 10:13:09 +0100582 struct p9_trans_fd *ts = client->trans;
583 struct p9_conn *m = &ts->conn;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500584
Joe Perches5d385152011-11-28 10:40:46 -0800585 p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500586
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500587 INIT_LIST_HEAD(&m->mux_list);
588 m->client = client;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500589
590 INIT_LIST_HEAD(&m->req_list);
591 INIT_LIST_HEAD(&m->unsent_req_list);
592 INIT_WORK(&m->rq, p9_read_work);
593 INIT_WORK(&m->wq, p9_write_work);
594 INIT_LIST_HEAD(&m->poll_pending_link);
595 init_poll_funcptr(&m->pt, p9_pollwait);
596
Al Viro7594bf32017-07-16 22:53:08 -0400597 n = p9_fd_poll(client, &m->pt, NULL);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800598 if (n & EPOLLIN) {
Joe Perches5d385152011-11-28 10:40:46 -0800599 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500600 set_bit(Rpending, &m->wsched);
601 }
602
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800603 if (n & EPOLLOUT) {
Joe Perches5d385152011-11-28 10:40:46 -0800604 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500605 set_bit(Wpending, &m->wsched);
606 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500607}
608
609/**
610 * p9_poll_mux - polls a mux and schedules read or write works if necessary
611 * @m: connection to poll
612 *
613 */
614
615static void p9_poll_mux(struct p9_conn *m)
616{
Al Viro7594bf32017-07-16 22:53:08 -0400617 __poll_t n;
618 int err = -ECONNRESET;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500619
620 if (m->err < 0)
621 return;
622
Al Viro7594bf32017-07-16 22:53:08 -0400623 n = p9_fd_poll(m->client, NULL, &err);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800624 if (n & (EPOLLERR | EPOLLHUP | EPOLLNVAL)) {
Joe Perches5d385152011-11-28 10:40:46 -0800625 p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
Al Viro7594bf32017-07-16 22:53:08 -0400626 p9_conn_cancel(m, err);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500627 }
628
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800629 if (n & EPOLLIN) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500630 set_bit(Rpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800631 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500632 if (!test_and_set_bit(Rworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800633 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100634 schedule_work(&m->rq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500635 }
636 }
637
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800638 if (n & EPOLLOUT) {
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500639 set_bit(Wpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800640 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800641 if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
642 !test_and_set_bit(Wworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800643 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100644 schedule_work(&m->wq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500645 }
646 }
647}
648
649/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500650 * p9_fd_request - send 9P request
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600651 * The function can sleep until the request is scheduled for sending.
652 * The function can be interrupted. Return from the function is not
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500653 * a guarantee that the request is sent successfully.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600654 *
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500655 * @client: client instance
656 * @req: request to be sent
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600657 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600658 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600659
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500660static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600661{
Al Viro7594bf32017-07-16 22:53:08 -0400662 __poll_t n;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500663 struct p9_trans_fd *ts = client->trans;
Simon Derr263c5822014-03-11 10:13:09 +0100664 struct p9_conn *m = &ts->conn;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600665
Joe Perches5d385152011-11-28 10:40:46 -0800666 p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
Dominique Martinet523adb62018-07-30 05:55:19 +0000667 m, current, &req->tc, req->tc.id);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600668 if (m->err < 0)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500669 return m->err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600670
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500671 spin_lock(&client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500672 req->status = REQ_STATUS_UNSENT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600673 list_add_tail(&req->req_list, &m->unsent_req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500674 spin_unlock(&client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600675
676 if (test_and_clear_bit(Wpending, &m->wsched))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800677 n = EPOLLOUT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600678 else
Al Viro7594bf32017-07-16 22:53:08 -0400679 n = p9_fd_poll(m->client, NULL, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600680
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800681 if (n & EPOLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
Tejun Heo61edeee2011-02-01 11:42:43 +0100682 schedule_work(&m->wq);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600683
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500684 return 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600685}
686
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500687static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600688{
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500689 int ret = 1;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600690
Joe Perches5d385152011-11-28 10:40:46 -0800691 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600692
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500693 spin_lock(&client->lock);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500694
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500695 if (req->status == REQ_STATUS_UNSENT) {
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500696 list_del(&req->req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500697 req->status = REQ_STATUS_FLSHD;
Tomas Bortoli728356d2018-08-14 19:43:42 +0200698 p9_req_put(req);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500699 ret = 0;
Simon Derr0bfd6842014-03-10 16:38:52 +0100700 }
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500701 spin_unlock(&client->lock);
702
703 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600704}
705
Simon Derrafd8d652014-03-10 16:38:49 +0100706static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
707{
708 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
709
Wang Hai74d6a5d2020-06-12 17:08:33 +0800710 spin_lock(&client->lock);
711 /* Ignore cancelled request if message has been received
712 * before lock.
713 */
714 if (req->status == REQ_STATUS_RCVD) {
715 spin_unlock(&client->lock);
716 return 0;
717 }
718
Simon Derrafd8d652014-03-10 16:38:49 +0100719 /* we haven't received a response for oldreq,
720 * remove it from the list.
721 */
Simon Derrafd8d652014-03-10 16:38:49 +0100722 list_del(&req->req_list);
Wang Hai74d6a5d2020-06-12 17:08:33 +0800723 req->status = REQ_STATUS_FLSHD;
Simon Derrafd8d652014-03-10 16:38:49 +0100724 spin_unlock(&client->lock);
Tomas Bortoli728356d2018-08-14 19:43:42 +0200725 p9_req_put(req);
Simon Derrafd8d652014-03-10 16:38:49 +0100726
727 return 0;
728}
729
David Howellsc4fac912017-07-05 16:25:37 +0100730static int p9_fd_show_options(struct seq_file *m, struct p9_client *clnt)
731{
732 if (clnt->trans_mod == &p9_tcp_trans) {
733 if (clnt->trans_opts.tcp.port != P9_PORT)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200734 seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port);
David Howellsc4fac912017-07-05 16:25:37 +0100735 } else if (clnt->trans_mod == &p9_fd_trans) {
736 if (clnt->trans_opts.fd.rfd != ~0)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200737 seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd);
David Howellsc4fac912017-07-05 16:25:37 +0100738 if (clnt->trans_opts.fd.wfd != ~0)
Tuomas Tynkkynen61b272c2017-11-19 11:28:43 +0200739 seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd);
David Howellsc4fac912017-07-05 16:25:37 +0100740 }
741 return 0;
742}
743
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600744/**
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600745 * parse_opts - parse mount options into p9_fd_opts structure
746 * @params: options string passed from mount
747 * @opts: fd transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500748 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600749 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500750 */
751
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600752static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500753{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500754 char *p;
755 substring_t args[MAX_OPT_ARGS];
756 int option;
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600757 char *options, *tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500758
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500759 opts->port = P9_PORT;
760 opts->rfd = ~0;
761 opts->wfd = ~0;
David Howellsc4fac912017-07-05 16:25:37 +0100762 opts->privport = false;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500763
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600764 if (!params)
765 return 0;
766
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600767 tmp_options = kstrdup(params, GFP_KERNEL);
768 if (!tmp_options) {
Joe Perches5d385152011-11-28 10:40:46 -0800769 p9_debug(P9_DEBUG_ERROR,
770 "failed to allocate copy of option string\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600771 return -ENOMEM;
772 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600773 options = tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500774
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500775 while ((p = strsep(&options, ",")) != NULL) {
776 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600777 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500778 if (!*p)
779 continue;
780 token = match_token(p, tokens, args);
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700781 if ((token != Opt_err) && (token != Opt_privport)) {
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000782 r = match_int(&args[0], &option);
783 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800784 p9_debug(P9_DEBUG_ERROR,
785 "integer field, but no integer?\n");
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000786 continue;
787 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500788 }
789 switch (token) {
790 case Opt_port:
791 opts->port = option;
792 break;
793 case Opt_rfdno:
794 opts->rfd = option;
795 break;
796 case Opt_wfdno:
797 opts->wfd = option;
798 break;
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700799 case Opt_privport:
David Howellsc4fac912017-07-05 16:25:37 +0100800 opts->privport = true;
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700801 break;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500802 default:
803 continue;
804 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500805 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600806
807 kfree(tmp_options);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600808 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500809}
810
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500811static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500812{
Simon Derr263c5822014-03-11 10:13:09 +0100813 struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500814 GFP_KERNEL);
815 if (!ts)
816 return -ENOMEM;
817
818 ts->rd = fget(rfd);
Christoph Hellwiga39c4602020-07-10 10:57:22 +0200819 if (!ts->rd)
820 goto out_free_ts;
821 if (!(ts->rd->f_mode & FMODE_READ))
822 goto out_put_rd;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500823 ts->wr = fget(wfd);
Christoph Hellwiga39c4602020-07-10 10:57:22 +0200824 if (!ts->wr)
825 goto out_put_rd;
826 if (!(ts->wr->f_mode & FMODE_WRITE))
827 goto out_put_wr;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500828
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500829 client->trans = ts;
830 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500831
832 return 0;
Christoph Hellwiga39c4602020-07-10 10:57:22 +0200833
834out_put_wr:
835 fput(ts->wr);
836out_put_rd:
837 fput(ts->rd);
838out_free_ts:
839 kfree(ts);
840 return -EIO;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500841}
842
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500843static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500844{
Al Viro6b186622009-08-06 02:02:43 +0400845 struct p9_trans_fd *p;
Al Viro56b31d12012-08-18 00:25:51 -0400846 struct file *file;
Al Viro6b186622009-08-06 02:02:43 +0400847
Simon Derr263c5822014-03-11 10:13:09 +0100848 p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
Al Viro6b186622009-08-06 02:02:43 +0400849 if (!p)
850 return -ENOMEM;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500851
852 csocket->sk->sk_allocation = GFP_NOIO;
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700853 file = sock_alloc_file(csocket, 0, NULL);
Al Viro56b31d12012-08-18 00:25:51 -0400854 if (IS_ERR(file)) {
Joe Perches5d385152011-11-28 10:40:46 -0800855 pr_err("%s (%d): failed to map fd\n",
856 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400857 kfree(p);
Al Viro56b31d12012-08-18 00:25:51 -0400858 return PTR_ERR(file);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500859 }
860
Al Viro56b31d12012-08-18 00:25:51 -0400861 get_file(file);
862 p->wr = p->rd = file;
Al Viro6b186622009-08-06 02:02:43 +0400863 client->trans = p;
864 client->status = Connected;
865
Al Viro6b186622009-08-06 02:02:43 +0400866 p->rd->f_flags |= O_NONBLOCK;
867
Simon Derr263c5822014-03-11 10:13:09 +0100868 p9_conn_create(client);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500869 return 0;
870}
871
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500872/**
Simon Derr263c5822014-03-11 10:13:09 +0100873 * p9_mux_destroy - cancels all pending requests of mux
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500874 * @m: mux to destroy
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500875 *
876 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600877
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500878static void p9_conn_destroy(struct p9_conn *m)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500879{
Joe Perches5d385152011-11-28 10:40:46 -0800880 p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
881 m, m->mux_list.prev, m->mux_list.next);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500882
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500883 p9_mux_poll_stop(m);
884 cancel_work_sync(&m->rq);
Dominique Martinetfb488fc2018-10-09 11:38:00 +0900885 if (m->rreq) {
886 p9_req_put(m->rreq);
887 m->rreq = NULL;
888 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500889 cancel_work_sync(&m->wq);
Dominique Martinetfb488fc2018-10-09 11:38:00 +0900890 if (m->wreq) {
891 p9_req_put(m->wreq);
892 m->wreq = NULL;
893 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500894
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500895 p9_conn_cancel(m, -ECONNRESET);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500896
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500897 m->client = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500898}
899
900/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500901 * p9_fd_close - shutdown file descriptor transport
902 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500903 *
904 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600905
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500906static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500907{
908 struct p9_trans_fd *ts;
909
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500910 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500911 return;
912
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500913 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500914 if (!ts)
915 return;
916
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500917 client->status = Disconnected;
918
Simon Derr263c5822014-03-11 10:13:09 +0100919 p9_conn_destroy(&ts->conn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600920
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500921 if (ts->rd)
922 fput(ts->rd);
923 if (ts->wr)
924 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500925
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500926 kfree(ts);
927}
928
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500929/*
930 * stolen from NFS - maybe should be made a generic function?
931 */
932static inline int valid_ipaddr4(const char *buf)
933{
934 int rc, count, in[4];
935
936 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
937 if (rc != 4)
938 return -EINVAL;
939 for (count = 0; count < 4; count++) {
940 if (in[count] > 255)
941 return -EINVAL;
942 }
943 return 0;
944}
945
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700946static int p9_bind_privport(struct socket *sock)
947{
948 struct sockaddr_in cl;
949 int port, err = -EINVAL;
950
951 memset(&cl, 0, sizeof(cl));
952 cl.sin_family = AF_INET;
Alexander Kapshuk6db6ea72020-06-18 21:34:17 +0300953 cl.sin_addr.s_addr = htonl(INADDR_ANY);
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700954 for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
955 cl.sin_port = htons((ushort)port);
956 err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
957 if (err != -EADDRINUSE)
958 break;
959 }
960 return err;
961}
962
963
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500964static int
965p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500966{
967 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500968 struct socket *csocket;
969 struct sockaddr_in sin_server;
970 struct p9_fd_opts opts;
971
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600972 err = parse_opts(args, &opts);
973 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500974 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500975
Tomas Bortoli10aa1452018-07-27 13:05:58 +0200976 if (addr == NULL || valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500977 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500978
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500979 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500980
David Howellsc4fac912017-07-05 16:25:37 +0100981 client->trans_opts.tcp.port = opts.port;
982 client->trans_opts.tcp.privport = opts.privport;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500983 sin_server.sin_family = AF_INET;
984 sin_server.sin_addr.s_addr = in_aton(addr);
985 sin_server.sin_port = htons(opts.port);
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -0500986 err = __sock_create(current->nsproxy->net_ns, PF_INET,
Rob Landleye75762f2011-05-23 21:46:56 -0500987 SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +0400988 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800989 pr_err("%s (%d): problem creating socket\n",
990 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400991 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500992 }
993
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700994 if (opts.privport) {
995 err = p9_bind_privport(csocket);
996 if (err < 0) {
997 pr_err("%s (%d): problem binding to privport\n",
998 __func__, task_pid_nr(current));
999 sock_release(csocket);
1000 return err;
1001 }
1002 }
1003
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001004 err = csocket->ops->connect(csocket,
1005 (struct sockaddr *)&sin_server,
1006 sizeof(struct sockaddr_in), 0);
1007 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001008 pr_err("%s (%d): problem connecting socket to %s\n",
1009 __func__, task_pid_nr(current), addr);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001010 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +04001011 return err;
1012 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001013
Al Viro6b186622009-08-06 02:02:43 +04001014 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001015}
1016
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001017static int
1018p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001019{
1020 int err;
1021 struct socket *csocket;
1022 struct sockaddr_un sun_server;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001023
1024 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001025
Tomas Bortoli10aa1452018-07-27 13:05:58 +02001026 if (addr == NULL)
1027 return -EINVAL;
1028
Dan Carpentercff6b8a2010-07-10 11:51:54 +02001029 if (strlen(addr) >= UNIX_PATH_MAX) {
Joe Perches5d385152011-11-28 10:40:46 -08001030 pr_err("%s (%d): address too long: %s\n",
1031 __func__, task_pid_nr(current), addr);
Al Viro6b186622009-08-06 02:02:43 +04001032 return -ENAMETOOLONG;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001033 }
1034
1035 sun_server.sun_family = PF_UNIX;
1036 strcpy(sun_server.sun_path, addr);
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -05001037 err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
Rob Landleye75762f2011-05-23 21:46:56 -05001038 SOCK_STREAM, 0, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +04001039 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001040 pr_err("%s (%d): problem creating socket\n",
1041 __func__, task_pid_nr(current));
1042
Al Viro6b186622009-08-06 02:02:43 +04001043 return err;
1044 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001045 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1046 sizeof(struct sockaddr_un) - 1, 0);
1047 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001048 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1049 __func__, task_pid_nr(current), addr, err);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001050 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +04001051 return err;
1052 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001053
Al Viro6b186622009-08-06 02:02:43 +04001054 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001055}
1056
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001057static int
1058p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001059{
1060 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001061 struct p9_fd_opts opts;
1062
1063 parse_opts(args, &opts);
David Howellsc4fac912017-07-05 16:25:37 +01001064 client->trans_opts.fd.rfd = opts.rfd;
1065 client->trans_opts.fd.wfd = opts.wfd;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001066
1067 if (opts.rfd == ~0 || opts.wfd == ~0) {
Joe Perches5d385152011-11-28 10:40:46 -08001068 pr_err("Insufficient options for proto=fd\n");
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001069 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001070 }
1071
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001072 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001073 if (err < 0)
Al Viro6b186622009-08-06 02:02:43 +04001074 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001075
Simon Derr263c5822014-03-11 10:13:09 +01001076 p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001077
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001078 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001079}
1080
1081static struct p9_trans_module p9_tcp_trans = {
1082 .name = "tcp",
1083 .maxsize = MAX_SOCK_BUF,
Eric Van Hensbergenf94741f2013-11-12 10:20:03 -06001084 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001085 .create = p9_fd_create_tcp,
1086 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001087 .request = p9_fd_request,
1088 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001089 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001090 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001091 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001092};
1093
1094static struct p9_trans_module p9_unix_trans = {
1095 .name = "unix",
1096 .maxsize = MAX_SOCK_BUF,
1097 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001098 .create = p9_fd_create_unix,
1099 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001100 .request = p9_fd_request,
1101 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001102 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001103 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001104 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001105};
1106
1107static struct p9_trans_module p9_fd_trans = {
1108 .name = "fd",
1109 .maxsize = MAX_SOCK_BUF,
1110 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001111 .create = p9_fd_create,
1112 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001113 .request = p9_fd_request,
1114 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001115 .cancelled = p9_fd_cancelled,
David Howellsc4fac912017-07-05 16:25:37 +01001116 .show_options = p9_fd_show_options,
Tejun Heo72029fe2008-09-24 16:22:23 -05001117 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001118};
1119
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001120/**
Sun Lianwen4a026da2018-05-08 09:49:38 +08001121 * p9_poll_workfn - poll worker thread
1122 * @work: work queue
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001123 *
1124 * polls all v9fs transports for new events and queues the appropriate
1125 * work to the work queue
1126 *
1127 */
1128
Tejun Heoaa70c582011-02-01 11:42:43 +01001129static void p9_poll_workfn(struct work_struct *work)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001130{
1131 unsigned long flags;
1132
Joe Perches5d385152011-11-28 10:40:46 -08001133 p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
Tejun Heoaa70c582011-02-01 11:42:43 +01001134
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001135 spin_lock_irqsave(&p9_poll_lock, flags);
1136 while (!list_empty(&p9_poll_pending_list)) {
1137 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1138 struct p9_conn,
1139 poll_pending_link);
1140 list_del_init(&conn->poll_pending_link);
1141 spin_unlock_irqrestore(&p9_poll_lock, flags);
1142
1143 p9_poll_mux(conn);
1144
1145 spin_lock_irqsave(&p9_poll_lock, flags);
1146 }
1147 spin_unlock_irqrestore(&p9_poll_lock, flags);
1148
Joe Perches5d385152011-11-28 10:40:46 -08001149 p9_debug(P9_DEBUG_TRANS, "finish\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001150}
1151
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001152int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001153{
1154 v9fs_register_trans(&p9_tcp_trans);
1155 v9fs_register_trans(&p9_unix_trans);
1156 v9fs_register_trans(&p9_fd_trans);
1157
Andrew Morton3387b802008-03-28 14:15:57 -07001158 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001159}
Tejun Heo72029fe2008-09-24 16:22:23 -05001160
1161void p9_trans_fd_exit(void)
1162{
Tejun Heo43829732012-08-20 14:51:24 -07001163 flush_work(&p9_poll_work);
Tejun Heo72029fe2008-09-24 16:22:23 -05001164 v9fs_unregister_trans(&p9_tcp_trans);
1165 v9fs_unregister_trans(&p9_unix_trans);
1166 v9fs_unregister_trans(&p9_fd_trans);
1167}