Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cooloney...
[linux-2.6/kmemtrace.git] / net / 9p / trans_fd.c
blob4507f744f44e527cc140b6b5601e556c10bfd361
1 /*
2 * linux/fs/9p/trans_fd.c
4 * Fd transport layer. Includes deprecated socket layer.
6 * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
7 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
8 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
9 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
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.
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.
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
28 #include <linux/in.h>
29 #include <linux/module.h>
30 #include <linux/net.h>
31 #include <linux/ipv6.h>
32 #include <linux/kthread.h>
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>
40 #include <linux/parser.h>
41 #include <net/9p/9p.h>
42 #include <net/9p/transport.h>
44 #define P9_PORT 564
45 #define MAX_SOCK_BUF (64*1024)
46 #define ERREQFLUSH 1
47 #define SCHED_TIMEOUT 10
48 #define MAXPOLLWADDR 2
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)
58 struct p9_fd_opts {
59 int rfd;
60 int wfd;
61 u16 port;
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
73 struct p9_trans_fd {
74 struct file *rd;
75 struct file *wr;
76 struct p9_conn *conn;
80 * Option Parsing (code inspired by NFS code)
81 * - a little lazy - parse all fd-transport options
84 enum {
85 /* Options that take integer arguments */
86 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
89 static match_table_t tokens = {
90 {Opt_port, "port=%u"},
91 {Opt_rfdno, "rfdno=%u"},
92 {Opt_wfdno, "wfdno=%u"},
93 {Opt_err, NULL},
96 enum {
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 */
103 enum {
104 None,
105 Flushing,
106 Flushed,
109 struct p9_req;
110 typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
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
126 struct p9_req {
127 spinlock_t lock;
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;
138 struct p9_mux_poll_task {
139 struct task_struct *task;
140 struct list_head mux_list;
141 int muxnum;
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: ????
172 struct 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;
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
206 * Bug: isn't this information duplicated elsewhere like &p9_req
209 struct 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;
217 static int p9_poll_proc(void *);
218 static void p9_read_work(struct work_struct *work);
219 static void p9_write_work(struct work_struct *work);
220 static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
221 poll_table *p);
222 static int p9_fd_write(struct p9_trans *trans, void *v, int len);
223 static int p9_fd_read(struct p9_trans *trans, void *v, int len);
225 static DEFINE_MUTEX(p9_mux_task_lock);
226 static struct workqueue_struct *p9_mux_wq;
228 static int p9_mux_num;
229 static int p9_mux_poll_task_num;
230 static struct p9_mux_poll_task p9_mux_poll_tasks[100];
232 static void p9_conn_destroy(struct p9_conn *);
233 static unsigned int p9_fd_poll(struct p9_trans *trans,
234 struct poll_table_struct *pt);
236 #ifdef P9_NONBLOCK
237 static int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
238 p9_conn_req_callback cb, void *a);
239 #endif /* P9_NONBLOCK */
241 static void p9_conn_cancel(struct p9_conn *m, int err);
243 static int p9_mux_global_init(void)
245 int i;
247 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++)
248 p9_mux_poll_tasks[i].task = NULL;
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;
256 return 0;
259 static u16 p9_mux_get_tag(struct p9_conn *m)
261 int tag;
263 tag = p9_idpool_get(m->tagpool);
264 if (tag < 0)
265 return P9_NOTAG;
266 else
267 return (u16) tag;
270 static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
272 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
273 p9_idpool_put(tag, m->tagpool);
277 * p9_mux_calc_poll_procs - calculates the number of polling procs
278 * @muxnum: number of mounts
280 * Calculation is based on the number of mounted v9fs filesystems.
281 * The current implementation returns sqrt of the number of mounts.
284 static int p9_mux_calc_poll_procs(int muxnum)
286 int n;
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;
294 if (n > ARRAY_SIZE(p9_mux_poll_tasks))
295 n = ARRAY_SIZE(p9_mux_poll_tasks);
297 return n;
300 static int p9_mux_poll_start(struct p9_conn *m)
302 int i, n;
303 struct p9_mux_poll_task *vpt, *vptlast;
304 struct task_struct *pproc;
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);
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");
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);
327 break;
331 if (i >= ARRAY_SIZE(p9_mux_poll_tasks))
332 P9_DPRINTK(P9_DEBUG_ERROR,
333 "warning: no free poll slots\n");
336 n = (p9_mux_num + 1) / p9_mux_poll_task_num +
337 ((p9_mux_num + 1) % p9_mux_poll_task_num ? 1 : 0);
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;
357 if (i >= ARRAY_SIZE(p9_mux_poll_tasks)) {
358 if (vptlast == NULL) {
359 mutex_unlock(&p9_mux_task_lock);
360 return -ENOMEM;
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);
371 p9_mux_num++;
372 mutex_unlock(&p9_mux_task_lock);
374 return 0;
377 static void p9_mux_poll_stop(struct p9_conn *m)
379 int i;
380 struct p9_mux_poll_task *vpt;
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;
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--;
398 p9_mux_num--;
399 mutex_unlock(&p9_mux_task_lock);
403 * p9_conn_create - allocate and initialize the per-session mux data
404 * @trans: transport structure
406 * Note: Creates the polling task if this is the first session.
409 static struct p9_conn *p9_conn_create(struct p9_trans *trans)
411 int i, n;
412 struct p9_conn *m, *mtmp;
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);
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;
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);
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);
458 if (n & POLLOUT) {
459 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
460 set_bit(Wpending, &m->wsched);
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;
473 return m;
477 * p9_mux_destroy - cancels all pending requests and frees mux resources
478 * @m: mux to destroy
482 static void p9_conn_destroy(struct p9_conn *m)
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);
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));
497 p9_mux_poll_stop(m);
498 m->trans = NULL;
499 p9_idpool_destroy(m->tagpool);
500 kfree(m);
504 * 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
509 * called by files poll operation to add v9fs-poll task to files wait queue
512 static void
513 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
515 int i;
516 struct p9_conn *m;
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;
523 if (i >= ARRAY_SIZE(m->poll_waddr)) {
524 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
525 return;
528 m->poll_waddr[i] = wait_address;
530 if (!wait_address) {
531 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
532 m->poll_waddr[i] = ERR_PTR(-EIO);
533 return;
536 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
537 add_wait_queue(wait_address, &m->poll_wait[i]);
541 * p9_poll_mux - polls a mux and schedules read or write works if necessary
542 * @m: connection to poll
546 static void p9_poll_mux(struct p9_conn *m)
548 int n;
550 if (m->err < 0)
551 return;
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);
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);
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);
582 * p9_poll_proc - poll worker thread
583 * @a: thread state and arguments
585 * polls all v9fs transports for new events and queues the appropriate
586 * work to the work queue
590 static int p9_poll_proc(void *a)
592 struct p9_conn *m, *mtmp;
593 struct p9_mux_poll_task *vpt;
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);
600 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
601 p9_poll_mux(m);
604 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
605 schedule_timeout(SCHED_TIMEOUT * HZ);
608 __set_current_state(TASK_RUNNING);
609 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
610 return 0;
614 * p9_write_work - called when a transport can send some data
615 * @work: container for work to be done
619 static void p9_write_work(struct work_struct *work)
621 int n, err;
622 struct p9_conn *m;
623 struct p9_req *req;
625 m = container_of(work, struct p9_conn, wq);
627 if (m->err < 0) {
628 clear_bit(Wworksched, &m->wsched);
629 return;
632 if (!m->wsize) {
633 if (list_empty(&m->unsent_req_list)) {
634 clear_bit(Wworksched, &m->wsched);
635 return;
638 spin_lock(&m->lock);
639 again:
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;
646 m->wbuf = req->tcall->sdata;
647 m->wsize = req->tcall->size;
648 m->wpos = 0;
649 spin_unlock(&m->lock);
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;
662 if (err < 0)
663 goto error;
664 else if (err == 0) {
665 err = -EREMOTEIO;
666 goto error;
669 m->wpos += err;
670 if (m->wpos == m->wsize)
671 m->wpos = m->wsize = 0;
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);
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);
687 return;
689 error:
690 p9_conn_cancel(m, err);
691 clear_bit(Wworksched, &m->wsched);
694 static void process_request(struct p9_conn *m, struct p9_req *req)
696 int ecode;
697 struct p9_str *ename;
699 if (!req->err && req->rcall->id == P9_RERROR) {
700 ecode = req->rcall->params.rerror.errno;
701 ename = &req->rcall->params.rerror.error;
703 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
704 ename->str);
706 if (m->extended)
707 req->err = -ecode;
709 if (!req->err) {
710 req->err = p9_errstr2errno(ename->str, ename->len);
712 /* string match failed */
713 if (!req->err) {
714 PRINT_FCALL_ERROR("unknown error", req->rcall);
715 req->err = -ESERVERFAULT;
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;
728 * p9_read_work - called when there is some data to be read from a transport
729 * @work: container of work to be done
733 static void p9_read_work(struct work_struct *work)
735 int n, err;
736 struct p9_conn *m;
737 struct p9_req *req, *rptr, *rreq;
738 struct p9_fcall *rcall;
739 char *rbuf;
741 m = container_of(work, struct p9_conn, rq);
743 if (m->err < 0)
744 return;
746 rcall = NULL;
747 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
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;
757 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
758 m->rpos = 0;
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;
769 if (err <= 0)
770 goto error;
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;
782 if (m->rpos < n)
783 break;
785 err =
786 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->extended);
787 if (err < 0)
788 goto error;
790 #ifdef CONFIG_NET_9P_DEBUG
791 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
792 char buf[150];
794 p9_printfcall(buf, sizeof(buf), m->rcall,
795 m->extended);
796 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
798 #endif
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;
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;
819 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
820 rcall->id, rcall->tag);
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;
832 spin_unlock(&m->lock);
834 if (req) {
835 req->rcall = rcall;
836 process_request(m, req);
838 if (req->flush != Flushing) {
839 if (req->cb)
840 (*req->cb) (req, req->cba);
841 else
842 kfree(req->rcall);
844 wake_up(&m->equeue);
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);
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);
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);
869 return;
871 error:
872 p9_conn_cancel(m, err);
873 clear_bit(Rworksched, &m->wsched);
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.
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
890 static struct p9_req *p9_send_request(struct p9_conn *m,
891 struct p9_fcall *tc,
892 p9_conn_req_callback cb, void *cba)
894 int n;
895 struct p9_req *req;
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);
902 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
903 if (!req)
904 return ERR_PTR(-ENOMEM);
906 if (tc->id == P9_TVERSION)
907 n = P9_NOTAG;
908 else
909 n = p9_mux_get_tag(m);
911 if (n < 0)
912 return ERR_PTR(-ENOMEM);
914 p9_set_tag(tc, n);
916 #ifdef CONFIG_NET_9P_DEBUG
917 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
918 char buf[150];
920 p9_printfcall(buf, sizeof(buf), tc, m->extended);
921 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
923 #endif
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;
934 spin_lock(&m->lock);
935 list_add_tail(&req->req_list, &m->unsent_req_list);
936 spin_unlock(&m->lock);
938 if (test_and_clear_bit(Wpending, &m->wsched))
939 n = POLLOUT;
940 else
941 n = p9_fd_poll(m->trans, NULL);
943 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
944 queue_work(p9_mux_wq, &m->wq);
946 return req;
949 static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
951 p9_mux_put_tag(m, req->tag);
952 kfree(req);
955 static void p9_mux_flush_cb(struct p9_req *freq, void *a)
957 int tag;
958 struct p9_conn *m;
959 struct p9_req *req, *rreq, *rptr;
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);
966 spin_lock(&m->lock);
967 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;
976 spin_unlock(&m->lock);
978 if (req) {
979 spin_lock(&req->lock);
980 req->flush = Flushed;
981 spin_unlock(&req->lock);
983 if (req->cb)
984 (*req->cb) (req, req->cba);
985 else
986 kfree(req->rcall);
988 wake_up(&m->equeue);
991 kfree(freq->tcall);
992 kfree(freq->rcall);
993 p9_mux_free_request(m, freq);
996 static int
997 p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
999 struct p9_fcall *fc;
1000 struct p9_req *rreq, *rptr;
1002 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
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;
1013 req->flush = Flushing;
1014 spin_unlock(&req->lock);
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;
1030 spin_unlock(&m->lock);
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;
1038 static void
1039 p9_conn_rpc_cb(struct p9_req *req, void *a)
1041 struct p9_mux_rpc *r;
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;
1048 if (req->flush != None && !req->err)
1049 r->err = -ERESTARTSYS;
1051 wake_up(&r->wqueue);
1055 * p9_fd_rpc- sends 9P request and waits until a response is available.
1056 * The function can be interrupted.
1057 * @t: transport data
1058 * @tc: request to be sent
1059 * @rc: pointer where a pointer to the response is stored
1064 p9_fd_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
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;
1073 r.err = 0;
1074 r.tcall = tc;
1075 r.rcall = NULL;
1076 r.m = m;
1077 init_waitqueue_head(&r.wqueue);
1079 if (rc)
1080 *rc = NULL;
1082 sigpending = 0;
1083 if (signal_pending(current)) {
1084 sigpending = 1;
1085 clear_thread_flag(TIF_SIGPENDING);
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;
1095 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
1096 if (r.err < 0)
1097 err = r.err;
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);
1110 err = -ERESTARTSYS;
1112 sigpending = 1;
1115 if (sigpending) {
1116 spin_lock_irqsave(&current->sighand->siglock, flags);
1117 recalc_sigpending();
1118 spin_unlock_irqrestore(&current->sighand->siglock, flags);
1121 if (rc)
1122 *rc = r.rcall;
1123 else
1124 kfree(r.rcall);
1126 p9_mux_free_request(m, req);
1127 if (err > 0)
1128 err = -EIO;
1130 return err;
1133 #ifdef P9_NONBLOCK
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
1139 * @a: value to pass to the callback function
1143 int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1144 p9_conn_req_callback cb, void *a)
1146 int err;
1147 struct p9_req *req;
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);
1156 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1157 return 0;
1159 #endif /* P9_NONBLOCK */
1162 * p9_conn_cancel - cancel all pending requests with error
1163 * @m: mux data
1164 * @err: error code
1168 void p9_conn_cancel(struct p9_conn *m, int err)
1170 struct p9_req *req, *rtmp;
1171 LIST_HEAD(cancel_list);
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);
1179 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1180 list_move(&req->req_list, &cancel_list);
1182 spin_unlock(&m->lock);
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;
1189 if (req->cb)
1190 (*req->cb) (req, req->cba);
1191 else
1192 kfree(req->rcall);
1195 wake_up(&m->equeue);
1199 * parse_options - parse mount options into session structure
1200 * @options: options string passed from mount
1201 * @opts: transport-specific structure to parse options into
1203 * Returns 0 upon success, -ERRNO upon failure
1206 static int parse_opts(char *params, struct p9_fd_opts *opts)
1208 char *p;
1209 substring_t args[MAX_OPT_ARGS];
1210 int option;
1211 char *options;
1212 int ret;
1214 opts->port = P9_PORT;
1215 opts->rfd = ~0;
1216 opts->wfd = ~0;
1218 if (!params)
1219 return 0;
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;
1228 while ((p = strsep(&options, ",")) != NULL) {
1229 int token;
1230 int r;
1231 if (!*p)
1232 continue;
1233 token = match_token(p, tokens, args);
1234 r = match_int(&args[0], &option);
1235 if (r < 0) {
1236 P9_DPRINTK(P9_DEBUG_ERROR,
1237 "integer field, but no integer?\n");
1238 ret = r;
1239 continue;
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;
1255 kfree(options);
1256 return 0;
1259 static int p9_fd_open(struct p9_trans *trans, int rfd, int wfd)
1261 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1262 GFP_KERNEL);
1263 if (!ts)
1264 return -ENOMEM;
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;
1277 trans->priv = ts;
1278 trans->status = Connected;
1280 return 0;
1283 static int p9_socket_open(struct p9_trans *trans, struct socket *csocket)
1285 int fd, ret;
1287 csocket->sk->sk_allocation = GFP_NOIO;
1288 fd = sock_map_fd(csocket);
1289 if (fd < 0) {
1290 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1291 return fd;
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;
1301 ((struct p9_trans_fd *)trans->priv)->rd->f_flags |= O_NONBLOCK;
1303 return 0;
1307 * p9_fd_read- read from a fd
1308 * @trans: transport instance state
1309 * @v: buffer to receive data into
1310 * @len: size of receive buffer
1314 static int p9_fd_read(struct p9_trans *trans, void *v, int len)
1316 int ret;
1317 struct p9_trans_fd *ts = NULL;
1319 if (trans && trans->status != Disconnected)
1320 ts = trans->priv;
1322 if (!ts)
1323 return -EREMOTEIO;
1325 if (!(ts->rd->f_flags & O_NONBLOCK))
1326 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
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;
1335 * p9_fd_write - write to a socket
1336 * @trans: transport instance state
1337 * @v: buffer to send data from
1338 * @len: size of send buffer
1342 static int p9_fd_write(struct p9_trans *trans, void *v, int len)
1344 int ret;
1345 mm_segment_t oldfs;
1346 struct p9_trans_fd *ts = NULL;
1348 if (trans && trans->status != Disconnected)
1349 ts = trans->priv;
1351 if (!ts)
1352 return -EREMOTEIO;
1354 if (!(ts->wr->f_flags & O_NONBLOCK))
1355 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
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);
1363 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1364 trans->status = Disconnected;
1365 return ret;
1368 static unsigned int
1369 p9_fd_poll(struct p9_trans *trans, struct poll_table_struct *pt)
1371 int ret, n;
1372 struct p9_trans_fd *ts = NULL;
1373 mm_segment_t oldfs;
1375 if (trans && trans->status == Connected)
1376 ts = trans->priv;
1378 if (!ts)
1379 return -EREMOTEIO;
1381 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1382 return -EIO;
1384 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1385 return -EIO;
1387 oldfs = get_fs();
1388 set_fs(get_ds());
1390 ret = ts->rd->f_op->poll(ts->rd, pt);
1391 if (ret < 0)
1392 goto end;
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;
1400 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1403 end:
1404 set_fs(oldfs);
1405 return ret;
1409 * p9_fd_close - shutdown socket
1410 * @trans: private socket structure
1414 static void p9_fd_close(struct p9_trans *trans)
1416 struct p9_trans_fd *ts;
1418 if (!trans)
1419 return;
1421 ts = xchg(&trans->priv, NULL);
1423 if (!ts)
1424 return;
1426 p9_conn_destroy(ts->conn);
1428 trans->status = Disconnected;
1429 if (ts->rd)
1430 fput(ts->rd);
1431 if (ts->wr)
1432 fput(ts->wr);
1433 kfree(ts);
1437 * stolen from NFS - maybe should be made a generic function?
1439 static inline int valid_ipaddr4(const char *buf)
1441 int rc, count, in[4];
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;
1450 return 0;
1453 static struct p9_trans *
1454 p9_trans_create_tcp(const char *addr, char *args, int msize, unsigned char dotu)
1456 int err;
1457 struct p9_trans *trans;
1458 struct socket *csocket;
1459 struct sockaddr_in sin_server;
1460 struct p9_fd_opts opts;
1461 struct p9_trans_fd *p;
1463 err = parse_opts(args, &opts);
1464 if (err < 0)
1465 return ERR_PTR(err);
1467 if (valid_ipaddr4(addr) < 0)
1468 return ERR_PTR(-EINVAL);
1470 csocket = NULL;
1471 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1472 if (!trans)
1473 return ERR_PTR(-ENOMEM);
1474 trans->msize = msize;
1475 trans->extended = dotu;
1476 trans->rpc = p9_fd_rpc;
1477 trans->close = p9_fd_close;
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);
1484 if (!csocket) {
1485 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1486 err = -EIO;
1487 goto error;
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;
1500 err = p9_socket_open(trans, csocket);
1501 if (err < 0)
1502 goto error;
1504 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;
1512 return trans;
1514 error:
1515 if (csocket)
1516 sock_release(csocket);
1518 kfree(trans);
1519 return ERR_PTR(err);
1522 static struct p9_trans *
1523 p9_trans_create_unix(const char *addr, char *args, int msize,
1524 unsigned char dotu)
1526 int err;
1527 struct socket *csocket;
1528 struct sockaddr_un sun_server;
1529 struct p9_trans *trans;
1530 struct p9_trans_fd *p;
1532 csocket = NULL;
1533 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1534 if (!trans)
1535 return ERR_PTR(-ENOMEM);
1537 trans->rpc = p9_fd_rpc;
1538 trans->close = p9_fd_close;
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;
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;
1559 err = p9_socket_open(trans, csocket);
1560 if (err < 0)
1561 goto error;
1563 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;
1573 return trans;
1575 error:
1576 if (csocket)
1577 sock_release(csocket);
1579 kfree(trans);
1580 return ERR_PTR(err);
1583 static struct p9_trans *
1584 p9_trans_create_fd(const char *name, char *args, int msize,
1585 unsigned char extended)
1587 int err;
1588 struct p9_trans *trans;
1589 struct p9_fd_opts opts;
1590 struct p9_trans_fd *p;
1592 parse_opts(args, &opts);
1594 if (opts.rfd == ~0 || opts.wfd == ~0) {
1595 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
1596 return ERR_PTR(-ENOPROTOOPT);
1599 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1600 if (!trans)
1601 return ERR_PTR(-ENOMEM);
1603 trans->rpc = p9_fd_rpc;
1604 trans->close = p9_fd_close;
1606 err = p9_fd_open(trans, opts.rfd, opts.wfd);
1607 if (err < 0)
1608 goto error;
1610 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;
1620 return trans;
1622 error:
1623 kfree(trans);
1624 return ERR_PTR(err);
1627 static struct p9_trans_module p9_tcp_trans = {
1628 .name = "tcp",
1629 .maxsize = MAX_SOCK_BUF,
1630 .def = 1,
1631 .create = p9_trans_create_tcp,
1634 static struct p9_trans_module p9_unix_trans = {
1635 .name = "unix",
1636 .maxsize = MAX_SOCK_BUF,
1637 .def = 0,
1638 .create = p9_trans_create_unix,
1641 static struct p9_trans_module p9_fd_trans = {
1642 .name = "fd",
1643 .maxsize = MAX_SOCK_BUF,
1644 .def = 0,
1645 .create = p9_trans_create_fd,
1648 int p9_trans_fd_init(void)
1650 int ret = p9_mux_global_init();
1651 if (ret) {
1652 printk(KERN_WARNING "9p: starting mux failed\n");
1653 return ret;
1656 v9fs_register_trans(&p9_tcp_trans);
1657 v9fs_register_trans(&p9_unix_trans);
1658 v9fs_register_trans(&p9_fd_trans);
1660 return 0;
1662 EXPORT_SYMBOL(p9_trans_fd_init);