blob: cdf137af7adc87d580280fbbce9b3c2d64b9dc82 [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>
42#include <net/9p/transport.h>
43
44#define P9_PORT 564
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050045#define MAX_SOCK_BUF (64*1024)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060046#define ERREQFLUSH 1
47#define SCHED_TIMEOUT 10
48#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/**
66 * struct p9_trans_fd - transport state
67 * @rd: reference to file to read from
68 * @wr: reference of file to write to
69 * @conn: connection state reference
70 *
71 */
72
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050073struct p9_trans_fd {
74 struct file *rd;
75 struct file *wr;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060076 struct p9_conn *conn;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050077};
78
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050079/*
80 * Option Parsing (code inspired by NFS code)
81 * - a little lazy - parse all fd-transport options
82 */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050083
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050084enum {
85 /* Options that take integer arguments */
Latchesar Ionkov55762692007-11-06 08:02:53 -060086 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050087};
88
89static match_table_t tokens = {
90 {Opt_port, "port=%u"},
91 {Opt_rfdno, "rfdno=%u"},
92 {Opt_wfdno, "wfdno=%u"},
Latchesar Ionkov55762692007-11-06 08:02:53 -060093 {Opt_err, NULL},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050094};
95
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060096enum {
97 Rworksched = 1, /* read work scheduled or running */
98 Rpending = 2, /* can read */
99 Wworksched = 4, /* write work scheduled or running */
100 Wpending = 8, /* can write */
101};
102
103enum {
104 None,
105 Flushing,
106 Flushed,
107};
108
109struct p9_req;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600110typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600111
112/**
113 * struct p9_req - fd mux encoding of an rpc transaction
114 * @lock: protects req_list
115 * @tag: numeric tag for rpc transaction
116 * @tcall: request &p9_fcall structure
117 * @rcall: response &p9_fcall structure
118 * @err: error state
119 * @cb: callback for when response is received
120 * @cba: argument to pass to callback
121 * @flush: flag to indicate RPC has been flushed
122 * @req_list: list link for higher level objects to chain requests
123 *
124 */
125
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600126struct p9_req {
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600127 spinlock_t lock;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600128 int tag;
129 struct p9_fcall *tcall;
130 struct p9_fcall *rcall;
131 int err;
132 p9_conn_req_callback cb;
133 void *cba;
134 int flush;
135 struct list_head req_list;
136};
137
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600138struct p9_mux_poll_task {
139 struct task_struct *task;
140 struct list_head mux_list;
141 int muxnum;
142};
143
144/**
145 * struct p9_conn - fd mux connection state information
146 * @lock: protects mux_list (?)
147 * @mux_list: list link for mux to manage multiple connections (?)
148 * @poll_task: task polling on this connection
149 * @msize: maximum size for connection (dup)
150 * @extended: 9p2000.u flag (dup)
151 * @trans: reference to transport instance for this connection
152 * @tagpool: id accounting for transactions
153 * @err: error state
154 * @equeue: event wait_q (?)
155 * @req_list: accounting for requests which have been sent
156 * @unsent_req_list: accounting for requests that haven't been sent
157 * @rcall: current response &p9_fcall structure
158 * @rpos: read position in current frame
159 * @rbuf: current read buffer
160 * @wpos: write position for current frame
161 * @wsize: amount of data to write for current frame
162 * @wbuf: current write buffer
163 * @poll_wait: array of wait_q's for various worker threads
164 * @poll_waddr: ????
165 * @pt: poll state
166 * @rq: current read work
167 * @wq: current write work
168 * @wsched: ????
169 *
170 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600171
172struct p9_conn {
173 spinlock_t lock; /* protect lock structure */
174 struct list_head mux_list;
175 struct p9_mux_poll_task *poll_task;
176 int msize;
177 unsigned char extended;
178 struct p9_trans *trans;
179 struct p9_idpool *tagpool;
180 int err;
181 wait_queue_head_t equeue;
182 struct list_head req_list;
183 struct list_head unsent_req_list;
184 struct p9_fcall *rcall;
185 int rpos;
186 char *rbuf;
187 int wpos;
188 int wsize;
189 char *wbuf;
190 wait_queue_t poll_wait[MAXPOLLWADDR];
191 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
192 poll_table pt;
193 struct work_struct rq;
194 struct work_struct wq;
195 unsigned long wsched;
196};
197
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600198/**
199 * struct p9_mux_rpc - fd mux rpc accounting structure
200 * @m: connection this request was issued on
201 * @err: error state
202 * @tcall: request &p9_fcall
203 * @rcall: response &p9_fcall
204 * @wqueue: wait queue that client is blocked on for this rpc
205 *
206 * Bug: isn't this information duplicated elsewhere like &p9_req
207 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600208
209struct p9_mux_rpc {
210 struct p9_conn *m;
211 int err;
212 struct p9_fcall *tcall;
213 struct p9_fcall *rcall;
214 wait_queue_head_t wqueue;
215};
216
217static int p9_poll_proc(void *);
218static void p9_read_work(struct work_struct *work);
219static void p9_write_work(struct work_struct *work);
220static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
221 poll_table *p);
222static int p9_fd_write(struct p9_trans *trans, void *v, int len);
223static int p9_fd_read(struct p9_trans *trans, void *v, int len);
224
225static DEFINE_MUTEX(p9_mux_task_lock);
226static struct workqueue_struct *p9_mux_wq;
227
228static int p9_mux_num;
229static int p9_mux_poll_task_num;
230static struct p9_mux_poll_task p9_mux_poll_tasks[100];
231
232static void p9_conn_destroy(struct p9_conn *);
233static unsigned int p9_fd_poll(struct p9_trans *trans,
234 struct poll_table_struct *pt);
235
236#ifdef P9_NONBLOCK
237static int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
238 p9_conn_req_callback cb, void *a);
239#endif /* P9_NONBLOCK */
240
241static void p9_conn_cancel(struct p9_conn *m, int err);
242
243static int p9_mux_global_init(void)
244{
245 int i;
246
247 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++)
248 p9_mux_poll_tasks[i].task = NULL;
249
250 p9_mux_wq = create_workqueue("v9fs");
251 if (!p9_mux_wq) {
252 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
253 return -ENOMEM;
254 }
255
256 return 0;
257}
258
259static u16 p9_mux_get_tag(struct p9_conn *m)
260{
261 int tag;
262
263 tag = p9_idpool_get(m->tagpool);
264 if (tag < 0)
265 return P9_NOTAG;
266 else
267 return (u16) tag;
268}
269
270static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
271{
272 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
273 p9_idpool_put(tag, m->tagpool);
274}
275
276/**
277 * p9_mux_calc_poll_procs - calculates the number of polling procs
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600278 * @muxnum: number of mounts
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600279 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600280 * Calculation is based on the number of mounted v9fs filesystems.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600281 * The current implementation returns sqrt of the number of mounts.
282 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600283
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600284static int p9_mux_calc_poll_procs(int muxnum)
285{
286 int n;
287
288 if (p9_mux_poll_task_num)
289 n = muxnum / p9_mux_poll_task_num +
290 (muxnum % p9_mux_poll_task_num ? 1 : 0);
291 else
292 n = 1;
293
294 if (n > ARRAY_SIZE(p9_mux_poll_tasks))
295 n = ARRAY_SIZE(p9_mux_poll_tasks);
296
297 return n;
298}
299
300static int p9_mux_poll_start(struct p9_conn *m)
301{
302 int i, n;
303 struct p9_mux_poll_task *vpt, *vptlast;
304 struct task_struct *pproc;
305
306 P9_DPRINTK(P9_DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, p9_mux_num,
307 p9_mux_poll_task_num);
308 mutex_lock(&p9_mux_task_lock);
309
310 n = p9_mux_calc_poll_procs(p9_mux_num + 1);
311 if (n > p9_mux_poll_task_num) {
312 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
313 if (p9_mux_poll_tasks[i].task == NULL) {
314 vpt = &p9_mux_poll_tasks[i];
315 P9_DPRINTK(P9_DEBUG_MUX, "create proc %p\n",
316 vpt);
317 pproc = kthread_create(p9_poll_proc, vpt,
318 "v9fs-poll");
319
320 if (!IS_ERR(pproc)) {
321 vpt->task = pproc;
322 INIT_LIST_HEAD(&vpt->mux_list);
323 vpt->muxnum = 0;
324 p9_mux_poll_task_num++;
325 wake_up_process(vpt->task);
326 }
327 break;
328 }
329 }
330
331 if (i >= ARRAY_SIZE(p9_mux_poll_tasks))
332 P9_DPRINTK(P9_DEBUG_ERROR,
333 "warning: no free poll slots\n");
334 }
335
336 n = (p9_mux_num + 1) / p9_mux_poll_task_num +
337 ((p9_mux_num + 1) % p9_mux_poll_task_num ? 1 : 0);
338
339 vptlast = NULL;
340 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
341 vpt = &p9_mux_poll_tasks[i];
342 if (vpt->task != NULL) {
343 vptlast = vpt;
344 if (vpt->muxnum < n) {
345 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
346 list_add(&m->mux_list, &vpt->mux_list);
347 vpt->muxnum++;
348 m->poll_task = vpt;
349 memset(&m->poll_waddr, 0,
350 sizeof(m->poll_waddr));
351 init_poll_funcptr(&m->pt, p9_pollwait);
352 break;
353 }
354 }
355 }
356
357 if (i >= ARRAY_SIZE(p9_mux_poll_tasks)) {
358 if (vptlast == NULL) {
359 mutex_unlock(&p9_mux_task_lock);
360 return -ENOMEM;
361 }
362
363 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
364 list_add(&m->mux_list, &vptlast->mux_list);
365 vptlast->muxnum++;
366 m->poll_task = vptlast;
367 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
368 init_poll_funcptr(&m->pt, p9_pollwait);
369 }
370
371 p9_mux_num++;
372 mutex_unlock(&p9_mux_task_lock);
373
374 return 0;
375}
376
377static void p9_mux_poll_stop(struct p9_conn *m)
378{
379 int i;
380 struct p9_mux_poll_task *vpt;
381
382 mutex_lock(&p9_mux_task_lock);
383 vpt = m->poll_task;
384 list_del(&m->mux_list);
385 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
386 if (m->poll_waddr[i] != NULL) {
387 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
388 m->poll_waddr[i] = NULL;
389 }
390 }
391 vpt->muxnum--;
392 if (!vpt->muxnum) {
393 P9_DPRINTK(P9_DEBUG_MUX, "destroy proc %p\n", vpt);
394 kthread_stop(vpt->task);
395 vpt->task = NULL;
396 p9_mux_poll_task_num--;
397 }
398 p9_mux_num--;
399 mutex_unlock(&p9_mux_task_lock);
400}
401
402/**
403 * p9_conn_create - allocate and initialize the per-session mux data
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600404 * @trans: transport structure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600405 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600406 * Note: Creates the polling task if this is the first session.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600407 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600408
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600409static struct p9_conn *p9_conn_create(struct p9_trans *trans)
410{
411 int i, n;
412 struct p9_conn *m, *mtmp;
413
414 P9_DPRINTK(P9_DEBUG_MUX, "transport %p msize %d\n", trans,
415 trans->msize);
416 m = kmalloc(sizeof(struct p9_conn), GFP_KERNEL);
417 if (!m)
418 return ERR_PTR(-ENOMEM);
419
420 spin_lock_init(&m->lock);
421 INIT_LIST_HEAD(&m->mux_list);
422 m->msize = trans->msize;
423 m->extended = trans->extended;
424 m->trans = trans;
425 m->tagpool = p9_idpool_create();
426 if (IS_ERR(m->tagpool)) {
427 mtmp = ERR_PTR(-ENOMEM);
428 kfree(m);
429 return mtmp;
430 }
431
432 m->err = 0;
433 init_waitqueue_head(&m->equeue);
434 INIT_LIST_HEAD(&m->req_list);
435 INIT_LIST_HEAD(&m->unsent_req_list);
436 m->rcall = NULL;
437 m->rpos = 0;
438 m->rbuf = NULL;
439 m->wpos = m->wsize = 0;
440 m->wbuf = NULL;
441 INIT_WORK(&m->rq, p9_read_work);
442 INIT_WORK(&m->wq, p9_write_work);
443 m->wsched = 0;
444 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
445 m->poll_task = NULL;
446 n = p9_mux_poll_start(m);
447 if (n) {
448 kfree(m);
449 return ERR_PTR(n);
450 }
451
452 n = p9_fd_poll(trans, &m->pt);
453 if (n & POLLIN) {
454 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
455 set_bit(Rpending, &m->wsched);
456 }
457
458 if (n & POLLOUT) {
459 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
460 set_bit(Wpending, &m->wsched);
461 }
462
463 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
464 if (IS_ERR(m->poll_waddr[i])) {
465 p9_mux_poll_stop(m);
466 mtmp = (void *)m->poll_waddr; /* the error code */
467 kfree(m);
468 m = mtmp;
469 break;
470 }
471 }
472
473 return m;
474}
475
476/**
477 * p9_mux_destroy - cancels all pending requests and frees mux resources
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600478 * @m: mux to destroy
479 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600480 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600481
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600482static void p9_conn_destroy(struct p9_conn *m)
483{
484 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
485 m->mux_list.prev, m->mux_list.next);
486 p9_conn_cancel(m, -ECONNRESET);
487
488 if (!list_empty(&m->req_list)) {
489 /* wait until all processes waiting on this session exit */
490 P9_DPRINTK(P9_DEBUG_MUX,
491 "mux %p waiting for empty request queue\n", m);
492 wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000);
493 P9_DPRINTK(P9_DEBUG_MUX, "mux %p request queue empty: %d\n", m,
494 list_empty(&m->req_list));
495 }
496
497 p9_mux_poll_stop(m);
498 m->trans = NULL;
499 p9_idpool_destroy(m->tagpool);
500 kfree(m);
501}
502
503/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600504 * p9_pollwait - add poll task to the wait queue
505 * @filp: file pointer being polled
506 * @wait_address: wait_q to block on
507 * @p: poll state
508 *
509 * called by files poll operation to add v9fs-poll task to files wait queue
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600510 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600511
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600512static void
513p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
514{
515 int i;
516 struct p9_conn *m;
517
518 m = container_of(p, struct p9_conn, pt);
519 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
520 if (m->poll_waddr[i] == NULL)
521 break;
522
523 if (i >= ARRAY_SIZE(m->poll_waddr)) {
524 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
525 return;
526 }
527
528 m->poll_waddr[i] = wait_address;
529
530 if (!wait_address) {
531 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
532 m->poll_waddr[i] = ERR_PTR(-EIO);
533 return;
534 }
535
536 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
537 add_wait_queue(wait_address, &m->poll_wait[i]);
538}
539
540/**
541 * p9_poll_mux - polls a mux and schedules read or write works if necessary
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600542 * @m: connection to poll
543 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600544 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600545
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600546static void p9_poll_mux(struct p9_conn *m)
547{
548 int n;
549
550 if (m->err < 0)
551 return;
552
553 n = p9_fd_poll(m->trans, NULL);
554 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
555 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
556 if (n >= 0)
557 n = -ECONNRESET;
558 p9_conn_cancel(m, n);
559 }
560
561 if (n & POLLIN) {
562 set_bit(Rpending, &m->wsched);
563 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
564 if (!test_and_set_bit(Rworksched, &m->wsched)) {
565 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
566 queue_work(p9_mux_wq, &m->rq);
567 }
568 }
569
570 if (n & POLLOUT) {
571 set_bit(Wpending, &m->wsched);
572 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
573 if ((m->wsize || !list_empty(&m->unsent_req_list))
574 && !test_and_set_bit(Wworksched, &m->wsched)) {
575 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
576 queue_work(p9_mux_wq, &m->wq);
577 }
578 }
579}
580
581/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600582 * p9_poll_proc - poll worker thread
583 * @a: thread state and arguments
584 *
585 * polls all v9fs transports for new events and queues the appropriate
586 * work to the work queue
587 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600588 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600589
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600590static int p9_poll_proc(void *a)
591{
592 struct p9_conn *m, *mtmp;
593 struct p9_mux_poll_task *vpt;
594
595 vpt = a;
596 P9_DPRINTK(P9_DEBUG_MUX, "start %p %p\n", current, vpt);
597 while (!kthread_should_stop()) {
598 set_current_state(TASK_INTERRUPTIBLE);
599
600 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
601 p9_poll_mux(m);
602 }
603
604 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
605 schedule_timeout(SCHED_TIMEOUT * HZ);
606 }
607
608 __set_current_state(TASK_RUNNING);
609 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
610 return 0;
611}
612
613/**
614 * p9_write_work - called when a transport can send some data
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600615 * @work: container for work to be done
616 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600617 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600618
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600619static void p9_write_work(struct work_struct *work)
620{
621 int n, err;
622 struct p9_conn *m;
623 struct p9_req *req;
624
625 m = container_of(work, struct p9_conn, wq);
626
627 if (m->err < 0) {
628 clear_bit(Wworksched, &m->wsched);
629 return;
630 }
631
632 if (!m->wsize) {
633 if (list_empty(&m->unsent_req_list)) {
634 clear_bit(Wworksched, &m->wsched);
635 return;
636 }
637
638 spin_lock(&m->lock);
639again:
640 req = list_entry(m->unsent_req_list.next, struct p9_req,
641 req_list);
642 list_move_tail(&req->req_list, &m->req_list);
643 if (req->err == ERREQFLUSH)
644 goto again;
645
646 m->wbuf = req->tcall->sdata;
647 m->wsize = req->tcall->size;
648 m->wpos = 0;
649 spin_unlock(&m->lock);
650 }
651
652 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
653 m->wsize);
654 clear_bit(Wpending, &m->wsched);
655 err = p9_fd_write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
656 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
657 if (err == -EAGAIN) {
658 clear_bit(Wworksched, &m->wsched);
659 return;
660 }
661
662 if (err < 0)
663 goto error;
664 else if (err == 0) {
665 err = -EREMOTEIO;
666 goto error;
667 }
668
669 m->wpos += err;
670 if (m->wpos == m->wsize)
671 m->wpos = m->wsize = 0;
672
673 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
674 if (test_and_clear_bit(Wpending, &m->wsched))
675 n = POLLOUT;
676 else
677 n = p9_fd_poll(m->trans, NULL);
678
679 if (n & POLLOUT) {
680 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
681 queue_work(p9_mux_wq, &m->wq);
682 } else
683 clear_bit(Wworksched, &m->wsched);
684 } else
685 clear_bit(Wworksched, &m->wsched);
686
687 return;
688
689error:
690 p9_conn_cancel(m, err);
691 clear_bit(Wworksched, &m->wsched);
692}
693
694static void process_request(struct p9_conn *m, struct p9_req *req)
695{
696 int ecode;
697 struct p9_str *ename;
698
699 if (!req->err && req->rcall->id == P9_RERROR) {
700 ecode = req->rcall->params.rerror.errno;
701 ename = &req->rcall->params.rerror.error;
702
703 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
704 ename->str);
705
706 if (m->extended)
707 req->err = -ecode;
708
709 if (!req->err) {
710 req->err = p9_errstr2errno(ename->str, ename->len);
711
712 /* string match failed */
713 if (!req->err) {
714 PRINT_FCALL_ERROR("unknown error", req->rcall);
715 req->err = -ESERVERFAULT;
716 }
717 }
718 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
719 P9_DPRINTK(P9_DEBUG_ERROR,
720 "fcall mismatch: expected %d, got %d\n",
721 req->tcall->id + 1, req->rcall->id);
722 if (!req->err)
723 req->err = -EIO;
724 }
725}
726
727/**
728 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600729 * @work: container of work to be done
730 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600731 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600732
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600733static void p9_read_work(struct work_struct *work)
734{
735 int n, err;
736 struct p9_conn *m;
737 struct p9_req *req, *rptr, *rreq;
738 struct p9_fcall *rcall;
739 char *rbuf;
740
741 m = container_of(work, struct p9_conn, rq);
742
743 if (m->err < 0)
744 return;
745
746 rcall = NULL;
747 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
748
749 if (!m->rcall) {
750 m->rcall =
751 kmalloc(sizeof(struct p9_fcall) + m->msize, GFP_KERNEL);
752 if (!m->rcall) {
753 err = -ENOMEM;
754 goto error;
755 }
756
757 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
758 m->rpos = 0;
759 }
760
761 clear_bit(Rpending, &m->wsched);
762 err = p9_fd_read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
763 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
764 if (err == -EAGAIN) {
765 clear_bit(Rworksched, &m->wsched);
766 return;
767 }
768
769 if (err <= 0)
770 goto error;
771
772 m->rpos += err;
773 while (m->rpos > 4) {
774 n = le32_to_cpu(*(__le32 *) m->rbuf);
775 if (n >= m->msize) {
776 P9_DPRINTK(P9_DEBUG_ERROR,
777 "requested packet size too big: %d\n", n);
778 err = -EIO;
779 goto error;
780 }
781
782 if (m->rpos < n)
783 break;
784
785 err =
786 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->extended);
787 if (err < 0)
788 goto error;
789
790#ifdef CONFIG_NET_9P_DEBUG
791 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
792 char buf[150];
793
794 p9_printfcall(buf, sizeof(buf), m->rcall,
795 m->extended);
796 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
797 }
798#endif
799
800 rcall = m->rcall;
801 rbuf = m->rbuf;
802 if (m->rpos > n) {
803 m->rcall = kmalloc(sizeof(struct p9_fcall) + m->msize,
804 GFP_KERNEL);
805 if (!m->rcall) {
806 err = -ENOMEM;
807 goto error;
808 }
809
810 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
811 memmove(m->rbuf, rbuf + n, m->rpos - n);
812 m->rpos -= n;
813 } else {
814 m->rcall = NULL;
815 m->rbuf = NULL;
816 m->rpos = 0;
817 }
818
819 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
820 rcall->id, rcall->tag);
821
822 req = NULL;
823 spin_lock(&m->lock);
824 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
825 if (rreq->tag == rcall->tag) {
826 req = rreq;
827 if (req->flush != Flushing)
828 list_del(&req->req_list);
829 break;
830 }
831 }
832 spin_unlock(&m->lock);
833
834 if (req) {
835 req->rcall = rcall;
836 process_request(m, req);
837
838 if (req->flush != Flushing) {
839 if (req->cb)
840 (*req->cb) (req, req->cba);
841 else
842 kfree(req->rcall);
843
844 wake_up(&m->equeue);
845 }
846 } else {
847 if (err >= 0 && rcall->id != P9_RFLUSH)
848 P9_DPRINTK(P9_DEBUG_ERROR,
849 "unexpected response mux %p id %d tag %d\n",
850 m, rcall->id, rcall->tag);
851 kfree(rcall);
852 }
853 }
854
855 if (!list_empty(&m->req_list)) {
856 if (test_and_clear_bit(Rpending, &m->wsched))
857 n = POLLIN;
858 else
859 n = p9_fd_poll(m->trans, NULL);
860
861 if (n & POLLIN) {
862 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
863 queue_work(p9_mux_wq, &m->rq);
864 } else
865 clear_bit(Rworksched, &m->wsched);
866 } else
867 clear_bit(Rworksched, &m->wsched);
868
869 return;
870
871error:
872 p9_conn_cancel(m, err);
873 clear_bit(Rworksched, &m->wsched);
874}
875
876/**
877 * p9_send_request - send 9P request
878 * The function can sleep until the request is scheduled for sending.
879 * The function can be interrupted. Return from the function is not
880 * a guarantee that the request is sent successfully. Can return errors
881 * that can be retrieved by PTR_ERR macros.
882 *
883 * @m: mux data
884 * @tc: request to be sent
885 * @cb: callback function to call when response is received
886 * @cba: parameter to pass to the callback function
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600887 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600888 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600889
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600890static struct p9_req *p9_send_request(struct p9_conn *m,
891 struct p9_fcall *tc,
892 p9_conn_req_callback cb, void *cba)
893{
894 int n;
895 struct p9_req *req;
896
897 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
898 tc, tc->id);
899 if (m->err < 0)
900 return ERR_PTR(m->err);
901
902 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
903 if (!req)
904 return ERR_PTR(-ENOMEM);
905
906 if (tc->id == P9_TVERSION)
907 n = P9_NOTAG;
908 else
909 n = p9_mux_get_tag(m);
910
911 if (n < 0)
912 return ERR_PTR(-ENOMEM);
913
914 p9_set_tag(tc, n);
915
916#ifdef CONFIG_NET_9P_DEBUG
917 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
918 char buf[150];
919
920 p9_printfcall(buf, sizeof(buf), tc, m->extended);
921 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
922 }
923#endif
924
925 spin_lock_init(&req->lock);
926 req->tag = n;
927 req->tcall = tc;
928 req->rcall = NULL;
929 req->err = 0;
930 req->cb = cb;
931 req->cba = cba;
932 req->flush = None;
933
934 spin_lock(&m->lock);
935 list_add_tail(&req->req_list, &m->unsent_req_list);
936 spin_unlock(&m->lock);
937
938 if (test_and_clear_bit(Wpending, &m->wsched))
939 n = POLLOUT;
940 else
941 n = p9_fd_poll(m->trans, NULL);
942
943 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
944 queue_work(p9_mux_wq, &m->wq);
945
946 return req;
947}
948
949static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
950{
951 p9_mux_put_tag(m, req->tag);
952 kfree(req);
953}
954
955static void p9_mux_flush_cb(struct p9_req *freq, void *a)
956{
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600957 int tag;
958 struct p9_conn *m;
959 struct p9_req *req, *rreq, *rptr;
960
961 m = a;
962 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
963 freq->tcall, freq->rcall, freq->err,
964 freq->tcall->params.tflush.oldtag);
965
966 spin_lock(&m->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600967 tag = freq->tcall->params.tflush.oldtag;
968 req = NULL;
969 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
970 if (rreq->tag == tag) {
971 req = rreq;
972 list_del(&req->req_list);
973 break;
974 }
975 }
976 spin_unlock(&m->lock);
977
978 if (req) {
979 spin_lock(&req->lock);
980 req->flush = Flushed;
981 spin_unlock(&req->lock);
982
983 if (req->cb)
984 (*req->cb) (req, req->cba);
985 else
986 kfree(req->rcall);
987
988 wake_up(&m->equeue);
989 }
990
991 kfree(freq->tcall);
992 kfree(freq->rcall);
993 p9_mux_free_request(m, freq);
994}
995
996static int
997p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
998{
999 struct p9_fcall *fc;
1000 struct p9_req *rreq, *rptr;
1001
1002 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
1003
1004 /* if a response was received for a request, do nothing */
1005 spin_lock(&req->lock);
1006 if (req->rcall || req->err) {
1007 spin_unlock(&req->lock);
1008 P9_DPRINTK(P9_DEBUG_MUX,
1009 "mux %p req %p response already received\n", m, req);
1010 return 0;
1011 }
1012
1013 req->flush = Flushing;
1014 spin_unlock(&req->lock);
1015
1016 spin_lock(&m->lock);
1017 /* if the request is not sent yet, just remove it from the list */
1018 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
1019 if (rreq->tag == req->tag) {
1020 P9_DPRINTK(P9_DEBUG_MUX,
1021 "mux %p req %p request is not sent yet\n", m, req);
1022 list_del(&rreq->req_list);
1023 req->flush = Flushed;
1024 spin_unlock(&m->lock);
1025 if (req->cb)
1026 (*req->cb) (req, req->cba);
1027 return 0;
1028 }
1029 }
1030 spin_unlock(&m->lock);
1031
1032 clear_thread_flag(TIF_SIGPENDING);
1033 fc = p9_create_tflush(req->tag);
1034 p9_send_request(m, fc, p9_mux_flush_cb, m);
1035 return 1;
1036}
1037
1038static void
1039p9_conn_rpc_cb(struct p9_req *req, void *a)
1040{
1041 struct p9_mux_rpc *r;
1042
1043 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
1044 r = a;
1045 r->rcall = req->rcall;
1046 r->err = req->err;
1047
1048 if (req->flush != None && !req->err)
1049 r->err = -ERESTARTSYS;
1050
1051 wake_up(&r->wqueue);
1052}
1053
1054/**
1055 * p9_fd_rpc- sends 9P request and waits until a response is available.
1056 * The function can be interrupted.
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001057 * @t: transport data
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001058 * @tc: request to be sent
1059 * @rc: pointer where a pointer to the response is stored
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001060 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001061 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001062
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001063int
1064p9_fd_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
1065{
1066 struct p9_trans_fd *p = t->priv;
1067 struct p9_conn *m = p->conn;
1068 int err, sigpending;
1069 unsigned long flags;
1070 struct p9_req *req;
1071 struct p9_mux_rpc r;
1072
1073 r.err = 0;
1074 r.tcall = tc;
1075 r.rcall = NULL;
1076 r.m = m;
1077 init_waitqueue_head(&r.wqueue);
1078
1079 if (rc)
1080 *rc = NULL;
1081
1082 sigpending = 0;
1083 if (signal_pending(current)) {
1084 sigpending = 1;
1085 clear_thread_flag(TIF_SIGPENDING);
1086 }
1087
1088 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
1089 if (IS_ERR(req)) {
1090 err = PTR_ERR(req);
1091 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1092 return err;
1093 }
1094
1095 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
1096 if (r.err < 0)
1097 err = r.err;
1098
1099 if (err == -ERESTARTSYS && m->trans->status == Connected
1100 && m->err == 0) {
1101 if (p9_mux_flush_request(m, req)) {
1102 /* wait until we get response of the flush message */
1103 do {
1104 clear_thread_flag(TIF_SIGPENDING);
1105 err = wait_event_interruptible(r.wqueue,
1106 r.rcall || r.err);
1107 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
1108 m->trans->status == Connected && !m->err);
1109
1110 err = -ERESTARTSYS;
1111 }
1112 sigpending = 1;
1113 }
1114
1115 if (sigpending) {
1116 spin_lock_irqsave(&current->sighand->siglock, flags);
1117 recalc_sigpending();
1118 spin_unlock_irqrestore(&current->sighand->siglock, flags);
1119 }
1120
1121 if (rc)
1122 *rc = r.rcall;
1123 else
1124 kfree(r.rcall);
1125
1126 p9_mux_free_request(m, req);
1127 if (err > 0)
1128 err = -EIO;
1129
1130 return err;
1131}
1132
1133#ifdef P9_NONBLOCK
1134/**
1135 * p9_conn_rpcnb - sends 9P request without waiting for response.
1136 * @m: mux data
1137 * @tc: request to be sent
1138 * @cb: callback function to be called when response arrives
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001139 * @a: value to pass to the callback function
1140 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001141 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001142
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001143int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1144 p9_conn_req_callback cb, void *a)
1145{
1146 int err;
1147 struct p9_req *req;
1148
1149 req = p9_send_request(m, tc, cb, a);
1150 if (IS_ERR(req)) {
1151 err = PTR_ERR(req);
1152 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1153 return PTR_ERR(req);
1154 }
1155
1156 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1157 return 0;
1158}
1159#endif /* P9_NONBLOCK */
1160
1161/**
1162 * p9_conn_cancel - cancel all pending requests with error
1163 * @m: mux data
1164 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001165 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001166 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001167
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001168void p9_conn_cancel(struct p9_conn *m, int err)
1169{
1170 struct p9_req *req, *rtmp;
1171 LIST_HEAD(cancel_list);
1172
1173 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1174 m->err = err;
1175 spin_lock(&m->lock);
1176 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1177 list_move(&req->req_list, &cancel_list);
1178 }
1179 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1180 list_move(&req->req_list, &cancel_list);
1181 }
1182 spin_unlock(&m->lock);
1183
1184 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1185 list_del(&req->req_list);
1186 if (!req->err)
1187 req->err = err;
1188
1189 if (req->cb)
1190 (*req->cb) (req, req->cba);
1191 else
1192 kfree(req->rcall);
1193 }
1194
1195 wake_up(&m->equeue);
1196}
1197
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001198/**
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001199 * parse_options - parse mount options into session structure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001200 * @options: options string passed from mount
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001201 * @opts: transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001202 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001203 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001204 */
1205
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001206static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001207{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001208 char *p;
1209 substring_t args[MAX_OPT_ARGS];
1210 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001211 char *options;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001212 int ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001213
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001214 opts->port = P9_PORT;
1215 opts->rfd = ~0;
1216 opts->wfd = ~0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001217
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001218 if (!params)
1219 return 0;
1220
1221 options = kstrdup(params, GFP_KERNEL);
1222 if (!options) {
1223 P9_DPRINTK(P9_DEBUG_ERROR,
1224 "failed to allocate copy of option string\n");
1225 return -ENOMEM;
1226 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001227
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001228 while ((p = strsep(&options, ",")) != NULL) {
1229 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001230 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001231 if (!*p)
1232 continue;
1233 token = match_token(p, tokens, args);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001234 r = match_int(&args[0], &option);
1235 if (r < 0) {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001236 P9_DPRINTK(P9_DEBUG_ERROR,
1237 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001238 ret = r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001239 continue;
1240 }
1241 switch (token) {
1242 case Opt_port:
1243 opts->port = option;
1244 break;
1245 case Opt_rfdno:
1246 opts->rfd = option;
1247 break;
1248 case Opt_wfdno:
1249 opts->wfd = option;
1250 break;
1251 default:
1252 continue;
1253 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001254 }
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001255 kfree(options);
1256 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001257}
1258
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001259static int p9_fd_open(struct p9_trans *trans, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001260{
1261 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1262 GFP_KERNEL);
1263 if (!ts)
1264 return -ENOMEM;
1265
1266 ts->rd = fget(rfd);
1267 ts->wr = fget(wfd);
1268 if (!ts->rd || !ts->wr) {
1269 if (ts->rd)
1270 fput(ts->rd);
1271 if (ts->wr)
1272 fput(ts->wr);
1273 kfree(ts);
1274 return -EIO;
1275 }
1276
1277 trans->priv = ts;
1278 trans->status = Connected;
1279
1280 return 0;
1281}
1282
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001283static int p9_socket_open(struct p9_trans *trans, struct socket *csocket)
1284{
1285 int fd, ret;
1286
1287 csocket->sk->sk_allocation = GFP_NOIO;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001288 fd = sock_map_fd(csocket, 0);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001289 if (fd < 0) {
1290 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1291 return fd;
1292 }
1293
1294 ret = p9_fd_open(trans, fd, fd);
1295 if (ret < 0) {
1296 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1297 sockfd_put(csocket);
1298 return ret;
1299 }
1300
1301 ((struct p9_trans_fd *)trans->priv)->rd->f_flags |= O_NONBLOCK;
1302
1303 return 0;
1304}
1305
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001306/**
1307 * p9_fd_read- read from a fd
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001308 * @trans: transport instance state
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001309 * @v: buffer to receive data into
1310 * @len: size of receive buffer
1311 *
1312 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001313
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001314static int p9_fd_read(struct p9_trans *trans, void *v, int len)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001315{
1316 int ret;
1317 struct p9_trans_fd *ts = NULL;
1318
1319 if (trans && trans->status != Disconnected)
1320 ts = trans->priv;
1321
1322 if (!ts)
1323 return -EREMOTEIO;
1324
1325 if (!(ts->rd->f_flags & O_NONBLOCK))
1326 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
1327
1328 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
1329 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1330 trans->status = Disconnected;
1331 return ret;
1332}
1333
1334/**
1335 * p9_fd_write - write to a socket
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001336 * @trans: transport instance state
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001337 * @v: buffer to send data from
1338 * @len: size of send buffer
1339 *
1340 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001341
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001342static int p9_fd_write(struct p9_trans *trans, void *v, int len)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001343{
1344 int ret;
1345 mm_segment_t oldfs;
1346 struct p9_trans_fd *ts = NULL;
1347
1348 if (trans && trans->status != Disconnected)
1349 ts = trans->priv;
1350
1351 if (!ts)
1352 return -EREMOTEIO;
1353
1354 if (!(ts->wr->f_flags & O_NONBLOCK))
1355 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
1356
1357 oldfs = get_fs();
1358 set_fs(get_ds());
1359 /* The cast to a user pointer is valid due to the set_fs() */
1360 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
1361 set_fs(oldfs);
1362
1363 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1364 trans->status = Disconnected;
1365 return ret;
1366}
1367
1368static unsigned int
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001369p9_fd_poll(struct p9_trans *trans, struct poll_table_struct *pt)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001370{
1371 int ret, n;
1372 struct p9_trans_fd *ts = NULL;
1373 mm_segment_t oldfs;
1374
1375 if (trans && trans->status == Connected)
1376 ts = trans->priv;
1377
1378 if (!ts)
1379 return -EREMOTEIO;
1380
1381 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1382 return -EIO;
1383
1384 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1385 return -EIO;
1386
1387 oldfs = get_fs();
1388 set_fs(get_ds());
1389
1390 ret = ts->rd->f_op->poll(ts->rd, pt);
1391 if (ret < 0)
1392 goto end;
1393
1394 if (ts->rd != ts->wr) {
1395 n = ts->wr->f_op->poll(ts->wr, pt);
1396 if (n < 0) {
1397 ret = n;
1398 goto end;
1399 }
1400 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1401 }
1402
1403end:
1404 set_fs(oldfs);
1405 return ret;
1406}
1407
1408/**
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001409 * p9_fd_close - shutdown socket
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001410 * @trans: private socket structure
1411 *
1412 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001413
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001414static void p9_fd_close(struct p9_trans *trans)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001415{
1416 struct p9_trans_fd *ts;
1417
1418 if (!trans)
1419 return;
1420
1421 ts = xchg(&trans->priv, NULL);
1422
1423 if (!ts)
1424 return;
1425
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001426 p9_conn_destroy(ts->conn);
1427
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001428 trans->status = Disconnected;
1429 if (ts->rd)
1430 fput(ts->rd);
1431 if (ts->wr)
1432 fput(ts->wr);
1433 kfree(ts);
1434}
1435
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001436/*
1437 * stolen from NFS - maybe should be made a generic function?
1438 */
1439static inline int valid_ipaddr4(const char *buf)
1440{
1441 int rc, count, in[4];
1442
1443 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
1444 if (rc != 4)
1445 return -EINVAL;
1446 for (count = 0; count < 4; count++) {
1447 if (in[count] > 255)
1448 return -EINVAL;
1449 }
1450 return 0;
1451}
1452
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001453static struct p9_trans *
1454p9_trans_create_tcp(const char *addr, char *args, int msize, unsigned char dotu)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001455{
1456 int err;
1457 struct p9_trans *trans;
1458 struct socket *csocket;
1459 struct sockaddr_in sin_server;
1460 struct p9_fd_opts opts;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001461 struct p9_trans_fd *p;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001462
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001463 err = parse_opts(args, &opts);
1464 if (err < 0)
1465 return ERR_PTR(err);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001466
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001467 if (valid_ipaddr4(addr) < 0)
1468 return ERR_PTR(-EINVAL);
1469
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001470 csocket = NULL;
1471 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1472 if (!trans)
1473 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001474 trans->msize = msize;
1475 trans->extended = dotu;
1476 trans->rpc = p9_fd_rpc;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001477 trans->close = p9_fd_close;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001478
1479 sin_server.sin_family = AF_INET;
1480 sin_server.sin_addr.s_addr = in_aton(addr);
1481 sin_server.sin_port = htons(opts.port);
1482 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1483
1484 if (!csocket) {
1485 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1486 err = -EIO;
1487 goto error;
1488 }
1489
1490 err = csocket->ops->connect(csocket,
1491 (struct sockaddr *)&sin_server,
1492 sizeof(struct sockaddr_in), 0);
1493 if (err < 0) {
1494 P9_EPRINTK(KERN_ERR,
1495 "p9_trans_tcp: problem connecting socket to %s\n",
1496 addr);
1497 goto error;
1498 }
1499
1500 err = p9_socket_open(trans, csocket);
1501 if (err < 0)
1502 goto error;
1503
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001504 p = (struct p9_trans_fd *) trans->priv;
1505 p->conn = p9_conn_create(trans);
1506 if (IS_ERR(p->conn)) {
1507 err = PTR_ERR(p->conn);
1508 p->conn = NULL;
1509 goto error;
1510 }
1511
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001512 return trans;
1513
1514error:
1515 if (csocket)
1516 sock_release(csocket);
1517
1518 kfree(trans);
1519 return ERR_PTR(err);
1520}
1521
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001522static struct p9_trans *
1523p9_trans_create_unix(const char *addr, char *args, int msize,
1524 unsigned char dotu)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001525{
1526 int err;
1527 struct socket *csocket;
1528 struct sockaddr_un sun_server;
1529 struct p9_trans *trans;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001530 struct p9_trans_fd *p;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001531
1532 csocket = NULL;
1533 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1534 if (!trans)
1535 return ERR_PTR(-ENOMEM);
1536
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001537 trans->rpc = p9_fd_rpc;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001538 trans->close = p9_fd_close;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001539
1540 if (strlen(addr) > UNIX_PATH_MAX) {
1541 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1542 addr);
1543 err = -ENAMETOOLONG;
1544 goto error;
1545 }
1546
1547 sun_server.sun_family = PF_UNIX;
1548 strcpy(sun_server.sun_path, addr);
1549 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1550 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1551 sizeof(struct sockaddr_un) - 1, 0);
1552 if (err < 0) {
1553 P9_EPRINTK(KERN_ERR,
1554 "p9_trans_unix: problem connecting socket: %s: %d\n",
1555 addr, err);
1556 goto error;
1557 }
1558
1559 err = p9_socket_open(trans, csocket);
1560 if (err < 0)
1561 goto error;
1562
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001563 trans->msize = msize;
1564 trans->extended = dotu;
1565 p = (struct p9_trans_fd *) trans->priv;
1566 p->conn = p9_conn_create(trans);
1567 if (IS_ERR(p->conn)) {
1568 err = PTR_ERR(p->conn);
1569 p->conn = NULL;
1570 goto error;
1571 }
1572
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001573 return trans;
1574
1575error:
1576 if (csocket)
1577 sock_release(csocket);
1578
1579 kfree(trans);
1580 return ERR_PTR(err);
1581}
1582
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001583static struct p9_trans *
1584p9_trans_create_fd(const char *name, char *args, int msize,
1585 unsigned char extended)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001586{
1587 int err;
1588 struct p9_trans *trans;
1589 struct p9_fd_opts opts;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001590 struct p9_trans_fd *p;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001591
1592 parse_opts(args, &opts);
1593
1594 if (opts.rfd == ~0 || opts.wfd == ~0) {
1595 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
1596 return ERR_PTR(-ENOPROTOOPT);
1597 }
1598
1599 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1600 if (!trans)
1601 return ERR_PTR(-ENOMEM);
1602
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001603 trans->rpc = p9_fd_rpc;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001604 trans->close = p9_fd_close;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001605
1606 err = p9_fd_open(trans, opts.rfd, opts.wfd);
1607 if (err < 0)
1608 goto error;
1609
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001610 trans->msize = msize;
1611 trans->extended = extended;
1612 p = (struct p9_trans_fd *) trans->priv;
1613 p->conn = p9_conn_create(trans);
1614 if (IS_ERR(p->conn)) {
1615 err = PTR_ERR(p->conn);
1616 p->conn = NULL;
1617 goto error;
1618 }
1619
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001620 return trans;
1621
1622error:
1623 kfree(trans);
1624 return ERR_PTR(err);
1625}
1626
1627static struct p9_trans_module p9_tcp_trans = {
1628 .name = "tcp",
1629 .maxsize = MAX_SOCK_BUF,
1630 .def = 1,
1631 .create = p9_trans_create_tcp,
1632};
1633
1634static struct p9_trans_module p9_unix_trans = {
1635 .name = "unix",
1636 .maxsize = MAX_SOCK_BUF,
1637 .def = 0,
1638 .create = p9_trans_create_unix,
1639};
1640
1641static struct p9_trans_module p9_fd_trans = {
1642 .name = "fd",
1643 .maxsize = MAX_SOCK_BUF,
1644 .def = 0,
1645 .create = p9_trans_create_fd,
1646};
1647
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001648int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001649{
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001650 int ret = p9_mux_global_init();
1651 if (ret) {
1652 printk(KERN_WARNING "9p: starting mux failed\n");
1653 return ret;
1654 }
1655
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001656 v9fs_register_trans(&p9_tcp_trans);
1657 v9fs_register_trans(&p9_unix_trans);
1658 v9fs_register_trans(&p9_fd_trans);
1659
Andrew Morton3387b802008-03-28 14:15:57 -07001660 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001661}
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001662EXPORT_SYMBOL(p9_trans_fd_init);