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