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
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>
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>
45 #define MAX_SOCK_BUF (64*1024)
47 #define SCHED_TIMEOUT 10
48 #define MAXPOLLWADDR 2
63 * Option Parsing (code inspired by NFS code)
64 * - a little lazy - parse all fd-transport options
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"},
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 */
94 typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
96 spinlock_t lock; /* protect request structure */
98 struct p9_fcall *tcall;
99 struct p9_fcall *rcall;
101 p9_conn_req_callback cb;
104 struct list_head req_list;
107 struct p9_mux_poll_task;
110 spinlock_t lock; /* protect lock structure */
111 struct list_head mux_list;
112 struct p9_mux_poll_task *poll_task;
114 unsigned char extended;
115 struct p9_trans *trans;
116 struct p9_idpool *tagpool;
118 wait_queue_head_t equeue;
119 struct list_head req_list;
120 struct list_head unsent_req_list;
121 struct p9_fcall *rcall;
127 wait_queue_t poll_wait[MAXPOLLWADDR];
128 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
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;
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,
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);
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)
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");
184 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
191 static u16 p9_mux_get_tag(struct p9_conn *m)
195 tag = p9_idpool_get(m->tagpool);
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)
218 if (p9_mux_poll_task_num)
219 n = muxnum / p9_mux_poll_task_num +
220 (muxnum % p9_mux_poll_task_num ? 1 : 0);
224 if (n > ARRAY_SIZE(p9_mux_poll_tasks))
225 n = ARRAY_SIZE(p9_mux_poll_tasks);
230 static int p9_mux_poll_start(struct p9_conn *m)
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",
247 pproc = kthread_create(p9_poll_proc, vpt,
250 if (!IS_ERR(pproc)) {
252 INIT_LIST_HEAD(&vpt->mux_list);
254 p9_mux_poll_task_num++;
255 wake_up_process(vpt->task);
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);
270 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
271 vpt = &p9_mux_poll_tasks[i];
272 if (vpt->task != NULL) {
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);
279 memset(&m->poll_waddr, 0,
280 sizeof(m->poll_waddr));
281 init_poll_funcptr(&m->pt, p9_pollwait);
287 if (i >= ARRAY_SIZE(p9_mux_poll_tasks)) {
288 if (vptlast == NULL) {
289 mutex_unlock(&p9_mux_task_lock);
293 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
294 list_add(&m->mux_list, &vptlast->mux_list);
296 m->poll_task = vptlast;
297 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
298 init_poll_funcptr(&m->pt, p9_pollwait);
302 mutex_unlock(&p9_mux_task_lock);
307 static void p9_mux_poll_stop(struct p9_conn *m)
310 struct p9_mux_poll_task *vpt;
312 mutex_lock(&p9_mux_task_lock);
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;
323 P9_DPRINTK(P9_DEBUG_MUX, "destroy proc %p\n", vpt);
324 kthread_stop(vpt->task);
326 p9_mux_poll_task_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)
343 struct p9_conn *m, *mtmp;
345 P9_DPRINTK(P9_DEBUG_MUX, "transport %p msize %d\n", trans,
347 m = kmalloc(sizeof(struct p9_conn), GFP_KERNEL);
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;
356 m->tagpool = p9_idpool_create();
357 if (IS_ERR(m->tagpool)) {
358 mtmp = ERR_PTR(-ENOMEM);
364 init_waitqueue_head(&m->equeue);
365 INIT_LIST_HEAD(&m->req_list);
366 INIT_LIST_HEAD(&m->unsent_req_list);
370 m->wpos = m->wsize = 0;
372 INIT_WORK(&m->rq, p9_read_work);
373 INIT_WORK(&m->wq, p9_write_work);
375 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
377 n = p9_mux_poll_start(m);
383 n = p9_fd_poll(trans, &m->pt);
385 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
386 set_bit(Rpending, &m->wsched);
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])) {
397 mtmp = (void *)m->poll_waddr; /* the error code */
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));
427 p9_idpool_destroy(m->tagpool);
432 * p9_pollwait - called by files poll operation to add v9fs-poll task
433 * to files wait queue
436 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
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)
446 if (i >= ARRAY_SIZE(m->poll_waddr)) {
447 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
451 m->poll_waddr[i] = wait_address;
454 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
455 m->poll_waddr[i] = ERR_PTR(-EIO);
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)
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);
478 p9_conn_cancel(m, n);
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);
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;
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) {
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");
529 * p9_write_work - called when a transport can send some data
531 static void p9_write_work(struct work_struct *work)
537 m = container_of(work, struct p9_conn, wq);
540 clear_bit(Wworksched, &m->wsched);
545 if (list_empty(&m->unsent_req_list)) {
546 clear_bit(Wworksched, &m->wsched);
552 req = list_entry(m->unsent_req_list.next, struct p9_req,
554 list_move_tail(&req->req_list, &m->req_list);
555 if (req->err == ERREQFLUSH)
558 m->wbuf = req->tcall->sdata;
559 m->wsize = req->tcall->size;
561 spin_unlock(&m->lock);
564 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
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);
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))
589 n = p9_fd_poll(m->trans, NULL);
592 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
593 queue_work(p9_mux_wq, &m->wq);
595 clear_bit(Wworksched, &m->wsched);
597 clear_bit(Wworksched, &m->wsched);
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)
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,
622 req->err = p9_errstr2errno(ename->str, ename->len);
624 /* string match failed */
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);
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)
646 struct p9_req *req, *rptr, *rreq;
647 struct p9_fcall *rcall;
650 m = container_of(work, struct p9_conn, rq);
656 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
660 kmalloc(sizeof(struct p9_fcall) + m->msize, GFP_KERNEL);
666 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
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);
682 while (m->rpos > 4) {
683 n = le32_to_cpu(*(__le32 *) m->rbuf);
685 P9_DPRINTK(P9_DEBUG_ERROR,
686 "requested packet size too big: %d\n", n);
695 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->extended);
699 #ifdef CONFIG_NET_9P_DEBUG
700 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
703 p9_printfcall(buf, sizeof(buf), m->rcall,
705 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
712 m->rcall = kmalloc(sizeof(struct p9_fcall) + m->msize,
719 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
720 memmove(m->rbuf, rbuf + n, m->rpos - n);
728 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
729 rcall->id, rcall->tag);
733 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
734 if (rreq->tag == rcall->tag) {
736 if (req->flush != Flushing)
737 list_del(&req->req_list);
741 spin_unlock(&m->lock);
745 process_request(m, req);
747 if (req->flush != Flushing) {
749 (*req->cb) (req, req->cba);
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);
764 if (!list_empty(&m->req_list)) {
765 if (test_and_clear_bit(Rpending, &m->wsched))
768 n = p9_fd_poll(m->trans, NULL);
771 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
772 queue_work(p9_mux_wq, &m->rq);
774 clear_bit(Rworksched, &m->wsched);
776 clear_bit(Rworksched, &m->wsched);
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.
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,
799 p9_conn_req_callback cb, void *cba)
804 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
807 return ERR_PTR(m->err);
809 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
811 return ERR_PTR(-ENOMEM);
813 if (tc->id == P9_TVERSION)
816 n = p9_mux_get_tag(m);
819 return ERR_PTR(-ENOMEM);
823 #ifdef CONFIG_NET_9P_DEBUG
824 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
827 p9_printfcall(buf, sizeof(buf), tc, m->extended);
828 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
832 spin_lock_init(&req->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))
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);
856 static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
858 p9_mux_put_tag(m, req->tag);
862 static void p9_mux_flush_cb(struct p9_req *freq, void *a)
864 p9_conn_req_callback cb;
867 struct p9_req *req, *rreq, *rptr;
870 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
871 freq->tcall, freq->rcall, freq->err,
872 freq->tcall->params.tflush.oldtag);
876 tag = freq->tcall->params.tflush.oldtag;
878 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
879 if (rreq->tag == tag) {
881 list_del(&req->req_list);
885 spin_unlock(&m->lock);
888 spin_lock(&req->lock);
889 req->flush = Flushed;
890 spin_unlock(&req->lock);
893 (*req->cb) (req, req->cba);
902 p9_mux_free_request(m, freq);
906 p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
909 struct p9_req *rreq, *rptr;
911 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
913 /* if a response was received for a request, do nothing */
914 spin_lock(&req->lock);
915 if (req->rcall || req->err) {
916 spin_unlock(&req->lock);
917 P9_DPRINTK(P9_DEBUG_MUX,
918 "mux %p req %p response already received\n", m, req);
922 req->flush = Flushing;
923 spin_unlock(&req->lock);
926 /* if the request is not sent yet, just remove it from the list */
927 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
928 if (rreq->tag == req->tag) {
929 P9_DPRINTK(P9_DEBUG_MUX,
930 "mux %p req %p request is not sent yet\n", m, req);
931 list_del(&rreq->req_list);
932 req->flush = Flushed;
933 spin_unlock(&m->lock);
935 (*req->cb) (req, req->cba);
939 spin_unlock(&m->lock);
941 clear_thread_flag(TIF_SIGPENDING);
942 fc = p9_create_tflush(req->tag);
943 p9_send_request(m, fc, p9_mux_flush_cb, m);
948 p9_conn_rpc_cb(struct p9_req *req, void *a)
950 struct p9_mux_rpc *r;
952 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
954 r->rcall = req->rcall;
957 if (req->flush != None && !req->err)
958 r->err = -ERESTARTSYS;
964 * p9_fd_rpc- sends 9P request and waits until a response is available.
965 * The function can be interrupted.
967 * @tc: request to be sent
968 * @rc: pointer where a pointer to the response is stored
971 p9_fd_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
973 struct p9_trans_fd *p = t->priv;
974 struct p9_conn *m = p->conn;
984 init_waitqueue_head(&r.wqueue);
990 if (signal_pending(current)) {
992 clear_thread_flag(TIF_SIGPENDING);
995 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
998 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1002 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
1006 if (err == -ERESTARTSYS && m->trans->status == Connected
1008 if (p9_mux_flush_request(m, req)) {
1009 /* wait until we get response of the flush message */
1011 clear_thread_flag(TIF_SIGPENDING);
1012 err = wait_event_interruptible(r.wqueue,
1014 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
1015 m->trans->status == Connected && !m->err);
1023 spin_lock_irqsave(¤t->sighand->siglock, flags);
1024 recalc_sigpending();
1025 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
1033 p9_mux_free_request(m, req);
1042 * p9_conn_rpcnb - sends 9P request without waiting for response.
1044 * @tc: request to be sent
1045 * @cb: callback function to be called when response arrives
1046 * @cba: value to pass to the callback function
1048 int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1049 p9_conn_req_callback cb, void *a)
1054 req = p9_send_request(m, tc, cb, a);
1057 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1058 return PTR_ERR(req);
1061 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1064 #endif /* P9_NONBLOCK */
1067 * p9_conn_cancel - cancel all pending requests with error
1071 void p9_conn_cancel(struct p9_conn *m, int err)
1073 struct p9_req *req, *rtmp;
1074 LIST_HEAD(cancel_list);
1076 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1078 spin_lock(&m->lock);
1079 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1080 list_move(&req->req_list, &cancel_list);
1082 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1083 list_move(&req->req_list, &cancel_list);
1085 spin_unlock(&m->lock);
1087 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1088 list_del(&req->req_list);
1093 (*req->cb) (req, req->cba);
1098 wake_up(&m->equeue);
1102 * v9fs_parse_options - parse mount options into session structure
1103 * @options: options string passed from mount
1104 * @v9ses: existing v9fs session information
1108 static void parse_opts(char *options, struct p9_fd_opts *opts)
1111 substring_t args[MAX_OPT_ARGS];
1115 opts->port = P9_PORT;
1122 while ((p = strsep(&options, ",")) != NULL) {
1126 token = match_token(p, tokens, args);
1127 ret = match_int(&args[0], &option);
1129 P9_DPRINTK(P9_DEBUG_ERROR,
1130 "integer field, but no integer?\n");
1135 opts->port = option;
1149 static int p9_fd_open(struct p9_trans *trans, int rfd, int wfd)
1151 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1158 if (!ts->rd || !ts->wr) {
1168 trans->status = Connected;
1173 static int p9_socket_open(struct p9_trans *trans, struct socket *csocket)
1177 csocket->sk->sk_allocation = GFP_NOIO;
1178 fd = sock_map_fd(csocket);
1180 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1184 ret = p9_fd_open(trans, fd, fd);
1186 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1187 sockfd_put(csocket);
1191 ((struct p9_trans_fd *)trans->priv)->rd->f_flags |= O_NONBLOCK;
1197 * p9_fd_read- read from a fd
1198 * @v9ses: session information
1199 * @v: buffer to receive data into
1200 * @len: size of receive buffer
1203 static int p9_fd_read(struct p9_trans *trans, void *v, int len)
1206 struct p9_trans_fd *ts = NULL;
1208 if (trans && trans->status != Disconnected)
1214 if (!(ts->rd->f_flags & O_NONBLOCK))
1215 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
1217 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
1218 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1219 trans->status = Disconnected;
1224 * p9_fd_write - write to a socket
1225 * @v9ses: session information
1226 * @v: buffer to send data from
1227 * @len: size of send buffer
1230 static int p9_fd_write(struct p9_trans *trans, void *v, int len)
1234 struct p9_trans_fd *ts = NULL;
1236 if (trans && trans->status != Disconnected)
1242 if (!(ts->wr->f_flags & O_NONBLOCK))
1243 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
1247 /* The cast to a user pointer is valid due to the set_fs() */
1248 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
1251 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1252 trans->status = Disconnected;
1257 p9_fd_poll(struct p9_trans *trans, struct poll_table_struct *pt)
1260 struct p9_trans_fd *ts = NULL;
1263 if (trans && trans->status == Connected)
1269 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1272 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1278 ret = ts->rd->f_op->poll(ts->rd, pt);
1282 if (ts->rd != ts->wr) {
1283 n = ts->wr->f_op->poll(ts->wr, pt);
1288 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1297 * p9_fd_close - shutdown socket
1298 * @trans: private socket structure
1301 static void p9_fd_close(struct p9_trans *trans)
1303 struct p9_trans_fd *ts;
1308 ts = xchg(&trans->priv, NULL);
1313 p9_conn_destroy(ts->conn);
1315 trans->status = Disconnected;
1323 static struct p9_trans *
1324 p9_trans_create_tcp(const char *addr, char *args, int msize, unsigned char dotu)
1327 struct p9_trans *trans;
1328 struct socket *csocket;
1329 struct sockaddr_in sin_server;
1330 struct p9_fd_opts opts;
1331 struct p9_trans_fd *p;
1333 parse_opts(args, &opts);
1336 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1338 return ERR_PTR(-ENOMEM);
1339 trans->msize = msize;
1340 trans->extended = dotu;
1341 trans->rpc = p9_fd_rpc;
1342 trans->close = p9_fd_close;
1344 sin_server.sin_family = AF_INET;
1345 sin_server.sin_addr.s_addr = in_aton(addr);
1346 sin_server.sin_port = htons(opts.port);
1347 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1350 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1355 err = csocket->ops->connect(csocket,
1356 (struct sockaddr *)&sin_server,
1357 sizeof(struct sockaddr_in), 0);
1359 P9_EPRINTK(KERN_ERR,
1360 "p9_trans_tcp: problem connecting socket to %s\n",
1365 err = p9_socket_open(trans, csocket);
1369 p = (struct p9_trans_fd *) trans->priv;
1370 p->conn = p9_conn_create(trans);
1371 if (IS_ERR(p->conn)) {
1372 err = PTR_ERR(p->conn);
1381 sock_release(csocket);
1384 return ERR_PTR(err);
1387 static struct p9_trans *
1388 p9_trans_create_unix(const char *addr, char *args, int msize,
1392 struct socket *csocket;
1393 struct sockaddr_un sun_server;
1394 struct p9_trans *trans;
1395 struct p9_trans_fd *p;
1398 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1400 return ERR_PTR(-ENOMEM);
1402 trans->rpc = p9_fd_rpc;
1403 trans->close = p9_fd_close;
1405 if (strlen(addr) > UNIX_PATH_MAX) {
1406 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1408 err = -ENAMETOOLONG;
1412 sun_server.sun_family = PF_UNIX;
1413 strcpy(sun_server.sun_path, addr);
1414 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1415 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1416 sizeof(struct sockaddr_un) - 1, 0);
1418 P9_EPRINTK(KERN_ERR,
1419 "p9_trans_unix: problem connecting socket: %s: %d\n",
1424 err = p9_socket_open(trans, csocket);
1428 trans->msize = msize;
1429 trans->extended = dotu;
1430 p = (struct p9_trans_fd *) trans->priv;
1431 p->conn = p9_conn_create(trans);
1432 if (IS_ERR(p->conn)) {
1433 err = PTR_ERR(p->conn);
1442 sock_release(csocket);
1445 return ERR_PTR(err);
1448 static struct p9_trans *
1449 p9_trans_create_fd(const char *name, char *args, int msize,
1450 unsigned char extended)
1453 struct p9_trans *trans;
1454 struct p9_fd_opts opts;
1455 struct p9_trans_fd *p;
1457 parse_opts(args, &opts);
1459 if (opts.rfd == ~0 || opts.wfd == ~0) {
1460 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
1461 return ERR_PTR(-ENOPROTOOPT);
1464 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1466 return ERR_PTR(-ENOMEM);
1468 trans->rpc = p9_fd_rpc;
1469 trans->close = p9_fd_close;
1471 err = p9_fd_open(trans, opts.rfd, opts.wfd);
1475 trans->msize = msize;
1476 trans->extended = extended;
1477 p = (struct p9_trans_fd *) trans->priv;
1478 p->conn = p9_conn_create(trans);
1479 if (IS_ERR(p->conn)) {
1480 err = PTR_ERR(p->conn);
1489 return ERR_PTR(err);
1492 static struct p9_trans_module p9_tcp_trans = {
1494 .maxsize = MAX_SOCK_BUF,
1496 .create = p9_trans_create_tcp,
1499 static struct p9_trans_module p9_unix_trans = {
1501 .maxsize = MAX_SOCK_BUF,
1503 .create = p9_trans_create_unix,
1506 static struct p9_trans_module p9_fd_trans = {
1508 .maxsize = MAX_SOCK_BUF,
1510 .create = p9_trans_create_fd,
1513 static int __init p9_trans_fd_init(void)
1515 int ret = p9_mux_global_init();
1517 printk(KERN_WARNING "9p: starting mux failed\n");
1521 v9fs_register_trans(&p9_tcp_trans);
1522 v9fs_register_trans(&p9_unix_trans);
1523 v9fs_register_trans(&p9_fd_trans);
1528 module_init(p9_trans_fd_init);
1530 MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
1531 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
1532 MODULE_LICENSE("GPL");