blob: 334d39cc5ba31c4ccabce41b42f1509150e268f3 [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 ERREQFLUSH 1
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060048#define MAXPOLLWADDR 2
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050049
Eric Van Hensbergenee443992008-03-05 07:08:09 -060050/**
51 * struct p9_fd_opts - per-transport options
52 * @rfd: file descriptor for reading (trans=fd)
53 * @wfd: file descriptor for writing (trans=fd)
54 * @port: port to connect to (trans=tcp)
55 *
56 */
57
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050058struct p9_fd_opts {
59 int rfd;
60 int wfd;
61 u16 port;
62};
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050063
Eric Van Hensbergenee443992008-03-05 07:08:09 -060064/**
65 * struct p9_trans_fd - transport state
66 * @rd: reference to file to read from
67 * @wr: reference of file to write to
68 * @conn: connection state reference
69 *
70 */
71
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050072struct p9_trans_fd {
73 struct file *rd;
74 struct file *wr;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060075 struct p9_conn *conn;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050076};
77
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050078/*
79 * Option Parsing (code inspired by NFS code)
80 * - a little lazy - parse all fd-transport options
81 */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050082
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050083enum {
84 /* Options that take integer arguments */
Latchesar Ionkov55762692007-11-06 08:02:53 -060085 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050086};
87
Steven Whitehousea447c092008-10-13 10:46:57 +010088static const match_table_t tokens = {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050089 {Opt_port, "port=%u"},
90 {Opt_rfdno, "rfdno=%u"},
91 {Opt_wfdno, "wfdno=%u"},
Latchesar Ionkov55762692007-11-06 08:02:53 -060092 {Opt_err, NULL},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050093};
94
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060095enum {
96 Rworksched = 1, /* read work scheduled or running */
97 Rpending = 2, /* can read */
98 Wworksched = 4, /* write work scheduled or running */
99 Wpending = 8, /* can write */
100};
101
102enum {
103 None,
104 Flushing,
105 Flushed,
106};
107
108struct p9_req;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600109typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600110
111/**
112 * struct p9_req - fd mux encoding of an rpc transaction
113 * @lock: protects req_list
114 * @tag: numeric tag for rpc transaction
115 * @tcall: request &p9_fcall structure
116 * @rcall: response &p9_fcall structure
117 * @err: error state
118 * @cb: callback for when response is received
119 * @cba: argument to pass to callback
120 * @flush: flag to indicate RPC has been flushed
121 * @req_list: list link for higher level objects to chain requests
122 *
123 */
124
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600125struct p9_req {
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600126 spinlock_t lock;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600127 int tag;
128 struct p9_fcall *tcall;
129 struct p9_fcall *rcall;
130 int err;
131 p9_conn_req_callback cb;
132 void *cba;
133 int flush;
134 struct list_head req_list;
135};
136
Tejun Heo992b3f12008-10-13 18:45:25 -0500137struct p9_poll_wait {
138 struct p9_conn *conn;
139 wait_queue_t wait;
140 wait_queue_head_t *wait_addr;
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600141};
142
143/**
144 * struct p9_conn - fd mux connection state information
145 * @lock: protects mux_list (?)
146 * @mux_list: list link for mux to manage multiple connections (?)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500147 * @client: reference to client instance for this connection
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600148 * @tagpool: id accounting for transactions
149 * @err: error state
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600150 * @req_list: accounting for requests which have been sent
151 * @unsent_req_list: accounting for requests that haven't been sent
152 * @rcall: current response &p9_fcall structure
153 * @rpos: read position in current frame
154 * @rbuf: current read buffer
155 * @wpos: write position for current frame
156 * @wsize: amount of data to write for current frame
157 * @wbuf: current write buffer
158 * @poll_wait: array of wait_q's for various worker threads
159 * @poll_waddr: ????
160 * @pt: poll state
161 * @rq: current read work
162 * @wq: current write work
163 * @wsched: ????
164 *
165 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600166
167struct p9_conn {
168 spinlock_t lock; /* protect lock structure */
169 struct list_head mux_list;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500170 struct p9_client *client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600171 struct p9_idpool *tagpool;
172 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600173 struct list_head req_list;
174 struct list_head unsent_req_list;
175 struct p9_fcall *rcall;
176 int rpos;
177 char *rbuf;
178 int wpos;
179 int wsize;
180 char *wbuf;
Tejun Heo992b3f12008-10-13 18:45:25 -0500181 struct list_head poll_pending_link;
182 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600183 poll_table pt;
184 struct work_struct rq;
185 struct work_struct wq;
186 unsigned long wsched;
187};
188
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600189/**
190 * struct p9_mux_rpc - fd mux rpc accounting structure
191 * @m: connection this request was issued on
192 * @err: error state
193 * @tcall: request &p9_fcall
194 * @rcall: response &p9_fcall
195 * @wqueue: wait queue that client is blocked on for this rpc
196 *
197 * Bug: isn't this information duplicated elsewhere like &p9_req
198 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600199
200struct p9_mux_rpc {
201 struct p9_conn *m;
202 int err;
203 struct p9_fcall *tcall;
204 struct p9_fcall *rcall;
205 wait_queue_head_t wqueue;
206};
207
Tejun Heo992b3f12008-10-13 18:45:25 -0500208static DEFINE_SPINLOCK(p9_poll_lock);
209static LIST_HEAD(p9_poll_pending_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600210static struct workqueue_struct *p9_mux_wq;
Tejun Heo992b3f12008-10-13 18:45:25 -0500211static struct task_struct *p9_poll_task;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600212
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600213static u16 p9_mux_get_tag(struct p9_conn *m)
214{
215 int tag;
216
217 tag = p9_idpool_get(m->tagpool);
218 if (tag < 0)
219 return P9_NOTAG;
220 else
221 return (u16) tag;
222}
223
224static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
225{
226 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
227 p9_idpool_put(tag, m->tagpool);
228}
229
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600230static void p9_mux_poll_stop(struct p9_conn *m)
231{
Tejun Heo992b3f12008-10-13 18:45:25 -0500232 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600233 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600234
Tejun Heo992b3f12008-10-13 18:45:25 -0500235 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
236 struct p9_poll_wait *pwait = &m->poll_wait[i];
237
238 if (pwait->wait_addr) {
239 remove_wait_queue(pwait->wait_addr, &pwait->wait);
240 pwait->wait_addr = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600241 }
242 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500243
244 spin_lock_irqsave(&p9_poll_lock, flags);
245 list_del_init(&m->poll_pending_link);
246 spin_unlock_irqrestore(&p9_poll_lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600247}
248
249/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500250 * p9_conn_cancel - cancel all pending requests with error
251 * @m: mux data
252 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600253 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600254 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600255
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500256void p9_conn_cancel(struct p9_conn *m, int err)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600257{
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500258 struct p9_req *req, *rtmp;
259 LIST_HEAD(cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600260
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500261 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
262 m->err = err;
263 spin_lock(&m->lock);
264 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
265 list_move(&req->req_list, &cancel_list);
Tejun Heo992b3f12008-10-13 18:45:25 -0500266 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500267 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
268 list_move(&req->req_list, &cancel_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600269 }
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500270 spin_unlock(&m->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600271
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500272 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
273 list_del(&req->req_list);
274 if (!req->err)
275 req->err = err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600276
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500277 if (req->cb)
278 (*req->cb) (req, req->cba);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600279 else
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500280 kfree(req->rcall);
281 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600282}
283
284static void process_request(struct p9_conn *m, struct p9_req *req)
285{
286 int ecode;
287 struct p9_str *ename;
288
289 if (!req->err && req->rcall->id == P9_RERROR) {
290 ecode = req->rcall->params.rerror.errno;
291 ename = &req->rcall->params.rerror.error;
292
293 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
294 ename->str);
295
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500296 if (m->client->dotu)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600297 req->err = -ecode;
298
299 if (!req->err) {
300 req->err = p9_errstr2errno(ename->str, ename->len);
301
302 /* string match failed */
303 if (!req->err) {
304 PRINT_FCALL_ERROR("unknown error", req->rcall);
305 req->err = -ESERVERFAULT;
306 }
307 }
308 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
309 P9_DPRINTK(P9_DEBUG_ERROR,
310 "fcall mismatch: expected %d, got %d\n",
311 req->tcall->id + 1, req->rcall->id);
312 if (!req->err)
313 req->err = -EIO;
314 }
315}
316
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500317static unsigned int
318p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
319{
320 int ret, n;
321 struct p9_trans_fd *ts = NULL;
322
323 if (client && client->status == Connected)
324 ts = client->trans;
325
326 if (!ts)
327 return -EREMOTEIO;
328
329 if (!ts->rd->f_op || !ts->rd->f_op->poll)
330 return -EIO;
331
332 if (!ts->wr->f_op || !ts->wr->f_op->poll)
333 return -EIO;
334
335 ret = ts->rd->f_op->poll(ts->rd, pt);
336 if (ret < 0)
337 return ret;
338
339 if (ts->rd != ts->wr) {
340 n = ts->wr->f_op->poll(ts->wr, pt);
341 if (n < 0)
342 return n;
343 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
344 }
345
346 return ret;
347}
348
349/**
350 * p9_fd_read- read from a fd
351 * @client: client instance
352 * @v: buffer to receive data into
353 * @len: size of receive buffer
354 *
355 */
356
357static int p9_fd_read(struct p9_client *client, void *v, int len)
358{
359 int ret;
360 struct p9_trans_fd *ts = NULL;
361
362 if (client && client->status != Disconnected)
363 ts = client->trans;
364
365 if (!ts)
366 return -EREMOTEIO;
367
368 if (!(ts->rd->f_flags & O_NONBLOCK))
369 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
370
371 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
372 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
373 client->status = Disconnected;
374 return ret;
375}
376
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600377/**
378 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600379 * @work: container of work to be done
380 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600381 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600382
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600383static void p9_read_work(struct work_struct *work)
384{
385 int n, err;
386 struct p9_conn *m;
387 struct p9_req *req, *rptr, *rreq;
388 struct p9_fcall *rcall;
389 char *rbuf;
390
391 m = container_of(work, struct p9_conn, rq);
392
393 if (m->err < 0)
394 return;
395
396 rcall = NULL;
397 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
398
399 if (!m->rcall) {
400 m->rcall =
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500401 kmalloc(sizeof(struct p9_fcall) + m->client->msize,
402 GFP_KERNEL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600403 if (!m->rcall) {
404 err = -ENOMEM;
405 goto error;
406 }
407
408 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
409 m->rpos = 0;
410 }
411
412 clear_bit(Rpending, &m->wsched);
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500413 err = p9_fd_read(m->client, m->rbuf + m->rpos,
414 m->client->msize - m->rpos);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600415 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
416 if (err == -EAGAIN) {
417 clear_bit(Rworksched, &m->wsched);
418 return;
419 }
420
421 if (err <= 0)
422 goto error;
423
424 m->rpos += err;
425 while (m->rpos > 4) {
426 n = le32_to_cpu(*(__le32 *) m->rbuf);
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500427 if (n >= m->client->msize) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600428 P9_DPRINTK(P9_DEBUG_ERROR,
429 "requested packet size too big: %d\n", n);
430 err = -EIO;
431 goto error;
432 }
433
434 if (m->rpos < n)
435 break;
436
437 err =
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500438 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->client->dotu);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600439 if (err < 0)
440 goto error;
441
442#ifdef CONFIG_NET_9P_DEBUG
443 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
444 char buf[150];
445
446 p9_printfcall(buf, sizeof(buf), m->rcall,
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500447 m->client->dotu);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600448 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
449 }
450#endif
451
452 rcall = m->rcall;
453 rbuf = m->rbuf;
454 if (m->rpos > n) {
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500455 m->rcall = kmalloc(sizeof(struct p9_fcall) +
456 m->client->msize, GFP_KERNEL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600457 if (!m->rcall) {
458 err = -ENOMEM;
459 goto error;
460 }
461
462 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
463 memmove(m->rbuf, rbuf + n, m->rpos - n);
464 m->rpos -= n;
465 } else {
466 m->rcall = NULL;
467 m->rbuf = NULL;
468 m->rpos = 0;
469 }
470
471 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
472 rcall->id, rcall->tag);
473
474 req = NULL;
475 spin_lock(&m->lock);
476 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
477 if (rreq->tag == rcall->tag) {
478 req = rreq;
479 if (req->flush != Flushing)
480 list_del(&req->req_list);
481 break;
482 }
483 }
484 spin_unlock(&m->lock);
485
486 if (req) {
487 req->rcall = rcall;
488 process_request(m, req);
489
490 if (req->flush != Flushing) {
491 if (req->cb)
492 (*req->cb) (req, req->cba);
493 else
494 kfree(req->rcall);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600495 }
496 } else {
497 if (err >= 0 && rcall->id != P9_RFLUSH)
498 P9_DPRINTK(P9_DEBUG_ERROR,
499 "unexpected response mux %p id %d tag %d\n",
500 m, rcall->id, rcall->tag);
501 kfree(rcall);
502 }
503 }
504
505 if (!list_empty(&m->req_list)) {
506 if (test_and_clear_bit(Rpending, &m->wsched))
507 n = POLLIN;
508 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500509 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600510
511 if (n & POLLIN) {
512 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
513 queue_work(p9_mux_wq, &m->rq);
514 } else
515 clear_bit(Rworksched, &m->wsched);
516 } else
517 clear_bit(Rworksched, &m->wsched);
518
519 return;
520
521error:
522 p9_conn_cancel(m, err);
523 clear_bit(Rworksched, &m->wsched);
524}
525
526/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -0500527 * p9_fd_write - write to a socket
528 * @client: client instance
529 * @v: buffer to send data from
530 * @len: size of send buffer
531 *
532 */
533
534static int p9_fd_write(struct p9_client *client, void *v, int len)
535{
536 int ret;
537 mm_segment_t oldfs;
538 struct p9_trans_fd *ts = NULL;
539
540 if (client && client->status != Disconnected)
541 ts = client->trans;
542
543 if (!ts)
544 return -EREMOTEIO;
545
546 if (!(ts->wr->f_flags & O_NONBLOCK))
547 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
548
549 oldfs = get_fs();
550 set_fs(get_ds());
551 /* The cast to a user pointer is valid due to the set_fs() */
552 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
553 set_fs(oldfs);
554
555 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
556 client->status = Disconnected;
557 return ret;
558}
559
560/**
561 * p9_write_work - called when a transport can send some data
562 * @work: container for work to be done
563 *
564 */
565
566static void p9_write_work(struct work_struct *work)
567{
568 int n, err;
569 struct p9_conn *m;
570 struct p9_req *req;
571
572 m = container_of(work, struct p9_conn, wq);
573
574 if (m->err < 0) {
575 clear_bit(Wworksched, &m->wsched);
576 return;
577 }
578
579 if (!m->wsize) {
580 if (list_empty(&m->unsent_req_list)) {
581 clear_bit(Wworksched, &m->wsched);
582 return;
583 }
584
585 spin_lock(&m->lock);
586again:
587 req = list_entry(m->unsent_req_list.next, struct p9_req,
588 req_list);
589 list_move_tail(&req->req_list, &m->req_list);
590 if (req->err == ERREQFLUSH)
591 goto again;
592
593 m->wbuf = req->tcall->sdata;
594 m->wsize = req->tcall->size;
595 m->wpos = 0;
596 spin_unlock(&m->lock);
597 }
598
599 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
600 m->wsize);
601 clear_bit(Wpending, &m->wsched);
602 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
603 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
604 if (err == -EAGAIN) {
605 clear_bit(Wworksched, &m->wsched);
606 return;
607 }
608
609 if (err < 0)
610 goto error;
611 else if (err == 0) {
612 err = -EREMOTEIO;
613 goto error;
614 }
615
616 m->wpos += err;
617 if (m->wpos == m->wsize)
618 m->wpos = m->wsize = 0;
619
620 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
621 if (test_and_clear_bit(Wpending, &m->wsched))
622 n = POLLOUT;
623 else
624 n = p9_fd_poll(m->client, NULL);
625
626 if (n & POLLOUT) {
627 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
628 queue_work(p9_mux_wq, &m->wq);
629 } else
630 clear_bit(Wworksched, &m->wsched);
631 } else
632 clear_bit(Wworksched, &m->wsched);
633
634 return;
635
636error:
637 p9_conn_cancel(m, err);
638 clear_bit(Wworksched, &m->wsched);
639}
640
641static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
642{
643 struct p9_poll_wait *pwait =
644 container_of(wait, struct p9_poll_wait, wait);
645 struct p9_conn *m = pwait->conn;
646 unsigned long flags;
647 DECLARE_WAITQUEUE(dummy_wait, p9_poll_task);
648
649 spin_lock_irqsave(&p9_poll_lock, flags);
650 if (list_empty(&m->poll_pending_link))
651 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
652 spin_unlock_irqrestore(&p9_poll_lock, flags);
653
654 /* perform the default wake up operation */
655 return default_wake_function(&dummy_wait, mode, sync, key);
656}
657
658/**
659 * p9_pollwait - add poll task to the wait queue
660 * @filp: file pointer being polled
661 * @wait_address: wait_q to block on
662 * @p: poll state
663 *
664 * called by files poll operation to add v9fs-poll task to files wait queue
665 */
666
667static void
668p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
669{
670 struct p9_conn *m = container_of(p, struct p9_conn, pt);
671 struct p9_poll_wait *pwait = NULL;
672 int i;
673
674 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
675 if (m->poll_wait[i].wait_addr == NULL) {
676 pwait = &m->poll_wait[i];
677 break;
678 }
679 }
680
681 if (!pwait) {
682 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
683 return;
684 }
685
686 if (!wait_address) {
687 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
688 pwait->wait_addr = ERR_PTR(-EIO);
689 return;
690 }
691
692 pwait->conn = m;
693 pwait->wait_addr = wait_address;
694 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
695 add_wait_queue(wait_address, &pwait->wait);
696}
697
698/**
699 * p9_conn_create - allocate and initialize the per-session mux data
700 * @client: client instance
701 *
702 * Note: Creates the polling task if this is the first session.
703 */
704
705static struct p9_conn *p9_conn_create(struct p9_client *client)
706{
707 int i, n;
708 struct p9_conn *m;
709
710 P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize);
711 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
712 if (!m)
713 return ERR_PTR(-ENOMEM);
714
715 spin_lock_init(&m->lock);
716 INIT_LIST_HEAD(&m->mux_list);
717 m->client = client;
718 m->tagpool = p9_idpool_create();
719 if (IS_ERR(m->tagpool)) {
720 kfree(m);
721 return ERR_PTR(-ENOMEM);
722 }
723
724 INIT_LIST_HEAD(&m->req_list);
725 INIT_LIST_HEAD(&m->unsent_req_list);
726 INIT_WORK(&m->rq, p9_read_work);
727 INIT_WORK(&m->wq, p9_write_work);
728 INIT_LIST_HEAD(&m->poll_pending_link);
729 init_poll_funcptr(&m->pt, p9_pollwait);
730
731 n = p9_fd_poll(client, &m->pt);
732 if (n & POLLIN) {
733 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
734 set_bit(Rpending, &m->wsched);
735 }
736
737 if (n & POLLOUT) {
738 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
739 set_bit(Wpending, &m->wsched);
740 }
741
742 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
743 if (IS_ERR(m->poll_wait[i].wait_addr)) {
744 p9_mux_poll_stop(m);
745 kfree(m);
746 /* return the error code */
747 return (void *)m->poll_wait[i].wait_addr;
748 }
749 }
750
751 return m;
752}
753
754/**
755 * p9_poll_mux - polls a mux and schedules read or write works if necessary
756 * @m: connection to poll
757 *
758 */
759
760static void p9_poll_mux(struct p9_conn *m)
761{
762 int n;
763
764 if (m->err < 0)
765 return;
766
767 n = p9_fd_poll(m->client, NULL);
768 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
769 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
770 if (n >= 0)
771 n = -ECONNRESET;
772 p9_conn_cancel(m, n);
773 }
774
775 if (n & POLLIN) {
776 set_bit(Rpending, &m->wsched);
777 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
778 if (!test_and_set_bit(Rworksched, &m->wsched)) {
779 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
780 queue_work(p9_mux_wq, &m->rq);
781 }
782 }
783
784 if (n & POLLOUT) {
785 set_bit(Wpending, &m->wsched);
786 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
787 if ((m->wsize || !list_empty(&m->unsent_req_list))
788 && !test_and_set_bit(Wworksched, &m->wsched)) {
789 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
790 queue_work(p9_mux_wq, &m->wq);
791 }
792 }
793}
794
795/**
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600796 * p9_send_request - send 9P request
797 * The function can sleep until the request is scheduled for sending.
798 * The function can be interrupted. Return from the function is not
799 * a guarantee that the request is sent successfully. Can return errors
800 * that can be retrieved by PTR_ERR macros.
801 *
802 * @m: mux data
803 * @tc: request to be sent
804 * @cb: callback function to call when response is received
805 * @cba: parameter to pass to the callback function
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600806 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600807 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600808
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600809static struct p9_req *p9_send_request(struct p9_conn *m,
810 struct p9_fcall *tc,
811 p9_conn_req_callback cb, void *cba)
812{
813 int n;
814 struct p9_req *req;
815
816 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
817 tc, tc->id);
818 if (m->err < 0)
819 return ERR_PTR(m->err);
820
821 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
822 if (!req)
823 return ERR_PTR(-ENOMEM);
824
825 if (tc->id == P9_TVERSION)
826 n = P9_NOTAG;
827 else
828 n = p9_mux_get_tag(m);
829
Julia Lawall62067822008-09-24 16:22:22 -0500830 if (n < 0) {
831 kfree(req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600832 return ERR_PTR(-ENOMEM);
Julia Lawall62067822008-09-24 16:22:22 -0500833 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600834
835 p9_set_tag(tc, n);
836
837#ifdef CONFIG_NET_9P_DEBUG
838 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
839 char buf[150];
840
Eric Van Hensbergenbead27f2008-10-13 18:45:24 -0500841 p9_printfcall(buf, sizeof(buf), tc, m->client->dotu);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600842 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
843 }
844#endif
845
846 spin_lock_init(&req->lock);
847 req->tag = n;
848 req->tcall = tc;
849 req->rcall = NULL;
850 req->err = 0;
851 req->cb = cb;
852 req->cba = cba;
853 req->flush = None;
854
855 spin_lock(&m->lock);
856 list_add_tail(&req->req_list, &m->unsent_req_list);
857 spin_unlock(&m->lock);
858
859 if (test_and_clear_bit(Wpending, &m->wsched))
860 n = POLLOUT;
861 else
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500862 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600863
864 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
865 queue_work(p9_mux_wq, &m->wq);
866
867 return req;
868}
869
870static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
871{
872 p9_mux_put_tag(m, req->tag);
873 kfree(req);
874}
875
876static void p9_mux_flush_cb(struct p9_req *freq, void *a)
877{
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600878 int tag;
879 struct p9_conn *m;
880 struct p9_req *req, *rreq, *rptr;
881
882 m = a;
883 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
884 freq->tcall, freq->rcall, freq->err,
885 freq->tcall->params.tflush.oldtag);
886
887 spin_lock(&m->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600888 tag = freq->tcall->params.tflush.oldtag;
889 req = NULL;
890 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
891 if (rreq->tag == tag) {
892 req = rreq;
893 list_del(&req->req_list);
894 break;
895 }
896 }
897 spin_unlock(&m->lock);
898
899 if (req) {
900 spin_lock(&req->lock);
901 req->flush = Flushed;
902 spin_unlock(&req->lock);
903
904 if (req->cb)
905 (*req->cb) (req, req->cba);
906 else
907 kfree(req->rcall);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600908 }
909
910 kfree(freq->tcall);
911 kfree(freq->rcall);
912 p9_mux_free_request(m, freq);
913}
914
915static int
916p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
917{
918 struct p9_fcall *fc;
919 struct p9_req *rreq, *rptr;
920
921 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
922
923 /* if a response was received for a request, do nothing */
924 spin_lock(&req->lock);
925 if (req->rcall || req->err) {
926 spin_unlock(&req->lock);
927 P9_DPRINTK(P9_DEBUG_MUX,
928 "mux %p req %p response already received\n", m, req);
929 return 0;
930 }
931
932 req->flush = Flushing;
933 spin_unlock(&req->lock);
934
935 spin_lock(&m->lock);
936 /* if the request is not sent yet, just remove it from the list */
937 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
938 if (rreq->tag == req->tag) {
939 P9_DPRINTK(P9_DEBUG_MUX,
940 "mux %p req %p request is not sent yet\n", m, req);
941 list_del(&rreq->req_list);
942 req->flush = Flushed;
943 spin_unlock(&m->lock);
944 if (req->cb)
945 (*req->cb) (req, req->cba);
946 return 0;
947 }
948 }
949 spin_unlock(&m->lock);
950
951 clear_thread_flag(TIF_SIGPENDING);
952 fc = p9_create_tflush(req->tag);
953 p9_send_request(m, fc, p9_mux_flush_cb, m);
954 return 1;
955}
956
957static void
958p9_conn_rpc_cb(struct p9_req *req, void *a)
959{
960 struct p9_mux_rpc *r;
961
962 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
963 r = a;
964 r->rcall = req->rcall;
965 r->err = req->err;
966
967 if (req->flush != None && !req->err)
968 r->err = -ERESTARTSYS;
969
970 wake_up(&r->wqueue);
971}
972
973/**
974 * p9_fd_rpc- sends 9P request and waits until a response is available.
975 * The function can be interrupted.
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500976 * @client: client instance
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600977 * @tc: request to be sent
978 * @rc: pointer where a pointer to the response is stored
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600979 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600980 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600981
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600982int
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500983p9_fd_rpc(struct p9_client *client, struct p9_fcall *tc, struct p9_fcall **rc)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600984{
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500985 struct p9_trans_fd *p = client->trans;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600986 struct p9_conn *m = p->conn;
987 int err, sigpending;
988 unsigned long flags;
989 struct p9_req *req;
990 struct p9_mux_rpc r;
991
992 r.err = 0;
993 r.tcall = tc;
994 r.rcall = NULL;
995 r.m = m;
996 init_waitqueue_head(&r.wqueue);
997
998 if (rc)
999 *rc = NULL;
1000
1001 sigpending = 0;
1002 if (signal_pending(current)) {
1003 sigpending = 1;
1004 clear_thread_flag(TIF_SIGPENDING);
1005 }
1006
1007 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
1008 if (IS_ERR(req)) {
1009 err = PTR_ERR(req);
1010 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1011 return err;
1012 }
1013
1014 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
1015 if (r.err < 0)
1016 err = r.err;
1017
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001018 if (err == -ERESTARTSYS && client->status == Connected
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001019 && m->err == 0) {
1020 if (p9_mux_flush_request(m, req)) {
1021 /* wait until we get response of the flush message */
1022 do {
1023 clear_thread_flag(TIF_SIGPENDING);
1024 err = wait_event_interruptible(r.wqueue,
1025 r.rcall || r.err);
1026 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001027 client->status == Connected && !m->err);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001028
1029 err = -ERESTARTSYS;
1030 }
1031 sigpending = 1;
1032 }
1033
1034 if (sigpending) {
1035 spin_lock_irqsave(&current->sighand->siglock, flags);
1036 recalc_sigpending();
1037 spin_unlock_irqrestore(&current->sighand->siglock, flags);
1038 }
1039
1040 if (rc)
1041 *rc = r.rcall;
1042 else
1043 kfree(r.rcall);
1044
1045 p9_mux_free_request(m, req);
1046 if (err > 0)
1047 err = -EIO;
1048
1049 return err;
1050}
1051
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001052/**
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001053 * parse_options - parse mount options into session structure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001054 * @options: options string passed from mount
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001055 * @opts: transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001056 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001057 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001058 */
1059
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001060static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001061{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001062 char *p;
1063 substring_t args[MAX_OPT_ARGS];
1064 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001065 char *options;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001066 int ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001067
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001068 opts->port = P9_PORT;
1069 opts->rfd = ~0;
1070 opts->wfd = ~0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001071
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001072 if (!params)
1073 return 0;
1074
1075 options = kstrdup(params, GFP_KERNEL);
1076 if (!options) {
1077 P9_DPRINTK(P9_DEBUG_ERROR,
1078 "failed to allocate copy of option string\n");
1079 return -ENOMEM;
1080 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001081
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001082 while ((p = strsep(&options, ",")) != NULL) {
1083 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001084 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001085 if (!*p)
1086 continue;
1087 token = match_token(p, tokens, args);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001088 r = match_int(&args[0], &option);
1089 if (r < 0) {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001090 P9_DPRINTK(P9_DEBUG_ERROR,
1091 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001092 ret = r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001093 continue;
1094 }
1095 switch (token) {
1096 case Opt_port:
1097 opts->port = option;
1098 break;
1099 case Opt_rfdno:
1100 opts->rfd = option;
1101 break;
1102 case Opt_wfdno:
1103 opts->wfd = option;
1104 break;
1105 default:
1106 continue;
1107 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001108 }
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001109 kfree(options);
1110 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001111}
1112
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001113static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001114{
1115 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1116 GFP_KERNEL);
1117 if (!ts)
1118 return -ENOMEM;
1119
1120 ts->rd = fget(rfd);
1121 ts->wr = fget(wfd);
1122 if (!ts->rd || !ts->wr) {
1123 if (ts->rd)
1124 fput(ts->rd);
1125 if (ts->wr)
1126 fput(ts->wr);
1127 kfree(ts);
1128 return -EIO;
1129 }
1130
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001131 client->trans = ts;
1132 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001133
1134 return 0;
1135}
1136
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001137static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001138{
1139 int fd, ret;
1140
1141 csocket->sk->sk_allocation = GFP_NOIO;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001142 fd = sock_map_fd(csocket, 0);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001143 if (fd < 0) {
1144 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1145 return fd;
1146 }
1147
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001148 ret = p9_fd_open(client, fd, fd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001149 if (ret < 0) {
1150 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1151 sockfd_put(csocket);
1152 return ret;
1153 }
1154
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001155 ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001156
1157 return 0;
1158}
1159
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001160/**
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001161 * p9_mux_destroy - cancels all pending requests and frees mux resources
1162 * @m: mux to destroy
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001163 *
1164 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001165
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001166static void p9_conn_destroy(struct p9_conn *m)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001167{
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001168 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
1169 m->mux_list.prev, m->mux_list.next);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001170
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001171 p9_mux_poll_stop(m);
1172 cancel_work_sync(&m->rq);
1173 cancel_work_sync(&m->wq);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001174
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001175 p9_conn_cancel(m, -ECONNRESET);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001176
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001177 m->client = NULL;
1178 p9_idpool_destroy(m->tagpool);
1179 kfree(m);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001180}
1181
1182/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001183 * p9_fd_close - shutdown file descriptor transport
1184 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001185 *
1186 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001187
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001188static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001189{
1190 struct p9_trans_fd *ts;
1191
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001192 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001193 return;
1194
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001195 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001196 if (!ts)
1197 return;
1198
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001199 client->status = Disconnected;
1200
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001201 p9_conn_destroy(ts->conn);
1202
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001203 if (ts->rd)
1204 fput(ts->rd);
1205 if (ts->wr)
1206 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001207
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001208 kfree(ts);
1209}
1210
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001211/*
1212 * stolen from NFS - maybe should be made a generic function?
1213 */
1214static inline int valid_ipaddr4(const char *buf)
1215{
1216 int rc, count, in[4];
1217
1218 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
1219 if (rc != 4)
1220 return -EINVAL;
1221 for (count = 0; count < 4; count++) {
1222 if (in[count] > 255)
1223 return -EINVAL;
1224 }
1225 return 0;
1226}
1227
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001228static int
1229p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001230{
1231 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001232 struct socket *csocket;
1233 struct sockaddr_in sin_server;
1234 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001235 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001236
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001237 err = parse_opts(args, &opts);
1238 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001239 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001240
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001241 if (valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001242 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001243
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001244 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001245
1246 sin_server.sin_family = AF_INET;
1247 sin_server.sin_addr.s_addr = in_aton(addr);
1248 sin_server.sin_port = htons(opts.port);
1249 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1250
1251 if (!csocket) {
1252 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1253 err = -EIO;
1254 goto error;
1255 }
1256
1257 err = csocket->ops->connect(csocket,
1258 (struct sockaddr *)&sin_server,
1259 sizeof(struct sockaddr_in), 0);
1260 if (err < 0) {
1261 P9_EPRINTK(KERN_ERR,
1262 "p9_trans_tcp: problem connecting socket to %s\n",
1263 addr);
1264 goto error;
1265 }
1266
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001267 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001268 if (err < 0)
1269 goto error;
1270
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001271 p = (struct p9_trans_fd *) client->trans;
1272 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001273 if (IS_ERR(p->conn)) {
1274 err = PTR_ERR(p->conn);
1275 p->conn = NULL;
1276 goto error;
1277 }
1278
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001279 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001280
1281error:
1282 if (csocket)
1283 sock_release(csocket);
1284
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001285 kfree(p);
1286
1287 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001288}
1289
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001290static int
1291p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001292{
1293 int err;
1294 struct socket *csocket;
1295 struct sockaddr_un sun_server;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001296 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001297
1298 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001299
1300 if (strlen(addr) > UNIX_PATH_MAX) {
1301 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1302 addr);
1303 err = -ENAMETOOLONG;
1304 goto error;
1305 }
1306
1307 sun_server.sun_family = PF_UNIX;
1308 strcpy(sun_server.sun_path, addr);
1309 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1310 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1311 sizeof(struct sockaddr_un) - 1, 0);
1312 if (err < 0) {
1313 P9_EPRINTK(KERN_ERR,
1314 "p9_trans_unix: problem connecting socket: %s: %d\n",
1315 addr, err);
1316 goto error;
1317 }
1318
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001319 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001320 if (err < 0)
1321 goto error;
1322
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001323 p = (struct p9_trans_fd *) client->trans;
1324 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001325 if (IS_ERR(p->conn)) {
1326 err = PTR_ERR(p->conn);
1327 p->conn = NULL;
1328 goto error;
1329 }
1330
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001331 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001332
1333error:
1334 if (csocket)
1335 sock_release(csocket);
1336
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001337 kfree(p);
1338 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001339}
1340
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001341static int
1342p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001343{
1344 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001345 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001346 struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001347
1348 parse_opts(args, &opts);
1349
1350 if (opts.rfd == ~0 || opts.wfd == ~0) {
1351 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001352 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001353 }
1354
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001355 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001356 if (err < 0)
1357 goto error;
1358
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001359 p = (struct p9_trans_fd *) client->trans;
1360 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001361 if (IS_ERR(p->conn)) {
1362 err = PTR_ERR(p->conn);
1363 p->conn = NULL;
1364 goto error;
1365 }
1366
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001367 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001368
1369error:
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001370 kfree(p);
1371 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001372}
1373
1374static struct p9_trans_module p9_tcp_trans = {
1375 .name = "tcp",
1376 .maxsize = MAX_SOCK_BUF,
1377 .def = 1,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001378 .create = p9_fd_create_tcp,
1379 .close = p9_fd_close,
1380 .rpc = p9_fd_rpc,
Tejun Heo72029fe2008-09-24 16:22:23 -05001381 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001382};
1383
1384static struct p9_trans_module p9_unix_trans = {
1385 .name = "unix",
1386 .maxsize = MAX_SOCK_BUF,
1387 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001388 .create = p9_fd_create_unix,
1389 .close = p9_fd_close,
1390 .rpc = p9_fd_rpc,
Tejun Heo72029fe2008-09-24 16:22:23 -05001391 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001392};
1393
1394static struct p9_trans_module p9_fd_trans = {
1395 .name = "fd",
1396 .maxsize = MAX_SOCK_BUF,
1397 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001398 .create = p9_fd_create,
1399 .close = p9_fd_close,
1400 .rpc = p9_fd_rpc,
Tejun Heo72029fe2008-09-24 16:22:23 -05001401 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001402};
1403
Eric Van Hensbergen5503ac52008-10-13 18:45:24 -05001404/**
1405 * p9_poll_proc - poll worker thread
1406 * @a: thread state and arguments
1407 *
1408 * polls all v9fs transports for new events and queues the appropriate
1409 * work to the work queue
1410 *
1411 */
1412
1413static int p9_poll_proc(void *a)
1414{
1415 unsigned long flags;
1416
1417 P9_DPRINTK(P9_DEBUG_MUX, "start %p\n", current);
1418 repeat:
1419 spin_lock_irqsave(&p9_poll_lock, flags);
1420 while (!list_empty(&p9_poll_pending_list)) {
1421 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
1422 struct p9_conn,
1423 poll_pending_link);
1424 list_del_init(&conn->poll_pending_link);
1425 spin_unlock_irqrestore(&p9_poll_lock, flags);
1426
1427 p9_poll_mux(conn);
1428
1429 spin_lock_irqsave(&p9_poll_lock, flags);
1430 }
1431 spin_unlock_irqrestore(&p9_poll_lock, flags);
1432
1433 set_current_state(TASK_INTERRUPTIBLE);
1434 if (list_empty(&p9_poll_pending_list)) {
1435 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
1436 schedule();
1437 }
1438 __set_current_state(TASK_RUNNING);
1439
1440 if (!kthread_should_stop())
1441 goto repeat;
1442
1443 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
1444 return 0;
1445}
1446
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001447int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001448{
Tejun Heo206ca502008-09-24 16:22:23 -05001449 p9_mux_wq = create_workqueue("v9fs");
1450 if (!p9_mux_wq) {
1451 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
1452 return -ENOMEM;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001453 }
1454
Tejun Heo992b3f12008-10-13 18:45:25 -05001455 p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll");
1456 if (IS_ERR(p9_poll_task)) {
1457 destroy_workqueue(p9_mux_wq);
1458 printk(KERN_WARNING "v9fs: mux: creating poll task failed\n");
1459 return PTR_ERR(p9_poll_task);
1460 }
1461
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001462 v9fs_register_trans(&p9_tcp_trans);
1463 v9fs_register_trans(&p9_unix_trans);
1464 v9fs_register_trans(&p9_fd_trans);
1465
Andrew Morton3387b802008-03-28 14:15:57 -07001466 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001467}
Tejun Heo72029fe2008-09-24 16:22:23 -05001468
1469void p9_trans_fd_exit(void)
1470{
Tejun Heo992b3f12008-10-13 18:45:25 -05001471 kthread_stop(p9_poll_task);
Tejun Heo72029fe2008-09-24 16:22:23 -05001472 v9fs_unregister_trans(&p9_tcp_trans);
1473 v9fs_unregister_trans(&p9_unix_trans);
1474 v9fs_unregister_trans(&p9_fd_trans);
Tejun Heo206ca502008-09-24 16:22:23 -05001475
1476 destroy_workqueue(p9_mux_wq);
Tejun Heo72029fe2008-09-24 16:22:23 -05001477}