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 * @equeue: event wait_q (?)
155 * @req_list: accounting for requests which have been sent
156 * @unsent_req_list: accounting for requests that haven't been sent
157 * @rcall: current response &p9_fcall structure
158 * @rpos: read position in current frame
159 * @rbuf: current read buffer
160 * @wpos: write position for current frame
161 * @wsize: amount of data to write for current frame
162 * @wbuf: current write buffer
163 * @poll_wait: array of wait_q's for various worker threads
166 * @rq: current read work
167 * @wq: current write work
173 spinlock_t lock
; /* protect lock structure */
174 struct list_head mux_list
;
175 struct p9_mux_poll_task
*poll_task
;
177 unsigned char extended
;
178 struct p9_trans
*trans
;
179 struct p9_idpool
*tagpool
;
181 wait_queue_head_t equeue
;
182 struct list_head req_list
;
183 struct list_head unsent_req_list
;
184 struct p9_fcall
*rcall
;
190 wait_queue_t poll_wait
[MAXPOLLWADDR
];
191 wait_queue_head_t
*poll_waddr
[MAXPOLLWADDR
];
193 struct work_struct rq
;
194 struct work_struct wq
;
195 unsigned long wsched
;
199 * struct p9_mux_rpc - fd mux rpc accounting structure
200 * @m: connection this request was issued on
202 * @tcall: request &p9_fcall
203 * @rcall: response &p9_fcall
204 * @wqueue: wait queue that client is blocked on for this rpc
206 * Bug: isn't this information duplicated elsewhere like &p9_req
212 struct p9_fcall
*tcall
;
213 struct p9_fcall
*rcall
;
214 wait_queue_head_t wqueue
;
217 static int p9_poll_proc(void *);
218 static void p9_read_work(struct work_struct
*work
);
219 static void p9_write_work(struct work_struct
*work
);
220 static void p9_pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
,
222 static int p9_fd_write(struct p9_trans
*trans
, void *v
, int len
);
223 static int p9_fd_read(struct p9_trans
*trans
, void *v
, int len
);
225 static DEFINE_MUTEX(p9_mux_task_lock
);
226 static struct workqueue_struct
*p9_mux_wq
;
228 static int p9_mux_num
;
229 static int p9_mux_poll_task_num
;
230 static struct p9_mux_poll_task p9_mux_poll_tasks
[100];
232 static void p9_conn_destroy(struct p9_conn
*);
233 static unsigned int p9_fd_poll(struct p9_trans
*trans
,
234 struct poll_table_struct
*pt
);
237 static int p9_conn_rpcnb(struct p9_conn
*m
, struct p9_fcall
*tc
,
238 p9_conn_req_callback cb
, void *a
);
239 #endif /* P9_NONBLOCK */
241 static void p9_conn_cancel(struct p9_conn
*m
, int err
);
243 static int p9_mux_global_init(void)
247 for (i
= 0; i
< ARRAY_SIZE(p9_mux_poll_tasks
); i
++)
248 p9_mux_poll_tasks
[i
].task
= NULL
;
250 p9_mux_wq
= create_workqueue("v9fs");
252 printk(KERN_WARNING
"v9fs: mux: creating workqueue failed\n");
259 static u16
p9_mux_get_tag(struct p9_conn
*m
)
263 tag
= p9_idpool_get(m
->tagpool
);
270 static void p9_mux_put_tag(struct p9_conn
*m
, u16 tag
)
272 if (tag
!= P9_NOTAG
&& p9_idpool_check(tag
, m
->tagpool
))
273 p9_idpool_put(tag
, m
->tagpool
);
277 * p9_mux_calc_poll_procs - calculates the number of polling procs
278 * @muxnum: number of mounts
280 * Calculation is based on the number of mounted v9fs filesystems.
281 * The current implementation returns sqrt of the number of mounts.
284 static int p9_mux_calc_poll_procs(int muxnum
)
288 if (p9_mux_poll_task_num
)
289 n
= muxnum
/ p9_mux_poll_task_num
+
290 (muxnum
% p9_mux_poll_task_num
? 1 : 0);
294 if (n
> ARRAY_SIZE(p9_mux_poll_tasks
))
295 n
= ARRAY_SIZE(p9_mux_poll_tasks
);
300 static int p9_mux_poll_start(struct p9_conn
*m
)
303 struct p9_mux_poll_task
*vpt
, *vptlast
;
304 struct task_struct
*pproc
;
306 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p muxnum %d procnum %d\n", m
, p9_mux_num
,
307 p9_mux_poll_task_num
);
308 mutex_lock(&p9_mux_task_lock
);
310 n
= p9_mux_calc_poll_procs(p9_mux_num
+ 1);
311 if (n
> p9_mux_poll_task_num
) {
312 for (i
= 0; i
< ARRAY_SIZE(p9_mux_poll_tasks
); i
++) {
313 if (p9_mux_poll_tasks
[i
].task
== NULL
) {
314 vpt
= &p9_mux_poll_tasks
[i
];
315 P9_DPRINTK(P9_DEBUG_MUX
, "create proc %p\n",
317 pproc
= kthread_create(p9_poll_proc
, vpt
,
320 if (!IS_ERR(pproc
)) {
322 INIT_LIST_HEAD(&vpt
->mux_list
);
324 p9_mux_poll_task_num
++;
325 wake_up_process(vpt
->task
);
331 if (i
>= ARRAY_SIZE(p9_mux_poll_tasks
))
332 P9_DPRINTK(P9_DEBUG_ERROR
,
333 "warning: no free poll slots\n");
336 n
= (p9_mux_num
+ 1) / p9_mux_poll_task_num
+
337 ((p9_mux_num
+ 1) % p9_mux_poll_task_num
? 1 : 0);
340 for (i
= 0; i
< ARRAY_SIZE(p9_mux_poll_tasks
); i
++) {
341 vpt
= &p9_mux_poll_tasks
[i
];
342 if (vpt
->task
!= NULL
) {
344 if (vpt
->muxnum
< n
) {
345 P9_DPRINTK(P9_DEBUG_MUX
, "put in proc %d\n", i
);
346 list_add(&m
->mux_list
, &vpt
->mux_list
);
349 memset(&m
->poll_waddr
, 0,
350 sizeof(m
->poll_waddr
));
351 init_poll_funcptr(&m
->pt
, p9_pollwait
);
357 if (i
>= ARRAY_SIZE(p9_mux_poll_tasks
)) {
358 if (vptlast
== NULL
) {
359 mutex_unlock(&p9_mux_task_lock
);
363 P9_DPRINTK(P9_DEBUG_MUX
, "put in proc %d\n", i
);
364 list_add(&m
->mux_list
, &vptlast
->mux_list
);
366 m
->poll_task
= vptlast
;
367 memset(&m
->poll_waddr
, 0, sizeof(m
->poll_waddr
));
368 init_poll_funcptr(&m
->pt
, p9_pollwait
);
372 mutex_unlock(&p9_mux_task_lock
);
377 static void p9_mux_poll_stop(struct p9_conn
*m
)
380 struct p9_mux_poll_task
*vpt
;
382 mutex_lock(&p9_mux_task_lock
);
384 list_del(&m
->mux_list
);
385 for (i
= 0; i
< ARRAY_SIZE(m
->poll_waddr
); i
++) {
386 if (m
->poll_waddr
[i
] != NULL
) {
387 remove_wait_queue(m
->poll_waddr
[i
], &m
->poll_wait
[i
]);
388 m
->poll_waddr
[i
] = NULL
;
393 P9_DPRINTK(P9_DEBUG_MUX
, "destroy proc %p\n", vpt
);
394 kthread_stop(vpt
->task
);
396 p9_mux_poll_task_num
--;
399 mutex_unlock(&p9_mux_task_lock
);
403 * p9_conn_create - allocate and initialize the per-session mux data
404 * @trans: transport structure
406 * Note: Creates the polling task if this is the first session.
409 static struct p9_conn
*p9_conn_create(struct p9_trans
*trans
)
412 struct p9_conn
*m
, *mtmp
;
414 P9_DPRINTK(P9_DEBUG_MUX
, "transport %p msize %d\n", trans
,
416 m
= kmalloc(sizeof(struct p9_conn
), GFP_KERNEL
);
418 return ERR_PTR(-ENOMEM
);
420 spin_lock_init(&m
->lock
);
421 INIT_LIST_HEAD(&m
->mux_list
);
422 m
->msize
= trans
->msize
;
423 m
->extended
= trans
->extended
;
425 m
->tagpool
= p9_idpool_create();
426 if (IS_ERR(m
->tagpool
)) {
427 mtmp
= ERR_PTR(-ENOMEM
);
433 init_waitqueue_head(&m
->equeue
);
434 INIT_LIST_HEAD(&m
->req_list
);
435 INIT_LIST_HEAD(&m
->unsent_req_list
);
439 m
->wpos
= m
->wsize
= 0;
441 INIT_WORK(&m
->rq
, p9_read_work
);
442 INIT_WORK(&m
->wq
, p9_write_work
);
444 memset(&m
->poll_waddr
, 0, sizeof(m
->poll_waddr
));
446 n
= p9_mux_poll_start(m
);
452 n
= p9_fd_poll(trans
, &m
->pt
);
454 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can read\n", m
);
455 set_bit(Rpending
, &m
->wsched
);
459 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can write\n", m
);
460 set_bit(Wpending
, &m
->wsched
);
463 for (i
= 0; i
< ARRAY_SIZE(m
->poll_waddr
); i
++) {
464 if (IS_ERR(m
->poll_waddr
[i
])) {
466 mtmp
= (void *)m
->poll_waddr
; /* the error code */
477 * p9_mux_destroy - cancels all pending requests and frees mux resources
482 static void p9_conn_destroy(struct p9_conn
*m
)
484 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p prev %p next %p\n", m
,
485 m
->mux_list
.prev
, m
->mux_list
.next
);
486 p9_conn_cancel(m
, -ECONNRESET
);
488 if (!list_empty(&m
->req_list
)) {
489 /* wait until all processes waiting on this session exit */
490 P9_DPRINTK(P9_DEBUG_MUX
,
491 "mux %p waiting for empty request queue\n", m
);
492 wait_event_timeout(m
->equeue
, (list_empty(&m
->req_list
)), 5000);
493 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p request queue empty: %d\n", m
,
494 list_empty(&m
->req_list
));
499 p9_idpool_destroy(m
->tagpool
);
504 * p9_pollwait - add poll task to the wait queue
505 * @filp: file pointer being polled
506 * @wait_address: wait_q to block on
509 * called by files poll operation to add v9fs-poll task to files wait queue
513 p9_pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
, poll_table
*p
)
518 m
= container_of(p
, struct p9_conn
, pt
);
519 for (i
= 0; i
< ARRAY_SIZE(m
->poll_waddr
); i
++)
520 if (m
->poll_waddr
[i
] == NULL
)
523 if (i
>= ARRAY_SIZE(m
->poll_waddr
)) {
524 P9_DPRINTK(P9_DEBUG_ERROR
, "not enough wait_address slots\n");
528 m
->poll_waddr
[i
] = wait_address
;
531 P9_DPRINTK(P9_DEBUG_ERROR
, "no wait_address\n");
532 m
->poll_waddr
[i
] = ERR_PTR(-EIO
);
536 init_waitqueue_entry(&m
->poll_wait
[i
], m
->poll_task
->task
);
537 add_wait_queue(wait_address
, &m
->poll_wait
[i
]);
541 * p9_poll_mux - polls a mux and schedules read or write works if necessary
542 * @m: connection to poll
546 static void p9_poll_mux(struct p9_conn
*m
)
553 n
= p9_fd_poll(m
->trans
, NULL
);
554 if (n
< 0 || n
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
555 P9_DPRINTK(P9_DEBUG_MUX
, "error mux %p err %d\n", m
, n
);
558 p9_conn_cancel(m
, n
);
562 set_bit(Rpending
, &m
->wsched
);
563 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can read\n", m
);
564 if (!test_and_set_bit(Rworksched
, &m
->wsched
)) {
565 P9_DPRINTK(P9_DEBUG_MUX
, "schedule read work %p\n", m
);
566 queue_work(p9_mux_wq
, &m
->rq
);
571 set_bit(Wpending
, &m
->wsched
);
572 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can write\n", m
);
573 if ((m
->wsize
|| !list_empty(&m
->unsent_req_list
))
574 && !test_and_set_bit(Wworksched
, &m
->wsched
)) {
575 P9_DPRINTK(P9_DEBUG_MUX
, "schedule write work %p\n", m
);
576 queue_work(p9_mux_wq
, &m
->wq
);
582 * p9_poll_proc - poll worker thread
583 * @a: thread state and arguments
585 * polls all v9fs transports for new events and queues the appropriate
586 * work to the work queue
590 static int p9_poll_proc(void *a
)
592 struct p9_conn
*m
, *mtmp
;
593 struct p9_mux_poll_task
*vpt
;
596 P9_DPRINTK(P9_DEBUG_MUX
, "start %p %p\n", current
, vpt
);
597 while (!kthread_should_stop()) {
598 set_current_state(TASK_INTERRUPTIBLE
);
600 list_for_each_entry_safe(m
, mtmp
, &vpt
->mux_list
, mux_list
) {
604 P9_DPRINTK(P9_DEBUG_MUX
, "sleeping...\n");
605 schedule_timeout(SCHED_TIMEOUT
* HZ
);
608 __set_current_state(TASK_RUNNING
);
609 P9_DPRINTK(P9_DEBUG_MUX
, "finish\n");
614 * p9_write_work - called when a transport can send some data
615 * @work: container for work to be done
619 static void p9_write_work(struct work_struct
*work
)
625 m
= container_of(work
, struct p9_conn
, wq
);
628 clear_bit(Wworksched
, &m
->wsched
);
633 if (list_empty(&m
->unsent_req_list
)) {
634 clear_bit(Wworksched
, &m
->wsched
);
640 req
= list_entry(m
->unsent_req_list
.next
, struct p9_req
,
642 list_move_tail(&req
->req_list
, &m
->req_list
);
643 if (req
->err
== ERREQFLUSH
)
646 m
->wbuf
= req
->tcall
->sdata
;
647 m
->wsize
= req
->tcall
->size
;
649 spin_unlock(&m
->lock
);
652 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p pos %d size %d\n", m
, m
->wpos
,
654 clear_bit(Wpending
, &m
->wsched
);
655 err
= p9_fd_write(m
->trans
, m
->wbuf
+ m
->wpos
, m
->wsize
- m
->wpos
);
656 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p sent %d bytes\n", m
, err
);
657 if (err
== -EAGAIN
) {
658 clear_bit(Wworksched
, &m
->wsched
);
670 if (m
->wpos
== m
->wsize
)
671 m
->wpos
= m
->wsize
= 0;
673 if (m
->wsize
== 0 && !list_empty(&m
->unsent_req_list
)) {
674 if (test_and_clear_bit(Wpending
, &m
->wsched
))
677 n
= p9_fd_poll(m
->trans
, NULL
);
680 P9_DPRINTK(P9_DEBUG_MUX
, "schedule write work %p\n", m
);
681 queue_work(p9_mux_wq
, &m
->wq
);
683 clear_bit(Wworksched
, &m
->wsched
);
685 clear_bit(Wworksched
, &m
->wsched
);
690 p9_conn_cancel(m
, err
);
691 clear_bit(Wworksched
, &m
->wsched
);
694 static void process_request(struct p9_conn
*m
, struct p9_req
*req
)
697 struct p9_str
*ename
;
699 if (!req
->err
&& req
->rcall
->id
== P9_RERROR
) {
700 ecode
= req
->rcall
->params
.rerror
.errno
;
701 ename
= &req
->rcall
->params
.rerror
.error
;
703 P9_DPRINTK(P9_DEBUG_MUX
, "Rerror %.*s\n", ename
->len
,
710 req
->err
= p9_errstr2errno(ename
->str
, ename
->len
);
712 /* string match failed */
714 PRINT_FCALL_ERROR("unknown error", req
->rcall
);
715 req
->err
= -ESERVERFAULT
;
718 } else if (req
->tcall
&& req
->rcall
->id
!= req
->tcall
->id
+ 1) {
719 P9_DPRINTK(P9_DEBUG_ERROR
,
720 "fcall mismatch: expected %d, got %d\n",
721 req
->tcall
->id
+ 1, req
->rcall
->id
);
728 * p9_read_work - called when there is some data to be read from a transport
729 * @work: container of work to be done
733 static void p9_read_work(struct work_struct
*work
)
737 struct p9_req
*req
, *rptr
, *rreq
;
738 struct p9_fcall
*rcall
;
741 m
= container_of(work
, struct p9_conn
, rq
);
747 P9_DPRINTK(P9_DEBUG_MUX
, "start mux %p pos %d\n", m
, m
->rpos
);
751 kmalloc(sizeof(struct p9_fcall
) + m
->msize
, GFP_KERNEL
);
757 m
->rbuf
= (char *)m
->rcall
+ sizeof(struct p9_fcall
);
761 clear_bit(Rpending
, &m
->wsched
);
762 err
= p9_fd_read(m
->trans
, m
->rbuf
+ m
->rpos
, m
->msize
- m
->rpos
);
763 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p got %d bytes\n", m
, err
);
764 if (err
== -EAGAIN
) {
765 clear_bit(Rworksched
, &m
->wsched
);
773 while (m
->rpos
> 4) {
774 n
= le32_to_cpu(*(__le32
*) m
->rbuf
);
776 P9_DPRINTK(P9_DEBUG_ERROR
,
777 "requested packet size too big: %d\n", n
);
786 p9_deserialize_fcall(m
->rbuf
, n
, m
->rcall
, m
->extended
);
790 #ifdef CONFIG_NET_9P_DEBUG
791 if ((p9_debug_level
&P9_DEBUG_FCALL
) == P9_DEBUG_FCALL
) {
794 p9_printfcall(buf
, sizeof(buf
), m
->rcall
,
796 printk(KERN_NOTICE
">>> %p %s\n", m
, buf
);
803 m
->rcall
= kmalloc(sizeof(struct p9_fcall
) + m
->msize
,
810 m
->rbuf
= (char *)m
->rcall
+ sizeof(struct p9_fcall
);
811 memmove(m
->rbuf
, rbuf
+ n
, m
->rpos
- n
);
819 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p fcall id %d tag %d\n", m
,
820 rcall
->id
, rcall
->tag
);
824 list_for_each_entry_safe(rreq
, rptr
, &m
->req_list
, req_list
) {
825 if (rreq
->tag
== rcall
->tag
) {
827 if (req
->flush
!= Flushing
)
828 list_del(&req
->req_list
);
832 spin_unlock(&m
->lock
);
836 process_request(m
, req
);
838 if (req
->flush
!= Flushing
) {
840 (*req
->cb
) (req
, req
->cba
);
847 if (err
>= 0 && rcall
->id
!= P9_RFLUSH
)
848 P9_DPRINTK(P9_DEBUG_ERROR
,
849 "unexpected response mux %p id %d tag %d\n",
850 m
, rcall
->id
, rcall
->tag
);
855 if (!list_empty(&m
->req_list
)) {
856 if (test_and_clear_bit(Rpending
, &m
->wsched
))
859 n
= p9_fd_poll(m
->trans
, NULL
);
862 P9_DPRINTK(P9_DEBUG_MUX
, "schedule read work %p\n", m
);
863 queue_work(p9_mux_wq
, &m
->rq
);
865 clear_bit(Rworksched
, &m
->wsched
);
867 clear_bit(Rworksched
, &m
->wsched
);
872 p9_conn_cancel(m
, err
);
873 clear_bit(Rworksched
, &m
->wsched
);
877 * p9_send_request - send 9P request
878 * The function can sleep until the request is scheduled for sending.
879 * The function can be interrupted. Return from the function is not
880 * a guarantee that the request is sent successfully. Can return errors
881 * that can be retrieved by PTR_ERR macros.
884 * @tc: request to be sent
885 * @cb: callback function to call when response is received
886 * @cba: parameter to pass to the callback function
890 static struct p9_req
*p9_send_request(struct p9_conn
*m
,
892 p9_conn_req_callback cb
, void *cba
)
897 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p task %p tcall %p id %d\n", m
, current
,
900 return ERR_PTR(m
->err
);
902 req
= kmalloc(sizeof(struct p9_req
), GFP_KERNEL
);
904 return ERR_PTR(-ENOMEM
);
906 if (tc
->id
== P9_TVERSION
)
909 n
= p9_mux_get_tag(m
);
912 return ERR_PTR(-ENOMEM
);
916 #ifdef CONFIG_NET_9P_DEBUG
917 if ((p9_debug_level
&P9_DEBUG_FCALL
) == P9_DEBUG_FCALL
) {
920 p9_printfcall(buf
, sizeof(buf
), tc
, m
->extended
);
921 printk(KERN_NOTICE
"<<< %p %s\n", m
, buf
);
925 spin_lock_init(&req
->lock
);
935 list_add_tail(&req
->req_list
, &m
->unsent_req_list
);
936 spin_unlock(&m
->lock
);
938 if (test_and_clear_bit(Wpending
, &m
->wsched
))
941 n
= p9_fd_poll(m
->trans
, NULL
);
943 if (n
& POLLOUT
&& !test_and_set_bit(Wworksched
, &m
->wsched
))
944 queue_work(p9_mux_wq
, &m
->wq
);
949 static void p9_mux_free_request(struct p9_conn
*m
, struct p9_req
*req
)
951 p9_mux_put_tag(m
, req
->tag
);
955 static void p9_mux_flush_cb(struct p9_req
*freq
, void *a
)
959 struct p9_req
*req
, *rreq
, *rptr
;
962 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p tc %p rc %p err %d oldtag %d\n", m
,
963 freq
->tcall
, freq
->rcall
, freq
->err
,
964 freq
->tcall
->params
.tflush
.oldtag
);
967 tag
= freq
->tcall
->params
.tflush
.oldtag
;
969 list_for_each_entry_safe(rreq
, rptr
, &m
->req_list
, req_list
) {
970 if (rreq
->tag
== tag
) {
972 list_del(&req
->req_list
);
976 spin_unlock(&m
->lock
);
979 spin_lock(&req
->lock
);
980 req
->flush
= Flushed
;
981 spin_unlock(&req
->lock
);
984 (*req
->cb
) (req
, req
->cba
);
993 p9_mux_free_request(m
, freq
);
997 p9_mux_flush_request(struct p9_conn
*m
, struct p9_req
*req
)
1000 struct p9_req
*rreq
, *rptr
;
1002 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p req %p tag %d\n", m
, req
, req
->tag
);
1004 /* if a response was received for a request, do nothing */
1005 spin_lock(&req
->lock
);
1006 if (req
->rcall
|| req
->err
) {
1007 spin_unlock(&req
->lock
);
1008 P9_DPRINTK(P9_DEBUG_MUX
,
1009 "mux %p req %p response already received\n", m
, req
);
1013 req
->flush
= Flushing
;
1014 spin_unlock(&req
->lock
);
1016 spin_lock(&m
->lock
);
1017 /* if the request is not sent yet, just remove it from the list */
1018 list_for_each_entry_safe(rreq
, rptr
, &m
->unsent_req_list
, req_list
) {
1019 if (rreq
->tag
== req
->tag
) {
1020 P9_DPRINTK(P9_DEBUG_MUX
,
1021 "mux %p req %p request is not sent yet\n", m
, req
);
1022 list_del(&rreq
->req_list
);
1023 req
->flush
= Flushed
;
1024 spin_unlock(&m
->lock
);
1026 (*req
->cb
) (req
, req
->cba
);
1030 spin_unlock(&m
->lock
);
1032 clear_thread_flag(TIF_SIGPENDING
);
1033 fc
= p9_create_tflush(req
->tag
);
1034 p9_send_request(m
, fc
, p9_mux_flush_cb
, m
);
1039 p9_conn_rpc_cb(struct p9_req
*req
, void *a
)
1041 struct p9_mux_rpc
*r
;
1043 P9_DPRINTK(P9_DEBUG_MUX
, "req %p r %p\n", req
, a
);
1045 r
->rcall
= req
->rcall
;
1048 if (req
->flush
!= None
&& !req
->err
)
1049 r
->err
= -ERESTARTSYS
;
1051 wake_up(&r
->wqueue
);
1055 * p9_fd_rpc- sends 9P request and waits until a response is available.
1056 * The function can be interrupted.
1057 * @t: transport data
1058 * @tc: request to be sent
1059 * @rc: pointer where a pointer to the response is stored
1064 p9_fd_rpc(struct p9_trans
*t
, struct p9_fcall
*tc
, struct p9_fcall
**rc
)
1066 struct p9_trans_fd
*p
= t
->priv
;
1067 struct p9_conn
*m
= p
->conn
;
1068 int err
, sigpending
;
1069 unsigned long flags
;
1071 struct p9_mux_rpc r
;
1077 init_waitqueue_head(&r
.wqueue
);
1083 if (signal_pending(current
)) {
1085 clear_thread_flag(TIF_SIGPENDING
);
1088 req
= p9_send_request(m
, tc
, p9_conn_rpc_cb
, &r
);
1091 P9_DPRINTK(P9_DEBUG_MUX
, "error %d\n", err
);
1095 err
= wait_event_interruptible(r
.wqueue
, r
.rcall
!= NULL
|| r
.err
< 0);
1099 if (err
== -ERESTARTSYS
&& m
->trans
->status
== Connected
1101 if (p9_mux_flush_request(m
, req
)) {
1102 /* wait until we get response of the flush message */
1104 clear_thread_flag(TIF_SIGPENDING
);
1105 err
= wait_event_interruptible(r
.wqueue
,
1107 } while (!r
.rcall
&& !r
.err
&& err
== -ERESTARTSYS
&&
1108 m
->trans
->status
== Connected
&& !m
->err
);
1116 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
1117 recalc_sigpending();
1118 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
1126 p9_mux_free_request(m
, req
);
1135 * p9_conn_rpcnb - sends 9P request without waiting for response.
1137 * @tc: request to be sent
1138 * @cb: callback function to be called when response arrives
1139 * @a: value to pass to the callback function
1143 int p9_conn_rpcnb(struct p9_conn
*m
, struct p9_fcall
*tc
,
1144 p9_conn_req_callback cb
, void *a
)
1149 req
= p9_send_request(m
, tc
, cb
, a
);
1152 P9_DPRINTK(P9_DEBUG_MUX
, "error %d\n", err
);
1153 return PTR_ERR(req
);
1156 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p tc %p tag %d\n", m
, tc
, req
->tag
);
1159 #endif /* P9_NONBLOCK */
1162 * p9_conn_cancel - cancel all pending requests with error
1168 void p9_conn_cancel(struct p9_conn
*m
, int err
)
1170 struct p9_req
*req
, *rtmp
;
1171 LIST_HEAD(cancel_list
);
1173 P9_DPRINTK(P9_DEBUG_ERROR
, "mux %p err %d\n", m
, err
);
1175 spin_lock(&m
->lock
);
1176 list_for_each_entry_safe(req
, rtmp
, &m
->req_list
, req_list
) {
1177 list_move(&req
->req_list
, &cancel_list
);
1179 list_for_each_entry_safe(req
, rtmp
, &m
->unsent_req_list
, req_list
) {
1180 list_move(&req
->req_list
, &cancel_list
);
1182 spin_unlock(&m
->lock
);
1184 list_for_each_entry_safe(req
, rtmp
, &cancel_list
, req_list
) {
1185 list_del(&req
->req_list
);
1190 (*req
->cb
) (req
, req
->cba
);
1195 wake_up(&m
->equeue
);
1199 * parse_options - parse mount options into session structure
1200 * @options: options string passed from mount
1201 * @opts: transport-specific structure to parse options into
1203 * Returns 0 upon success, -ERRNO upon failure
1206 static int parse_opts(char *params
, struct p9_fd_opts
*opts
)
1209 substring_t args
[MAX_OPT_ARGS
];
1214 opts
->port
= P9_PORT
;
1221 options
= kstrdup(params
, GFP_KERNEL
);
1223 P9_DPRINTK(P9_DEBUG_ERROR
,
1224 "failed to allocate copy of option string\n");
1228 while ((p
= strsep(&options
, ",")) != NULL
) {
1233 token
= match_token(p
, tokens
, args
);
1234 r
= match_int(&args
[0], &option
);
1236 P9_DPRINTK(P9_DEBUG_ERROR
,
1237 "integer field, but no integer?\n");
1243 opts
->port
= option
;
1259 static int p9_fd_open(struct p9_trans
*trans
, int rfd
, int wfd
)
1261 struct p9_trans_fd
*ts
= kmalloc(sizeof(struct p9_trans_fd
),
1268 if (!ts
->rd
|| !ts
->wr
) {
1278 trans
->status
= Connected
;
1283 static int p9_socket_open(struct p9_trans
*trans
, struct socket
*csocket
)
1287 csocket
->sk
->sk_allocation
= GFP_NOIO
;
1288 fd
= sock_map_fd(csocket
, 0);
1290 P9_EPRINTK(KERN_ERR
, "p9_socket_open: failed to map fd\n");
1294 ret
= p9_fd_open(trans
, fd
, fd
);
1296 P9_EPRINTK(KERN_ERR
, "p9_socket_open: failed to open fd\n");
1297 sockfd_put(csocket
);
1301 ((struct p9_trans_fd
*)trans
->priv
)->rd
->f_flags
|= O_NONBLOCK
;
1307 * p9_fd_read- read from a fd
1308 * @trans: transport instance state
1309 * @v: buffer to receive data into
1310 * @len: size of receive buffer
1314 static int p9_fd_read(struct p9_trans
*trans
, void *v
, int len
)
1317 struct p9_trans_fd
*ts
= NULL
;
1319 if (trans
&& trans
->status
!= Disconnected
)
1325 if (!(ts
->rd
->f_flags
& O_NONBLOCK
))
1326 P9_DPRINTK(P9_DEBUG_ERROR
, "blocking read ...\n");
1328 ret
= kernel_read(ts
->rd
, ts
->rd
->f_pos
, v
, len
);
1329 if (ret
<= 0 && ret
!= -ERESTARTSYS
&& ret
!= -EAGAIN
)
1330 trans
->status
= Disconnected
;
1335 * p9_fd_write - write to a socket
1336 * @trans: transport instance state
1337 * @v: buffer to send data from
1338 * @len: size of send buffer
1342 static int p9_fd_write(struct p9_trans
*trans
, void *v
, int len
)
1346 struct p9_trans_fd
*ts
= NULL
;
1348 if (trans
&& trans
->status
!= Disconnected
)
1354 if (!(ts
->wr
->f_flags
& O_NONBLOCK
))
1355 P9_DPRINTK(P9_DEBUG_ERROR
, "blocking write ...\n");
1359 /* The cast to a user pointer is valid due to the set_fs() */
1360 ret
= vfs_write(ts
->wr
, (void __user
*)v
, len
, &ts
->wr
->f_pos
);
1363 if (ret
<= 0 && ret
!= -ERESTARTSYS
&& ret
!= -EAGAIN
)
1364 trans
->status
= Disconnected
;
1369 p9_fd_poll(struct p9_trans
*trans
, struct poll_table_struct
*pt
)
1372 struct p9_trans_fd
*ts
= NULL
;
1375 if (trans
&& trans
->status
== Connected
)
1381 if (!ts
->rd
->f_op
|| !ts
->rd
->f_op
->poll
)
1384 if (!ts
->wr
->f_op
|| !ts
->wr
->f_op
->poll
)
1390 ret
= ts
->rd
->f_op
->poll(ts
->rd
, pt
);
1394 if (ts
->rd
!= ts
->wr
) {
1395 n
= ts
->wr
->f_op
->poll(ts
->wr
, pt
);
1400 ret
= (ret
& ~POLLOUT
) | (n
& ~POLLIN
);
1409 * p9_fd_close - shutdown socket
1410 * @trans: private socket structure
1414 static void p9_fd_close(struct p9_trans
*trans
)
1416 struct p9_trans_fd
*ts
;
1421 ts
= xchg(&trans
->priv
, NULL
);
1426 p9_conn_destroy(ts
->conn
);
1428 trans
->status
= Disconnected
;
1437 * stolen from NFS - maybe should be made a generic function?
1439 static inline int valid_ipaddr4(const char *buf
)
1441 int rc
, count
, in
[4];
1443 rc
= sscanf(buf
, "%d.%d.%d.%d", &in
[0], &in
[1], &in
[2], &in
[3]);
1446 for (count
= 0; count
< 4; count
++) {
1447 if (in
[count
] > 255)
1453 static struct p9_trans
*
1454 p9_trans_create_tcp(const char *addr
, char *args
, int msize
, unsigned char dotu
)
1457 struct p9_trans
*trans
;
1458 struct socket
*csocket
;
1459 struct sockaddr_in sin_server
;
1460 struct p9_fd_opts opts
;
1461 struct p9_trans_fd
*p
;
1463 err
= parse_opts(args
, &opts
);
1465 return ERR_PTR(err
);
1467 if (valid_ipaddr4(addr
) < 0)
1468 return ERR_PTR(-EINVAL
);
1471 trans
= kmalloc(sizeof(struct p9_trans
), GFP_KERNEL
);
1473 return ERR_PTR(-ENOMEM
);
1474 trans
->msize
= msize
;
1475 trans
->extended
= dotu
;
1476 trans
->rpc
= p9_fd_rpc
;
1477 trans
->close
= p9_fd_close
;
1479 sin_server
.sin_family
= AF_INET
;
1480 sin_server
.sin_addr
.s_addr
= in_aton(addr
);
1481 sin_server
.sin_port
= htons(opts
.port
);
1482 sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &csocket
);
1485 P9_EPRINTK(KERN_ERR
, "p9_trans_tcp: problem creating socket\n");
1490 err
= csocket
->ops
->connect(csocket
,
1491 (struct sockaddr
*)&sin_server
,
1492 sizeof(struct sockaddr_in
), 0);
1494 P9_EPRINTK(KERN_ERR
,
1495 "p9_trans_tcp: problem connecting socket to %s\n",
1500 err
= p9_socket_open(trans
, csocket
);
1504 p
= (struct p9_trans_fd
*) trans
->priv
;
1505 p
->conn
= p9_conn_create(trans
);
1506 if (IS_ERR(p
->conn
)) {
1507 err
= PTR_ERR(p
->conn
);
1516 sock_release(csocket
);
1519 return ERR_PTR(err
);
1522 static struct p9_trans
*
1523 p9_trans_create_unix(const char *addr
, char *args
, int msize
,
1527 struct socket
*csocket
;
1528 struct sockaddr_un sun_server
;
1529 struct p9_trans
*trans
;
1530 struct p9_trans_fd
*p
;
1533 trans
= kmalloc(sizeof(struct p9_trans
), GFP_KERNEL
);
1535 return ERR_PTR(-ENOMEM
);
1537 trans
->rpc
= p9_fd_rpc
;
1538 trans
->close
= p9_fd_close
;
1540 if (strlen(addr
) > UNIX_PATH_MAX
) {
1541 P9_EPRINTK(KERN_ERR
, "p9_trans_unix: address too long: %s\n",
1543 err
= -ENAMETOOLONG
;
1547 sun_server
.sun_family
= PF_UNIX
;
1548 strcpy(sun_server
.sun_path
, addr
);
1549 sock_create_kern(PF_UNIX
, SOCK_STREAM
, 0, &csocket
);
1550 err
= csocket
->ops
->connect(csocket
, (struct sockaddr
*)&sun_server
,
1551 sizeof(struct sockaddr_un
) - 1, 0);
1553 P9_EPRINTK(KERN_ERR
,
1554 "p9_trans_unix: problem connecting socket: %s: %d\n",
1559 err
= p9_socket_open(trans
, csocket
);
1563 trans
->msize
= msize
;
1564 trans
->extended
= dotu
;
1565 p
= (struct p9_trans_fd
*) trans
->priv
;
1566 p
->conn
= p9_conn_create(trans
);
1567 if (IS_ERR(p
->conn
)) {
1568 err
= PTR_ERR(p
->conn
);
1577 sock_release(csocket
);
1580 return ERR_PTR(err
);
1583 static struct p9_trans
*
1584 p9_trans_create_fd(const char *name
, char *args
, int msize
,
1585 unsigned char extended
)
1588 struct p9_trans
*trans
;
1589 struct p9_fd_opts opts
;
1590 struct p9_trans_fd
*p
;
1592 parse_opts(args
, &opts
);
1594 if (opts
.rfd
== ~0 || opts
.wfd
== ~0) {
1595 printk(KERN_ERR
"v9fs: Insufficient options for proto=fd\n");
1596 return ERR_PTR(-ENOPROTOOPT
);
1599 trans
= kmalloc(sizeof(struct p9_trans
), GFP_KERNEL
);
1601 return ERR_PTR(-ENOMEM
);
1603 trans
->rpc
= p9_fd_rpc
;
1604 trans
->close
= p9_fd_close
;
1606 err
= p9_fd_open(trans
, opts
.rfd
, opts
.wfd
);
1610 trans
->msize
= msize
;
1611 trans
->extended
= extended
;
1612 p
= (struct p9_trans_fd
*) trans
->priv
;
1613 p
->conn
= p9_conn_create(trans
);
1614 if (IS_ERR(p
->conn
)) {
1615 err
= PTR_ERR(p
->conn
);
1624 return ERR_PTR(err
);
1627 static struct p9_trans_module p9_tcp_trans
= {
1629 .maxsize
= MAX_SOCK_BUF
,
1631 .create
= p9_trans_create_tcp
,
1634 static struct p9_trans_module p9_unix_trans
= {
1636 .maxsize
= MAX_SOCK_BUF
,
1638 .create
= p9_trans_create_unix
,
1641 static struct p9_trans_module p9_fd_trans
= {
1643 .maxsize
= MAX_SOCK_BUF
,
1645 .create
= p9_trans_create_fd
,
1648 int p9_trans_fd_init(void)
1650 int ret
= p9_mux_global_init();
1652 printk(KERN_WARNING
"9p: starting mux failed\n");
1656 v9fs_register_trans(&p9_tcp_trans
);
1657 v9fs_register_trans(&p9_unix_trans
);
1658 v9fs_register_trans(&p9_fd_trans
);
1662 EXPORT_SYMBOL(p9_trans_fd_init
);