blob: 1df0356f242b651ddedaa937394a4cd9944e8c27 [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);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500191
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500192 spin_lock_irqsave(&m->client->lock, flags);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500193
194 if (m->err) {
195 spin_unlock_irqrestore(&m->client->lock, flags);
196 return;
197 }
198
199 m->err = err;
200
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500201 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500202 req->status = REQ_STATUS_ERROR;
203 if (!req->t_err)
204 req->t_err = err;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500205 list_move(&req->req_list, &cancel_list);
Tejun Heo992b3f12008-10-13 18:45:25 -0500206 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500207 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500208 req->status = REQ_STATUS_ERROR;
209 if (!req->t_err)
210 req->t_err = err;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500211 list_move(&req->req_list, &cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600212 }
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500213 spin_unlock_irqrestore(&m->client->lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600214
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500215 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
216 list_del(&req->req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500217 P9_DPRINTK(P9_DEBUG_ERROR, "call back req %p\n", req);
218 p9_client_cb(m->client, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600219 }
220}
221
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500222static unsigned int
223p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
224{
225 int ret, n;
226 struct p9_trans_fd *ts = NULL;
227
228 if (client && client->status == Connected)
229 ts = client->trans;
230
231 if (!ts)
232 return -EREMOTEIO;
233
234 if (!ts->rd->f_op || !ts->rd->f_op->poll)
235 return -EIO;
236
237 if (!ts->wr->f_op || !ts->wr->f_op->poll)
238 return -EIO;
239
240 ret = ts->rd->f_op->poll(ts->rd, pt);
241 if (ret < 0)
242 return ret;
243
244 if (ts->rd != ts->wr) {
245 n = ts->wr->f_op->poll(ts->wr, pt);
246 if (n < 0)
247 return n;
248 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
249 }
250
251 return ret;
252}
253
254/**
255 * p9_fd_read- read from a fd
256 * @client: client instance
257 * @v: buffer to receive data into
258 * @len: size of receive buffer
259 *
260 */
261
262static int p9_fd_read(struct p9_client *client, void *v, int len)
263{
264 int ret;
265 struct p9_trans_fd *ts = NULL;
266
267 if (client && client->status != Disconnected)
268 ts = client->trans;
269
270 if (!ts)
271 return -EREMOTEIO;
272
273 if (!(ts->rd->f_flags & O_NONBLOCK))
274 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
275
276 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
277 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
278 client->status = Disconnected;
279 return ret;
280}
281
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600282/**
283 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600284 * @work: container of work to be done
285 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600286 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600287
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600288static void p9_read_work(struct work_struct *work)
289{
290 int n, err;
291 struct p9_conn *m;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600292
293 m = container_of(work, struct p9_conn, rq);
294
295 if (m->err < 0)
296 return;
297
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500298 P9_DPRINTK(P9_DEBUG_TRANS, "start mux %p pos %d\n", m, m->rpos);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600299
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500300 if (!m->rbuf) {
301 m->rbuf = m->tmp_buf;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600302 m->rpos = 0;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500303 m->rsize = 7; /* start by reading header */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600304 }
305
306 clear_bit(Rpending, &m->wsched);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500307 P9_DPRINTK(P9_DEBUG_TRANS, "read mux %p pos %d size: %d = %d\n", m,
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500308 m->rpos, m->rsize, m->rsize-m->rpos);
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500309 err = p9_fd_read(m->client, m->rbuf + m->rpos,
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500310 m->rsize - m->rpos);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500311 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600312 if (err == -EAGAIN) {
313 clear_bit(Rworksched, &m->wsched);
314 return;
315 }
316
317 if (err <= 0)
318 goto error;
319
320 m->rpos += err;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500321
322 if ((!m->req) && (m->rpos == m->rsize)) { /* header read in */
323 u16 tag;
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500324 P9_DPRINTK(P9_DEBUG_TRANS, "got new header\n");
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500325
326 n = le32_to_cpu(*(__le32 *) m->rbuf); /* read packet size */
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500327 if (n >= m->client->msize) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600328 P9_DPRINTK(P9_DEBUG_ERROR,
329 "requested packet size too big: %d\n", n);
330 err = -EIO;
331 goto error;
332 }
333
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500334 tag = le16_to_cpu(*(__le16 *) (m->rbuf+5)); /* read tag */
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500335 P9_DPRINTK(P9_DEBUG_TRANS,
336 "mux %p pkt: size: %d bytes tag: %d\n", m, n, tag);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600337
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500338 m->req = p9_tag_lookup(m->client, tag);
339 if (!m->req) {
340 P9_DPRINTK(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
341 tag);
342 err = -EIO;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600343 goto error;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500344 }
345
346 if (m->req->rc == NULL) {
347 m->req->rc = kmalloc(sizeof(struct p9_fcall) +
348 m->client->msize, GFP_KERNEL);
349 if (!m->req->rc) {
350 m->req = NULL;
351 err = -ENOMEM;
352 goto error;
353 }
354 }
355 m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall);
356 memcpy(m->rbuf, m->tmp_buf, m->rsize);
357 m->rsize = n;
358 }
359
360 /* not an else because some packets (like clunk) have no payload */
361 if ((m->req) && (m->rpos == m->rsize)) { /* packet is read in */
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500362 P9_DPRINTK(P9_DEBUG_TRANS, "got new packet\n");
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500363 spin_lock(&m->client->lock);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500364 list_del(&m->req->req_list);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500365 spin_unlock(&m->client->lock);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500366 p9_client_cb(m->client, m->req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600367
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500368 m->rbuf = NULL;
369 m->rpos = 0;
370 m->rsize = 0;
Eric Van Hensbergen1b0a7632008-10-13 18:45:22 -0500371 m->req = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600372 }
373
374 if (!list_empty(&m->req_list)) {
375 if (test_and_clear_bit(Rpending, &m->wsched))
376 n = POLLIN;
377 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500378 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600379
380 if (n & POLLIN) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500381 P9_DPRINTK(P9_DEBUG_TRANS, "sched read work %p\n", m);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600382 queue_work(p9_mux_wq, &m->rq);
383 } else
384 clear_bit(Rworksched, &m->wsched);
385 } else
386 clear_bit(Rworksched, &m->wsched);
387
388 return;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600389error:
390 p9_conn_cancel(m, err);
391 clear_bit(Rworksched, &m->wsched);
392}
393
394/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500395 * p9_fd_write - write to a socket
396 * @client: client instance
397 * @v: buffer to send data from
398 * @len: size of send buffer
399 *
400 */
401
402static int p9_fd_write(struct p9_client *client, void *v, int len)
403{
404 int ret;
405 mm_segment_t oldfs;
406 struct p9_trans_fd *ts = NULL;
407
408 if (client && client->status != Disconnected)
409 ts = client->trans;
410
411 if (!ts)
412 return -EREMOTEIO;
413
414 if (!(ts->wr->f_flags & O_NONBLOCK))
415 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
416
417 oldfs = get_fs();
418 set_fs(get_ds());
419 /* The cast to a user pointer is valid due to the set_fs() */
420 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
421 set_fs(oldfs);
422
423 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
424 client->status = Disconnected;
425 return ret;
426}
427
428/**
429 * p9_write_work - called when a transport can send some data
430 * @work: container for work to be done
431 *
432 */
433
434static void p9_write_work(struct work_struct *work)
435{
436 int n, err;
437 struct p9_conn *m;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500438 struct p9_req_t *req;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500439
440 m = container_of(work, struct p9_conn, wq);
441
442 if (m->err < 0) {
443 clear_bit(Wworksched, &m->wsched);
444 return;
445 }
446
447 if (!m->wsize) {
448 if (list_empty(&m->unsent_req_list)) {
449 clear_bit(Wworksched, &m->wsched);
450 return;
451 }
452
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500453 spin_lock(&m->client->lock);
454 req = list_entry(m->unsent_req_list.next, struct p9_req_t,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500455 req_list);
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500456 req->status = REQ_STATUS_SENT;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500457 list_move_tail(&req->req_list, &m->req_list);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500458
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500459 m->wbuf = req->tc->sdata;
460 m->wsize = req->tc->size;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500461 m->wpos = 0;
Eric Van Hensbergen673d62cd2008-10-13 18:45:22 -0500462 spin_unlock(&m->client->lock);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500463 }
464
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500465 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p pos %d size %d\n", m, m->wpos,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500466 m->wsize);
467 clear_bit(Wpending, &m->wsched);
468 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500469 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500470 if (err == -EAGAIN) {
471 clear_bit(Wworksched, &m->wsched);
472 return;
473 }
474
475 if (err < 0)
476 goto error;
477 else if (err == 0) {
478 err = -EREMOTEIO;
479 goto error;
480 }
481
482 m->wpos += err;
483 if (m->wpos == m->wsize)
484 m->wpos = m->wsize = 0;
485
486 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
487 if (test_and_clear_bit(Wpending, &m->wsched))
488 n = POLLOUT;
489 else
490 n = p9_fd_poll(m->client, NULL);
491
492 if (n & POLLOUT) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500493 P9_DPRINTK(P9_DEBUG_TRANS, "sched write work %p\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500494 queue_work(p9_mux_wq, &m->wq);
495 } else
496 clear_bit(Wworksched, &m->wsched);
497 } else
498 clear_bit(Wworksched, &m->wsched);
499
500 return;
501
502error:
503 p9_conn_cancel(m, err);
504 clear_bit(Wworksched, &m->wsched);
505}
506
507static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
508{
509 struct p9_poll_wait *pwait =
510 container_of(wait, struct p9_poll_wait, wait);
511 struct p9_conn *m = pwait->conn;
512 unsigned long flags;
513 DECLARE_WAITQUEUE(dummy_wait, p9_poll_task);
514
515 spin_lock_irqsave(&p9_poll_lock, flags);
516 if (list_empty(&m->poll_pending_link))
517 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
518 spin_unlock_irqrestore(&p9_poll_lock, flags);
519
520 /* perform the default wake up operation */
521 return default_wake_function(&dummy_wait, mode, sync, key);
522}
523
524/**
525 * p9_pollwait - add poll task to the wait queue
526 * @filp: file pointer being polled
527 * @wait_address: wait_q to block on
528 * @p: poll state
529 *
530 * called by files poll operation to add v9fs-poll task to files wait queue
531 */
532
533static void
534p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
535{
536 struct p9_conn *m = container_of(p, struct p9_conn, pt);
537 struct p9_poll_wait *pwait = NULL;
538 int i;
539
540 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
541 if (m->poll_wait[i].wait_addr == NULL) {
542 pwait = &m->poll_wait[i];
543 break;
544 }
545 }
546
547 if (!pwait) {
548 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
549 return;
550 }
551
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500552 pwait->conn = m;
553 pwait->wait_addr = wait_address;
554 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
555 add_wait_queue(wait_address, &pwait->wait);
556}
557
558/**
559 * p9_conn_create - allocate and initialize the per-session mux data
560 * @client: client instance
561 *
562 * Note: Creates the polling task if this is the first session.
563 */
564
565static struct p9_conn *p9_conn_create(struct p9_client *client)
566{
Tejun Heo95820a32008-10-13 18:45:20 -0500567 int n;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500568 struct p9_conn *m;
569
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500570 P9_DPRINTK(P9_DEBUG_TRANS, "client %p msize %d\n", client,
571 client->msize);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500572 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
573 if (!m)
574 return ERR_PTR(-ENOMEM);
575
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500576 INIT_LIST_HEAD(&m->mux_list);
577 m->client = client;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500578
579 INIT_LIST_HEAD(&m->req_list);
580 INIT_LIST_HEAD(&m->unsent_req_list);
581 INIT_WORK(&m->rq, p9_read_work);
582 INIT_WORK(&m->wq, p9_write_work);
583 INIT_LIST_HEAD(&m->poll_pending_link);
584 init_poll_funcptr(&m->pt, p9_pollwait);
585
586 n = p9_fd_poll(client, &m->pt);
587 if (n & POLLIN) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500588 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500589 set_bit(Rpending, &m->wsched);
590 }
591
592 if (n & POLLOUT) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500593 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can write\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500594 set_bit(Wpending, &m->wsched);
595 }
596
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500597 return m;
598}
599
600/**
601 * p9_poll_mux - polls a mux and schedules read or write works if necessary
602 * @m: connection to poll
603 *
604 */
605
606static void p9_poll_mux(struct p9_conn *m)
607{
608 int n;
609
610 if (m->err < 0)
611 return;
612
613 n = p9_fd_poll(m->client, NULL);
614 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500615 P9_DPRINTK(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500616 if (n >= 0)
617 n = -ECONNRESET;
618 p9_conn_cancel(m, n);
619 }
620
621 if (n & POLLIN) {
622 set_bit(Rpending, &m->wsched);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500623 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can read\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500624 if (!test_and_set_bit(Rworksched, &m->wsched)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500625 P9_DPRINTK(P9_DEBUG_TRANS, "sched read work %p\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500626 queue_work(p9_mux_wq, &m->rq);
627 }
628 }
629
630 if (n & POLLOUT) {
631 set_bit(Wpending, &m->wsched);
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500632 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p can write\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500633 if ((m->wsize || !list_empty(&m->unsent_req_list))
634 && !test_and_set_bit(Wworksched, &m->wsched)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500635 P9_DPRINTK(P9_DEBUG_TRANS, "sched write work %p\n", m);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500636 queue_work(p9_mux_wq, &m->wq);
637 }
638 }
639}
640
641/**
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500642 * p9_fd_request - send 9P request
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600643 * The function can sleep until the request is scheduled for sending.
644 * The function can be interrupted. Return from the function is not
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500645 * a guarantee that the request is sent successfully.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600646 *
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500647 * @client: client instance
648 * @req: request to be sent
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600649 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600650 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600651
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500652static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600653{
654 int n;
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500655 struct p9_trans_fd *ts = client->trans;
656 struct p9_conn *m = ts->conn;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600657
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500658 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n", m,
659 current, req->tc, req->tc->id);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600660 if (m->err < 0)
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500661 return m->err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600662
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500663 spin_lock(&client->lock);
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500664 req->status = REQ_STATUS_UNSENT;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600665 list_add_tail(&req->req_list, &m->unsent_req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500666 spin_unlock(&client->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600667
668 if (test_and_clear_bit(Wpending, &m->wsched))
669 n = POLLOUT;
670 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500671 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600672
673 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
674 queue_work(p9_mux_wq, &m->wq);
675
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500676 return 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600677}
678
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500679static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600680{
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500681 int ret = 1;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600682
Eric Van Hensbergen0b15a3a2008-10-22 18:47:40 -0500683 P9_DPRINTK(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600684
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500685 spin_lock(&client->lock);
686 list_del(&req->req_list);
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500687
Eric Van Hensbergen91b85342008-10-13 18:45:21 -0500688 if (req->status == REQ_STATUS_UNSENT) {
689 req->status = REQ_STATUS_FLSHD;
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500690 ret = 0;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600691 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600692
Eric Van Hensbergen7eb923b2008-10-17 12:45:40 -0500693 spin_unlock(&client->lock);
694
695 return ret;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600696}
697
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600698/**
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600699 * parse_options - parse mount options into session structure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500700 * @options: options string passed from mount
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600701 * @opts: transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500702 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600703 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500704 */
705
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600706static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500707{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500708 char *p;
709 substring_t args[MAX_OPT_ARGS];
710 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600711 char *options;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500712 int ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500713
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500714 opts->port = P9_PORT;
715 opts->rfd = ~0;
716 opts->wfd = ~0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500717
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600718 if (!params)
719 return 0;
720
721 options = kstrdup(params, GFP_KERNEL);
722 if (!options) {
723 P9_DPRINTK(P9_DEBUG_ERROR,
724 "failed to allocate copy of option string\n");
725 return -ENOMEM;
726 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500727
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500728 while ((p = strsep(&options, ",")) != NULL) {
729 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600730 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500731 if (!*p)
732 continue;
733 token = match_token(p, tokens, args);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600734 r = match_int(&args[0], &option);
735 if (r < 0) {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500736 P9_DPRINTK(P9_DEBUG_ERROR,
737 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600738 ret = r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500739 continue;
740 }
741 switch (token) {
742 case Opt_port:
743 opts->port = option;
744 break;
745 case Opt_rfdno:
746 opts->rfd = option;
747 break;
748 case Opt_wfdno:
749 opts->wfd = option;
750 break;
751 default:
752 continue;
753 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500754 }
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600755 kfree(options);
756 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500757}
758
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500759static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500760{
761 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
762 GFP_KERNEL);
763 if (!ts)
764 return -ENOMEM;
765
766 ts->rd = fget(rfd);
767 ts->wr = fget(wfd);
768 if (!ts->rd || !ts->wr) {
769 if (ts->rd)
770 fput(ts->rd);
771 if (ts->wr)
772 fput(ts->wr);
773 kfree(ts);
774 return -EIO;
775 }
776
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500777 client->trans = ts;
778 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500779
780 return 0;
781}
782
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500783static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500784{
785 int fd, ret;
786
787 csocket->sk->sk_allocation = GFP_NOIO;
Ulrich Dreppera677a032008-07-23 21:29:17 -0700788 fd = sock_map_fd(csocket, 0);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500789 if (fd < 0) {
790 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
791 return fd;
792 }
793
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500794 ret = p9_fd_open(client, fd, fd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500795 if (ret < 0) {
796 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
797 sockfd_put(csocket);
798 return ret;
799 }
800
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500801 ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500802
803 return 0;
804}
805
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500806/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500807 * p9_mux_destroy - cancels all pending requests and frees mux resources
808 * @m: mux to destroy
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500809 *
810 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600811
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500812static void p9_conn_destroy(struct p9_conn *m)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500813{
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500814 P9_DPRINTK(P9_DEBUG_TRANS, "mux %p prev %p next %p\n", m,
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500815 m->mux_list.prev, m->mux_list.next);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500816
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500817 p9_mux_poll_stop(m);
818 cancel_work_sync(&m->rq);
819 cancel_work_sync(&m->wq);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500820
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500821 p9_conn_cancel(m, -ECONNRESET);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500822
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500823 m->client = NULL;
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500824 kfree(m);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500825}
826
827/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500828 * p9_fd_close - shutdown file descriptor transport
829 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500830 *
831 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600832
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500833static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500834{
835 struct p9_trans_fd *ts;
836
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500837 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500838 return;
839
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500840 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500841 if (!ts)
842 return;
843
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500844 client->status = Disconnected;
845
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600846 p9_conn_destroy(ts->conn);
847
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500848 if (ts->rd)
849 fput(ts->rd);
850 if (ts->wr)
851 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500852
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500853 kfree(ts);
854}
855
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500856/*
857 * stolen from NFS - maybe should be made a generic function?
858 */
859static inline int valid_ipaddr4(const char *buf)
860{
861 int rc, count, in[4];
862
863 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
864 if (rc != 4)
865 return -EINVAL;
866 for (count = 0; count < 4; count++) {
867 if (in[count] > 255)
868 return -EINVAL;
869 }
870 return 0;
871}
872
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500873static int
874p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500875{
876 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500877 struct socket *csocket;
878 struct sockaddr_in sin_server;
879 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500880 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500881
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -0600882 err = parse_opts(args, &opts);
883 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500884 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500885
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500886 if (valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500887 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -0500888
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500889 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500890
891 sin_server.sin_family = AF_INET;
892 sin_server.sin_addr.s_addr = in_aton(addr);
893 sin_server.sin_port = htons(opts.port);
894 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
895
896 if (!csocket) {
897 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
898 err = -EIO;
899 goto error;
900 }
901
902 err = csocket->ops->connect(csocket,
903 (struct sockaddr *)&sin_server,
904 sizeof(struct sockaddr_in), 0);
905 if (err < 0) {
906 P9_EPRINTK(KERN_ERR,
907 "p9_trans_tcp: problem connecting socket to %s\n",
908 addr);
909 goto error;
910 }
911
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500912 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500913 if (err < 0)
914 goto error;
915
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500916 p = (struct p9_trans_fd *) client->trans;
917 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600918 if (IS_ERR(p->conn)) {
919 err = PTR_ERR(p->conn);
920 p->conn = NULL;
921 goto error;
922 }
923
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500924 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500925
926error:
927 if (csocket)
928 sock_release(csocket);
929
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500930 kfree(p);
931
932 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500933}
934
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500935static int
936p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500937{
938 int err;
939 struct socket *csocket;
940 struct sockaddr_un sun_server;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500941 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500942
943 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500944
945 if (strlen(addr) > UNIX_PATH_MAX) {
946 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
947 addr);
948 err = -ENAMETOOLONG;
949 goto error;
950 }
951
952 sun_server.sun_family = PF_UNIX;
953 strcpy(sun_server.sun_path, addr);
954 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
955 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
956 sizeof(struct sockaddr_un) - 1, 0);
957 if (err < 0) {
958 P9_EPRINTK(KERN_ERR,
959 "p9_trans_unix: problem connecting socket: %s: %d\n",
960 addr, err);
961 goto error;
962 }
963
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500964 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500965 if (err < 0)
966 goto error;
967
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500968 p = (struct p9_trans_fd *) client->trans;
969 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600970 if (IS_ERR(p->conn)) {
971 err = PTR_ERR(p->conn);
972 p->conn = NULL;
973 goto error;
974 }
975
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500976 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500977
978error:
979 if (csocket)
980 sock_release(csocket);
981
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500982 kfree(p);
983 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500984}
985
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500986static int
987p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500988{
989 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500990 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500991 struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500992
993 parse_opts(args, &opts);
994
995 if (opts.rfd == ~0 || opts.wfd == ~0) {
996 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500997 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -0500998 }
999
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001000 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001001 if (err < 0)
1002 goto error;
1003
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001004 p = (struct p9_trans_fd *) client->trans;
1005 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001006 if (IS_ERR(p->conn)) {
1007 err = PTR_ERR(p->conn);
1008 p->conn = NULL;
1009 goto error;
1010 }
1011
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001012 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001013
1014error:
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001015 kfree(p);
1016 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001017}
1018
1019static struct p9_trans_module p9_tcp_trans = {
1020 .name = "tcp",
1021 .maxsize = MAX_SOCK_BUF,
1022 .def = 1,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001023 .create = p9_fd_create_tcp,
1024 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001025 .request = p9_fd_request,
1026 .cancel = p9_fd_cancel,
Tejun Heo72029fe2008-09-24 16:22:23 -05001027 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001028};
1029
1030static struct p9_trans_module p9_unix_trans = {
1031 .name = "unix",
1032 .maxsize = MAX_SOCK_BUF,
1033 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001034 .create = p9_fd_create_unix,
1035 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001036 .request = p9_fd_request,
1037 .cancel = p9_fd_cancel,
Tejun Heo72029fe2008-09-24 16:22:23 -05001038 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001039};
1040
1041static struct p9_trans_module p9_fd_trans = {
1042 .name = "fd",
1043 .maxsize = MAX_SOCK_BUF,
1044 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001045 .create = p9_fd_create,
1046 .close = p9_fd_close,
Eric Van Hensbergen91b85342008-10-13 18:45:21 -05001047 .request = p9_fd_request,
1048 .cancel = p9_fd_cancel,
Tejun Heo72029fe2008-09-24 16:22:23 -05001049 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001050};
1051
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001052/**
1053 * p9_poll_proc - poll worker thread
1054 * @a: thread state and arguments
1055 *
1056 * polls all v9fs transports for new events and queues the appropriate
1057 * work to the work queue
1058 *
1059 */
1060
1061static int p9_poll_proc(void *a)
1062{
1063 unsigned long flags;
1064
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001065 P9_DPRINTK(P9_DEBUG_TRANS, "start %p\n", current);
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001066 repeat:
1067 spin_lock_irqsave(&p9_poll_lock, flags);
1068 while (!list_empty(&p9_poll_pending_list)) {
1069 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1070 struct p9_conn,
1071 poll_pending_link);
1072 list_del_init(&conn->poll_pending_link);
1073 spin_unlock_irqrestore(&p9_poll_lock, flags);
1074
1075 p9_poll_mux(conn);
1076
1077 spin_lock_irqsave(&p9_poll_lock, flags);
1078 }
1079 spin_unlock_irqrestore(&p9_poll_lock, flags);
1080
1081 set_current_state(TASK_INTERRUPTIBLE);
1082 if (list_empty(&p9_poll_pending_list)) {
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001083 P9_DPRINTK(P9_DEBUG_TRANS, "sleeping...\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001084 schedule();
1085 }
1086 __set_current_state(TASK_RUNNING);
1087
1088 if (!kthread_should_stop())
1089 goto repeat;
1090
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -05001091 P9_DPRINTK(P9_DEBUG_TRANS, "finish\n");
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001092 return 0;
1093}
1094
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001095int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001096{
Tejun Heo206ca502008-09-24 16:22:23 -05001097 p9_mux_wq = create_workqueue("v9fs");
1098 if (!p9_mux_wq) {
1099 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
1100 return -ENOMEM;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001101 }
1102
Tejun Heo992b3f12008-10-13 18:45:25 -05001103 p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll");
1104 if (IS_ERR(p9_poll_task)) {
1105 destroy_workqueue(p9_mux_wq);
1106 printk(KERN_WARNING "v9fs: mux: creating poll task failed\n");
1107 return PTR_ERR(p9_poll_task);
1108 }
1109
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001110 v9fs_register_trans(&p9_tcp_trans);
1111 v9fs_register_trans(&p9_unix_trans);
1112 v9fs_register_trans(&p9_fd_trans);
1113
Andrew Morton3387b802008-03-28 14:15:57 -07001114 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001115}
Tejun Heo72029fe2008-09-24 16:22:23 -05001116
1117void p9_trans_fd_exit(void)
1118{
Tejun Heo992b3f12008-10-13 18:45:25 -05001119 kthread_stop(p9_poll_task);
Tejun Heo72029fe2008-09-24 16:22:23 -05001120 v9fs_unregister_trans(&p9_tcp_trans);
1121 v9fs_unregister_trans(&p9_unix_trans);
1122 v9fs_unregister_trans(&p9_fd_trans);
Tejun Heo206ca502008-09-24 16:22:23 -05001123
1124 destroy_workqueue(p9_mux_wq);
Tejun Heo72029fe2008-09-24 16:22:23 -05001125}