Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-2.6
[linux-2.6/zen-sources.git] / net / 9p / trans_fd.c
blobf624dff76852f1881d266320f8ea1362c9a8eb1d
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 struct p9_fd_opts {
51 int rfd;
52 int wfd;
53 u16 port;
56 struct p9_trans_fd {
57 struct file *rd;
58 struct file *wr;
59 struct p9_conn *conn;
63 * Option Parsing (code inspired by NFS code)
64 * - a little lazy - parse all fd-transport options
67 enum {
68 /* Options that take integer arguments */
69 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
72 static match_table_t tokens = {
73 {Opt_port, "port=%u"},
74 {Opt_rfdno, "rfdno=%u"},
75 {Opt_wfdno, "wfdno=%u"},
76 {Opt_err, NULL},
79 enum {
80 Rworksched = 1, /* read work scheduled or running */
81 Rpending = 2, /* can read */
82 Wworksched = 4, /* write work scheduled or running */
83 Wpending = 8, /* can write */
86 enum {
87 None,
88 Flushing,
89 Flushed,
92 struct p9_req;
94 typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
95 struct p9_req {
96 spinlock_t lock; /* protect request structure */
97 int tag;
98 struct p9_fcall *tcall;
99 struct p9_fcall *rcall;
100 int err;
101 p9_conn_req_callback cb;
102 void *cba;
103 int flush;
104 struct list_head req_list;
107 struct p9_mux_poll_task;
109 struct p9_conn {
110 spinlock_t lock; /* protect lock structure */
111 struct list_head mux_list;
112 struct p9_mux_poll_task *poll_task;
113 int msize;
114 unsigned char extended;
115 struct p9_trans *trans;
116 struct p9_idpool *tagpool;
117 int err;
118 wait_queue_head_t equeue;
119 struct list_head req_list;
120 struct list_head unsent_req_list;
121 struct p9_fcall *rcall;
122 int rpos;
123 char *rbuf;
124 int wpos;
125 int wsize;
126 char *wbuf;
127 wait_queue_t poll_wait[MAXPOLLWADDR];
128 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
129 poll_table pt;
130 struct work_struct rq;
131 struct work_struct wq;
132 unsigned long wsched;
135 struct p9_mux_poll_task {
136 struct task_struct *task;
137 struct list_head mux_list;
138 int muxnum;
141 struct p9_mux_rpc {
142 struct p9_conn *m;
143 int err;
144 struct p9_fcall *tcall;
145 struct p9_fcall *rcall;
146 wait_queue_head_t wqueue;
149 static int p9_poll_proc(void *);
150 static void p9_read_work(struct work_struct *work);
151 static void p9_write_work(struct work_struct *work);
152 static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
153 poll_table *p);
154 static int p9_fd_write(struct p9_trans *trans, void *v, int len);
155 static int p9_fd_read(struct p9_trans *trans, void *v, int len);
157 static DEFINE_MUTEX(p9_mux_task_lock);
158 static struct workqueue_struct *p9_mux_wq;
160 static int p9_mux_num;
161 static int p9_mux_poll_task_num;
162 static struct p9_mux_poll_task p9_mux_poll_tasks[100];
164 static void p9_conn_destroy(struct p9_conn *);
165 static unsigned int p9_fd_poll(struct p9_trans *trans,
166 struct poll_table_struct *pt);
168 #ifdef P9_NONBLOCK
169 static int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
170 p9_conn_req_callback cb, void *a);
171 #endif /* P9_NONBLOCK */
173 static void p9_conn_cancel(struct p9_conn *m, int err);
175 static int p9_mux_global_init(void)
177 int i;
179 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++)
180 p9_mux_poll_tasks[i].task = NULL;
182 p9_mux_wq = create_workqueue("v9fs");
183 if (!p9_mux_wq) {
184 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
185 return -ENOMEM;
188 return 0;
191 static u16 p9_mux_get_tag(struct p9_conn *m)
193 int tag;
195 tag = p9_idpool_get(m->tagpool);
196 if (tag < 0)
197 return P9_NOTAG;
198 else
199 return (u16) tag;
202 static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
204 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
205 p9_idpool_put(tag, m->tagpool);
209 * p9_mux_calc_poll_procs - calculates the number of polling procs
210 * based on the number of mounted v9fs filesystems.
212 * The current implementation returns sqrt of the number of mounts.
214 static int p9_mux_calc_poll_procs(int muxnum)
216 int n;
218 if (p9_mux_poll_task_num)
219 n = muxnum / p9_mux_poll_task_num +
220 (muxnum % p9_mux_poll_task_num ? 1 : 0);
221 else
222 n = 1;
224 if (n > ARRAY_SIZE(p9_mux_poll_tasks))
225 n = ARRAY_SIZE(p9_mux_poll_tasks);
227 return n;
230 static int p9_mux_poll_start(struct p9_conn *m)
232 int i, n;
233 struct p9_mux_poll_task *vpt, *vptlast;
234 struct task_struct *pproc;
236 P9_DPRINTK(P9_DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, p9_mux_num,
237 p9_mux_poll_task_num);
238 mutex_lock(&p9_mux_task_lock);
240 n = p9_mux_calc_poll_procs(p9_mux_num + 1);
241 if (n > p9_mux_poll_task_num) {
242 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
243 if (p9_mux_poll_tasks[i].task == NULL) {
244 vpt = &p9_mux_poll_tasks[i];
245 P9_DPRINTK(P9_DEBUG_MUX, "create proc %p\n",
246 vpt);
247 pproc = kthread_create(p9_poll_proc, vpt,
248 "v9fs-poll");
250 if (!IS_ERR(pproc)) {
251 vpt->task = pproc;
252 INIT_LIST_HEAD(&vpt->mux_list);
253 vpt->muxnum = 0;
254 p9_mux_poll_task_num++;
255 wake_up_process(vpt->task);
257 break;
261 if (i >= ARRAY_SIZE(p9_mux_poll_tasks))
262 P9_DPRINTK(P9_DEBUG_ERROR,
263 "warning: no free poll slots\n");
266 n = (p9_mux_num + 1) / p9_mux_poll_task_num +
267 ((p9_mux_num + 1) % p9_mux_poll_task_num ? 1 : 0);
269 vptlast = NULL;
270 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
271 vpt = &p9_mux_poll_tasks[i];
272 if (vpt->task != NULL) {
273 vptlast = vpt;
274 if (vpt->muxnum < n) {
275 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
276 list_add(&m->mux_list, &vpt->mux_list);
277 vpt->muxnum++;
278 m->poll_task = vpt;
279 memset(&m->poll_waddr, 0,
280 sizeof(m->poll_waddr));
281 init_poll_funcptr(&m->pt, p9_pollwait);
282 break;
287 if (i >= ARRAY_SIZE(p9_mux_poll_tasks)) {
288 if (vptlast == NULL) {
289 mutex_unlock(&p9_mux_task_lock);
290 return -ENOMEM;
293 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
294 list_add(&m->mux_list, &vptlast->mux_list);
295 vptlast->muxnum++;
296 m->poll_task = vptlast;
297 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
298 init_poll_funcptr(&m->pt, p9_pollwait);
301 p9_mux_num++;
302 mutex_unlock(&p9_mux_task_lock);
304 return 0;
307 static void p9_mux_poll_stop(struct p9_conn *m)
309 int i;
310 struct p9_mux_poll_task *vpt;
312 mutex_lock(&p9_mux_task_lock);
313 vpt = m->poll_task;
314 list_del(&m->mux_list);
315 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
316 if (m->poll_waddr[i] != NULL) {
317 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
318 m->poll_waddr[i] = NULL;
321 vpt->muxnum--;
322 if (!vpt->muxnum) {
323 P9_DPRINTK(P9_DEBUG_MUX, "destroy proc %p\n", vpt);
324 kthread_stop(vpt->task);
325 vpt->task = NULL;
326 p9_mux_poll_task_num--;
328 p9_mux_num--;
329 mutex_unlock(&p9_mux_task_lock);
333 * p9_conn_create - allocate and initialize the per-session mux data
334 * Creates the polling task if this is the first session.
336 * @trans - transport structure
337 * @msize - maximum message size
338 * @extended - extended flag
340 static struct p9_conn *p9_conn_create(struct p9_trans *trans)
342 int i, n;
343 struct p9_conn *m, *mtmp;
345 P9_DPRINTK(P9_DEBUG_MUX, "transport %p msize %d\n", trans,
346 trans->msize);
347 m = kmalloc(sizeof(struct p9_conn), GFP_KERNEL);
348 if (!m)
349 return ERR_PTR(-ENOMEM);
351 spin_lock_init(&m->lock);
352 INIT_LIST_HEAD(&m->mux_list);
353 m->msize = trans->msize;
354 m->extended = trans->extended;
355 m->trans = trans;
356 m->tagpool = p9_idpool_create();
357 if (IS_ERR(m->tagpool)) {
358 mtmp = ERR_PTR(-ENOMEM);
359 kfree(m);
360 return mtmp;
363 m->err = 0;
364 init_waitqueue_head(&m->equeue);
365 INIT_LIST_HEAD(&m->req_list);
366 INIT_LIST_HEAD(&m->unsent_req_list);
367 m->rcall = NULL;
368 m->rpos = 0;
369 m->rbuf = NULL;
370 m->wpos = m->wsize = 0;
371 m->wbuf = NULL;
372 INIT_WORK(&m->rq, p9_read_work);
373 INIT_WORK(&m->wq, p9_write_work);
374 m->wsched = 0;
375 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
376 m->poll_task = NULL;
377 n = p9_mux_poll_start(m);
378 if (n) {
379 kfree(m);
380 return ERR_PTR(n);
383 n = p9_fd_poll(trans, &m->pt);
384 if (n & POLLIN) {
385 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
386 set_bit(Rpending, &m->wsched);
389 if (n & POLLOUT) {
390 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
391 set_bit(Wpending, &m->wsched);
394 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
395 if (IS_ERR(m->poll_waddr[i])) {
396 p9_mux_poll_stop(m);
397 mtmp = (void *)m->poll_waddr; /* the error code */
398 kfree(m);
399 m = mtmp;
400 break;
404 return m;
408 * p9_mux_destroy - cancels all pending requests and frees mux resources
410 static void p9_conn_destroy(struct p9_conn *m)
412 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
413 m->mux_list.prev, m->mux_list.next);
414 p9_conn_cancel(m, -ECONNRESET);
416 if (!list_empty(&m->req_list)) {
417 /* wait until all processes waiting on this session exit */
418 P9_DPRINTK(P9_DEBUG_MUX,
419 "mux %p waiting for empty request queue\n", m);
420 wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000);
421 P9_DPRINTK(P9_DEBUG_MUX, "mux %p request queue empty: %d\n", m,
422 list_empty(&m->req_list));
425 p9_mux_poll_stop(m);
426 m->trans = NULL;
427 p9_idpool_destroy(m->tagpool);
428 kfree(m);
432 * p9_pollwait - called by files poll operation to add v9fs-poll task
433 * to files wait queue
435 static void
436 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
438 int i;
439 struct p9_conn *m;
441 m = container_of(p, struct p9_conn, pt);
442 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
443 if (m->poll_waddr[i] == NULL)
444 break;
446 if (i >= ARRAY_SIZE(m->poll_waddr)) {
447 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
448 return;
451 m->poll_waddr[i] = wait_address;
453 if (!wait_address) {
454 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
455 m->poll_waddr[i] = ERR_PTR(-EIO);
456 return;
459 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
460 add_wait_queue(wait_address, &m->poll_wait[i]);
464 * p9_poll_mux - polls a mux and schedules read or write works if necessary
466 static void p9_poll_mux(struct p9_conn *m)
468 int n;
470 if (m->err < 0)
471 return;
473 n = p9_fd_poll(m->trans, NULL);
474 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
475 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
476 if (n >= 0)
477 n = -ECONNRESET;
478 p9_conn_cancel(m, n);
481 if (n & POLLIN) {
482 set_bit(Rpending, &m->wsched);
483 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
484 if (!test_and_set_bit(Rworksched, &m->wsched)) {
485 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
486 queue_work(p9_mux_wq, &m->rq);
490 if (n & POLLOUT) {
491 set_bit(Wpending, &m->wsched);
492 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
493 if ((m->wsize || !list_empty(&m->unsent_req_list))
494 && !test_and_set_bit(Wworksched, &m->wsched)) {
495 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
496 queue_work(p9_mux_wq, &m->wq);
502 * p9_poll_proc - polls all v9fs transports for new events and queues
503 * the appropriate work to the work queue
505 static int p9_poll_proc(void *a)
507 struct p9_conn *m, *mtmp;
508 struct p9_mux_poll_task *vpt;
510 vpt = a;
511 P9_DPRINTK(P9_DEBUG_MUX, "start %p %p\n", current, vpt);
512 while (!kthread_should_stop()) {
513 set_current_state(TASK_INTERRUPTIBLE);
515 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
516 p9_poll_mux(m);
519 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
520 schedule_timeout(SCHED_TIMEOUT * HZ);
523 __set_current_state(TASK_RUNNING);
524 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
525 return 0;
529 * p9_write_work - called when a transport can send some data
531 static void p9_write_work(struct work_struct *work)
533 int n, err;
534 struct p9_conn *m;
535 struct p9_req *req;
537 m = container_of(work, struct p9_conn, wq);
539 if (m->err < 0) {
540 clear_bit(Wworksched, &m->wsched);
541 return;
544 if (!m->wsize) {
545 if (list_empty(&m->unsent_req_list)) {
546 clear_bit(Wworksched, &m->wsched);
547 return;
550 spin_lock(&m->lock);
551 again:
552 req = list_entry(m->unsent_req_list.next, struct p9_req,
553 req_list);
554 list_move_tail(&req->req_list, &m->req_list);
555 if (req->err == ERREQFLUSH)
556 goto again;
558 m->wbuf = req->tcall->sdata;
559 m->wsize = req->tcall->size;
560 m->wpos = 0;
561 spin_unlock(&m->lock);
564 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
565 m->wsize);
566 clear_bit(Wpending, &m->wsched);
567 err = p9_fd_write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
568 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
569 if (err == -EAGAIN) {
570 clear_bit(Wworksched, &m->wsched);
571 return;
574 if (err < 0)
575 goto error;
576 else if (err == 0) {
577 err = -EREMOTEIO;
578 goto error;
581 m->wpos += err;
582 if (m->wpos == m->wsize)
583 m->wpos = m->wsize = 0;
585 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
586 if (test_and_clear_bit(Wpending, &m->wsched))
587 n = POLLOUT;
588 else
589 n = p9_fd_poll(m->trans, NULL);
591 if (n & POLLOUT) {
592 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
593 queue_work(p9_mux_wq, &m->wq);
594 } else
595 clear_bit(Wworksched, &m->wsched);
596 } else
597 clear_bit(Wworksched, &m->wsched);
599 return;
601 error:
602 p9_conn_cancel(m, err);
603 clear_bit(Wworksched, &m->wsched);
606 static void process_request(struct p9_conn *m, struct p9_req *req)
608 int ecode;
609 struct p9_str *ename;
611 if (!req->err && req->rcall->id == P9_RERROR) {
612 ecode = req->rcall->params.rerror.errno;
613 ename = &req->rcall->params.rerror.error;
615 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
616 ename->str);
618 if (m->extended)
619 req->err = -ecode;
621 if (!req->err) {
622 req->err = p9_errstr2errno(ename->str, ename->len);
624 /* string match failed */
625 if (!req->err) {
626 PRINT_FCALL_ERROR("unknown error", req->rcall);
627 req->err = -ESERVERFAULT;
630 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
631 P9_DPRINTK(P9_DEBUG_ERROR,
632 "fcall mismatch: expected %d, got %d\n",
633 req->tcall->id + 1, req->rcall->id);
634 if (!req->err)
635 req->err = -EIO;
640 * p9_read_work - called when there is some data to be read from a transport
642 static void p9_read_work(struct work_struct *work)
644 int n, err;
645 struct p9_conn *m;
646 struct p9_req *req, *rptr, *rreq;
647 struct p9_fcall *rcall;
648 char *rbuf;
650 m = container_of(work, struct p9_conn, rq);
652 if (m->err < 0)
653 return;
655 rcall = NULL;
656 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
658 if (!m->rcall) {
659 m->rcall =
660 kmalloc(sizeof(struct p9_fcall) + m->msize, GFP_KERNEL);
661 if (!m->rcall) {
662 err = -ENOMEM;
663 goto error;
666 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
667 m->rpos = 0;
670 clear_bit(Rpending, &m->wsched);
671 err = p9_fd_read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
672 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
673 if (err == -EAGAIN) {
674 clear_bit(Rworksched, &m->wsched);
675 return;
678 if (err <= 0)
679 goto error;
681 m->rpos += err;
682 while (m->rpos > 4) {
683 n = le32_to_cpu(*(__le32 *) m->rbuf);
684 if (n >= m->msize) {
685 P9_DPRINTK(P9_DEBUG_ERROR,
686 "requested packet size too big: %d\n", n);
687 err = -EIO;
688 goto error;
691 if (m->rpos < n)
692 break;
694 err =
695 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->extended);
696 if (err < 0)
697 goto error;
699 #ifdef CONFIG_NET_9P_DEBUG
700 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
701 char buf[150];
703 p9_printfcall(buf, sizeof(buf), m->rcall,
704 m->extended);
705 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
707 #endif
709 rcall = m->rcall;
710 rbuf = m->rbuf;
711 if (m->rpos > n) {
712 m->rcall = kmalloc(sizeof(struct p9_fcall) + m->msize,
713 GFP_KERNEL);
714 if (!m->rcall) {
715 err = -ENOMEM;
716 goto error;
719 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
720 memmove(m->rbuf, rbuf + n, m->rpos - n);
721 m->rpos -= n;
722 } else {
723 m->rcall = NULL;
724 m->rbuf = NULL;
725 m->rpos = 0;
728 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
729 rcall->id, rcall->tag);
731 req = NULL;
732 spin_lock(&m->lock);
733 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
734 if (rreq->tag == rcall->tag) {
735 req = rreq;
736 if (req->flush != Flushing)
737 list_del(&req->req_list);
738 break;
741 spin_unlock(&m->lock);
743 if (req) {
744 req->rcall = rcall;
745 process_request(m, req);
747 if (req->flush != Flushing) {
748 if (req->cb)
749 (*req->cb) (req, req->cba);
750 else
751 kfree(req->rcall);
753 wake_up(&m->equeue);
755 } else {
756 if (err >= 0 && rcall->id != P9_RFLUSH)
757 P9_DPRINTK(P9_DEBUG_ERROR,
758 "unexpected response mux %p id %d tag %d\n",
759 m, rcall->id, rcall->tag);
760 kfree(rcall);
764 if (!list_empty(&m->req_list)) {
765 if (test_and_clear_bit(Rpending, &m->wsched))
766 n = POLLIN;
767 else
768 n = p9_fd_poll(m->trans, NULL);
770 if (n & POLLIN) {
771 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
772 queue_work(p9_mux_wq, &m->rq);
773 } else
774 clear_bit(Rworksched, &m->wsched);
775 } else
776 clear_bit(Rworksched, &m->wsched);
778 return;
780 error:
781 p9_conn_cancel(m, err);
782 clear_bit(Rworksched, &m->wsched);
786 * p9_send_request - send 9P request
787 * The function can sleep until the request is scheduled for sending.
788 * The function can be interrupted. Return from the function is not
789 * a guarantee that the request is sent successfully. Can return errors
790 * that can be retrieved by PTR_ERR macros.
792 * @m: mux data
793 * @tc: request to be sent
794 * @cb: callback function to call when response is received
795 * @cba: parameter to pass to the callback function
797 static struct p9_req *p9_send_request(struct p9_conn *m,
798 struct p9_fcall *tc,
799 p9_conn_req_callback cb, void *cba)
801 int n;
802 struct p9_req *req;
804 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
805 tc, tc->id);
806 if (m->err < 0)
807 return ERR_PTR(m->err);
809 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
810 if (!req)
811 return ERR_PTR(-ENOMEM);
813 if (tc->id == P9_TVERSION)
814 n = P9_NOTAG;
815 else
816 n = p9_mux_get_tag(m);
818 if (n < 0)
819 return ERR_PTR(-ENOMEM);
821 p9_set_tag(tc, n);
823 #ifdef CONFIG_NET_9P_DEBUG
824 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
825 char buf[150];
827 p9_printfcall(buf, sizeof(buf), tc, m->extended);
828 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
830 #endif
832 spin_lock_init(&req->lock);
833 req->tag = n;
834 req->tcall = tc;
835 req->rcall = NULL;
836 req->err = 0;
837 req->cb = cb;
838 req->cba = cba;
839 req->flush = None;
841 spin_lock(&m->lock);
842 list_add_tail(&req->req_list, &m->unsent_req_list);
843 spin_unlock(&m->lock);
845 if (test_and_clear_bit(Wpending, &m->wsched))
846 n = POLLOUT;
847 else
848 n = p9_fd_poll(m->trans, NULL);
850 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
851 queue_work(p9_mux_wq, &m->wq);
853 return req;
856 static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
858 p9_mux_put_tag(m, req->tag);
859 kfree(req);
862 static void p9_mux_flush_cb(struct p9_req *freq, void *a)
864 int tag;
865 struct p9_conn *m;
866 struct p9_req *req, *rreq, *rptr;
868 m = a;
869 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
870 freq->tcall, freq->rcall, freq->err,
871 freq->tcall->params.tflush.oldtag);
873 spin_lock(&m->lock);
874 tag = freq->tcall->params.tflush.oldtag;
875 req = NULL;
876 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
877 if (rreq->tag == tag) {
878 req = rreq;
879 list_del(&req->req_list);
880 break;
883 spin_unlock(&m->lock);
885 if (req) {
886 spin_lock(&req->lock);
887 req->flush = Flushed;
888 spin_unlock(&req->lock);
890 if (req->cb)
891 (*req->cb) (req, req->cba);
892 else
893 kfree(req->rcall);
895 wake_up(&m->equeue);
898 kfree(freq->tcall);
899 kfree(freq->rcall);
900 p9_mux_free_request(m, freq);
903 static int
904 p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
906 struct p9_fcall *fc;
907 struct p9_req *rreq, *rptr;
909 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
911 /* if a response was received for a request, do nothing */
912 spin_lock(&req->lock);
913 if (req->rcall || req->err) {
914 spin_unlock(&req->lock);
915 P9_DPRINTK(P9_DEBUG_MUX,
916 "mux %p req %p response already received\n", m, req);
917 return 0;
920 req->flush = Flushing;
921 spin_unlock(&req->lock);
923 spin_lock(&m->lock);
924 /* if the request is not sent yet, just remove it from the list */
925 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
926 if (rreq->tag == req->tag) {
927 P9_DPRINTK(P9_DEBUG_MUX,
928 "mux %p req %p request is not sent yet\n", m, req);
929 list_del(&rreq->req_list);
930 req->flush = Flushed;
931 spin_unlock(&m->lock);
932 if (req->cb)
933 (*req->cb) (req, req->cba);
934 return 0;
937 spin_unlock(&m->lock);
939 clear_thread_flag(TIF_SIGPENDING);
940 fc = p9_create_tflush(req->tag);
941 p9_send_request(m, fc, p9_mux_flush_cb, m);
942 return 1;
945 static void
946 p9_conn_rpc_cb(struct p9_req *req, void *a)
948 struct p9_mux_rpc *r;
950 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
951 r = a;
952 r->rcall = req->rcall;
953 r->err = req->err;
955 if (req->flush != None && !req->err)
956 r->err = -ERESTARTSYS;
958 wake_up(&r->wqueue);
962 * p9_fd_rpc- sends 9P request and waits until a response is available.
963 * The function can be interrupted.
964 * @m: mux data
965 * @tc: request to be sent
966 * @rc: pointer where a pointer to the response is stored
969 p9_fd_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
971 struct p9_trans_fd *p = t->priv;
972 struct p9_conn *m = p->conn;
973 int err, sigpending;
974 unsigned long flags;
975 struct p9_req *req;
976 struct p9_mux_rpc r;
978 r.err = 0;
979 r.tcall = tc;
980 r.rcall = NULL;
981 r.m = m;
982 init_waitqueue_head(&r.wqueue);
984 if (rc)
985 *rc = NULL;
987 sigpending = 0;
988 if (signal_pending(current)) {
989 sigpending = 1;
990 clear_thread_flag(TIF_SIGPENDING);
993 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
994 if (IS_ERR(req)) {
995 err = PTR_ERR(req);
996 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
997 return err;
1000 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
1001 if (r.err < 0)
1002 err = r.err;
1004 if (err == -ERESTARTSYS && m->trans->status == Connected
1005 && m->err == 0) {
1006 if (p9_mux_flush_request(m, req)) {
1007 /* wait until we get response of the flush message */
1008 do {
1009 clear_thread_flag(TIF_SIGPENDING);
1010 err = wait_event_interruptible(r.wqueue,
1011 r.rcall || r.err);
1012 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
1013 m->trans->status == Connected && !m->err);
1015 err = -ERESTARTSYS;
1017 sigpending = 1;
1020 if (sigpending) {
1021 spin_lock_irqsave(&current->sighand->siglock, flags);
1022 recalc_sigpending();
1023 spin_unlock_irqrestore(&current->sighand->siglock, flags);
1026 if (rc)
1027 *rc = r.rcall;
1028 else
1029 kfree(r.rcall);
1031 p9_mux_free_request(m, req);
1032 if (err > 0)
1033 err = -EIO;
1035 return err;
1038 #ifdef P9_NONBLOCK
1040 * p9_conn_rpcnb - sends 9P request without waiting for response.
1041 * @m: mux data
1042 * @tc: request to be sent
1043 * @cb: callback function to be called when response arrives
1044 * @cba: value to pass to the callback function
1046 int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1047 p9_conn_req_callback cb, void *a)
1049 int err;
1050 struct p9_req *req;
1052 req = p9_send_request(m, tc, cb, a);
1053 if (IS_ERR(req)) {
1054 err = PTR_ERR(req);
1055 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1056 return PTR_ERR(req);
1059 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1060 return 0;
1062 #endif /* P9_NONBLOCK */
1065 * p9_conn_cancel - cancel all pending requests with error
1066 * @m: mux data
1067 * @err: error code
1069 void p9_conn_cancel(struct p9_conn *m, int err)
1071 struct p9_req *req, *rtmp;
1072 LIST_HEAD(cancel_list);
1074 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1075 m->err = err;
1076 spin_lock(&m->lock);
1077 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1078 list_move(&req->req_list, &cancel_list);
1080 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1081 list_move(&req->req_list, &cancel_list);
1083 spin_unlock(&m->lock);
1085 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1086 list_del(&req->req_list);
1087 if (!req->err)
1088 req->err = err;
1090 if (req->cb)
1091 (*req->cb) (req, req->cba);
1092 else
1093 kfree(req->rcall);
1096 wake_up(&m->equeue);
1100 * v9fs_parse_options - parse mount options into session structure
1101 * @options: options string passed from mount
1102 * @v9ses: existing v9fs session information
1106 static void parse_opts(char *options, struct p9_fd_opts *opts)
1108 char *p;
1109 substring_t args[MAX_OPT_ARGS];
1110 int option;
1111 int ret;
1113 opts->port = P9_PORT;
1114 opts->rfd = ~0;
1115 opts->wfd = ~0;
1117 if (!options)
1118 return;
1120 while ((p = strsep(&options, ",")) != NULL) {
1121 int token;
1122 if (!*p)
1123 continue;
1124 token = match_token(p, tokens, args);
1125 ret = match_int(&args[0], &option);
1126 if (ret < 0) {
1127 P9_DPRINTK(P9_DEBUG_ERROR,
1128 "integer field, but no integer?\n");
1129 continue;
1131 switch (token) {
1132 case Opt_port:
1133 opts->port = option;
1134 break;
1135 case Opt_rfdno:
1136 opts->rfd = option;
1137 break;
1138 case Opt_wfdno:
1139 opts->wfd = option;
1140 break;
1141 default:
1142 continue;
1147 static int p9_fd_open(struct p9_trans *trans, int rfd, int wfd)
1149 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1150 GFP_KERNEL);
1151 if (!ts)
1152 return -ENOMEM;
1154 ts->rd = fget(rfd);
1155 ts->wr = fget(wfd);
1156 if (!ts->rd || !ts->wr) {
1157 if (ts->rd)
1158 fput(ts->rd);
1159 if (ts->wr)
1160 fput(ts->wr);
1161 kfree(ts);
1162 return -EIO;
1165 trans->priv = ts;
1166 trans->status = Connected;
1168 return 0;
1171 static int p9_socket_open(struct p9_trans *trans, struct socket *csocket)
1173 int fd, ret;
1175 csocket->sk->sk_allocation = GFP_NOIO;
1176 fd = sock_map_fd(csocket);
1177 if (fd < 0) {
1178 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1179 return fd;
1182 ret = p9_fd_open(trans, fd, fd);
1183 if (ret < 0) {
1184 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1185 sockfd_put(csocket);
1186 return ret;
1189 ((struct p9_trans_fd *)trans->priv)->rd->f_flags |= O_NONBLOCK;
1191 return 0;
1195 * p9_fd_read- read from a fd
1196 * @v9ses: session information
1197 * @v: buffer to receive data into
1198 * @len: size of receive buffer
1201 static int p9_fd_read(struct p9_trans *trans, void *v, int len)
1203 int ret;
1204 struct p9_trans_fd *ts = NULL;
1206 if (trans && trans->status != Disconnected)
1207 ts = trans->priv;
1209 if (!ts)
1210 return -EREMOTEIO;
1212 if (!(ts->rd->f_flags & O_NONBLOCK))
1213 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
1215 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
1216 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1217 trans->status = Disconnected;
1218 return ret;
1222 * p9_fd_write - write to a socket
1223 * @v9ses: session information
1224 * @v: buffer to send data from
1225 * @len: size of send buffer
1228 static int p9_fd_write(struct p9_trans *trans, void *v, int len)
1230 int ret;
1231 mm_segment_t oldfs;
1232 struct p9_trans_fd *ts = NULL;
1234 if (trans && trans->status != Disconnected)
1235 ts = trans->priv;
1237 if (!ts)
1238 return -EREMOTEIO;
1240 if (!(ts->wr->f_flags & O_NONBLOCK))
1241 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
1243 oldfs = get_fs();
1244 set_fs(get_ds());
1245 /* The cast to a user pointer is valid due to the set_fs() */
1246 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
1247 set_fs(oldfs);
1249 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1250 trans->status = Disconnected;
1251 return ret;
1254 static unsigned int
1255 p9_fd_poll(struct p9_trans *trans, struct poll_table_struct *pt)
1257 int ret, n;
1258 struct p9_trans_fd *ts = NULL;
1259 mm_segment_t oldfs;
1261 if (trans && trans->status == Connected)
1262 ts = trans->priv;
1264 if (!ts)
1265 return -EREMOTEIO;
1267 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1268 return -EIO;
1270 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1271 return -EIO;
1273 oldfs = get_fs();
1274 set_fs(get_ds());
1276 ret = ts->rd->f_op->poll(ts->rd, pt);
1277 if (ret < 0)
1278 goto end;
1280 if (ts->rd != ts->wr) {
1281 n = ts->wr->f_op->poll(ts->wr, pt);
1282 if (n < 0) {
1283 ret = n;
1284 goto end;
1286 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1289 end:
1290 set_fs(oldfs);
1291 return ret;
1295 * p9_fd_close - shutdown socket
1296 * @trans: private socket structure
1299 static void p9_fd_close(struct p9_trans *trans)
1301 struct p9_trans_fd *ts;
1303 if (!trans)
1304 return;
1306 ts = xchg(&trans->priv, NULL);
1308 if (!ts)
1309 return;
1311 p9_conn_destroy(ts->conn);
1313 trans->status = Disconnected;
1314 if (ts->rd)
1315 fput(ts->rd);
1316 if (ts->wr)
1317 fput(ts->wr);
1318 kfree(ts);
1321 static struct p9_trans *
1322 p9_trans_create_tcp(const char *addr, char *args, int msize, unsigned char dotu)
1324 int err;
1325 struct p9_trans *trans;
1326 struct socket *csocket;
1327 struct sockaddr_in sin_server;
1328 struct p9_fd_opts opts;
1329 struct p9_trans_fd *p;
1331 parse_opts(args, &opts);
1333 csocket = NULL;
1334 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1335 if (!trans)
1336 return ERR_PTR(-ENOMEM);
1337 trans->msize = msize;
1338 trans->extended = dotu;
1339 trans->rpc = p9_fd_rpc;
1340 trans->close = p9_fd_close;
1342 sin_server.sin_family = AF_INET;
1343 sin_server.sin_addr.s_addr = in_aton(addr);
1344 sin_server.sin_port = htons(opts.port);
1345 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1347 if (!csocket) {
1348 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1349 err = -EIO;
1350 goto error;
1353 err = csocket->ops->connect(csocket,
1354 (struct sockaddr *)&sin_server,
1355 sizeof(struct sockaddr_in), 0);
1356 if (err < 0) {
1357 P9_EPRINTK(KERN_ERR,
1358 "p9_trans_tcp: problem connecting socket to %s\n",
1359 addr);
1360 goto error;
1363 err = p9_socket_open(trans, csocket);
1364 if (err < 0)
1365 goto error;
1367 p = (struct p9_trans_fd *) trans->priv;
1368 p->conn = p9_conn_create(trans);
1369 if (IS_ERR(p->conn)) {
1370 err = PTR_ERR(p->conn);
1371 p->conn = NULL;
1372 goto error;
1375 return trans;
1377 error:
1378 if (csocket)
1379 sock_release(csocket);
1381 kfree(trans);
1382 return ERR_PTR(err);
1385 static struct p9_trans *
1386 p9_trans_create_unix(const char *addr, char *args, int msize,
1387 unsigned char dotu)
1389 int err;
1390 struct socket *csocket;
1391 struct sockaddr_un sun_server;
1392 struct p9_trans *trans;
1393 struct p9_trans_fd *p;
1395 csocket = NULL;
1396 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1397 if (!trans)
1398 return ERR_PTR(-ENOMEM);
1400 trans->rpc = p9_fd_rpc;
1401 trans->close = p9_fd_close;
1403 if (strlen(addr) > UNIX_PATH_MAX) {
1404 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1405 addr);
1406 err = -ENAMETOOLONG;
1407 goto error;
1410 sun_server.sun_family = PF_UNIX;
1411 strcpy(sun_server.sun_path, addr);
1412 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1413 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1414 sizeof(struct sockaddr_un) - 1, 0);
1415 if (err < 0) {
1416 P9_EPRINTK(KERN_ERR,
1417 "p9_trans_unix: problem connecting socket: %s: %d\n",
1418 addr, err);
1419 goto error;
1422 err = p9_socket_open(trans, csocket);
1423 if (err < 0)
1424 goto error;
1426 trans->msize = msize;
1427 trans->extended = dotu;
1428 p = (struct p9_trans_fd *) trans->priv;
1429 p->conn = p9_conn_create(trans);
1430 if (IS_ERR(p->conn)) {
1431 err = PTR_ERR(p->conn);
1432 p->conn = NULL;
1433 goto error;
1436 return trans;
1438 error:
1439 if (csocket)
1440 sock_release(csocket);
1442 kfree(trans);
1443 return ERR_PTR(err);
1446 static struct p9_trans *
1447 p9_trans_create_fd(const char *name, char *args, int msize,
1448 unsigned char extended)
1450 int err;
1451 struct p9_trans *trans;
1452 struct p9_fd_opts opts;
1453 struct p9_trans_fd *p;
1455 parse_opts(args, &opts);
1457 if (opts.rfd == ~0 || opts.wfd == ~0) {
1458 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
1459 return ERR_PTR(-ENOPROTOOPT);
1462 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1463 if (!trans)
1464 return ERR_PTR(-ENOMEM);
1466 trans->rpc = p9_fd_rpc;
1467 trans->close = p9_fd_close;
1469 err = p9_fd_open(trans, opts.rfd, opts.wfd);
1470 if (err < 0)
1471 goto error;
1473 trans->msize = msize;
1474 trans->extended = extended;
1475 p = (struct p9_trans_fd *) trans->priv;
1476 p->conn = p9_conn_create(trans);
1477 if (IS_ERR(p->conn)) {
1478 err = PTR_ERR(p->conn);
1479 p->conn = NULL;
1480 goto error;
1483 return trans;
1485 error:
1486 kfree(trans);
1487 return ERR_PTR(err);
1490 static struct p9_trans_module p9_tcp_trans = {
1491 .name = "tcp",
1492 .maxsize = MAX_SOCK_BUF,
1493 .def = 1,
1494 .create = p9_trans_create_tcp,
1497 static struct p9_trans_module p9_unix_trans = {
1498 .name = "unix",
1499 .maxsize = MAX_SOCK_BUF,
1500 .def = 0,
1501 .create = p9_trans_create_unix,
1504 static struct p9_trans_module p9_fd_trans = {
1505 .name = "fd",
1506 .maxsize = MAX_SOCK_BUF,
1507 .def = 0,
1508 .create = p9_trans_create_fd,
1511 static int __init p9_trans_fd_init(void)
1513 int ret = p9_mux_global_init();
1514 if (ret) {
1515 printk(KERN_WARNING "9p: starting mux failed\n");
1516 return ret;
1519 v9fs_register_trans(&p9_tcp_trans);
1520 v9fs_register_trans(&p9_unix_trans);
1521 v9fs_register_trans(&p9_fd_trans);
1523 return 0;
1526 module_init(p9_trans_fd_init);
1528 MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
1529 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
1530 MODULE_LICENSE("GPL");