9p: consolidate transport structure
[linux-2.6-block.git] / net / 9p / trans_fd.c
CommitLineData
bd238fb4
LI
1/*
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>
8a0dc95f 8 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
bd238fb4
LI
9 * 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>
8a0dc95f 32#include <linux/kthread.h>
bd238fb4
LI
33#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>
a80d923e 40#include <linux/parser.h>
bd238fb4 41#include <net/9p/9p.h>
8b81ef58 42#include <net/9p/client.h>
bd238fb4
LI
43#include <net/9p/transport.h>
44
45#define P9_PORT 564
a80d923e 46#define MAX_SOCK_BUF (64*1024)
8a0dc95f 47#define ERREQFLUSH 1
8a0dc95f 48#define MAXPOLLWADDR 2
a80d923e 49
ee443996
EVH
50/**
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
a80d923e
EVH
58struct p9_fd_opts {
59 int rfd;
60 int wfd;
61 u16 port;
62};
bd238fb4 63
ee443996
EVH
64
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
bd238fb4
LI
73struct p9_trans_fd {
74 struct file *rd;
75 struct file *wr;
8a0dc95f 76 struct p9_conn *conn;
bd238fb4
LI
77};
78
a80d923e
EVH
79/*
80 * Option Parsing (code inspired by NFS code)
81 * - a little lazy - parse all fd-transport options
82 */
bd238fb4 83
a80d923e
EVH
84enum {
85 /* Options that take integer arguments */
55762690 86 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
a80d923e 87};
bd238fb4 88
a447c093 89static const match_table_t tokens = {
a80d923e
EVH
90 {Opt_port, "port=%u"},
91 {Opt_rfdno, "rfdno=%u"},
92 {Opt_wfdno, "wfdno=%u"},
55762690 93 {Opt_err, NULL},
a80d923e 94};
bd238fb4 95
8a0dc95f
EVH
96enum {
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;
8a0dc95f 110typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
ee443996
EVH
111
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
8a0dc95f 126struct p9_req {
ee443996 127 spinlock_t lock;
8a0dc95f
EVH
128 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
992b3f1d
TH
138struct p9_poll_wait {
139 struct p9_conn *conn;
140 wait_queue_t wait;
141 wait_queue_head_t *wait_addr;
ee443996
EVH
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 (?)
ee443996
EVH
148 * @msize: maximum size for connection (dup)
149 * @extended: 9p2000.u flag (dup)
8b81ef58 150 * @client: reference to client instance for this connection
ee443996
EVH
151 * @tagpool: id accounting for transactions
152 * @err: error state
ee443996
EVH
153 * @req_list: accounting for requests which have been sent
154 * @unsent_req_list: accounting for requests that haven't been sent
155 * @rcall: current response &p9_fcall structure
156 * @rpos: read position in current frame
157 * @rbuf: current read buffer
158 * @wpos: write position for current frame
159 * @wsize: amount of data to write for current frame
160 * @wbuf: current write buffer
161 * @poll_wait: array of wait_q's for various worker threads
162 * @poll_waddr: ????
163 * @pt: poll state
164 * @rq: current read work
165 * @wq: current write work
166 * @wsched: ????
167 *
168 */
8a0dc95f
EVH
169
170struct p9_conn {
171 spinlock_t lock; /* protect lock structure */
172 struct list_head mux_list;
8a0dc95f
EVH
173 int msize;
174 unsigned char extended;
8b81ef58 175 struct p9_client *client;
8a0dc95f
EVH
176 struct p9_idpool *tagpool;
177 int err;
8a0dc95f
EVH
178 struct list_head req_list;
179 struct list_head unsent_req_list;
180 struct p9_fcall *rcall;
181 int rpos;
182 char *rbuf;
183 int wpos;
184 int wsize;
185 char *wbuf;
992b3f1d
TH
186 struct list_head poll_pending_link;
187 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
8a0dc95f
EVH
188 poll_table pt;
189 struct work_struct rq;
190 struct work_struct wq;
191 unsigned long wsched;
192};
193
ee443996
EVH
194/**
195 * struct p9_mux_rpc - fd mux rpc accounting structure
196 * @m: connection this request was issued on
197 * @err: error state
198 * @tcall: request &p9_fcall
199 * @rcall: response &p9_fcall
200 * @wqueue: wait queue that client is blocked on for this rpc
201 *
202 * Bug: isn't this information duplicated elsewhere like &p9_req
203 */
8a0dc95f
EVH
204
205struct p9_mux_rpc {
206 struct p9_conn *m;
207 int err;
208 struct p9_fcall *tcall;
209 struct p9_fcall *rcall;
210 wait_queue_head_t wqueue;
211};
212
213static int p9_poll_proc(void *);
214static void p9_read_work(struct work_struct *work);
215static void p9_write_work(struct work_struct *work);
216static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
217 poll_table *p);
8b81ef58
EVH
218static int p9_fd_write(struct p9_client *client, void *v, int len);
219static int p9_fd_read(struct p9_client *client, void *v, int len);
8a0dc95f 220
992b3f1d
TH
221static DEFINE_SPINLOCK(p9_poll_lock);
222static LIST_HEAD(p9_poll_pending_list);
8a0dc95f 223static struct workqueue_struct *p9_mux_wq;
992b3f1d 224static struct task_struct *p9_poll_task;
8a0dc95f
EVH
225
226static void p9_conn_destroy(struct p9_conn *);
8b81ef58 227static unsigned int p9_fd_poll(struct p9_client *client,
8a0dc95f
EVH
228 struct poll_table_struct *pt);
229
230#ifdef P9_NONBLOCK
231static int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
232 p9_conn_req_callback cb, void *a);
233#endif /* P9_NONBLOCK */
234
235static void p9_conn_cancel(struct p9_conn *m, int err);
236
8a0dc95f
EVH
237static u16 p9_mux_get_tag(struct p9_conn *m)
238{
239 int tag;
240
241 tag = p9_idpool_get(m->tagpool);
242 if (tag < 0)
243 return P9_NOTAG;
244 else
245 return (u16) tag;
246}
247
248static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
249{
250 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
251 p9_idpool_put(tag, m->tagpool);
252}
253
992b3f1d 254static void p9_mux_poll_stop(struct p9_conn *m)
8a0dc95f 255{
992b3f1d
TH
256 unsigned long flags;
257 int i;
8a0dc95f 258
992b3f1d
TH
259 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
260 struct p9_poll_wait *pwait = &m->poll_wait[i];
8a0dc95f 261
992b3f1d
TH
262 if (pwait->wait_addr) {
263 remove_wait_queue(pwait->wait_addr, &pwait->wait);
264 pwait->wait_addr = NULL;
8a0dc95f 265 }
8a0dc95f
EVH
266 }
267
992b3f1d
TH
268 spin_lock_irqsave(&p9_poll_lock, flags);
269 list_del_init(&m->poll_pending_link);
270 spin_unlock_irqrestore(&p9_poll_lock, flags);
8a0dc95f
EVH
271}
272
273/**
274 * p9_conn_create - allocate and initialize the per-session mux data
8b81ef58 275 * @client: client instance
8a0dc95f 276 *
ee443996 277 * Note: Creates the polling task if this is the first session.
8a0dc95f 278 */
ee443996 279
8b81ef58 280static struct p9_conn *p9_conn_create(struct p9_client *client)
8a0dc95f
EVH
281{
282 int i, n;
571ffeaf 283 struct p9_conn *m;
8a0dc95f 284
8b81ef58 285 P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize);
571ffeaf 286 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
8a0dc95f
EVH
287 if (!m)
288 return ERR_PTR(-ENOMEM);
289
290 spin_lock_init(&m->lock);
291 INIT_LIST_HEAD(&m->mux_list);
8b81ef58
EVH
292 m->msize = client->msize;
293 m->extended = client->dotu;
294 m->client = client;
8a0dc95f
EVH
295 m->tagpool = p9_idpool_create();
296 if (IS_ERR(m->tagpool)) {
8a0dc95f 297 kfree(m);
571ffeaf 298 return ERR_PTR(-ENOMEM);
8a0dc95f
EVH
299 }
300
8a0dc95f
EVH
301 INIT_LIST_HEAD(&m->req_list);
302 INIT_LIST_HEAD(&m->unsent_req_list);
8a0dc95f
EVH
303 INIT_WORK(&m->rq, p9_read_work);
304 INIT_WORK(&m->wq, p9_write_work);
992b3f1d
TH
305 INIT_LIST_HEAD(&m->poll_pending_link);
306 init_poll_funcptr(&m->pt, p9_pollwait);
8a0dc95f 307
8b81ef58 308 n = p9_fd_poll(client, &m->pt);
8a0dc95f
EVH
309 if (n & POLLIN) {
310 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
311 set_bit(Rpending, &m->wsched);
312 }
313
314 if (n & POLLOUT) {
315 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
316 set_bit(Wpending, &m->wsched);
317 }
318
992b3f1d
TH
319 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
320 if (IS_ERR(m->poll_wait[i].wait_addr)) {
8a0dc95f 321 p9_mux_poll_stop(m);
8a0dc95f 322 kfree(m);
992b3f1d
TH
323 /* return the error code */
324 return (void *)m->poll_wait[i].wait_addr;
8a0dc95f
EVH
325 }
326 }
327
328 return m;
329}
330
331/**
332 * p9_mux_destroy - cancels all pending requests and frees mux resources
ee443996
EVH
333 * @m: mux to destroy
334 *
8a0dc95f 335 */
ee443996 336
8a0dc95f
EVH
337static void p9_conn_destroy(struct p9_conn *m)
338{
339 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
340 m->mux_list.prev, m->mux_list.next);
8a0dc95f
EVH
341
342 p9_mux_poll_stop(m);
7dc5d24b
TH
343 cancel_work_sync(&m->rq);
344 cancel_work_sync(&m->wq);
345
346 p9_conn_cancel(m, -ECONNRESET);
347
8b81ef58 348 m->client = NULL;
8a0dc95f
EVH
349 p9_idpool_destroy(m->tagpool);
350 kfree(m);
351}
352
992b3f1d
TH
353static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
354{
355 struct p9_poll_wait *pwait =
356 container_of(wait, struct p9_poll_wait, wait);
357 struct p9_conn *m = pwait->conn;
358 unsigned long flags;
359 DECLARE_WAITQUEUE(dummy_wait, p9_poll_task);
360
361 spin_lock_irqsave(&p9_poll_lock, flags);
362 if (list_empty(&m->poll_pending_link))
363 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
364 spin_unlock_irqrestore(&p9_poll_lock, flags);
365
366 /* perform the default wake up operation */
367 return default_wake_function(&dummy_wait, mode, sync, key);
368}
369
8a0dc95f 370/**
ee443996
EVH
371 * p9_pollwait - add poll task to the wait queue
372 * @filp: file pointer being polled
373 * @wait_address: wait_q to block on
374 * @p: poll state
375 *
376 * called by files poll operation to add v9fs-poll task to files wait queue
8a0dc95f 377 */
ee443996 378
8a0dc95f
EVH
379static void
380p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
381{
992b3f1d
TH
382 struct p9_conn *m = container_of(p, struct p9_conn, pt);
383 struct p9_poll_wait *pwait = NULL;
8a0dc95f 384 int i;
8a0dc95f 385
992b3f1d
TH
386 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
387 if (m->poll_wait[i].wait_addr == NULL) {
388 pwait = &m->poll_wait[i];
8a0dc95f 389 break;
992b3f1d
TH
390 }
391 }
8a0dc95f 392
992b3f1d 393 if (!pwait) {
8a0dc95f
EVH
394 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
395 return;
396 }
397
8a0dc95f
EVH
398 if (!wait_address) {
399 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
992b3f1d 400 pwait->wait_addr = ERR_PTR(-EIO);
8a0dc95f
EVH
401 return;
402 }
403
992b3f1d
TH
404 pwait->conn = m;
405 pwait->wait_addr = wait_address;
406 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
407 add_wait_queue(wait_address, &pwait->wait);
8a0dc95f
EVH
408}
409
410/**
411 * p9_poll_mux - polls a mux and schedules read or write works if necessary
ee443996
EVH
412 * @m: connection to poll
413 *
8a0dc95f 414 */
ee443996 415
8a0dc95f
EVH
416static void p9_poll_mux(struct p9_conn *m)
417{
418 int n;
419
420 if (m->err < 0)
421 return;
422
8b81ef58 423 n = p9_fd_poll(m->client, NULL);
8a0dc95f
EVH
424 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
425 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
426 if (n >= 0)
427 n = -ECONNRESET;
428 p9_conn_cancel(m, n);
429 }
430
431 if (n & POLLIN) {
432 set_bit(Rpending, &m->wsched);
433 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
434 if (!test_and_set_bit(Rworksched, &m->wsched)) {
435 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
436 queue_work(p9_mux_wq, &m->rq);
437 }
438 }
439
440 if (n & POLLOUT) {
441 set_bit(Wpending, &m->wsched);
442 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
443 if ((m->wsize || !list_empty(&m->unsent_req_list))
444 && !test_and_set_bit(Wworksched, &m->wsched)) {
445 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
446 queue_work(p9_mux_wq, &m->wq);
447 }
448 }
449}
450
451/**
ee443996
EVH
452 * p9_poll_proc - poll worker thread
453 * @a: thread state and arguments
454 *
455 * polls all v9fs transports for new events and queues the appropriate
456 * work to the work queue
457 *
8a0dc95f 458 */
ee443996 459
8a0dc95f
EVH
460static int p9_poll_proc(void *a)
461{
992b3f1d 462 unsigned long flags;
8a0dc95f 463
992b3f1d
TH
464 P9_DPRINTK(P9_DEBUG_MUX, "start %p\n", current);
465 repeat:
466 spin_lock_irqsave(&p9_poll_lock, flags);
467 while (!list_empty(&p9_poll_pending_list)) {
468 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
469 struct p9_conn,
470 poll_pending_link);
471 list_del_init(&conn->poll_pending_link);
472 spin_unlock_irqrestore(&p9_poll_lock, flags);
8a0dc95f 473
992b3f1d 474 p9_poll_mux(conn);
8a0dc95f 475
992b3f1d 476 spin_lock_irqsave(&p9_poll_lock, flags);
8a0dc95f 477 }
992b3f1d 478 spin_unlock_irqrestore(&p9_poll_lock, flags);
8a0dc95f 479
992b3f1d
TH
480 set_current_state(TASK_INTERRUPTIBLE);
481 if (list_empty(&p9_poll_pending_list)) {
482 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
483 schedule();
484 }
8a0dc95f 485 __set_current_state(TASK_RUNNING);
992b3f1d
TH
486
487 if (!kthread_should_stop())
488 goto repeat;
489
8a0dc95f
EVH
490 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
491 return 0;
492}
493
494/**
495 * p9_write_work - called when a transport can send some data
ee443996
EVH
496 * @work: container for work to be done
497 *
8a0dc95f 498 */
ee443996 499
8a0dc95f
EVH
500static void p9_write_work(struct work_struct *work)
501{
502 int n, err;
503 struct p9_conn *m;
504 struct p9_req *req;
505
506 m = container_of(work, struct p9_conn, wq);
507
508 if (m->err < 0) {
509 clear_bit(Wworksched, &m->wsched);
510 return;
511 }
512
513 if (!m->wsize) {
514 if (list_empty(&m->unsent_req_list)) {
515 clear_bit(Wworksched, &m->wsched);
516 return;
517 }
518
519 spin_lock(&m->lock);
520again:
521 req = list_entry(m->unsent_req_list.next, struct p9_req,
522 req_list);
523 list_move_tail(&req->req_list, &m->req_list);
524 if (req->err == ERREQFLUSH)
525 goto again;
526
527 m->wbuf = req->tcall->sdata;
528 m->wsize = req->tcall->size;
529 m->wpos = 0;
530 spin_unlock(&m->lock);
531 }
532
533 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
534 m->wsize);
535 clear_bit(Wpending, &m->wsched);
8b81ef58 536 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
8a0dc95f
EVH
537 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
538 if (err == -EAGAIN) {
539 clear_bit(Wworksched, &m->wsched);
540 return;
541 }
542
543 if (err < 0)
544 goto error;
545 else if (err == 0) {
546 err = -EREMOTEIO;
547 goto error;
548 }
549
550 m->wpos += err;
551 if (m->wpos == m->wsize)
552 m->wpos = m->wsize = 0;
553
554 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
555 if (test_and_clear_bit(Wpending, &m->wsched))
556 n = POLLOUT;
557 else
8b81ef58 558 n = p9_fd_poll(m->client, NULL);
8a0dc95f
EVH
559
560 if (n & POLLOUT) {
561 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
562 queue_work(p9_mux_wq, &m->wq);
563 } else
564 clear_bit(Wworksched, &m->wsched);
565 } else
566 clear_bit(Wworksched, &m->wsched);
567
568 return;
569
570error:
571 p9_conn_cancel(m, err);
572 clear_bit(Wworksched, &m->wsched);
573}
574
575static void process_request(struct p9_conn *m, struct p9_req *req)
576{
577 int ecode;
578 struct p9_str *ename;
579
580 if (!req->err && req->rcall->id == P9_RERROR) {
581 ecode = req->rcall->params.rerror.errno;
582 ename = &req->rcall->params.rerror.error;
583
584 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
585 ename->str);
586
587 if (m->extended)
588 req->err = -ecode;
589
590 if (!req->err) {
591 req->err = p9_errstr2errno(ename->str, ename->len);
592
593 /* string match failed */
594 if (!req->err) {
595 PRINT_FCALL_ERROR("unknown error", req->rcall);
596 req->err = -ESERVERFAULT;
597 }
598 }
599 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
600 P9_DPRINTK(P9_DEBUG_ERROR,
601 "fcall mismatch: expected %d, got %d\n",
602 req->tcall->id + 1, req->rcall->id);
603 if (!req->err)
604 req->err = -EIO;
605 }
606}
607
608/**
609 * p9_read_work - called when there is some data to be read from a transport
ee443996
EVH
610 * @work: container of work to be done
611 *
8a0dc95f 612 */
ee443996 613
8a0dc95f
EVH
614static void p9_read_work(struct work_struct *work)
615{
616 int n, err;
617 struct p9_conn *m;
618 struct p9_req *req, *rptr, *rreq;
619 struct p9_fcall *rcall;
620 char *rbuf;
621
622 m = container_of(work, struct p9_conn, rq);
623
624 if (m->err < 0)
625 return;
626
627 rcall = NULL;
628 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
629
630 if (!m->rcall) {
631 m->rcall =
632 kmalloc(sizeof(struct p9_fcall) + m->msize, GFP_KERNEL);
633 if (!m->rcall) {
634 err = -ENOMEM;
635 goto error;
636 }
637
638 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
639 m->rpos = 0;
640 }
641
642 clear_bit(Rpending, &m->wsched);
8b81ef58 643 err = p9_fd_read(m->client, m->rbuf + m->rpos, m->msize - m->rpos);
8a0dc95f
EVH
644 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
645 if (err == -EAGAIN) {
646 clear_bit(Rworksched, &m->wsched);
647 return;
648 }
649
650 if (err <= 0)
651 goto error;
652
653 m->rpos += err;
654 while (m->rpos > 4) {
655 n = le32_to_cpu(*(__le32 *) m->rbuf);
656 if (n >= m->msize) {
657 P9_DPRINTK(P9_DEBUG_ERROR,
658 "requested packet size too big: %d\n", n);
659 err = -EIO;
660 goto error;
661 }
662
663 if (m->rpos < n)
664 break;
665
666 err =
667 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->extended);
668 if (err < 0)
669 goto error;
670
671#ifdef CONFIG_NET_9P_DEBUG
672 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
673 char buf[150];
674
675 p9_printfcall(buf, sizeof(buf), m->rcall,
676 m->extended);
677 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
678 }
679#endif
680
681 rcall = m->rcall;
682 rbuf = m->rbuf;
683 if (m->rpos > n) {
684 m->rcall = kmalloc(sizeof(struct p9_fcall) + m->msize,
685 GFP_KERNEL);
686 if (!m->rcall) {
687 err = -ENOMEM;
688 goto error;
689 }
690
691 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
692 memmove(m->rbuf, rbuf + n, m->rpos - n);
693 m->rpos -= n;
694 } else {
695 m->rcall = NULL;
696 m->rbuf = NULL;
697 m->rpos = 0;
698 }
699
700 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
701 rcall->id, rcall->tag);
702
703 req = NULL;
704 spin_lock(&m->lock);
705 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
706 if (rreq->tag == rcall->tag) {
707 req = rreq;
708 if (req->flush != Flushing)
709 list_del(&req->req_list);
710 break;
711 }
712 }
713 spin_unlock(&m->lock);
714
715 if (req) {
716 req->rcall = rcall;
717 process_request(m, req);
718
719 if (req->flush != Flushing) {
720 if (req->cb)
721 (*req->cb) (req, req->cba);
722 else
723 kfree(req->rcall);
8a0dc95f
EVH
724 }
725 } else {
726 if (err >= 0 && rcall->id != P9_RFLUSH)
727 P9_DPRINTK(P9_DEBUG_ERROR,
728 "unexpected response mux %p id %d tag %d\n",
729 m, rcall->id, rcall->tag);
730 kfree(rcall);
731 }
732 }
733
734 if (!list_empty(&m->req_list)) {
735 if (test_and_clear_bit(Rpending, &m->wsched))
736 n = POLLIN;
737 else
8b81ef58 738 n = p9_fd_poll(m->client, NULL);
8a0dc95f
EVH
739
740 if (n & POLLIN) {
741 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
742 queue_work(p9_mux_wq, &m->rq);
743 } else
744 clear_bit(Rworksched, &m->wsched);
745 } else
746 clear_bit(Rworksched, &m->wsched);
747
748 return;
749
750error:
751 p9_conn_cancel(m, err);
752 clear_bit(Rworksched, &m->wsched);
753}
754
755/**
756 * p9_send_request - send 9P request
757 * The function can sleep until the request is scheduled for sending.
758 * The function can be interrupted. Return from the function is not
759 * a guarantee that the request is sent successfully. Can return errors
760 * that can be retrieved by PTR_ERR macros.
761 *
762 * @m: mux data
763 * @tc: request to be sent
764 * @cb: callback function to call when response is received
765 * @cba: parameter to pass to the callback function
ee443996 766 *
8a0dc95f 767 */
ee443996 768
8a0dc95f
EVH
769static struct p9_req *p9_send_request(struct p9_conn *m,
770 struct p9_fcall *tc,
771 p9_conn_req_callback cb, void *cba)
772{
773 int n;
774 struct p9_req *req;
775
776 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
777 tc, tc->id);
778 if (m->err < 0)
779 return ERR_PTR(m->err);
780
781 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
782 if (!req)
783 return ERR_PTR(-ENOMEM);
784
785 if (tc->id == P9_TVERSION)
786 n = P9_NOTAG;
787 else
788 n = p9_mux_get_tag(m);
789
62067824
JL
790 if (n < 0) {
791 kfree(req);
8a0dc95f 792 return ERR_PTR(-ENOMEM);
62067824 793 }
8a0dc95f
EVH
794
795 p9_set_tag(tc, n);
796
797#ifdef CONFIG_NET_9P_DEBUG
798 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
799 char buf[150];
800
801 p9_printfcall(buf, sizeof(buf), tc, m->extended);
802 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
803 }
804#endif
805
806 spin_lock_init(&req->lock);
807 req->tag = n;
808 req->tcall = tc;
809 req->rcall = NULL;
810 req->err = 0;
811 req->cb = cb;
812 req->cba = cba;
813 req->flush = None;
814
815 spin_lock(&m->lock);
816 list_add_tail(&req->req_list, &m->unsent_req_list);
817 spin_unlock(&m->lock);
818
819 if (test_and_clear_bit(Wpending, &m->wsched))
820 n = POLLOUT;
821 else
8b81ef58 822 n = p9_fd_poll(m->client, NULL);
8a0dc95f
EVH
823
824 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
825 queue_work(p9_mux_wq, &m->wq);
826
827 return req;
828}
829
830static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
831{
832 p9_mux_put_tag(m, req->tag);
833 kfree(req);
834}
835
836static void p9_mux_flush_cb(struct p9_req *freq, void *a)
837{
8a0dc95f
EVH
838 int tag;
839 struct p9_conn *m;
840 struct p9_req *req, *rreq, *rptr;
841
842 m = a;
843 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
844 freq->tcall, freq->rcall, freq->err,
845 freq->tcall->params.tflush.oldtag);
846
847 spin_lock(&m->lock);
8a0dc95f
EVH
848 tag = freq->tcall->params.tflush.oldtag;
849 req = NULL;
850 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
851 if (rreq->tag == tag) {
852 req = rreq;
853 list_del(&req->req_list);
854 break;
855 }
856 }
857 spin_unlock(&m->lock);
858
859 if (req) {
860 spin_lock(&req->lock);
861 req->flush = Flushed;
862 spin_unlock(&req->lock);
863
864 if (req->cb)
865 (*req->cb) (req, req->cba);
866 else
867 kfree(req->rcall);
8a0dc95f
EVH
868 }
869
870 kfree(freq->tcall);
871 kfree(freq->rcall);
872 p9_mux_free_request(m, freq);
873}
874
875static int
876p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
877{
878 struct p9_fcall *fc;
879 struct p9_req *rreq, *rptr;
880
881 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
882
883 /* if a response was received for a request, do nothing */
884 spin_lock(&req->lock);
885 if (req->rcall || req->err) {
886 spin_unlock(&req->lock);
887 P9_DPRINTK(P9_DEBUG_MUX,
888 "mux %p req %p response already received\n", m, req);
889 return 0;
890 }
891
892 req->flush = Flushing;
893 spin_unlock(&req->lock);
894
895 spin_lock(&m->lock);
896 /* if the request is not sent yet, just remove it from the list */
897 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
898 if (rreq->tag == req->tag) {
899 P9_DPRINTK(P9_DEBUG_MUX,
900 "mux %p req %p request is not sent yet\n", m, req);
901 list_del(&rreq->req_list);
902 req->flush = Flushed;
903 spin_unlock(&m->lock);
904 if (req->cb)
905 (*req->cb) (req, req->cba);
906 return 0;
907 }
908 }
909 spin_unlock(&m->lock);
910
911 clear_thread_flag(TIF_SIGPENDING);
912 fc = p9_create_tflush(req->tag);
913 p9_send_request(m, fc, p9_mux_flush_cb, m);
914 return 1;
915}
916
917static void
918p9_conn_rpc_cb(struct p9_req *req, void *a)
919{
920 struct p9_mux_rpc *r;
921
922 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
923 r = a;
924 r->rcall = req->rcall;
925 r->err = req->err;
926
927 if (req->flush != None && !req->err)
928 r->err = -ERESTARTSYS;
929
930 wake_up(&r->wqueue);
931}
932
933/**
934 * p9_fd_rpc- sends 9P request and waits until a response is available.
935 * The function can be interrupted.
8b81ef58 936 * @client: client instance
8a0dc95f
EVH
937 * @tc: request to be sent
938 * @rc: pointer where a pointer to the response is stored
ee443996 939 *
8a0dc95f 940 */
ee443996 941
8a0dc95f 942int
8b81ef58 943p9_fd_rpc(struct p9_client *client, struct p9_fcall *tc, struct p9_fcall **rc)
8a0dc95f 944{
8b81ef58 945 struct p9_trans_fd *p = client->trans;
8a0dc95f
EVH
946 struct p9_conn *m = p->conn;
947 int err, sigpending;
948 unsigned long flags;
949 struct p9_req *req;
950 struct p9_mux_rpc r;
951
952 r.err = 0;
953 r.tcall = tc;
954 r.rcall = NULL;
955 r.m = m;
956 init_waitqueue_head(&r.wqueue);
957
958 if (rc)
959 *rc = NULL;
960
961 sigpending = 0;
962 if (signal_pending(current)) {
963 sigpending = 1;
964 clear_thread_flag(TIF_SIGPENDING);
965 }
966
967 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
968 if (IS_ERR(req)) {
969 err = PTR_ERR(req);
970 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
971 return err;
972 }
973
974 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
975 if (r.err < 0)
976 err = r.err;
977
8b81ef58 978 if (err == -ERESTARTSYS && client->status == Connected
8a0dc95f
EVH
979 && m->err == 0) {
980 if (p9_mux_flush_request(m, req)) {
981 /* wait until we get response of the flush message */
982 do {
983 clear_thread_flag(TIF_SIGPENDING);
984 err = wait_event_interruptible(r.wqueue,
985 r.rcall || r.err);
986 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
8b81ef58 987 client->status == Connected && !m->err);
8a0dc95f
EVH
988
989 err = -ERESTARTSYS;
990 }
991 sigpending = 1;
992 }
993
994 if (sigpending) {
995 spin_lock_irqsave(&current->sighand->siglock, flags);
996 recalc_sigpending();
997 spin_unlock_irqrestore(&current->sighand->siglock, flags);
998 }
999
1000 if (rc)
1001 *rc = r.rcall;
1002 else
1003 kfree(r.rcall);
1004
1005 p9_mux_free_request(m, req);
1006 if (err > 0)
1007 err = -EIO;
1008
1009 return err;
1010}
1011
1012#ifdef P9_NONBLOCK
1013/**
1014 * p9_conn_rpcnb - sends 9P request without waiting for response.
1015 * @m: mux data
1016 * @tc: request to be sent
1017 * @cb: callback function to be called when response arrives
ee443996
EVH
1018 * @a: value to pass to the callback function
1019 *
8a0dc95f 1020 */
ee443996 1021
8a0dc95f
EVH
1022int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1023 p9_conn_req_callback cb, void *a)
1024{
1025 int err;
1026 struct p9_req *req;
1027
1028 req = p9_send_request(m, tc, cb, a);
1029 if (IS_ERR(req)) {
1030 err = PTR_ERR(req);
1031 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1032 return PTR_ERR(req);
1033 }
1034
1035 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1036 return 0;
1037}
1038#endif /* P9_NONBLOCK */
1039
1040/**
1041 * p9_conn_cancel - cancel all pending requests with error
1042 * @m: mux data
1043 * @err: error code
ee443996 1044 *
8a0dc95f 1045 */
ee443996 1046
8a0dc95f
EVH
1047void p9_conn_cancel(struct p9_conn *m, int err)
1048{
1049 struct p9_req *req, *rtmp;
1050 LIST_HEAD(cancel_list);
1051
1052 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1053 m->err = err;
1054 spin_lock(&m->lock);
1055 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1056 list_move(&req->req_list, &cancel_list);
1057 }
1058 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1059 list_move(&req->req_list, &cancel_list);
1060 }
1061 spin_unlock(&m->lock);
1062
1063 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1064 list_del(&req->req_list);
1065 if (!req->err)
1066 req->err = err;
1067
1068 if (req->cb)
1069 (*req->cb) (req, req->cba);
1070 else
1071 kfree(req->rcall);
1072 }
8a0dc95f
EVH
1073}
1074
a80d923e 1075/**
bb8ffdfc 1076 * parse_options - parse mount options into session structure
a80d923e 1077 * @options: options string passed from mount
ee443996 1078 * @opts: transport-specific structure to parse options into
a80d923e 1079 *
bb8ffdfc 1080 * Returns 0 upon success, -ERRNO upon failure
a80d923e 1081 */
bd238fb4 1082
bb8ffdfc 1083static int parse_opts(char *params, struct p9_fd_opts *opts)
bd238fb4 1084{
a80d923e
EVH
1085 char *p;
1086 substring_t args[MAX_OPT_ARGS];
1087 int option;
bb8ffdfc 1088 char *options;
a80d923e 1089 int ret;
bd238fb4 1090
a80d923e
EVH
1091 opts->port = P9_PORT;
1092 opts->rfd = ~0;
1093 opts->wfd = ~0;
bd238fb4 1094
bb8ffdfc
EVH
1095 if (!params)
1096 return 0;
1097
1098 options = kstrdup(params, GFP_KERNEL);
1099 if (!options) {
1100 P9_DPRINTK(P9_DEBUG_ERROR,
1101 "failed to allocate copy of option string\n");
1102 return -ENOMEM;
1103 }
bd238fb4 1104
a80d923e
EVH
1105 while ((p = strsep(&options, ",")) != NULL) {
1106 int token;
bb8ffdfc 1107 int r;
a80d923e
EVH
1108 if (!*p)
1109 continue;
1110 token = match_token(p, tokens, args);
bb8ffdfc
EVH
1111 r = match_int(&args[0], &option);
1112 if (r < 0) {
a80d923e
EVH
1113 P9_DPRINTK(P9_DEBUG_ERROR,
1114 "integer field, but no integer?\n");
bb8ffdfc 1115 ret = r;
a80d923e
EVH
1116 continue;
1117 }
1118 switch (token) {
1119 case Opt_port:
1120 opts->port = option;
1121 break;
1122 case Opt_rfdno:
1123 opts->rfd = option;
1124 break;
1125 case Opt_wfdno:
1126 opts->wfd = option;
1127 break;
1128 default:
1129 continue;
1130 }
bd238fb4 1131 }
bb8ffdfc
EVH
1132 kfree(options);
1133 return 0;
bd238fb4 1134}
bd238fb4 1135
8b81ef58 1136static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
bd238fb4 1137{
a80d923e
EVH
1138 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1139 GFP_KERNEL);
1140 if (!ts)
1141 return -ENOMEM;
bd238fb4 1142
a80d923e
EVH
1143 ts->rd = fget(rfd);
1144 ts->wr = fget(wfd);
1145 if (!ts->rd || !ts->wr) {
1146 if (ts->rd)
1147 fput(ts->rd);
1148 if (ts->wr)
1149 fput(ts->wr);
1150 kfree(ts);
1151 return -EIO;
bd238fb4
LI
1152 }
1153
8b81ef58
EVH
1154 client->trans = ts;
1155 client->status = Connected;
bd238fb4 1156
a80d923e 1157 return 0;
bd238fb4 1158}
bd238fb4 1159
8b81ef58 1160static int p9_socket_open(struct p9_client *client, struct socket *csocket)
bd238fb4
LI
1161{
1162 int fd, ret;
1163
1164 csocket->sk->sk_allocation = GFP_NOIO;
a677a039 1165 fd = sock_map_fd(csocket, 0);
bd238fb4
LI
1166 if (fd < 0) {
1167 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1168 return fd;
1169 }
1170
8b81ef58 1171 ret = p9_fd_open(client, fd, fd);
bd238fb4
LI
1172 if (ret < 0) {
1173 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1174 sockfd_put(csocket);
1175 return ret;
1176 }
1177
8b81ef58 1178 ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK;
bd238fb4
LI
1179
1180 return 0;
1181}
1182
bd238fb4
LI
1183/**
1184 * p9_fd_read- read from a fd
8b81ef58 1185 * @client: client instance
bd238fb4
LI
1186 * @v: buffer to receive data into
1187 * @len: size of receive buffer
1188 *
1189 */
ee443996 1190
8b81ef58 1191static int p9_fd_read(struct p9_client *client, void *v, int len)
bd238fb4
LI
1192{
1193 int ret;
1194 struct p9_trans_fd *ts = NULL;
1195
8b81ef58
EVH
1196 if (client && client->status != Disconnected)
1197 ts = client->trans;
bd238fb4
LI
1198
1199 if (!ts)
1200 return -EREMOTEIO;
1201
1202 if (!(ts->rd->f_flags & O_NONBLOCK))
1203 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
1204
1205 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
1206 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
8b81ef58 1207 client->status = Disconnected;
bd238fb4
LI
1208 return ret;
1209}
1210
1211/**
1212 * p9_fd_write - write to a socket
8b81ef58 1213 * @client: client instance
bd238fb4
LI
1214 * @v: buffer to send data from
1215 * @len: size of send buffer
1216 *
1217 */
ee443996 1218
8b81ef58 1219static int p9_fd_write(struct p9_client *client, void *v, int len)
bd238fb4
LI
1220{
1221 int ret;
1222 mm_segment_t oldfs;
1223 struct p9_trans_fd *ts = NULL;
1224
8b81ef58
EVH
1225 if (client && client->status != Disconnected)
1226 ts = client->trans;
bd238fb4
LI
1227
1228 if (!ts)
1229 return -EREMOTEIO;
1230
1231 if (!(ts->wr->f_flags & O_NONBLOCK))
1232 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
1233
1234 oldfs = get_fs();
1235 set_fs(get_ds());
1236 /* The cast to a user pointer is valid due to the set_fs() */
1237 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
1238 set_fs(oldfs);
1239
1240 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
8b81ef58 1241 client->status = Disconnected;
bd238fb4
LI
1242 return ret;
1243}
1244
1245static unsigned int
8b81ef58 1246p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
bd238fb4
LI
1247{
1248 int ret, n;
1249 struct p9_trans_fd *ts = NULL;
bd238fb4 1250
8b81ef58
EVH
1251 if (client && client->status == Connected)
1252 ts = client->trans;
bd238fb4
LI
1253
1254 if (!ts)
1255 return -EREMOTEIO;
1256
1257 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1258 return -EIO;
1259
1260 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1261 return -EIO;
1262
bd238fb4
LI
1263 ret = ts->rd->f_op->poll(ts->rd, pt);
1264 if (ret < 0)
ec3c68f2 1265 return ret;
bd238fb4
LI
1266
1267 if (ts->rd != ts->wr) {
1268 n = ts->wr->f_op->poll(ts->wr, pt);
ec3c68f2
TH
1269 if (n < 0)
1270 return n;
bd238fb4
LI
1271 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1272 }
1273
bd238fb4
LI
1274 return ret;
1275}
1276
1277/**
8b81ef58
EVH
1278 * p9_fd_close - shutdown file descriptor transport
1279 * @client: client instance
bd238fb4
LI
1280 *
1281 */
ee443996 1282
8b81ef58 1283static void p9_fd_close(struct p9_client *client)
bd238fb4
LI
1284{
1285 struct p9_trans_fd *ts;
1286
8b81ef58 1287 if (!client)
bd238fb4
LI
1288 return;
1289
8b81ef58 1290 ts = client->trans;
bd238fb4
LI
1291 if (!ts)
1292 return;
1293
8b81ef58
EVH
1294 client->status = Disconnected;
1295
8a0dc95f
EVH
1296 p9_conn_destroy(ts->conn);
1297
bd238fb4
LI
1298 if (ts->rd)
1299 fput(ts->rd);
1300 if (ts->wr)
1301 fput(ts->wr);
8b81ef58 1302
bd238fb4
LI
1303 kfree(ts);
1304}
1305
887b3ece
EVH
1306/*
1307 * stolen from NFS - maybe should be made a generic function?
1308 */
1309static inline int valid_ipaddr4(const char *buf)
1310{
1311 int rc, count, in[4];
1312
1313 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
1314 if (rc != 4)
1315 return -EINVAL;
1316 for (count = 0; count < 4; count++) {
1317 if (in[count] > 255)
1318 return -EINVAL;
1319 }
1320 return 0;
1321}
1322
8b81ef58
EVH
1323static int
1324p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
a80d923e
EVH
1325{
1326 int err;
a80d923e
EVH
1327 struct socket *csocket;
1328 struct sockaddr_in sin_server;
1329 struct p9_fd_opts opts;
8b81ef58 1330 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
a80d923e 1331
bb8ffdfc
EVH
1332 err = parse_opts(args, &opts);
1333 if (err < 0)
8b81ef58 1334 return err;
a80d923e 1335
887b3ece 1336 if (valid_ipaddr4(addr) < 0)
8b81ef58 1337 return -EINVAL;
887b3ece 1338
a80d923e 1339 csocket = NULL;
a80d923e
EVH
1340
1341 sin_server.sin_family = AF_INET;
1342 sin_server.sin_addr.s_addr = in_aton(addr);
1343 sin_server.sin_port = htons(opts.port);
1344 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1345
1346 if (!csocket) {
1347 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1348 err = -EIO;
1349 goto error;
1350 }
1351
1352 err = csocket->ops->connect(csocket,
1353 (struct sockaddr *)&sin_server,
1354 sizeof(struct sockaddr_in), 0);
1355 if (err < 0) {
1356 P9_EPRINTK(KERN_ERR,
1357 "p9_trans_tcp: problem connecting socket to %s\n",
1358 addr);
1359 goto error;
1360 }
1361
8b81ef58 1362 err = p9_socket_open(client, csocket);
a80d923e
EVH
1363 if (err < 0)
1364 goto error;
1365
8b81ef58
EVH
1366 p = (struct p9_trans_fd *) client->trans;
1367 p->conn = p9_conn_create(client);
8a0dc95f
EVH
1368 if (IS_ERR(p->conn)) {
1369 err = PTR_ERR(p->conn);
1370 p->conn = NULL;
1371 goto error;
1372 }
1373
8b81ef58 1374 return 0;
a80d923e
EVH
1375
1376error:
1377 if (csocket)
1378 sock_release(csocket);
1379
8b81ef58
EVH
1380 kfree(p);
1381
1382 return err;
a80d923e
EVH
1383}
1384
8b81ef58
EVH
1385static int
1386p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
a80d923e
EVH
1387{
1388 int err;
1389 struct socket *csocket;
1390 struct sockaddr_un sun_server;
8b81ef58 1391 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
a80d923e
EVH
1392
1393 csocket = NULL;
a80d923e
EVH
1394
1395 if (strlen(addr) > UNIX_PATH_MAX) {
1396 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1397 addr);
1398 err = -ENAMETOOLONG;
1399 goto error;
1400 }
1401
1402 sun_server.sun_family = PF_UNIX;
1403 strcpy(sun_server.sun_path, addr);
1404 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1405 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1406 sizeof(struct sockaddr_un) - 1, 0);
1407 if (err < 0) {
1408 P9_EPRINTK(KERN_ERR,
1409 "p9_trans_unix: problem connecting socket: %s: %d\n",
1410 addr, err);
1411 goto error;
1412 }
1413
8b81ef58 1414 err = p9_socket_open(client, csocket);
a80d923e
EVH
1415 if (err < 0)
1416 goto error;
1417
8b81ef58
EVH
1418 p = (struct p9_trans_fd *) client->trans;
1419 p->conn = p9_conn_create(client);
8a0dc95f
EVH
1420 if (IS_ERR(p->conn)) {
1421 err = PTR_ERR(p->conn);
1422 p->conn = NULL;
1423 goto error;
1424 }
1425
8b81ef58 1426 return 0;
a80d923e
EVH
1427
1428error:
1429 if (csocket)
1430 sock_release(csocket);
1431
8b81ef58
EVH
1432 kfree(p);
1433 return err;
a80d923e
EVH
1434}
1435
8b81ef58
EVH
1436static int
1437p9_fd_create(struct p9_client *client, const char *addr, char *args)
a80d923e
EVH
1438{
1439 int err;
a80d923e 1440 struct p9_fd_opts opts;
8b81ef58 1441 struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */
a80d923e
EVH
1442
1443 parse_opts(args, &opts);
1444
1445 if (opts.rfd == ~0 || opts.wfd == ~0) {
1446 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
8b81ef58 1447 return -ENOPROTOOPT;
a80d923e
EVH
1448 }
1449
8b81ef58 1450 err = p9_fd_open(client, opts.rfd, opts.wfd);
a80d923e
EVH
1451 if (err < 0)
1452 goto error;
1453
8b81ef58
EVH
1454 p = (struct p9_trans_fd *) client->trans;
1455 p->conn = p9_conn_create(client);
8a0dc95f
EVH
1456 if (IS_ERR(p->conn)) {
1457 err = PTR_ERR(p->conn);
1458 p->conn = NULL;
1459 goto error;
1460 }
1461
8b81ef58 1462 return 0;
a80d923e
EVH
1463
1464error:
8b81ef58
EVH
1465 kfree(p);
1466 return err;
a80d923e
EVH
1467}
1468
1469static struct p9_trans_module p9_tcp_trans = {
1470 .name = "tcp",
1471 .maxsize = MAX_SOCK_BUF,
1472 .def = 1,
8b81ef58
EVH
1473 .create = p9_fd_create_tcp,
1474 .close = p9_fd_close,
1475 .rpc = p9_fd_rpc,
72029fe8 1476 .owner = THIS_MODULE,
a80d923e
EVH
1477};
1478
1479static struct p9_trans_module p9_unix_trans = {
1480 .name = "unix",
1481 .maxsize = MAX_SOCK_BUF,
1482 .def = 0,
8b81ef58
EVH
1483 .create = p9_fd_create_unix,
1484 .close = p9_fd_close,
1485 .rpc = p9_fd_rpc,
72029fe8 1486 .owner = THIS_MODULE,
a80d923e
EVH
1487};
1488
1489static struct p9_trans_module p9_fd_trans = {
1490 .name = "fd",
1491 .maxsize = MAX_SOCK_BUF,
1492 .def = 0,
8b81ef58
EVH
1493 .create = p9_fd_create,
1494 .close = p9_fd_close,
1495 .rpc = p9_fd_rpc,
72029fe8 1496 .owner = THIS_MODULE,
a80d923e
EVH
1497};
1498
887b3ece 1499int p9_trans_fd_init(void)
a80d923e 1500{
206ca50d
TH
1501 p9_mux_wq = create_workqueue("v9fs");
1502 if (!p9_mux_wq) {
1503 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
1504 return -ENOMEM;
8a0dc95f
EVH
1505 }
1506
992b3f1d
TH
1507 p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll");
1508 if (IS_ERR(p9_poll_task)) {
1509 destroy_workqueue(p9_mux_wq);
1510 printk(KERN_WARNING "v9fs: mux: creating poll task failed\n");
1511 return PTR_ERR(p9_poll_task);
1512 }
1513
a80d923e
EVH
1514 v9fs_register_trans(&p9_tcp_trans);
1515 v9fs_register_trans(&p9_unix_trans);
1516 v9fs_register_trans(&p9_fd_trans);
1517
3387b804 1518 return 0;
a80d923e 1519}
72029fe8
TH
1520
1521void p9_trans_fd_exit(void)
1522{
992b3f1d 1523 kthread_stop(p9_poll_task);
72029fe8
TH
1524 v9fs_unregister_trans(&p9_tcp_trans);
1525 v9fs_unregister_trans(&p9_unix_trans);
1526 v9fs_unregister_trans(&p9_fd_trans);
206ca50d
TH
1527
1528 destroy_workqueue(p9_mux_wq);
72029fe8 1529}