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
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)
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
80 * Option Parsing (code inspired by NFS code)
81 * - a little lazy - parse all fd-transport options
85 /* Options that take integer arguments */
86 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
89 static match_table_t tokens = {
90 {Opt_port, "port=%u"},
91 {Opt_rfdno, "rfdno=%u"},
92 {Opt_wfdno, "wfdno=%u"},
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 */
110 typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
113 * struct p9_req - fd mux encoding of an rpc transaction
114 * @lock: protects req_list
115 * @tag: numeric tag for rpc transaction
116 * @tcall: request &p9_fcall structure
117 * @rcall: response &p9_fcall structure
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
129 struct p9_fcall *tcall;
130 struct p9_fcall *rcall;
132 p9_conn_req_callback cb;
135 struct list_head req_list;
138 struct p9_mux_poll_task {
139 struct task_struct *task;
140 struct list_head mux_list;
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
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
165 * @rq: current read work
166 * @wq: current write work
172 spinlock_t lock; /* protect lock structure */
173 struct list_head mux_list;
174 struct p9_mux_poll_task *poll_task;
176 unsigned char extended;
177 struct p9_trans *trans;
178 struct p9_idpool *tagpool;
180 struct list_head req_list;
181 struct list_head unsent_req_list;
182 struct p9_fcall *rcall;
188 wait_queue_t poll_wait[MAXPOLLWADDR];
189 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
191 struct work_struct rq;
192 struct work_struct wq;
193 unsigned long wsched;
197 * struct p9_mux_rpc - fd mux rpc accounting structure
198 * @m: connection this request was issued on
200 * @tcall: request &p9_fcall
201 * @rcall: response &p9_fcall
202 * @wqueue: wait queue that client is blocked on for this rpc
204 * Bug: isn't this information duplicated elsewhere like &p9_req
210 struct p9_fcall *tcall;
211 struct p9_fcall *rcall;
212 wait_queue_head_t wqueue;
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,
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);
223 static DEFINE_MUTEX(p9_mux_task_lock);
224 static struct workqueue_struct *p9_mux_wq;
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];
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);
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 */
239 static void p9_conn_cancel(struct p9_conn *m, int err);
241 static int p9_mux_global_init(void)
245 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++)
246 p9_mux_poll_tasks[i].task = NULL;
248 p9_mux_wq = create_workqueue("v9fs");
250 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
257 static u16 p9_mux_get_tag(struct p9_conn *m)
261 tag = p9_idpool_get(m->tagpool);
268 static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
270 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
271 p9_idpool_put(tag, m->tagpool);
275 * p9_mux_calc_poll_procs - calculates the number of polling procs
276 * @muxnum: number of mounts
278 * Calculation is based on the number of mounted v9fs filesystems.
279 * The current implementation returns sqrt of the number of mounts.
282 static int p9_mux_calc_poll_procs(int muxnum)
286 if (p9_mux_poll_task_num)
287 n = muxnum / p9_mux_poll_task_num +
288 (muxnum % p9_mux_poll_task_num ? 1 : 0);
292 if (n > ARRAY_SIZE(p9_mux_poll_tasks))
293 n = ARRAY_SIZE(p9_mux_poll_tasks);
298 static int p9_mux_poll_start(struct p9_conn *m)
301 struct p9_mux_poll_task *vpt, *vptlast;
302 struct task_struct *pproc;
304 P9_DPRINTK(P9_DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, p9_mux_num,
305 p9_mux_poll_task_num);
306 mutex_lock(&p9_mux_task_lock);
308 n = p9_mux_calc_poll_procs(p9_mux_num + 1);
309 if (n > p9_mux_poll_task_num) {
310 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
311 if (p9_mux_poll_tasks[i].task == NULL) {
312 vpt = &p9_mux_poll_tasks[i];
313 P9_DPRINTK(P9_DEBUG_MUX, "create proc %p\n",
315 pproc = kthread_create(p9_poll_proc, vpt,
318 if (!IS_ERR(pproc)) {
320 INIT_LIST_HEAD(&vpt->mux_list);
322 p9_mux_poll_task_num++;
323 wake_up_process(vpt->task);
329 if (i >= ARRAY_SIZE(p9_mux_poll_tasks))
330 P9_DPRINTK(P9_DEBUG_ERROR,
331 "warning: no free poll slots\n");
334 n = (p9_mux_num + 1) / p9_mux_poll_task_num +
335 ((p9_mux_num + 1) % p9_mux_poll_task_num ? 1 : 0);
338 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
339 vpt = &p9_mux_poll_tasks[i];
340 if (vpt->task != NULL) {
342 if (vpt->muxnum < n) {
343 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
344 list_add(&m->mux_list, &vpt->mux_list);
347 memset(&m->poll_waddr, 0,
348 sizeof(m->poll_waddr));
349 init_poll_funcptr(&m->pt, p9_pollwait);
355 if (i >= ARRAY_SIZE(p9_mux_poll_tasks)) {
356 if (vptlast == NULL) {
357 mutex_unlock(&p9_mux_task_lock);
361 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
362 list_add(&m->mux_list, &vptlast->mux_list);
364 m->poll_task = vptlast;
365 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
366 init_poll_funcptr(&m->pt, p9_pollwait);
370 mutex_unlock(&p9_mux_task_lock);
375 static void p9_mux_poll_stop(struct p9_conn *m)
378 struct p9_mux_poll_task *vpt;
380 mutex_lock(&p9_mux_task_lock);
382 list_del(&m->mux_list);
383 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
384 if (m->poll_waddr[i] != NULL) {
385 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
386 m->poll_waddr[i] = NULL;
391 P9_DPRINTK(P9_DEBUG_MUX, "destroy proc %p\n", vpt);
392 kthread_stop(vpt->task);
394 p9_mux_poll_task_num--;
397 mutex_unlock(&p9_mux_task_lock);
401 * p9_conn_create - allocate and initialize the per-session mux data
402 * @trans: transport structure
404 * Note: Creates the polling task if this is the first session.
407 static struct p9_conn *p9_conn_create(struct p9_trans *trans)
412 P9_DPRINTK(P9_DEBUG_MUX, "transport %p msize %d\n", trans,
414 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
416 return ERR_PTR(-ENOMEM);
418 spin_lock_init(&m->lock);
419 INIT_LIST_HEAD(&m->mux_list);
420 m->msize = trans->msize;
421 m->extended = trans->extended;
423 m->tagpool = p9_idpool_create();
424 if (IS_ERR(m->tagpool)) {
426 return ERR_PTR(-ENOMEM);
429 INIT_LIST_HEAD(&m->req_list);
430 INIT_LIST_HEAD(&m->unsent_req_list);
431 INIT_WORK(&m->rq, p9_read_work);
432 INIT_WORK(&m->wq, p9_write_work);
433 n = p9_mux_poll_start(m);
439 n = p9_fd_poll(trans, &m->pt);
441 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
442 set_bit(Rpending, &m->wsched);
446 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
447 set_bit(Wpending, &m->wsched);
450 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
451 if (IS_ERR(m->poll_waddr[i])) {
454 return (void *)m->poll_waddr; /* the error code */
462 * p9_mux_destroy - cancels all pending requests and frees mux resources
467 static void p9_conn_destroy(struct p9_conn *m)
469 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
470 m->mux_list.prev, m->mux_list.next);
473 cancel_work_sync(&m->rq);
474 cancel_work_sync(&m->wq);
476 p9_conn_cancel(m, -ECONNRESET);
479 p9_idpool_destroy(m->tagpool);
484 * p9_pollwait - add poll task to the wait queue
485 * @filp: file pointer being polled
486 * @wait_address: wait_q to block on
489 * called by files poll operation to add v9fs-poll task to files wait queue
493 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
498 m = container_of(p, struct p9_conn, pt);
499 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
500 if (m->poll_waddr[i] == NULL)
503 if (i >= ARRAY_SIZE(m->poll_waddr)) {
504 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
508 m->poll_waddr[i] = wait_address;
511 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
512 m->poll_waddr[i] = ERR_PTR(-EIO);
516 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
517 add_wait_queue(wait_address, &m->poll_wait[i]);
521 * p9_poll_mux - polls a mux and schedules read or write works if necessary
522 * @m: connection to poll
526 static void p9_poll_mux(struct p9_conn *m)
533 n = p9_fd_poll(m->trans, NULL);
534 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
535 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
538 p9_conn_cancel(m, n);
542 set_bit(Rpending, &m->wsched);
543 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
544 if (!test_and_set_bit(Rworksched, &m->wsched)) {
545 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
546 queue_work(p9_mux_wq, &m->rq);
551 set_bit(Wpending, &m->wsched);
552 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
553 if ((m->wsize || !list_empty(&m->unsent_req_list))
554 && !test_and_set_bit(Wworksched, &m->wsched)) {
555 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
556 queue_work(p9_mux_wq, &m->wq);
562 * p9_poll_proc - poll worker thread
563 * @a: thread state and arguments
565 * polls all v9fs transports for new events and queues the appropriate
566 * work to the work queue
570 static int p9_poll_proc(void *a)
572 struct p9_conn *m, *mtmp;
573 struct p9_mux_poll_task *vpt;
576 P9_DPRINTK(P9_DEBUG_MUX, "start %p %p\n", current, vpt);
577 while (!kthread_should_stop()) {
578 set_current_state(TASK_INTERRUPTIBLE);
580 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
584 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
585 schedule_timeout(SCHED_TIMEOUT * HZ);
588 __set_current_state(TASK_RUNNING);
589 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
594 * p9_write_work - called when a transport can send some data
595 * @work: container for work to be done
599 static void p9_write_work(struct work_struct *work)
605 m = container_of(work, struct p9_conn, wq);
608 clear_bit(Wworksched, &m->wsched);
613 if (list_empty(&m->unsent_req_list)) {
614 clear_bit(Wworksched, &m->wsched);
620 req = list_entry(m->unsent_req_list.next, struct p9_req,
622 list_move_tail(&req->req_list, &m->req_list);
623 if (req->err == ERREQFLUSH)
626 m->wbuf = req->tcall->sdata;
627 m->wsize = req->tcall->size;
629 spin_unlock(&m->lock);
632 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
634 clear_bit(Wpending, &m->wsched);
635 err = p9_fd_write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
636 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
637 if (err == -EAGAIN) {
638 clear_bit(Wworksched, &m->wsched);
650 if (m->wpos == m->wsize)
651 m->wpos = m->wsize = 0;
653 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
654 if (test_and_clear_bit(Wpending, &m->wsched))
657 n = p9_fd_poll(m->trans, NULL);
660 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
661 queue_work(p9_mux_wq, &m->wq);
663 clear_bit(Wworksched, &m->wsched);
665 clear_bit(Wworksched, &m->wsched);
670 p9_conn_cancel(m, err);
671 clear_bit(Wworksched, &m->wsched);
674 static void process_request(struct p9_conn *m, struct p9_req *req)
677 struct p9_str *ename;
679 if (!req->err && req->rcall->id == P9_RERROR) {
680 ecode = req->rcall->params.rerror.errno;
681 ename = &req->rcall->params.rerror.error;
683 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
690 req->err = p9_errstr2errno(ename->str, ename->len);
692 /* string match failed */
694 PRINT_FCALL_ERROR("unknown error", req->rcall);
695 req->err = -ESERVERFAULT;
698 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
699 P9_DPRINTK(P9_DEBUG_ERROR,
700 "fcall mismatch: expected %d, got %d\n",
701 req->tcall->id + 1, req->rcall->id);
708 * p9_read_work - called when there is some data to be read from a transport
709 * @work: container of work to be done
713 static void p9_read_work(struct work_struct *work)
717 struct p9_req *req, *rptr, *rreq;
718 struct p9_fcall *rcall;
721 m = container_of(work, struct p9_conn, rq);
727 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
731 kmalloc(sizeof(struct p9_fcall) + m->msize, GFP_KERNEL);
737 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
741 clear_bit(Rpending, &m->wsched);
742 err = p9_fd_read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
743 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
744 if (err == -EAGAIN) {
745 clear_bit(Rworksched, &m->wsched);
753 while (m->rpos > 4) {
754 n = le32_to_cpu(*(__le32 *) m->rbuf);
756 P9_DPRINTK(P9_DEBUG_ERROR,
757 "requested packet size too big: %d\n", n);
766 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->extended);
770 #ifdef CONFIG_NET_9P_DEBUG
771 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
774 p9_printfcall(buf, sizeof(buf), m->rcall,
776 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
783 m->rcall = kmalloc(sizeof(struct p9_fcall) + m->msize,
790 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
791 memmove(m->rbuf, rbuf + n, m->rpos - n);
799 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
800 rcall->id, rcall->tag);
804 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
805 if (rreq->tag == rcall->tag) {
807 if (req->flush != Flushing)
808 list_del(&req->req_list);
812 spin_unlock(&m->lock);
816 process_request(m, req);
818 if (req->flush != Flushing) {
820 (*req->cb) (req, req->cba);
825 if (err >= 0 && rcall->id != P9_RFLUSH)
826 P9_DPRINTK(P9_DEBUG_ERROR,
827 "unexpected response mux %p id %d tag %d\n",
828 m, rcall->id, rcall->tag);
833 if (!list_empty(&m->req_list)) {
834 if (test_and_clear_bit(Rpending, &m->wsched))
837 n = p9_fd_poll(m->trans, NULL);
840 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
841 queue_work(p9_mux_wq, &m->rq);
843 clear_bit(Rworksched, &m->wsched);
845 clear_bit(Rworksched, &m->wsched);
850 p9_conn_cancel(m, err);
851 clear_bit(Rworksched, &m->wsched);
855 * p9_send_request - send 9P request
856 * The function can sleep until the request is scheduled for sending.
857 * The function can be interrupted. Return from the function is not
858 * a guarantee that the request is sent successfully. Can return errors
859 * that can be retrieved by PTR_ERR macros.
862 * @tc: request to be sent
863 * @cb: callback function to call when response is received
864 * @cba: parameter to pass to the callback function
868 static struct p9_req *p9_send_request(struct p9_conn *m,
870 p9_conn_req_callback cb, void *cba)
875 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
878 return ERR_PTR(m->err);
880 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
882 return ERR_PTR(-ENOMEM);
884 if (tc->id == P9_TVERSION)
887 n = p9_mux_get_tag(m);
890 return ERR_PTR(-ENOMEM);
894 #ifdef CONFIG_NET_9P_DEBUG
895 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
898 p9_printfcall(buf, sizeof(buf), tc, m->extended);
899 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
903 spin_lock_init(&req->lock);
913 list_add_tail(&req->req_list, &m->unsent_req_list);
914 spin_unlock(&m->lock);
916 if (test_and_clear_bit(Wpending, &m->wsched))
919 n = p9_fd_poll(m->trans, NULL);
921 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
922 queue_work(p9_mux_wq, &m->wq);
927 static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
929 p9_mux_put_tag(m, req->tag);
933 static void p9_mux_flush_cb(struct p9_req *freq, void *a)
937 struct p9_req *req, *rreq, *rptr;
940 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
941 freq->tcall, freq->rcall, freq->err,
942 freq->tcall->params.tflush.oldtag);
945 tag = freq->tcall->params.tflush.oldtag;
947 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
948 if (rreq->tag == tag) {
950 list_del(&req->req_list);
954 spin_unlock(&m->lock);
957 spin_lock(&req->lock);
958 req->flush = Flushed;
959 spin_unlock(&req->lock);
962 (*req->cb) (req, req->cba);
969 p9_mux_free_request(m, freq);
973 p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
976 struct p9_req *rreq, *rptr;
978 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
980 /* if a response was received for a request, do nothing */
981 spin_lock(&req->lock);
982 if (req->rcall || req->err) {
983 spin_unlock(&req->lock);
984 P9_DPRINTK(P9_DEBUG_MUX,
985 "mux %p req %p response already received\n", m, req);
989 req->flush = Flushing;
990 spin_unlock(&req->lock);
993 /* if the request is not sent yet, just remove it from the list */
994 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
995 if (rreq->tag == req->tag) {
996 P9_DPRINTK(P9_DEBUG_MUX,
997 "mux %p req %p request is not sent yet\n", m, req);
998 list_del(&rreq->req_list);
999 req->flush = Flushed;
1000 spin_unlock(&m->lock);
1002 (*req->cb) (req, req->cba);
1006 spin_unlock(&m->lock);
1008 clear_thread_flag(TIF_SIGPENDING);
1009 fc = p9_create_tflush(req->tag);
1010 p9_send_request(m, fc, p9_mux_flush_cb, m);
1015 p9_conn_rpc_cb(struct p9_req *req, void *a)
1017 struct p9_mux_rpc *r;
1019 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
1021 r->rcall = req->rcall;
1024 if (req->flush != None && !req->err)
1025 r->err = -ERESTARTSYS;
1027 wake_up(&r->wqueue);
1031 * p9_fd_rpc- sends 9P request and waits until a response is available.
1032 * The function can be interrupted.
1033 * @t: transport data
1034 * @tc: request to be sent
1035 * @rc: pointer where a pointer to the response is stored
1040 p9_fd_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
1042 struct p9_trans_fd *p = t->priv;
1043 struct p9_conn *m = p->conn;
1044 int err, sigpending;
1045 unsigned long flags;
1047 struct p9_mux_rpc r;
1053 init_waitqueue_head(&r.wqueue);
1059 if (signal_pending(current)) {
1061 clear_thread_flag(TIF_SIGPENDING);
1064 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
1067 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1071 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
1075 if (err == -ERESTARTSYS && m->trans->status == Connected
1077 if (p9_mux_flush_request(m, req)) {
1078 /* wait until we get response of the flush message */
1080 clear_thread_flag(TIF_SIGPENDING);
1081 err = wait_event_interruptible(r.wqueue,
1083 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
1084 m->trans->status == Connected && !m->err);
1092 spin_lock_irqsave(¤t->sighand->siglock, flags);
1093 recalc_sigpending();
1094 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
1102 p9_mux_free_request(m, req);
1111 * p9_conn_rpcnb - sends 9P request without waiting for response.
1113 * @tc: request to be sent
1114 * @cb: callback function to be called when response arrives
1115 * @a: value to pass to the callback function
1119 int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1120 p9_conn_req_callback cb, void *a)
1125 req = p9_send_request(m, tc, cb, a);
1128 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1129 return PTR_ERR(req);
1132 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1135 #endif /* P9_NONBLOCK */
1138 * p9_conn_cancel - cancel all pending requests with error
1144 void p9_conn_cancel(struct p9_conn *m, int err)
1146 struct p9_req *req, *rtmp;
1147 LIST_HEAD(cancel_list);
1149 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1151 spin_lock(&m->lock);
1152 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1153 list_move(&req->req_list, &cancel_list);
1155 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1156 list_move(&req->req_list, &cancel_list);
1158 spin_unlock(&m->lock);
1160 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1161 list_del(&req->req_list);
1166 (*req->cb) (req, req->cba);
1173 * parse_options - parse mount options into session structure
1174 * @options: options string passed from mount
1175 * @opts: transport-specific structure to parse options into
1177 * Returns 0 upon success, -ERRNO upon failure
1180 static int parse_opts(char *params, struct p9_fd_opts *opts)
1183 substring_t args[MAX_OPT_ARGS];
1188 opts->port = P9_PORT;
1195 options = kstrdup(params, GFP_KERNEL);
1197 P9_DPRINTK(P9_DEBUG_ERROR,
1198 "failed to allocate copy of option string\n");
1202 while ((p = strsep(&options, ",")) != NULL) {
1207 token = match_token(p, tokens, args);
1208 r = match_int(&args[0], &option);
1210 P9_DPRINTK(P9_DEBUG_ERROR,
1211 "integer field, but no integer?\n");
1217 opts->port = option;
1233 static int p9_fd_open(struct p9_trans *trans, int rfd, int wfd)
1235 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1242 if (!ts->rd || !ts->wr) {
1252 trans->status = Connected;
1257 static int p9_socket_open(struct p9_trans *trans, struct socket *csocket)
1261 csocket->sk->sk_allocation = GFP_NOIO;
1262 fd = sock_map_fd(csocket, 0);
1264 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1268 ret = p9_fd_open(trans, fd, fd);
1270 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1271 sockfd_put(csocket);
1275 ((struct p9_trans_fd *)trans->priv)->rd->f_flags |= O_NONBLOCK;
1281 * p9_fd_read- read from a fd
1282 * @trans: transport instance state
1283 * @v: buffer to receive data into
1284 * @len: size of receive buffer
1288 static int p9_fd_read(struct p9_trans *trans, void *v, int len)
1291 struct p9_trans_fd *ts = NULL;
1293 if (trans && trans->status != Disconnected)
1299 if (!(ts->rd->f_flags & O_NONBLOCK))
1300 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
1302 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
1303 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1304 trans->status = Disconnected;
1309 * p9_fd_write - write to a socket
1310 * @trans: transport instance state
1311 * @v: buffer to send data from
1312 * @len: size of send buffer
1316 static int p9_fd_write(struct p9_trans *trans, void *v, int len)
1320 struct p9_trans_fd *ts = NULL;
1322 if (trans && trans->status != Disconnected)
1328 if (!(ts->wr->f_flags & O_NONBLOCK))
1329 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
1333 /* The cast to a user pointer is valid due to the set_fs() */
1334 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
1337 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1338 trans->status = Disconnected;
1343 p9_fd_poll(struct p9_trans *trans, struct poll_table_struct *pt)
1346 struct p9_trans_fd *ts = NULL;
1348 if (trans && trans->status == Connected)
1354 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1357 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1360 ret = ts->rd->f_op->poll(ts->rd, pt);
1364 if (ts->rd != ts->wr) {
1365 n = ts->wr->f_op->poll(ts->wr, pt);
1368 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1375 * p9_fd_close - shutdown socket
1376 * @trans: private socket structure
1380 static void p9_fd_close(struct p9_trans *trans)
1382 struct p9_trans_fd *ts;
1387 ts = xchg(&trans->priv, NULL);
1392 p9_conn_destroy(ts->conn);
1394 trans->status = Disconnected;
1403 * stolen from NFS - maybe should be made a generic function?
1405 static inline int valid_ipaddr4(const char *buf)
1407 int rc, count, in[4];
1409 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
1412 for (count = 0; count < 4; count++) {
1413 if (in[count] > 255)
1419 static struct p9_trans *
1420 p9_trans_create_tcp(const char *addr, char *args, int msize, unsigned char dotu)
1423 struct p9_trans *trans;
1424 struct socket *csocket;
1425 struct sockaddr_in sin_server;
1426 struct p9_fd_opts opts;
1427 struct p9_trans_fd *p;
1429 err = parse_opts(args, &opts);
1431 return ERR_PTR(err);
1433 if (valid_ipaddr4(addr) < 0)
1434 return ERR_PTR(-EINVAL);
1437 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1439 return ERR_PTR(-ENOMEM);
1440 trans->msize = msize;
1441 trans->extended = dotu;
1442 trans->rpc = p9_fd_rpc;
1443 trans->close = p9_fd_close;
1445 sin_server.sin_family = AF_INET;
1446 sin_server.sin_addr.s_addr = in_aton(addr);
1447 sin_server.sin_port = htons(opts.port);
1448 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1451 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1456 err = csocket->ops->connect(csocket,
1457 (struct sockaddr *)&sin_server,
1458 sizeof(struct sockaddr_in), 0);
1460 P9_EPRINTK(KERN_ERR,
1461 "p9_trans_tcp: problem connecting socket to %s\n",
1466 err = p9_socket_open(trans, csocket);
1470 p = (struct p9_trans_fd *) trans->priv;
1471 p->conn = p9_conn_create(trans);
1472 if (IS_ERR(p->conn)) {
1473 err = PTR_ERR(p->conn);
1482 sock_release(csocket);
1485 return ERR_PTR(err);
1488 static struct p9_trans *
1489 p9_trans_create_unix(const char *addr, char *args, int msize,
1493 struct socket *csocket;
1494 struct sockaddr_un sun_server;
1495 struct p9_trans *trans;
1496 struct p9_trans_fd *p;
1499 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1501 return ERR_PTR(-ENOMEM);
1503 trans->rpc = p9_fd_rpc;
1504 trans->close = p9_fd_close;
1506 if (strlen(addr) > UNIX_PATH_MAX) {
1507 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1509 err = -ENAMETOOLONG;
1513 sun_server.sun_family = PF_UNIX;
1514 strcpy(sun_server.sun_path, addr);
1515 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1516 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1517 sizeof(struct sockaddr_un) - 1, 0);
1519 P9_EPRINTK(KERN_ERR,
1520 "p9_trans_unix: problem connecting socket: %s: %d\n",
1525 err = p9_socket_open(trans, csocket);
1529 trans->msize = msize;
1530 trans->extended = dotu;
1531 p = (struct p9_trans_fd *) trans->priv;
1532 p->conn = p9_conn_create(trans);
1533 if (IS_ERR(p->conn)) {
1534 err = PTR_ERR(p->conn);
1543 sock_release(csocket);
1546 return ERR_PTR(err);
1549 static struct p9_trans *
1550 p9_trans_create_fd(const char *name, char *args, int msize,
1551 unsigned char extended)
1554 struct p9_trans *trans;
1555 struct p9_fd_opts opts;
1556 struct p9_trans_fd *p;
1558 parse_opts(args, &opts);
1560 if (opts.rfd == ~0 || opts.wfd == ~0) {
1561 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
1562 return ERR_PTR(-ENOPROTOOPT);
1565 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1567 return ERR_PTR(-ENOMEM);
1569 trans->rpc = p9_fd_rpc;
1570 trans->close = p9_fd_close;
1572 err = p9_fd_open(trans, opts.rfd, opts.wfd);
1576 trans->msize = msize;
1577 trans->extended = extended;
1578 p = (struct p9_trans_fd *) trans->priv;
1579 p->conn = p9_conn_create(trans);
1580 if (IS_ERR(p->conn)) {
1581 err = PTR_ERR(p->conn);
1590 return ERR_PTR(err);
1593 static struct p9_trans_module p9_tcp_trans = {
1595 .maxsize = MAX_SOCK_BUF,
1597 .create = p9_trans_create_tcp,
1598 .owner = THIS_MODULE,
1601 static struct p9_trans_module p9_unix_trans = {
1603 .maxsize = MAX_SOCK_BUF,
1605 .create = p9_trans_create_unix,
1606 .owner = THIS_MODULE,
1609 static struct p9_trans_module p9_fd_trans = {
1611 .maxsize = MAX_SOCK_BUF,
1613 .create = p9_trans_create_fd,
1614 .owner = THIS_MODULE,
1617 int p9_trans_fd_init(void)
1619 int ret = p9_mux_global_init();
1621 printk(KERN_WARNING "9p: starting mux failed\n");
1625 v9fs_register_trans(&p9_tcp_trans);
1626 v9fs_register_trans(&p9_unix_trans);
1627 v9fs_register_trans(&p9_fd_trans);
1632 void p9_trans_fd_exit(void)
1634 v9fs_unregister_trans(&p9_tcp_trans);
1635 v9fs_unregister_trans(&p9_unix_trans);
1636 v9fs_unregister_trans(&p9_fd_trans);