blob: e8ebe2cb7e8bbed14ba22b2bdcb789c010068b79 [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
28#include <linux/in.h>
29#include <linux/module.h>
30#include <linux/net.h>
31#include <linux/ipv6.h>
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060032#include <linux/kthread.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050033#include <linux/errno.h>
34#include <linux/kernel.h>
35#include <linux/un.h>
36#include <linux/uaccess.h>
37#include <linux/inet.h>
38#include <linux/idr.h>
39#include <linux/file.h>
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050040#include <linux/parser.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050041#include <net/9p/9p.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050042#include <net/9p/client.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050043#include <net/9p/transport.h>
44
45#define P9_PORT 564
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050046#define MAX_SOCK_BUF (64*1024)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060047#define MAXPOLLWADDR 2
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050048
Eric Van Hensbergenee443992008-03-05 07:08:09 -060049/**
50 * struct p9_fd_opts - per-transport options
51 * @rfd: file descriptor for reading (trans=fd)
52 * @wfd: file descriptor for writing (trans=fd)
53 * @port: port to connect to (trans=tcp)
54 *
55 */
56
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050057struct p9_fd_opts {
58 int rfd;
59 int wfd;
60 u16 port;
61};
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050062
Eric Van Hensbergenee443992008-03-05 07:08:09 -060063/**
64 * struct p9_trans_fd - transport state
65 * @rd: reference to file to read from
66 * @wr: reference of file to write to
67 * @conn: connection state reference
68 *
69 */
70
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050071struct p9_trans_fd {
72 struct file *rd;
73 struct file *wr;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060074 struct p9_conn *conn;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050075};
76
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050077/*
78 * Option Parsing (code inspired by NFS code)
79 * - a little lazy - parse all fd-transport options
80 */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050081
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050082enum {
83 /* Options that take integer arguments */
Latchesar Ionkov55762692007-11-06 08:02:53 -060084 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050085};
86
Steven Whitehousea447c092008-10-13 10:46:57 +010087static const match_table_t tokens = {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050088 {Opt_port, "port=%u"},
89 {Opt_rfdno, "rfdno=%u"},
90 {Opt_wfdno, "wfdno=%u"},
Latchesar Ionkov55762692007-11-06 08:02:53 -060091 {Opt_err, NULL},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050092};
93
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060094enum {
95 Rworksched = 1, /* read work scheduled or running */
96 Rpending = 2, /* can read */
97 Wworksched = 4, /* write work scheduled or running */
98 Wpending = 8, /* can write */
99};
100
Tejun Heo992b3f12008-10-13 18:45:25 -0500101struct p9_poll_wait {
102 struct p9_conn *conn;
103 wait_queue_t wait;
104 wait_queue_head_t *wait_addr;
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600105};
106
107/**
108 * struct p9_conn - fd mux connection state information
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600109 * @mux_list: list link for mux to manage multiple connections (?)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500110 * @client: reference to client instance for this connection
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600111 * @err: error state
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600112 * @req_list: accounting for requests which have been sent
113 * @unsent_req_list: accounting for requests that haven't been sent
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500114 * @req: current request being processed (if any)
115 * @tmp_buf: temporary buffer to read in header
116 * @rsize: amount to read for current frame
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600117 * @rpos: read position in current frame
118 * @rbuf: current read buffer
119 * @wpos: write position for current frame
120 * @wsize: amount of data to write for current frame
121 * @wbuf: current write buffer
122 * @poll_wait: array of wait_q's for various worker threads
123 * @poll_waddr: ????
124 * @pt: poll state
125 * @rq: current read work
126 * @wq: current write work
127 * @wsched: ????
128 *
129 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600130
131struct p9_conn {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600132 struct list_head mux_list;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500133 struct p9_client *client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600134 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600135 struct list_head req_list;
136 struct list_head unsent_req_list;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500137 struct p9_req_t *req;
138 char tmp_buf[7];
139 int rsize;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600140 int rpos;
141 char *rbuf;
142 int wpos;
143 int wsize;
144 char *wbuf;
Tejun Heo992b3f12008-10-13 18:45:25 -0500145 struct list_head poll_pending_link;
146 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600147 poll_table pt;
148 struct work_struct rq;
149 struct work_struct wq;
150 unsigned long wsched;
151};
152
Tejun Heo992b3f12008-10-13 18:45:25 -0500153static DEFINE_SPINLOCK(p9_poll_lock);
154static LIST_HEAD(p9_poll_pending_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600155static struct workqueue_struct *p9_mux_wq;
Tejun Heo992b3f12008-10-13 18:45:25 -0500156static struct task_struct *p9_poll_task;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600157
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600158static void p9_mux_poll_stop(struct p9_conn *m)
159{
Tejun Heo992b3f12008-10-13 18:45:25 -0500160 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600161 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600162
Tejun Heo992b3f12008-10-13 18:45:25 -0500163 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
164 struct p9_poll_wait *pwait = &m->poll_wait[i];
165
166 if (pwait->wait_addr) {
167 remove_wait_queue(pwait->wait_addr, &pwait->wait);
168 pwait->wait_addr = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600169 }
170 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500171
172 spin_lock_irqsave(&p9_poll_lock, flags);
173 list_del_init(&m->poll_pending_link);
174 spin_unlock_irqrestore(&p9_poll_lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600175}
176
177/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500178 * p9_conn_cancel - cancel all pending requests with error
179 * @m: mux data
180 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600181 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600182 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600183
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500184static void p9_conn_cancel(struct p9_conn *m, int err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600185{
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500186 struct p9_req_t *req, *rtmp;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500187 unsigned long flags;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500188 LIST_HEAD(cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600189
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500190 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
191 m->err = err;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500192 spin_lock_irqsave(&m->client->lock, flags);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500193 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500194 req->status = REQ_STATUS_ERROR;
195 if (!req->t_err)
196 req->t_err = err;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500197 list_move(&req->req_list, &cancel_list);
Tejun Heo992b3f12008-10-13 18:45:25 -0500198 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500199 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500200 req->status = REQ_STATUS_ERROR;
201 if (!req->t_err)
202 req->t_err = err;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500203 list_move(&req->req_list, &cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600204 }
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500205 spin_unlock_irqrestore(&m->client->lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600206
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500207 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
208 list_del(&req->req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500209 P9_DPRINTK(P9_DEBUG_ERROR, "call back req %p\n", req);
210 p9_client_cb(m->client, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600211 }
212}
213
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500214static unsigned int
215p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
216{
217 int ret, n;
218 struct p9_trans_fd *ts = NULL;
219
220 if (client && client->status == Connected)
221 ts = client->trans;
222
223 if (!ts)
224 return -EREMOTEIO;
225
226 if (!ts->rd->f_op || !ts->rd->f_op->poll)
227 return -EIO;
228
229 if (!ts->wr->f_op || !ts->wr->f_op->poll)
230 return -EIO;
231
232 ret = ts->rd->f_op->poll(ts->rd, pt);
233 if (ret < 0)
234 return ret;
235
236 if (ts->rd != ts->wr) {
237 n = ts->wr->f_op->poll(ts->wr, pt);
238 if (n < 0)
239 return n;
240 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
241 }
242
243 return ret;
244}
245
246/**
247 * p9_fd_read- read from a fd
248 * @client: client instance
249 * @v: buffer to receive data into
250 * @len: size of receive buffer
251 *
252 */
253
254static int p9_fd_read(struct p9_client *client, void *v, int len)
255{
256 int ret;
257 struct p9_trans_fd *ts = NULL;
258
259 if (client && client->status != Disconnected)
260 ts = client->trans;
261
262 if (!ts)
263 return -EREMOTEIO;
264
265 if (!(ts->rd->f_flags & O_NONBLOCK))
266 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
267
268 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
269 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
270 client->status = Disconnected;
271 return ret;
272}
273
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600274/**
275 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600276 * @work: container of work to be done
277 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600278 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600279
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600280static void p9_read_work(struct work_struct *work)
281{
282 int n, err;
283 struct p9_conn *m;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600284
285 m = container_of(work, struct p9_conn, rq);
286
287 if (m->err < 0)
288 return;
289
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500290 P9_DPRINTK(P9_DEBUG_TRANS, "start mux %p pos %d\n", m, m->rpos);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600291
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500292 if (!m->rbuf) {
293 m->rbuf = m->tmp_buf;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600294 m->rpos = 0;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500295 m->rsize = 7; /* start by reading header */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600296 }
297
298 clear_bit(Rpending, &m->wsched);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500299 P9_DPRINTK(P9_DEBUG_TRANS, "read mux %p pos %d size: %d = %d\n", m,
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500300 m->rpos, m->rsize, m->rsize-m->rpos);
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500301 err = p9_fd_read(m->client, m->rbuf + m->rpos,
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500302 m->rsize - m->rpos);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500303 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600304 if (err == -EAGAIN) {
305 clear_bit(Rworksched, &m->wsched);
306 return;
307 }
308
309 if (err <= 0)
310 goto error;
311
312 m->rpos += err;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500313
314 if ((!m->req) && (m->rpos == m->rsize)) { /* header read in */
315 u16 tag;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500316 P9_DPRINTK(P9_DEBUG_TRANS, "got new header\n");
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500317
318 n = le32_to_cpu(*(__le32 *) m->rbuf); /* read packet size */
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500319 if (n >= m->client->msize) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600320 P9_DPRINTK(P9_DEBUG_ERROR,
321 "requested packet size too big: %d\n", n);
322 err = -EIO;
323 goto error;
324 }
325
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500326 tag = le16_to_cpu(*(__le16 *) (m->rbuf+5)); /* read tag */
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500327 P9_DPRINTK(P9_DEBUG_TRANS,
328 "mux %p pkt: size: %d bytes tag: %d\n", m, n, tag);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600329
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500330 m->req = p9_tag_lookup(m->client, tag);
331 if (!m->req) {
332 P9_DPRINTK(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
333 tag);
334 err = -EIO;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600335 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500336 }
337
338 if (m->req->rc == NULL) {
339 m->req->rc = kmalloc(sizeof(struct p9_fcall) +
340 m->client->msize, GFP_KERNEL);
341 if (!m->req->rc) {
342 m->req = NULL;
343 err = -ENOMEM;
344 goto error;
345 }
346 }
347 m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall);
348 memcpy(m->rbuf, m->tmp_buf, m->rsize);
349 m->rsize = n;
350 }
351
352 /* not an else because some packets (like clunk) have no payload */
353 if ((m->req) && (m->rpos == m->rsize)) { /* packet is read in */
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500354 P9_DPRINTK(P9_DEBUG_TRANS, "got new packet\n");
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600355
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500356 list_del(&m->req->req_list);
357 p9_client_cb(m->client, m->req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600358
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500359 m->rbuf = NULL;
360 m->rpos = 0;
361 m->rsize = 0;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500362 m->req = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600363 }
364
365 if (!list_empty(&m->req_list)) {
366 if (test_and_clear_bit(Rpending, &m->wsched))
367 n = POLLIN;
368 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500369 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600370
371 if (n & POLLIN) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500372 P9_DPRINTK(P9_DEBUG_TRANS, "sched read work %p\n", m);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600373 queue_work(p9_mux_wq, &m->rq);
374 } else
375 clear_bit(Rworksched, &m->wsched);
376 } else
377 clear_bit(Rworksched, &m->wsched);
378
379 return;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600380error:
381 p9_conn_cancel(m, err);
382 clear_bit(Rworksched, &m->wsched);
383}
384
385/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500386 * p9_fd_write - write to a socket
387 * @client: client instance
388 * @v: buffer to send data from
389 * @len: size of send buffer
390 *
391 */
392
393static int p9_fd_write(struct p9_client *client, void *v, int len)
394{
395 int ret;
396 mm_segment_t oldfs;
397 struct p9_trans_fd *ts = NULL;
398
399 if (client && client->status != Disconnected)
400 ts = client->trans;
401
402 if (!ts)
403 return -EREMOTEIO;
404
405 if (!(ts->wr->f_flags & O_NONBLOCK))
406 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
407
408 oldfs = get_fs();
409 set_fs(get_ds());
410 /* The cast to a user pointer is valid due to the set_fs() */
411 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
412 set_fs(oldfs);
413
414 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
415 client->status = Disconnected;
416 return ret;
417}
418
419/**
420 * p9_write_work - called when a transport can send some data
421 * @work: container for work to be done
422 *
423 */
424
425static void p9_write_work(struct work_struct *work)
426{
427 int n, err;
428 struct p9_conn *m;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500429 struct p9_req_t *req;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500430
431 m = container_of(work, struct p9_conn, wq);
432
433 if (m->err < 0) {
434 clear_bit(Wworksched, &m->wsched);
435 return;
436 }
437
438 if (!m->wsize) {
439 if (list_empty(&m->unsent_req_list)) {
440 clear_bit(Wworksched, &m->wsched);
441 return;
442 }
443
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500444 spin_lock(&m->client->lock);
445 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500446 req_list);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500447 req->status = REQ_STATUS_SENT;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500448 list_move_tail(&req->req_list, &m->req_list);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500449
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500450 m->wbuf = req->tc->sdata;
451 m->wsize = req->tc->size;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500452 m->wpos = 0;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500453 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500454 }
455
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500456 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p pos %d size %d\n", m, m->wpos,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500457 m->wsize);
458 clear_bit(Wpending, &m->wsched);
459 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500460 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500461 if (err == -EAGAIN) {
462 clear_bit(Wworksched, &m->wsched);
463 return;
464 }
465
466 if (err < 0)
467 goto error;
468 else if (err == 0) {
469 err = -EREMOTEIO;
470 goto error;
471 }
472
473 m->wpos += err;
474 if (m->wpos == m->wsize)
475 m->wpos = m->wsize = 0;
476
477 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
478 if (test_and_clear_bit(Wpending, &m->wsched))
479 n = POLLOUT;
480 else
481 n = p9_fd_poll(m->client, NULL);
482
483 if (n & POLLOUT) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500484 P9_DPRINTK(P9_DEBUG_TRANS, "sched write work %p\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500485 queue_work(p9_mux_wq, &m->wq);
486 } else
487 clear_bit(Wworksched, &m->wsched);
488 } else
489 clear_bit(Wworksched, &m->wsched);
490
491 return;
492
493error:
494 p9_conn_cancel(m, err);
495 clear_bit(Wworksched, &m->wsched);
496}
497
498static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
499{
500 struct p9_poll_wait *pwait =
501 container_of(wait, struct p9_poll_wait, wait);
502 struct p9_conn *m = pwait->conn;
503 unsigned long flags;
504 DECLARE_WAITQUEUE(dummy_wait, p9_poll_task);
505
506 spin_lock_irqsave(&p9_poll_lock, flags);
507 if (list_empty(&m->poll_pending_link))
508 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
509 spin_unlock_irqrestore(&p9_poll_lock, flags);
510
511 /* perform the default wake up operation */
512 return default_wake_function(&dummy_wait, mode, sync, key);
513}
514
515/**
516 * p9_pollwait - add poll task to the wait queue
517 * @filp: file pointer being polled
518 * @wait_address: wait_q to block on
519 * @p: poll state
520 *
521 * called by files poll operation to add v9fs-poll task to files wait queue
522 */
523
524static void
525p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
526{
527 struct p9_conn *m = container_of(p, struct p9_conn, pt);
528 struct p9_poll_wait *pwait = NULL;
529 int i;
530
531 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
532 if (m->poll_wait[i].wait_addr == NULL) {
533 pwait = &m->poll_wait[i];
534 break;
535 }
536 }
537
538 if (!pwait) {
539 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
540 return;
541 }
542
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500543 pwait->conn = m;
544 pwait->wait_addr = wait_address;
545 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
546 add_wait_queue(wait_address, &pwait->wait);
547}
548
549/**
550 * p9_conn_create - allocate and initialize the per-session mux data
551 * @client: client instance
552 *
553 * Note: Creates the polling task if this is the first session.
554 */
555
556static struct p9_conn *p9_conn_create(struct p9_client *client)
557{
Tejun Heo95820a32008-10-13 18:45:20 -0500558 int n;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500559 struct p9_conn *m;
560
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500561 P9_DPRINTK(P9_DEBUG_TRANS, "client %p msize %d\n", client,
562 client->msize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500563 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
564 if (!m)
565 return ERR_PTR(-ENOMEM);
566
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500567 INIT_LIST_HEAD(&m->mux_list);
568 m->client = client;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500569
570 INIT_LIST_HEAD(&m->req_list);
571 INIT_LIST_HEAD(&m->unsent_req_list);
572 INIT_WORK(&m->rq, p9_read_work);
573 INIT_WORK(&m->wq, p9_write_work);
574 INIT_LIST_HEAD(&m->poll_pending_link);
575 init_poll_funcptr(&m->pt, p9_pollwait);
576
577 n = p9_fd_poll(client, &m->pt);
578 if (n & POLLIN) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500579 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500580 set_bit(Rpending, &m->wsched);
581 }
582
583 if (n & POLLOUT) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500584 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can write\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500585 set_bit(Wpending, &m->wsched);
586 }
587
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500588 return m;
589}
590
591/**
592 * p9_poll_mux - polls a mux and schedules read or write works if necessary
593 * @m: connection to poll
594 *
595 */
596
597static void p9_poll_mux(struct p9_conn *m)
598{
599 int n;
600
601 if (m->err < 0)
602 return;
603
604 n = p9_fd_poll(m->client, NULL);
605 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500606 P9_DPRINTK(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500607 if (n >= 0)
608 n = -ECONNRESET;
609 p9_conn_cancel(m, n);
610 }
611
612 if (n & POLLIN) {
613 set_bit(Rpending, &m->wsched);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500614 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500615 if (!test_and_set_bit(Rworksched, &m->wsched)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500616 P9_DPRINTK(P9_DEBUG_TRANS, "sched read work %p\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500617 queue_work(p9_mux_wq, &m->rq);
618 }
619 }
620
621 if (n & POLLOUT) {
622 set_bit(Wpending, &m->wsched);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500623 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can write\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500624 if ((m->wsize || !list_empty(&m->unsent_req_list))
625 && !test_and_set_bit(Wworksched, &m->wsched)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500626 P9_DPRINTK(P9_DEBUG_TRANS, "sched write work %p\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500627 queue_work(p9_mux_wq, &m->wq);
628 }
629 }
630}
631
632/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500633 * p9_fd_request - send 9P request
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600634 * The function can sleep until the request is scheduled for sending.
635 * The function can be interrupted. Return from the function is not
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500636 * a guarantee that the request is sent successfully.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600637 *
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500638 * @client: client instance
639 * @req: request to be sent
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600640 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600641 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600642
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500643static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600644{
645 int n;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500646 struct p9_trans_fd *ts = client->trans;
647 struct p9_conn *m = ts->conn;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600648
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500649 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n", m,
650 current, req->tc, req->tc->id);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600651 if (m->err < 0)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500652 return m->err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600653
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500654 req->status = REQ_STATUS_UNSENT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600655
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500656 spin_lock(&client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600657 list_add_tail(&req->req_list, &m->unsent_req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500658 spin_unlock(&client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600659
660 if (test_and_clear_bit(Wpending, &m->wsched))
661 n = POLLOUT;
662 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500663 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600664
665 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
666 queue_work(p9_mux_wq, &m->wq);
667
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500668 return 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600669}
670
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500671static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600672{
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500673 struct p9_trans_fd *ts = client->trans;
674 struct p9_conn *m = ts->conn;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600675
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500676 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p req %p\n", m, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600677
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500678 spin_lock(&client->lock);
679 list_del(&req->req_list);
680 spin_unlock(&client->lock);
681
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500682 if (req->status == REQ_STATUS_UNSENT) {
683 req->status = REQ_STATUS_FLSHD;
684 return 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600685 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600686
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600687 return 1;
688}
689
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600690/**
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600691 * parse_options - parse mount options into session structure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500692 * @options: options string passed from mount
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600693 * @opts: transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500694 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600695 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500696 */
697
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600698static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500699{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500700 char *p;
701 substring_t args[MAX_OPT_ARGS];
702 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600703 char *options;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500704 int ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500705
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500706 opts->port = P9_PORT;
707 opts->rfd = ~0;
708 opts->wfd = ~0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500709
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600710 if (!params)
711 return 0;
712
713 options = kstrdup(params, GFP_KERNEL);
714 if (!options) {
715 P9_DPRINTK(P9_DEBUG_ERROR,
716 "failed to allocate copy of option string\n");
717 return -ENOMEM;
718 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500719
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500720 while ((p = strsep(&options, ",")) != NULL) {
721 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600722 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500723 if (!*p)
724 continue;
725 token = match_token(p, tokens, args);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600726 r = match_int(&args[0], &option);
727 if (r < 0) {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500728 P9_DPRINTK(P9_DEBUG_ERROR,
729 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600730 ret = r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500731 continue;
732 }
733 switch (token) {
734 case Opt_port:
735 opts->port = option;
736 break;
737 case Opt_rfdno:
738 opts->rfd = option;
739 break;
740 case Opt_wfdno:
741 opts->wfd = option;
742 break;
743 default:
744 continue;
745 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500746 }
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600747 kfree(options);
748 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500749}
750
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500751static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500752{
753 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
754 GFP_KERNEL);
755 if (!ts)
756 return -ENOMEM;
757
758 ts->rd = fget(rfd);
759 ts->wr = fget(wfd);
760 if (!ts->rd || !ts->wr) {
761 if (ts->rd)
762 fput(ts->rd);
763 if (ts->wr)
764 fput(ts->wr);
765 kfree(ts);
766 return -EIO;
767 }
768
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500769 client->trans = ts;
770 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500771
772 return 0;
773}
774
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500775static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500776{
777 int fd, ret;
778
779 csocket->sk->sk_allocation = GFP_NOIO;
Ulrich Dreppera677a032008-07-23 21:29:17 -0700780 fd = sock_map_fd(csocket, 0);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500781 if (fd < 0) {
782 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
783 return fd;
784 }
785
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500786 ret = p9_fd_open(client, fd, fd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500787 if (ret < 0) {
788 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
789 sockfd_put(csocket);
790 return ret;
791 }
792
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500793 ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500794
795 return 0;
796}
797
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500798/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500799 * p9_mux_destroy - cancels all pending requests and frees mux resources
800 * @m: mux to destroy
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500801 *
802 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600803
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500804static void p9_conn_destroy(struct p9_conn *m)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500805{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500806 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p prev %p next %p\n", m,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500807 m->mux_list.prev, m->mux_list.next);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500808
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500809 p9_mux_poll_stop(m);
810 cancel_work_sync(&m->rq);
811 cancel_work_sync(&m->wq);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500812
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500813 p9_conn_cancel(m, -ECONNRESET);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500814
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500815 m->client = NULL;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500816 kfree(m);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500817}
818
819/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500820 * p9_fd_close - shutdown file descriptor transport
821 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500822 *
823 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600824
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500825static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500826{
827 struct p9_trans_fd *ts;
828
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500829 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500830 return;
831
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500832 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500833 if (!ts)
834 return;
835
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500836 client->status = Disconnected;
837
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600838 p9_conn_destroy(ts->conn);
839
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500840 if (ts->rd)
841 fput(ts->rd);
842 if (ts->wr)
843 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500844
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500845 kfree(ts);
846}
847
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500848/*
849 * stolen from NFS - maybe should be made a generic function?
850 */
851static inline int valid_ipaddr4(const char *buf)
852{
853 int rc, count, in[4];
854
855 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
856 if (rc != 4)
857 return -EINVAL;
858 for (count = 0; count < 4; count++) {
859 if (in[count] > 255)
860 return -EINVAL;
861 }
862 return 0;
863}
864
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500865static int
866p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500867{
868 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500869 struct socket *csocket;
870 struct sockaddr_in sin_server;
871 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500872 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500873
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600874 err = parse_opts(args, &opts);
875 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500876 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500877
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500878 if (valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500879 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500880
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500881 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500882
883 sin_server.sin_family = AF_INET;
884 sin_server.sin_addr.s_addr = in_aton(addr);
885 sin_server.sin_port = htons(opts.port);
886 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
887
888 if (!csocket) {
889 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
890 err = -EIO;
891 goto error;
892 }
893
894 err = csocket->ops->connect(csocket,
895 (struct sockaddr *)&sin_server,
896 sizeof(struct sockaddr_in), 0);
897 if (err < 0) {
898 P9_EPRINTK(KERN_ERR,
899 "p9_trans_tcp: problem connecting socket to %s\n",
900 addr);
901 goto error;
902 }
903
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500904 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500905 if (err < 0)
906 goto error;
907
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500908 p = (struct p9_trans_fd *) client->trans;
909 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600910 if (IS_ERR(p->conn)) {
911 err = PTR_ERR(p->conn);
912 p->conn = NULL;
913 goto error;
914 }
915
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500916 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500917
918error:
919 if (csocket)
920 sock_release(csocket);
921
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500922 kfree(p);
923
924 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500925}
926
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500927static int
928p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500929{
930 int err;
931 struct socket *csocket;
932 struct sockaddr_un sun_server;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500933 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500934
935 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500936
937 if (strlen(addr) > UNIX_PATH_MAX) {
938 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
939 addr);
940 err = -ENAMETOOLONG;
941 goto error;
942 }
943
944 sun_server.sun_family = PF_UNIX;
945 strcpy(sun_server.sun_path, addr);
946 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
947 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
948 sizeof(struct sockaddr_un) - 1, 0);
949 if (err < 0) {
950 P9_EPRINTK(KERN_ERR,
951 "p9_trans_unix: problem connecting socket: %s: %d\n",
952 addr, err);
953 goto error;
954 }
955
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500956 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500957 if (err < 0)
958 goto error;
959
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500960 p = (struct p9_trans_fd *) client->trans;
961 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600962 if (IS_ERR(p->conn)) {
963 err = PTR_ERR(p->conn);
964 p->conn = NULL;
965 goto error;
966 }
967
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500968 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500969
970error:
971 if (csocket)
972 sock_release(csocket);
973
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500974 kfree(p);
975 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500976}
977
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500978static int
979p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500980{
981 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500982 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500983 struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500984
985 parse_opts(args, &opts);
986
987 if (opts.rfd == ~0 || opts.wfd == ~0) {
988 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500989 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500990 }
991
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500992 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500993 if (err < 0)
994 goto error;
995
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500996 p = (struct p9_trans_fd *) client->trans;
997 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600998 if (IS_ERR(p->conn)) {
999 err = PTR_ERR(p->conn);
1000 p->conn = NULL;
1001 goto error;
1002 }
1003
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001004 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001005
1006error:
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001007 kfree(p);
1008 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001009}
1010
1011static struct p9_trans_module p9_tcp_trans = {
1012 .name = "tcp",
1013 .maxsize = MAX_SOCK_BUF,
1014 .def = 1,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001015 .create = p9_fd_create_tcp,
1016 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001017 .request = p9_fd_request,
1018 .cancel = p9_fd_cancel,
Tejun Heo72029fe2008-09-24 16:22:23 -05001019 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001020};
1021
1022static struct p9_trans_module p9_unix_trans = {
1023 .name = "unix",
1024 .maxsize = MAX_SOCK_BUF,
1025 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001026 .create = p9_fd_create_unix,
1027 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001028 .request = p9_fd_request,
1029 .cancel = p9_fd_cancel,
Tejun Heo72029fe2008-09-24 16:22:23 -05001030 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001031};
1032
1033static struct p9_trans_module p9_fd_trans = {
1034 .name = "fd",
1035 .maxsize = MAX_SOCK_BUF,
1036 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001037 .create = p9_fd_create,
1038 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001039 .request = p9_fd_request,
1040 .cancel = p9_fd_cancel,
Tejun Heo72029fe2008-09-24 16:22:23 -05001041 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001042};
1043
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001044/**
1045 * p9_poll_proc - poll worker thread
1046 * @a: thread state and arguments
1047 *
1048 * polls all v9fs transports for new events and queues the appropriate
1049 * work to the work queue
1050 *
1051 */
1052
1053static int p9_poll_proc(void *a)
1054{
1055 unsigned long flags;
1056
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001057 P9_DPRINTK(P9_DEBUG_TRANS, "start %p\n", current);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001058 repeat:
1059 spin_lock_irqsave(&p9_poll_lock, flags);
1060 while (!list_empty(&p9_poll_pending_list)) {
1061 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1062 struct p9_conn,
1063 poll_pending_link);
1064 list_del_init(&conn->poll_pending_link);
1065 spin_unlock_irqrestore(&p9_poll_lock, flags);
1066
1067 p9_poll_mux(conn);
1068
1069 spin_lock_irqsave(&p9_poll_lock, flags);
1070 }
1071 spin_unlock_irqrestore(&p9_poll_lock, flags);
1072
1073 set_current_state(TASK_INTERRUPTIBLE);
1074 if (list_empty(&p9_poll_pending_list)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001075 P9_DPRINTK(P9_DEBUG_TRANS, "sleeping...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001076 schedule();
1077 }
1078 __set_current_state(TASK_RUNNING);
1079
1080 if (!kthread_should_stop())
1081 goto repeat;
1082
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001083 P9_DPRINTK(P9_DEBUG_TRANS, "finish\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001084 return 0;
1085}
1086
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001087int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001088{
Tejun Heo206ca502008-09-24 16:22:23 -05001089 p9_mux_wq = create_workqueue("v9fs");
1090 if (!p9_mux_wq) {
1091 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
1092 return -ENOMEM;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001093 }
1094
Tejun Heo992b3f12008-10-13 18:45:25 -05001095 p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll");
1096 if (IS_ERR(p9_poll_task)) {
1097 destroy_workqueue(p9_mux_wq);
1098 printk(KERN_WARNING "v9fs: mux: creating poll task failed\n");
1099 return PTR_ERR(p9_poll_task);
1100 }
1101
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001102 v9fs_register_trans(&p9_tcp_trans);
1103 v9fs_register_trans(&p9_unix_trans);
1104 v9fs_register_trans(&p9_fd_trans);
1105
Andrew Morton3387b802008-03-28 14:15:57 -07001106 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001107}
Tejun Heo72029fe2008-09-24 16:22:23 -05001108
1109void p9_trans_fd_exit(void)
1110{
Tejun Heo992b3f12008-10-13 18:45:25 -05001111 kthread_stop(p9_poll_task);
Tejun Heo72029fe2008-09-24 16:22:23 -05001112 v9fs_unregister_trans(&p9_tcp_trans);
1113 v9fs_unregister_trans(&p9_unix_trans);
1114 v9fs_unregister_trans(&p9_fd_trans);
Tejun Heo206ca502008-09-24 16:22:23 -05001115
1116 destroy_workqueue(p9_mux_wq);
Tejun Heo72029fe2008-09-24 16:22:23 -05001117}