blob: fda4951c869e88cd4fefa1895a6ed986184d421f [file] [log] [blame]
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001/*
2 * linux/fs/9p/trans_fd.c
3 *
4 * Fd transport layer. Includes deprecated socket layer.
5 *
6 * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
7 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06008 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05009 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to:
22 * Free Software Foundation
23 * 51 Franklin Street, Fifth Floor
24 * Boston, MA 02111-1301 USA
25 *
26 */
27
Joe Perches5d385152011-11-28 10:40:46 -080028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050030#include <linux/in.h>
31#include <linux/module.h>
32#include <linux/net.h>
33#include <linux/ipv6.h>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060034#include <linux/kthread.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050035#include <linux/errno.h>
36#include <linux/kernel.h>
37#include <linux/un.h>
38#include <linux/uaccess.h>
39#include <linux/inet.h>
40#include <linux/idr.h>
41#include <linux/file.h>
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050042#include <linux/parser.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050044#include <net/9p/9p.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050045#include <net/9p/client.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050046#include <net/9p/transport.h>
47
Al Viro6b186622009-08-06 02:02:43 +040048#include <linux/syscalls.h> /* killme */
49
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050050#define P9_PORT 564
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050051#define MAX_SOCK_BUF (64*1024)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060052#define MAXPOLLWADDR 2
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050053
Eric Van Hensbergenee443992008-03-05 07:08:09 -060054/**
55 * struct p9_fd_opts - per-transport options
56 * @rfd: file descriptor for reading (trans=fd)
57 * @wfd: file descriptor for writing (trans=fd)
58 * @port: port to connect to (trans=tcp)
59 *
60 */
61
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050062struct p9_fd_opts {
63 int rfd;
64 int wfd;
65 u16 port;
Jim Garlick2f28c8b2013-05-29 12:15:07 -070066 int privport;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050067};
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050068
Eric Van Hensbergenee443992008-03-05 07:08:09 -060069/**
70 * struct p9_trans_fd - transport state
71 * @rd: reference to file to read from
72 * @wr: reference of file to write to
73 * @conn: connection state reference
74 *
75 */
76
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050077struct p9_trans_fd {
78 struct file *rd;
79 struct file *wr;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060080 struct p9_conn *conn;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050081};
82
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050083/*
84 * Option Parsing (code inspired by NFS code)
85 * - a little lazy - parse all fd-transport options
86 */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050087
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050088enum {
89 /* Options that take integer arguments */
Latchesar Ionkov55762692007-11-06 08:02:53 -060090 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
Jim Garlick2f28c8b2013-05-29 12:15:07 -070091 /* Options that take no arguments */
92 Opt_privport,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050093};
94
Steven Whitehousea447c092008-10-13 10:46:57 +010095static const match_table_t tokens = {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050096 {Opt_port, "port=%u"},
97 {Opt_rfdno, "rfdno=%u"},
98 {Opt_wfdno, "wfdno=%u"},
Jim Garlick2f28c8b2013-05-29 12:15:07 -070099 {Opt_privport, "privport"},
Latchesar Ionkov55762692007-11-06 08:02:53 -0600100 {Opt_err, NULL},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500101};
102
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600103enum {
104 Rworksched = 1, /* read work scheduled or running */
105 Rpending = 2, /* can read */
106 Wworksched = 4, /* write work scheduled or running */
107 Wpending = 8, /* can write */
108};
109
Tejun Heo992b3f12008-10-13 18:45:25 -0500110struct p9_poll_wait {
111 struct p9_conn *conn;
112 wait_queue_t wait;
113 wait_queue_head_t *wait_addr;
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600114};
115
116/**
117 * struct p9_conn - fd mux connection state information
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600118 * @mux_list: list link for mux to manage multiple connections (?)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500119 * @client: reference to client instance for this connection
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600120 * @err: error state
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600121 * @req_list: accounting for requests which have been sent
122 * @unsent_req_list: accounting for requests that haven't been sent
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500123 * @req: current request being processed (if any)
124 * @tmp_buf: temporary buffer to read in header
125 * @rsize: amount to read for current frame
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600126 * @rpos: read position in current frame
127 * @rbuf: current read buffer
128 * @wpos: write position for current frame
129 * @wsize: amount of data to write for current frame
130 * @wbuf: current write buffer
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600131 * @poll_pending_link: pending links to be polled per conn
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600132 * @poll_wait: array of wait_q's for various worker threads
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600133 * @pt: poll state
134 * @rq: current read work
135 * @wq: current write work
136 * @wsched: ????
137 *
138 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600139
140struct p9_conn {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600141 struct list_head mux_list;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500142 struct p9_client *client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600143 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600144 struct list_head req_list;
145 struct list_head unsent_req_list;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500146 struct p9_req_t *req;
147 char tmp_buf[7];
148 int rsize;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600149 int rpos;
150 char *rbuf;
151 int wpos;
152 int wsize;
153 char *wbuf;
Tejun Heo992b3f12008-10-13 18:45:25 -0500154 struct list_head poll_pending_link;
155 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600156 poll_table pt;
157 struct work_struct rq;
158 struct work_struct wq;
159 unsigned long wsched;
160};
161
Tejun Heoaa70c582011-02-01 11:42:43 +0100162static void p9_poll_workfn(struct work_struct *work);
163
Tejun Heo992b3f12008-10-13 18:45:25 -0500164static DEFINE_SPINLOCK(p9_poll_lock);
165static LIST_HEAD(p9_poll_pending_list);
Tejun Heoaa70c582011-02-01 11:42:43 +0100166static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600167
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700168static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
169static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
170
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600171static void p9_mux_poll_stop(struct p9_conn *m)
172{
Tejun Heo992b3f12008-10-13 18:45:25 -0500173 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600174 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600175
Tejun Heo992b3f12008-10-13 18:45:25 -0500176 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
177 struct p9_poll_wait *pwait = &m->poll_wait[i];
178
179 if (pwait->wait_addr) {
180 remove_wait_queue(pwait->wait_addr, &pwait->wait);
181 pwait->wait_addr = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600182 }
183 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500184
185 spin_lock_irqsave(&p9_poll_lock, flags);
186 list_del_init(&m->poll_pending_link);
187 spin_unlock_irqrestore(&p9_poll_lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600188}
189
190/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500191 * p9_conn_cancel - cancel all pending requests with error
192 * @m: mux data
193 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600194 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600195 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600196
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500197static void p9_conn_cancel(struct p9_conn *m, int err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600198{
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500199 struct p9_req_t *req, *rtmp;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500200 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500201 LIST_HEAD(cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600202
Joe Perches5d385152011-11-28 10:40:46 -0800203 p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500204
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500205 spin_lock_irqsave(&m->client->lock, flags);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500206
207 if (m->err) {
208 spin_unlock_irqrestore(&m->client->lock, flags);
209 return;
210 }
211
212 m->err = err;
213
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500214 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
215 list_move(&req->req_list, &cancel_list);
Tejun Heo992b3f12008-10-13 18:45:25 -0500216 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500217 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
218 list_move(&req->req_list, &cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600219 }
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500220 spin_unlock_irqrestore(&m->client->lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600221
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500222 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
Joe Perches5d385152011-11-28 10:40:46 -0800223 p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500224 list_del(&req->req_list);
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100225 if (!req->t_err)
226 req->t_err = err;
227 p9_client_cb(m->client, req, REQ_STATUS_ERROR);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600228 }
229}
230
Julia Lawall29af93092010-09-05 09:00:21 +0000231static int
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500232p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
233{
234 int ret, n;
235 struct p9_trans_fd *ts = NULL;
236
237 if (client && client->status == Connected)
238 ts = client->trans;
239
240 if (!ts)
241 return -EREMOTEIO;
242
Al Viro72c2d532013-09-22 16:27:52 -0400243 if (!ts->rd->f_op->poll)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500244 return -EIO;
245
Al Viro72c2d532013-09-22 16:27:52 -0400246 if (!ts->wr->f_op->poll)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500247 return -EIO;
248
249 ret = ts->rd->f_op->poll(ts->rd, pt);
250 if (ret < 0)
251 return ret;
252
253 if (ts->rd != ts->wr) {
254 n = ts->wr->f_op->poll(ts->wr, pt);
255 if (n < 0)
256 return n;
257 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
258 }
259
260 return ret;
261}
262
263/**
264 * p9_fd_read- read from a fd
265 * @client: client instance
266 * @v: buffer to receive data into
267 * @len: size of receive buffer
268 *
269 */
270
271static int p9_fd_read(struct p9_client *client, void *v, int len)
272{
273 int ret;
274 struct p9_trans_fd *ts = NULL;
275
276 if (client && client->status != Disconnected)
277 ts = client->trans;
278
279 if (!ts)
280 return -EREMOTEIO;
281
282 if (!(ts->rd->f_flags & O_NONBLOCK))
Joe Perches5d385152011-11-28 10:40:46 -0800283 p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500284
285 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
286 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
287 client->status = Disconnected;
288 return ret;
289}
290
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600291/**
292 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600293 * @work: container of work to be done
294 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600295 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600296
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600297static void p9_read_work(struct work_struct *work)
298{
299 int n, err;
300 struct p9_conn *m;
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100301 int status = REQ_STATUS_ERROR;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600302
303 m = container_of(work, struct p9_conn, rq);
304
305 if (m->err < 0)
306 return;
307
Joe Perches5d385152011-11-28 10:40:46 -0800308 p9_debug(P9_DEBUG_TRANS, "start mux %p pos %d\n", m, m->rpos);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600309
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500310 if (!m->rbuf) {
311 m->rbuf = m->tmp_buf;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600312 m->rpos = 0;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500313 m->rsize = 7; /* start by reading header */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600314 }
315
316 clear_bit(Rpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800317 p9_debug(P9_DEBUG_TRANS, "read mux %p pos %d size: %d = %d\n",
318 m, m->rpos, m->rsize, m->rsize-m->rpos);
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500319 err = p9_fd_read(m->client, m->rbuf + m->rpos,
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500320 m->rsize - m->rpos);
Joe Perches5d385152011-11-28 10:40:46 -0800321 p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600322 if (err == -EAGAIN) {
Simon Derr04621942012-09-17 15:16:28 +0200323 goto end_clear;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600324 }
325
326 if (err <= 0)
327 goto error;
328
329 m->rpos += err;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500330
331 if ((!m->req) && (m->rpos == m->rsize)) { /* header read in */
332 u16 tag;
Joe Perches5d385152011-11-28 10:40:46 -0800333 p9_debug(P9_DEBUG_TRANS, "got new header\n");
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500334
335 n = le32_to_cpu(*(__le32 *) m->rbuf); /* read packet size */
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500336 if (n >= m->client->msize) {
Joe Perches5d385152011-11-28 10:40:46 -0800337 p9_debug(P9_DEBUG_ERROR,
338 "requested packet size too big: %d\n", n);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600339 err = -EIO;
340 goto error;
341 }
342
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500343 tag = le16_to_cpu(*(__le16 *) (m->rbuf+5)); /* read tag */
Joe Perches5d385152011-11-28 10:40:46 -0800344 p9_debug(P9_DEBUG_TRANS,
345 "mux %p pkt: size: %d bytes tag: %d\n", m, n, tag);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600346
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500347 m->req = p9_tag_lookup(m->client, tag);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500348 if (!m->req || (m->req->status != REQ_STATUS_SENT &&
349 m->req->status != REQ_STATUS_FLSH)) {
Joe Perches5d385152011-11-28 10:40:46 -0800350 p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
351 tag);
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500352 err = -EIO;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600353 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500354 }
355
356 if (m->req->rc == NULL) {
357 m->req->rc = kmalloc(sizeof(struct p9_fcall) +
Aneesh Kumar K.Veeff66e2011-03-08 16:39:47 +0530358 m->client->msize, GFP_NOFS);
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500359 if (!m->req->rc) {
360 m->req = NULL;
361 err = -ENOMEM;
362 goto error;
363 }
364 }
365 m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall);
366 memcpy(m->rbuf, m->tmp_buf, m->rsize);
367 m->rsize = n;
368 }
369
370 /* not an else because some packets (like clunk) have no payload */
371 if ((m->req) && (m->rpos == m->rsize)) { /* packet is read in */
Joe Perches5d385152011-11-28 10:40:46 -0800372 p9_debug(P9_DEBUG_TRANS, "got new packet\n");
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500373 spin_lock(&m->client->lock);
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500374 if (m->req->status != REQ_STATUS_ERROR)
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100375 status = REQ_STATUS_RCVD;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500376 list_del(&m->req->req_list);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500377 spin_unlock(&m->client->lock);
Dominique Martinet2b6e72e2014-01-17 18:31:00 +0100378 p9_client_cb(m->client, m->req, status);
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500379 m->rbuf = NULL;
380 m->rpos = 0;
381 m->rsize = 0;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500382 m->req = 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))
390 n = POLLIN;
391 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500392 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600393
Simon Derr04621942012-09-17 15:16:28 +0200394 if ((n & POLLIN) && !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{
416 int ret;
417 mm_segment_t oldfs;
418 struct p9_trans_fd *ts = NULL;
419
420 if (client && client->status != Disconnected)
421 ts = client->trans;
422
423 if (!ts)
424 return -EREMOTEIO;
425
426 if (!(ts->wr->f_flags & O_NONBLOCK))
Joe Perches5d385152011-11-28 10:40:46 -0800427 p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500428
429 oldfs = get_fs();
430 set_fs(get_ds());
431 /* The cast to a user pointer is valid due to the set_fs() */
Hannes Edere3db6cb2009-02-25 10:31:37 +0000432 ret = vfs_write(ts->wr, (__force void __user *)v, len, &ts->wr->f_pos);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500433 set_fs(oldfs);
434
435 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
436 client->status = Disconnected;
437 return ret;
438}
439
440/**
441 * p9_write_work - called when a transport can send some data
442 * @work: container for work to be done
443 *
444 */
445
446static void p9_write_work(struct work_struct *work)
447{
448 int n, err;
449 struct p9_conn *m;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500450 struct p9_req_t *req;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500451
452 m = container_of(work, struct p9_conn, wq);
453
454 if (m->err < 0) {
455 clear_bit(Wworksched, &m->wsched);
456 return;
457 }
458
459 if (!m->wsize) {
Simon Derr759f4292012-09-17 15:16:31 +0200460 spin_lock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500461 if (list_empty(&m->unsent_req_list)) {
462 clear_bit(Wworksched, &m->wsched);
Simon Derr759f4292012-09-17 15:16:31 +0200463 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500464 return;
465 }
466
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500467 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500468 req_list);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500469 req->status = REQ_STATUS_SENT;
Joe Perches5d385152011-11-28 10:40:46 -0800470 p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500471 list_move_tail(&req->req_list, &m->req_list);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500472
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500473 m->wbuf = req->tc->sdata;
474 m->wsize = req->tc->size;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500475 m->wpos = 0;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500476 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500477 }
478
Joe Perches5d385152011-11-28 10:40:46 -0800479 p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
480 m, m->wpos, m->wsize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500481 clear_bit(Wpending, &m->wsched);
482 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
Joe Perches5d385152011-11-28 10:40:46 -0800483 p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
Simon Derr584a8c12012-09-17 15:16:30 +0200484 if (err == -EAGAIN)
485 goto end_clear;
486
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500487
488 if (err < 0)
489 goto error;
490 else if (err == 0) {
491 err = -EREMOTEIO;
492 goto error;
493 }
494
495 m->wpos += err;
496 if (m->wpos == m->wsize)
497 m->wpos = m->wsize = 0;
498
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))
504 n = POLLOUT;
505 else
506 n = p9_fd_poll(m->client, NULL);
507
Simon Derr584a8c12012-09-17 15:16:30 +0200508 if ((n & POLLOUT) &&
509 !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
Eric Dumazet95c96172012-04-15 05:58:06 +0000522static int p9_pollwake(wait_queue_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/**
573 * p9_conn_create - allocate and initialize the per-session mux data
574 * @client: client instance
575 *
576 * Note: Creates the polling task if this is the first session.
577 */
578
579static struct p9_conn *p9_conn_create(struct p9_client *client)
580{
Tejun Heo95820a32008-10-13 18:45:20 -0500581 int n;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500582 struct p9_conn *m;
583
Joe Perches5d385152011-11-28 10:40:46 -0800584 p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500585 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
586 if (!m)
587 return ERR_PTR(-ENOMEM);
588
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
599 n = p9_fd_poll(client, &m->pt);
600 if (n & POLLIN) {
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
605 if (n & POLLOUT) {
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 }
609
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500610 return m;
611}
612
613/**
614 * p9_poll_mux - polls a mux and schedules read or write works if necessary
615 * @m: connection to poll
616 *
617 */
618
619static void p9_poll_mux(struct p9_conn *m)
620{
621 int n;
622
623 if (m->err < 0)
624 return;
625
626 n = p9_fd_poll(m->client, NULL);
627 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
Joe Perches5d385152011-11-28 10:40:46 -0800628 p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500629 if (n >= 0)
630 n = -ECONNRESET;
631 p9_conn_cancel(m, n);
632 }
633
634 if (n & POLLIN) {
635 set_bit(Rpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800636 p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500637 if (!test_and_set_bit(Rworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800638 p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100639 schedule_work(&m->rq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500640 }
641 }
642
643 if (n & POLLOUT) {
644 set_bit(Wpending, &m->wsched);
Joe Perches5d385152011-11-28 10:40:46 -0800645 p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800646 if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
647 !test_and_set_bit(Wworksched, &m->wsched)) {
Joe Perches5d385152011-11-28 10:40:46 -0800648 p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
Tejun Heo61edeee2011-02-01 11:42:43 +0100649 schedule_work(&m->wq);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500650 }
651 }
652}
653
654/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500655 * p9_fd_request - send 9P request
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600656 * The function can sleep until the request is scheduled for sending.
657 * The function can be interrupted. Return from the function is not
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500658 * a guarantee that the request is sent successfully.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600659 *
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500660 * @client: client instance
661 * @req: request to be sent
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600662 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600663 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600664
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500665static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600666{
667 int n;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500668 struct p9_trans_fd *ts = client->trans;
669 struct p9_conn *m = ts->conn;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600670
Joe Perches5d385152011-11-28 10:40:46 -0800671 p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
672 m, current, req->tc, req->tc->id);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600673 if (m->err < 0)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500674 return m->err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600675
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500676 spin_lock(&client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500677 req->status = REQ_STATUS_UNSENT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600678 list_add_tail(&req->req_list, &m->unsent_req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500679 spin_unlock(&client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600680
681 if (test_and_clear_bit(Wpending, &m->wsched))
682 n = POLLOUT;
683 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500684 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600685
686 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
Tejun Heo61edeee2011-02-01 11:42:43 +0100687 schedule_work(&m->wq);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600688
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500689 return 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600690}
691
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500692static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600693{
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500694 int ret = 1;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600695
Joe Perches5d385152011-11-28 10:40:46 -0800696 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600697
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500698 spin_lock(&client->lock);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500699
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500700 if (req->status == REQ_STATUS_UNSENT) {
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500701 list_del(&req->req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500702 req->status = REQ_STATUS_FLSHD;
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500703 ret = 0;
Latchesar Ionkov1bab88b2009-04-05 16:28:59 -0500704 } else if (req->status == REQ_STATUS_SENT)
705 req->status = REQ_STATUS_FLSH;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600706
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500707 spin_unlock(&client->lock);
708
709 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600710}
711
Simon Derrafd8d652014-03-10 16:38:49 +0100712static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
713{
714 p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
715
716 /* we haven't received a response for oldreq,
717 * remove it from the list.
718 */
719 spin_lock(&client->lock);
720 list_del(&req->req_list);
721 spin_unlock(&client->lock);
722
723 return 0;
724}
725
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600726/**
Abhishek Kulkarni0e155972009-07-19 13:41:55 -0600727 * parse_opts - parse mount options into p9_fd_opts structure
728 * @params: options string passed from mount
729 * @opts: fd transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500730 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600731 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500732 */
733
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600734static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500735{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500736 char *p;
737 substring_t args[MAX_OPT_ARGS];
738 int option;
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600739 char *options, *tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500740
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500741 opts->port = P9_PORT;
742 opts->rfd = ~0;
743 opts->wfd = ~0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500744
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600745 if (!params)
746 return 0;
747
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600748 tmp_options = kstrdup(params, GFP_KERNEL);
749 if (!tmp_options) {
Joe Perches5d385152011-11-28 10:40:46 -0800750 p9_debug(P9_DEBUG_ERROR,
751 "failed to allocate copy of option string\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600752 return -ENOMEM;
753 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600754 options = tmp_options;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500755
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500756 while ((p = strsep(&options, ",")) != NULL) {
757 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600758 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500759 if (!*p)
760 continue;
761 token = match_token(p, tokens, args);
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700762 if ((token != Opt_err) && (token != Opt_privport)) {
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000763 r = match_int(&args[0], &option);
764 if (r < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800765 p9_debug(P9_DEBUG_ERROR,
766 "integer field, but no integer?\n");
Abhishek Kulkarni15da4b12009-07-01 08:50:54 +0000767 continue;
768 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500769 }
770 switch (token) {
771 case Opt_port:
772 opts->port = option;
773 break;
774 case Opt_rfdno:
775 opts->rfd = option;
776 break;
777 case Opt_wfdno:
778 opts->wfd = option;
779 break;
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700780 case Opt_privport:
781 opts->privport = 1;
782 break;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500783 default:
784 continue;
785 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500786 }
Eric Van Hensbergend8c8a9e2010-02-08 16:23:23 -0600787
788 kfree(tmp_options);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600789 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500790}
791
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500792static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500793{
794 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
795 GFP_KERNEL);
796 if (!ts)
797 return -ENOMEM;
798
799 ts->rd = fget(rfd);
800 ts->wr = fget(wfd);
801 if (!ts->rd || !ts->wr) {
802 if (ts->rd)
803 fput(ts->rd);
804 if (ts->wr)
805 fput(ts->wr);
806 kfree(ts);
807 return -EIO;
808 }
809
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500810 client->trans = ts;
811 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500812
813 return 0;
814}
815
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500816static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500817{
Al Viro6b186622009-08-06 02:02:43 +0400818 struct p9_trans_fd *p;
Al Viro56b31d12012-08-18 00:25:51 -0400819 struct file *file;
820 int ret;
Al Viro6b186622009-08-06 02:02:43 +0400821
822 p = kmalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
823 if (!p)
824 return -ENOMEM;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500825
826 csocket->sk->sk_allocation = GFP_NOIO;
Linus Torvaldsaab174f2012-10-02 20:25:04 -0700827 file = sock_alloc_file(csocket, 0, NULL);
Al Viro56b31d12012-08-18 00:25:51 -0400828 if (IS_ERR(file)) {
Joe Perches5d385152011-11-28 10:40:46 -0800829 pr_err("%s (%d): failed to map fd\n",
830 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400831 sock_release(csocket);
832 kfree(p);
Al Viro56b31d12012-08-18 00:25:51 -0400833 return PTR_ERR(file);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500834 }
835
Al Viro56b31d12012-08-18 00:25:51 -0400836 get_file(file);
837 p->wr = p->rd = file;
Al Viro6b186622009-08-06 02:02:43 +0400838 client->trans = p;
839 client->status = Connected;
840
Al Viro6b186622009-08-06 02:02:43 +0400841 p->rd->f_flags |= O_NONBLOCK;
842
843 p->conn = p9_conn_create(client);
844 if (IS_ERR(p->conn)) {
845 ret = PTR_ERR(p->conn);
846 p->conn = NULL;
847 kfree(p);
848 sockfd_put(csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500849 sockfd_put(csocket);
850 return ret;
851 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500852 return 0;
853}
854
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500855/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500856 * p9_mux_destroy - cancels all pending requests and frees mux resources
857 * @m: mux to destroy
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500858 *
859 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600860
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500861static void p9_conn_destroy(struct p9_conn *m)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500862{
Joe Perches5d385152011-11-28 10:40:46 -0800863 p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
864 m, m->mux_list.prev, m->mux_list.next);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500865
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500866 p9_mux_poll_stop(m);
867 cancel_work_sync(&m->rq);
868 cancel_work_sync(&m->wq);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500869
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500870 p9_conn_cancel(m, -ECONNRESET);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500871
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500872 m->client = NULL;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500873 kfree(m);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500874}
875
876/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500877 * p9_fd_close - shutdown file descriptor transport
878 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500879 *
880 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600881
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500882static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500883{
884 struct p9_trans_fd *ts;
885
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500886 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500887 return;
888
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500889 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500890 if (!ts)
891 return;
892
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500893 client->status = Disconnected;
894
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600895 p9_conn_destroy(ts->conn);
896
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500897 if (ts->rd)
898 fput(ts->rd);
899 if (ts->wr)
900 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500901
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500902 kfree(ts);
903}
904
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500905/*
906 * stolen from NFS - maybe should be made a generic function?
907 */
908static inline int valid_ipaddr4(const char *buf)
909{
910 int rc, count, in[4];
911
912 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
913 if (rc != 4)
914 return -EINVAL;
915 for (count = 0; count < 4; count++) {
916 if (in[count] > 255)
917 return -EINVAL;
918 }
919 return 0;
920}
921
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700922static int p9_bind_privport(struct socket *sock)
923{
924 struct sockaddr_in cl;
925 int port, err = -EINVAL;
926
927 memset(&cl, 0, sizeof(cl));
928 cl.sin_family = AF_INET;
929 cl.sin_addr.s_addr = INADDR_ANY;
930 for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
931 cl.sin_port = htons((ushort)port);
932 err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
933 if (err != -EADDRINUSE)
934 break;
935 }
936 return err;
937}
938
939
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500940static int
941p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500942{
943 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500944 struct socket *csocket;
945 struct sockaddr_in sin_server;
946 struct p9_fd_opts opts;
947
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600948 err = parse_opts(args, &opts);
949 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500950 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500951
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500952 if (valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500953 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500954
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500955 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500956
957 sin_server.sin_family = AF_INET;
958 sin_server.sin_addr.s_addr = in_aton(addr);
959 sin_server.sin_port = htons(opts.port);
Rob Landleye75762f2011-05-23 21:46:56 -0500960 err = __sock_create(read_pnet(&current->nsproxy->net_ns), PF_INET,
961 SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +0400962 if (err) {
Joe Perches5d385152011-11-28 10:40:46 -0800963 pr_err("%s (%d): problem creating socket\n",
964 __func__, task_pid_nr(current));
Al Viro6b186622009-08-06 02:02:43 +0400965 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500966 }
967
Jim Garlick2f28c8b2013-05-29 12:15:07 -0700968 if (opts.privport) {
969 err = p9_bind_privport(csocket);
970 if (err < 0) {
971 pr_err("%s (%d): problem binding to privport\n",
972 __func__, task_pid_nr(current));
973 sock_release(csocket);
974 return err;
975 }
976 }
977
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500978 err = csocket->ops->connect(csocket,
979 (struct sockaddr *)&sin_server,
980 sizeof(struct sockaddr_in), 0);
981 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800982 pr_err("%s (%d): problem connecting socket to %s\n",
983 __func__, task_pid_nr(current), addr);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500984 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +0400985 return err;
986 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500987
Al Viro6b186622009-08-06 02:02:43 +0400988 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500989}
990
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500991static int
992p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500993{
994 int err;
995 struct socket *csocket;
996 struct sockaddr_un sun_server;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500997
998 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500999
Dan Carpentercff6b8a2010-07-10 11:51:54 +02001000 if (strlen(addr) >= UNIX_PATH_MAX) {
Joe Perches5d385152011-11-28 10:40:46 -08001001 pr_err("%s (%d): address too long: %s\n",
1002 __func__, task_pid_nr(current), addr);
Al Viro6b186622009-08-06 02:02:43 +04001003 return -ENAMETOOLONG;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001004 }
1005
1006 sun_server.sun_family = PF_UNIX;
1007 strcpy(sun_server.sun_path, addr);
Rob Landleye75762f2011-05-23 21:46:56 -05001008 err = __sock_create(read_pnet(&current->nsproxy->net_ns), PF_UNIX,
1009 SOCK_STREAM, 0, &csocket, 1);
Al Viro6b186622009-08-06 02:02:43 +04001010 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001011 pr_err("%s (%d): problem creating socket\n",
1012 __func__, task_pid_nr(current));
1013
Al Viro6b186622009-08-06 02:02:43 +04001014 return err;
1015 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001016 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1017 sizeof(struct sockaddr_un) - 1, 0);
1018 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -08001019 pr_err("%s (%d): problem connecting socket: %s: %d\n",
1020 __func__, task_pid_nr(current), addr, err);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001021 sock_release(csocket);
Al Viro6b186622009-08-06 02:02:43 +04001022 return err;
1023 }
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001024
Al Viro6b186622009-08-06 02:02:43 +04001025 return p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001026}
1027
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001028static int
1029p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001030{
1031 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001032 struct p9_fd_opts opts;
Al Viro6b186622009-08-06 02:02:43 +04001033 struct p9_trans_fd *p;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001034
1035 parse_opts(args, &opts);
1036
1037 if (opts.rfd == ~0 || opts.wfd == ~0) {
Joe Perches5d385152011-11-28 10:40:46 -08001038 pr_err("Insufficient options for proto=fd\n");
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001039 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001040 }
1041
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001042 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001043 if (err < 0)
Al Viro6b186622009-08-06 02:02:43 +04001044 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001045
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001046 p = (struct p9_trans_fd *) client->trans;
1047 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001048 if (IS_ERR(p->conn)) {
1049 err = PTR_ERR(p->conn);
1050 p->conn = NULL;
Al Viro6b186622009-08-06 02:02:43 +04001051 fput(p->rd);
1052 fput(p->wr);
1053 return err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001054 }
1055
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001056 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001057}
1058
1059static struct p9_trans_module p9_tcp_trans = {
1060 .name = "tcp",
1061 .maxsize = MAX_SOCK_BUF,
Eric Van Hensbergenf94741f2013-11-12 10:20:03 -06001062 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001063 .create = p9_fd_create_tcp,
1064 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001065 .request = p9_fd_request,
1066 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001067 .cancelled = p9_fd_cancelled,
Tejun Heo72029fe2008-09-24 16:22:23 -05001068 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001069};
1070
1071static struct p9_trans_module p9_unix_trans = {
1072 .name = "unix",
1073 .maxsize = MAX_SOCK_BUF,
1074 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001075 .create = p9_fd_create_unix,
1076 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001077 .request = p9_fd_request,
1078 .cancel = p9_fd_cancel,
Simon Derrafd8d652014-03-10 16:38:49 +01001079 .cancelled = p9_fd_cancelled,
Tejun Heo72029fe2008-09-24 16:22:23 -05001080 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001081};
1082
1083static struct p9_trans_module p9_fd_trans = {
1084 .name = "fd",
1085 .maxsize = MAX_SOCK_BUF,
1086 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001087 .create = p9_fd_create,
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,
Tejun Heo72029fe2008-09-24 16:22:23 -05001092 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001093};
1094
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001095/**
1096 * p9_poll_proc - poll worker thread
1097 * @a: thread state and arguments
1098 *
1099 * polls all v9fs transports for new events and queues the appropriate
1100 * work to the work queue
1101 *
1102 */
1103
Tejun Heoaa70c582011-02-01 11:42:43 +01001104static void p9_poll_workfn(struct work_struct *work)
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001105{
1106 unsigned long flags;
1107
Joe Perches5d385152011-11-28 10:40:46 -08001108 p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
Tejun Heoaa70c582011-02-01 11:42:43 +01001109
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001110 spin_lock_irqsave(&p9_poll_lock, flags);
1111 while (!list_empty(&p9_poll_pending_list)) {
1112 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1113 struct p9_conn,
1114 poll_pending_link);
1115 list_del_init(&conn->poll_pending_link);
1116 spin_unlock_irqrestore(&p9_poll_lock, flags);
1117
1118 p9_poll_mux(conn);
1119
1120 spin_lock_irqsave(&p9_poll_lock, flags);
1121 }
1122 spin_unlock_irqrestore(&p9_poll_lock, flags);
1123
Joe Perches5d385152011-11-28 10:40:46 -08001124 p9_debug(P9_DEBUG_TRANS, "finish\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001125}
1126
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001127int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001128{
1129 v9fs_register_trans(&p9_tcp_trans);
1130 v9fs_register_trans(&p9_unix_trans);
1131 v9fs_register_trans(&p9_fd_trans);
1132
Andrew Morton3387b802008-03-28 14:15:57 -07001133 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001134}
Tejun Heo72029fe2008-09-24 16:22:23 -05001135
1136void p9_trans_fd_exit(void)
1137{
Tejun Heo43829732012-08-20 14:51:24 -07001138 flush_work(&p9_poll_work);
Tejun Heo72029fe2008-09-24 16:22:23 -05001139 v9fs_unregister_trans(&p9_tcp_trans);
1140 v9fs_unregister_trans(&p9_unix_trans);
1141 v9fs_unregister_trans(&p9_fd_trans);
1142}