6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
26 #include <linux/module.h>
27 #include <linux/errno.h>
29 #include <linux/poll.h>
30 #include <linux/kthread.h>
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <net/9p/9p.h>
34 #include <linux/parser.h>
35 #include <net/9p/transport.h>
36 #include <net/9p/conn.h>
39 #define SCHED_TIMEOUT 10
40 #define MAXPOLLWADDR 2
43 Rworksched
= 1, /* read work scheduled or running */
44 Rpending
= 2, /* can read */
45 Wworksched
= 4, /* write work scheduled or running */
46 Wpending
= 8, /* can write */
55 struct p9_mux_poll_task
;
58 spinlock_t lock
; /* protect request structure */
60 struct p9_fcall
*tcall
;
61 struct p9_fcall
*rcall
;
63 p9_conn_req_callback cb
;
66 struct list_head req_list
;
70 spinlock_t lock
; /* protect lock structure */
71 struct list_head mux_list
;
72 struct p9_mux_poll_task
*poll_task
;
74 unsigned char *extended
;
75 struct p9_trans
*trans
;
76 struct p9_idpool
*tagpool
;
78 wait_queue_head_t equeue
;
79 struct list_head req_list
;
80 struct list_head unsent_req_list
;
81 struct p9_fcall
*rcall
;
87 wait_queue_t poll_wait
[MAXPOLLWADDR
];
88 wait_queue_head_t
*poll_waddr
[MAXPOLLWADDR
];
90 struct work_struct rq
;
91 struct work_struct wq
;
95 struct p9_mux_poll_task
{
96 struct task_struct
*task
;
97 struct list_head mux_list
;
104 struct p9_fcall
*tcall
;
105 struct p9_fcall
*rcall
;
106 wait_queue_head_t wqueue
;
109 static int p9_poll_proc(void *);
110 static void p9_read_work(struct work_struct
*work
);
111 static void p9_write_work(struct work_struct
*work
);
112 static void p9_pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
,
114 static u16
p9_mux_get_tag(struct p9_conn
*);
115 static void p9_mux_put_tag(struct p9_conn
*, u16
);
117 static DEFINE_MUTEX(p9_mux_task_lock
);
118 static struct workqueue_struct
*p9_mux_wq
;
120 static int p9_mux_num
;
121 static int p9_mux_poll_task_num
;
122 static struct p9_mux_poll_task p9_mux_poll_tasks
[100];
124 int p9_mux_global_init(void)
128 for (i
= 0; i
< ARRAY_SIZE(p9_mux_poll_tasks
); i
++)
129 p9_mux_poll_tasks
[i
].task
= NULL
;
131 p9_mux_wq
= create_workqueue("v9fs");
133 printk(KERN_WARNING
"v9fs: mux: creating workqueue failed\n");
140 void p9_mux_global_exit(void)
142 destroy_workqueue(p9_mux_wq
);
146 * p9_mux_calc_poll_procs - calculates the number of polling procs
147 * based on the number of mounted v9fs filesystems.
149 * The current implementation returns sqrt of the number of mounts.
151 static int p9_mux_calc_poll_procs(int muxnum
)
155 if (p9_mux_poll_task_num
)
156 n
= muxnum
/ p9_mux_poll_task_num
+
157 (muxnum
% p9_mux_poll_task_num
? 1 : 0);
161 if (n
> ARRAY_SIZE(p9_mux_poll_tasks
))
162 n
= ARRAY_SIZE(p9_mux_poll_tasks
);
167 static int p9_mux_poll_start(struct p9_conn
*m
)
170 struct p9_mux_poll_task
*vpt
, *vptlast
;
171 struct task_struct
*pproc
;
173 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p muxnum %d procnum %d\n", m
, p9_mux_num
,
174 p9_mux_poll_task_num
);
175 mutex_lock(&p9_mux_task_lock
);
177 n
= p9_mux_calc_poll_procs(p9_mux_num
+ 1);
178 if (n
> p9_mux_poll_task_num
) {
179 for (i
= 0; i
< ARRAY_SIZE(p9_mux_poll_tasks
); i
++) {
180 if (p9_mux_poll_tasks
[i
].task
== NULL
) {
181 vpt
= &p9_mux_poll_tasks
[i
];
182 P9_DPRINTK(P9_DEBUG_MUX
, "create proc %p\n",
184 pproc
= kthread_create(p9_poll_proc
, vpt
,
187 if (!IS_ERR(pproc
)) {
189 INIT_LIST_HEAD(&vpt
->mux_list
);
191 p9_mux_poll_task_num
++;
192 wake_up_process(vpt
->task
);
198 if (i
>= ARRAY_SIZE(p9_mux_poll_tasks
))
199 P9_DPRINTK(P9_DEBUG_ERROR
,
200 "warning: no free poll slots\n");
203 n
= (p9_mux_num
+ 1) / p9_mux_poll_task_num
+
204 ((p9_mux_num
+ 1) % p9_mux_poll_task_num
? 1 : 0);
207 for (i
= 0; i
< ARRAY_SIZE(p9_mux_poll_tasks
); i
++) {
208 vpt
= &p9_mux_poll_tasks
[i
];
209 if (vpt
->task
!= NULL
) {
211 if (vpt
->muxnum
< n
) {
212 P9_DPRINTK(P9_DEBUG_MUX
, "put in proc %d\n", i
);
213 list_add(&m
->mux_list
, &vpt
->mux_list
);
216 memset(&m
->poll_waddr
, 0,
217 sizeof(m
->poll_waddr
));
218 init_poll_funcptr(&m
->pt
, p9_pollwait
);
224 if (i
>= ARRAY_SIZE(p9_mux_poll_tasks
)) {
225 if (vptlast
== NULL
) {
226 mutex_unlock(&p9_mux_task_lock
);
230 P9_DPRINTK(P9_DEBUG_MUX
, "put in proc %d\n", i
);
231 list_add(&m
->mux_list
, &vptlast
->mux_list
);
233 m
->poll_task
= vptlast
;
234 memset(&m
->poll_waddr
, 0, sizeof(m
->poll_waddr
));
235 init_poll_funcptr(&m
->pt
, p9_pollwait
);
239 mutex_unlock(&p9_mux_task_lock
);
244 static void p9_mux_poll_stop(struct p9_conn
*m
)
247 struct p9_mux_poll_task
*vpt
;
249 mutex_lock(&p9_mux_task_lock
);
251 list_del(&m
->mux_list
);
252 for (i
= 0; i
< ARRAY_SIZE(m
->poll_waddr
); i
++) {
253 if (m
->poll_waddr
[i
] != NULL
) {
254 remove_wait_queue(m
->poll_waddr
[i
], &m
->poll_wait
[i
]);
255 m
->poll_waddr
[i
] = NULL
;
260 P9_DPRINTK(P9_DEBUG_MUX
, "destroy proc %p\n", vpt
);
261 kthread_stop(vpt
->task
);
263 p9_mux_poll_task_num
--;
266 mutex_unlock(&p9_mux_task_lock
);
270 * p9_conn_create - allocate and initialize the per-session mux data
271 * Creates the polling task if this is the first session.
273 * @trans - transport structure
274 * @msize - maximum message size
275 * @extended - pointer to the extended flag
277 struct p9_conn
*p9_conn_create(struct p9_trans
*trans
, int msize
,
278 unsigned char *extended
)
281 struct p9_conn
*m
, *mtmp
;
283 P9_DPRINTK(P9_DEBUG_MUX
, "transport %p msize %d\n", trans
, msize
);
284 m
= kmalloc(sizeof(struct p9_conn
), GFP_KERNEL
);
286 return ERR_PTR(-ENOMEM
);
288 spin_lock_init(&m
->lock
);
289 INIT_LIST_HEAD(&m
->mux_list
);
291 m
->extended
= extended
;
293 m
->tagpool
= p9_idpool_create();
294 if (IS_ERR(m
->tagpool
)) {
295 mtmp
= ERR_PTR(-ENOMEM
);
301 init_waitqueue_head(&m
->equeue
);
302 INIT_LIST_HEAD(&m
->req_list
);
303 INIT_LIST_HEAD(&m
->unsent_req_list
);
307 m
->wpos
= m
->wsize
= 0;
309 INIT_WORK(&m
->rq
, p9_read_work
);
310 INIT_WORK(&m
->wq
, p9_write_work
);
312 memset(&m
->poll_waddr
, 0, sizeof(m
->poll_waddr
));
314 n
= p9_mux_poll_start(m
);
320 n
= trans
->poll(trans
, &m
->pt
);
322 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can read\n", m
);
323 set_bit(Rpending
, &m
->wsched
);
327 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can write\n", m
);
328 set_bit(Wpending
, &m
->wsched
);
331 for (i
= 0; i
< ARRAY_SIZE(m
->poll_waddr
); i
++) {
332 if (IS_ERR(m
->poll_waddr
[i
])) {
334 mtmp
= (void *)m
->poll_waddr
; /* the error code */
343 EXPORT_SYMBOL(p9_conn_create
);
346 * p9_mux_destroy - cancels all pending requests and frees mux resources
348 void p9_conn_destroy(struct p9_conn
*m
)
350 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p prev %p next %p\n", m
,
351 m
->mux_list
.prev
, m
->mux_list
.next
);
352 p9_conn_cancel(m
, -ECONNRESET
);
354 if (!list_empty(&m
->req_list
)) {
355 /* wait until all processes waiting on this session exit */
356 P9_DPRINTK(P9_DEBUG_MUX
,
357 "mux %p waiting for empty request queue\n", m
);
358 wait_event_timeout(m
->equeue
, (list_empty(&m
->req_list
)), 5000);
359 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p request queue empty: %d\n", m
,
360 list_empty(&m
->req_list
));
365 p9_idpool_destroy(m
->tagpool
);
368 EXPORT_SYMBOL(p9_conn_destroy
);
371 * p9_pollwait - called by files poll operation to add v9fs-poll task
372 * to files wait queue
375 p9_pollwait(struct file
*filp
, wait_queue_head_t
*wait_address
,
381 m
= container_of(p
, struct p9_conn
, pt
);
382 for (i
= 0; i
< ARRAY_SIZE(m
->poll_waddr
); i
++)
383 if (m
->poll_waddr
[i
] == NULL
)
386 if (i
>= ARRAY_SIZE(m
->poll_waddr
)) {
387 P9_DPRINTK(P9_DEBUG_ERROR
, "not enough wait_address slots\n");
391 m
->poll_waddr
[i
] = wait_address
;
394 P9_DPRINTK(P9_DEBUG_ERROR
, "no wait_address\n");
395 m
->poll_waddr
[i
] = ERR_PTR(-EIO
);
399 init_waitqueue_entry(&m
->poll_wait
[i
], m
->poll_task
->task
);
400 add_wait_queue(wait_address
, &m
->poll_wait
[i
]);
404 * p9_poll_mux - polls a mux and schedules read or write works if necessary
406 static void p9_poll_mux(struct p9_conn
*m
)
413 n
= m
->trans
->poll(m
->trans
, NULL
);
414 if (n
< 0 || n
& (POLLERR
| POLLHUP
| POLLNVAL
)) {
415 P9_DPRINTK(P9_DEBUG_MUX
, "error mux %p err %d\n", m
, n
);
418 p9_conn_cancel(m
, n
);
422 set_bit(Rpending
, &m
->wsched
);
423 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can read\n", m
);
424 if (!test_and_set_bit(Rworksched
, &m
->wsched
)) {
425 P9_DPRINTK(P9_DEBUG_MUX
, "schedule read work %p\n", m
);
426 queue_work(p9_mux_wq
, &m
->rq
);
431 set_bit(Wpending
, &m
->wsched
);
432 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p can write\n", m
);
433 if ((m
->wsize
|| !list_empty(&m
->unsent_req_list
))
434 && !test_and_set_bit(Wworksched
, &m
->wsched
)) {
435 P9_DPRINTK(P9_DEBUG_MUX
, "schedule write work %p\n", m
);
436 queue_work(p9_mux_wq
, &m
->wq
);
442 * p9_poll_proc - polls all v9fs transports for new events and queues
443 * the appropriate work to the work queue
445 static int p9_poll_proc(void *a
)
447 struct p9_conn
*m
, *mtmp
;
448 struct p9_mux_poll_task
*vpt
;
451 P9_DPRINTK(P9_DEBUG_MUX
, "start %p %p\n", current
, vpt
);
452 while (!kthread_should_stop()) {
453 set_current_state(TASK_INTERRUPTIBLE
);
455 list_for_each_entry_safe(m
, mtmp
, &vpt
->mux_list
, mux_list
) {
459 P9_DPRINTK(P9_DEBUG_MUX
, "sleeping...\n");
460 schedule_timeout(SCHED_TIMEOUT
* HZ
);
463 __set_current_state(TASK_RUNNING
);
464 P9_DPRINTK(P9_DEBUG_MUX
, "finish\n");
469 * p9_write_work - called when a transport can send some data
471 static void p9_write_work(struct work_struct
*work
)
477 m
= container_of(work
, struct p9_conn
, wq
);
480 clear_bit(Wworksched
, &m
->wsched
);
485 if (list_empty(&m
->unsent_req_list
)) {
486 clear_bit(Wworksched
, &m
->wsched
);
492 req
= list_entry(m
->unsent_req_list
.next
, struct p9_req
,
494 list_move_tail(&req
->req_list
, &m
->req_list
);
495 if (req
->err
== ERREQFLUSH
)
498 m
->wbuf
= req
->tcall
->sdata
;
499 m
->wsize
= req
->tcall
->size
;
501 spin_unlock(&m
->lock
);
504 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p pos %d size %d\n", m
, m
->wpos
,
506 clear_bit(Wpending
, &m
->wsched
);
507 err
= m
->trans
->write(m
->trans
, m
->wbuf
+ m
->wpos
, m
->wsize
- m
->wpos
);
508 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p sent %d bytes\n", m
, err
);
509 if (err
== -EAGAIN
) {
510 clear_bit(Wworksched
, &m
->wsched
);
522 if (m
->wpos
== m
->wsize
)
523 m
->wpos
= m
->wsize
= 0;
525 if (m
->wsize
== 0 && !list_empty(&m
->unsent_req_list
)) {
526 if (test_and_clear_bit(Wpending
, &m
->wsched
))
529 n
= m
->trans
->poll(m
->trans
, NULL
);
532 P9_DPRINTK(P9_DEBUG_MUX
, "schedule write work %p\n", m
);
533 queue_work(p9_mux_wq
, &m
->wq
);
535 clear_bit(Wworksched
, &m
->wsched
);
537 clear_bit(Wworksched
, &m
->wsched
);
542 p9_conn_cancel(m
, err
);
543 clear_bit(Wworksched
, &m
->wsched
);
546 static void process_request(struct p9_conn
*m
, struct p9_req
*req
)
549 struct p9_str
*ename
;
551 if (!req
->err
&& req
->rcall
->id
== P9_RERROR
) {
552 ecode
= req
->rcall
->params
.rerror
.errno
;
553 ename
= &req
->rcall
->params
.rerror
.error
;
555 P9_DPRINTK(P9_DEBUG_MUX
, "Rerror %.*s\n", ename
->len
,
562 req
->err
= p9_errstr2errno(ename
->str
, ename
->len
);
564 if (!req
->err
) { /* string match failed */
565 PRINT_FCALL_ERROR("unknown error", req
->rcall
);
569 req
->err
= -ESERVERFAULT
;
571 } else if (req
->tcall
&& req
->rcall
->id
!= req
->tcall
->id
+ 1) {
572 P9_DPRINTK(P9_DEBUG_ERROR
,
573 "fcall mismatch: expected %d, got %d\n",
574 req
->tcall
->id
+ 1, req
->rcall
->id
);
581 * p9_read_work - called when there is some data to be read from a transport
583 static void p9_read_work(struct work_struct
*work
)
587 struct p9_req
*req
, *rptr
, *rreq
;
588 struct p9_fcall
*rcall
;
591 m
= container_of(work
, struct p9_conn
, rq
);
597 P9_DPRINTK(P9_DEBUG_MUX
, "start mux %p pos %d\n", m
, m
->rpos
);
601 kmalloc(sizeof(struct p9_fcall
) + m
->msize
, GFP_KERNEL
);
607 m
->rbuf
= (char *)m
->rcall
+ sizeof(struct p9_fcall
);
611 clear_bit(Rpending
, &m
->wsched
);
612 err
= m
->trans
->read(m
->trans
, m
->rbuf
+ m
->rpos
, m
->msize
- m
->rpos
);
613 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p got %d bytes\n", m
, err
);
614 if (err
== -EAGAIN
) {
615 clear_bit(Rworksched
, &m
->wsched
);
623 while (m
->rpos
> 4) {
624 n
= le32_to_cpu(*(__le32
*) m
->rbuf
);
626 P9_DPRINTK(P9_DEBUG_ERROR
,
627 "requested packet size too big: %d\n", n
);
636 p9_deserialize_fcall(m
->rbuf
, n
, m
->rcall
, *m
->extended
);
641 #ifdef CONFIG_NET_9P_DEBUG
642 if ((p9_debug_level
&P9_DEBUG_FCALL
) == P9_DEBUG_FCALL
) {
645 p9_printfcall(buf
, sizeof(buf
), m
->rcall
,
647 printk(KERN_NOTICE
">>> %p %s\n", m
, buf
);
654 m
->rcall
= kmalloc(sizeof(struct p9_fcall
) + m
->msize
,
661 m
->rbuf
= (char *)m
->rcall
+ sizeof(struct p9_fcall
);
662 memmove(m
->rbuf
, rbuf
+ n
, m
->rpos
- n
);
670 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p fcall id %d tag %d\n", m
,
671 rcall
->id
, rcall
->tag
);
675 list_for_each_entry_safe(rreq
, rptr
, &m
->req_list
, req_list
) {
676 if (rreq
->tag
== rcall
->tag
) {
678 if (req
->flush
!= Flushing
)
679 list_del(&req
->req_list
);
683 spin_unlock(&m
->lock
);
687 process_request(m
, req
);
689 if (req
->flush
!= Flushing
) {
691 (*req
->cb
) (req
, req
->cba
);
698 if (err
>= 0 && rcall
->id
!= P9_RFLUSH
)
699 P9_DPRINTK(P9_DEBUG_ERROR
,
700 "unexpected response mux %p id %d tag %d\n",
701 m
, rcall
->id
, rcall
->tag
);
706 if (!list_empty(&m
->req_list
)) {
707 if (test_and_clear_bit(Rpending
, &m
->wsched
))
710 n
= m
->trans
->poll(m
->trans
, NULL
);
713 P9_DPRINTK(P9_DEBUG_MUX
, "schedule read work %p\n", m
);
714 queue_work(p9_mux_wq
, &m
->rq
);
716 clear_bit(Rworksched
, &m
->wsched
);
718 clear_bit(Rworksched
, &m
->wsched
);
723 p9_conn_cancel(m
, err
);
724 clear_bit(Rworksched
, &m
->wsched
);
728 * p9_send_request - send 9P request
729 * The function can sleep until the request is scheduled for sending.
730 * The function can be interrupted. Return from the function is not
731 * a guarantee that the request is sent successfully. Can return errors
732 * that can be retrieved by PTR_ERR macros.
735 * @tc: request to be sent
736 * @cb: callback function to call when response is received
737 * @cba: parameter to pass to the callback function
739 static struct p9_req
*p9_send_request(struct p9_conn
*m
,
741 p9_conn_req_callback cb
, void *cba
)
746 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p task %p tcall %p id %d\n", m
, current
,
749 return ERR_PTR(m
->err
);
751 req
= kmalloc(sizeof(struct p9_req
), GFP_KERNEL
);
753 return ERR_PTR(-ENOMEM
);
755 if (tc
->id
== P9_TVERSION
)
758 n
= p9_mux_get_tag(m
);
761 return ERR_PTR(-ENOMEM
);
765 #ifdef CONFIG_NET_9P_DEBUG
766 if ((p9_debug_level
&P9_DEBUG_FCALL
) == P9_DEBUG_FCALL
) {
769 p9_printfcall(buf
, sizeof(buf
), tc
, *m
->extended
);
770 printk(KERN_NOTICE
"<<< %p %s\n", m
, buf
);
774 spin_lock_init(&req
->lock
);
784 list_add_tail(&req
->req_list
, &m
->unsent_req_list
);
785 spin_unlock(&m
->lock
);
787 if (test_and_clear_bit(Wpending
, &m
->wsched
))
790 n
= m
->trans
->poll(m
->trans
, NULL
);
792 if (n
& POLLOUT
&& !test_and_set_bit(Wworksched
, &m
->wsched
))
793 queue_work(p9_mux_wq
, &m
->wq
);
798 static void p9_mux_free_request(struct p9_conn
*m
, struct p9_req
*req
)
800 p9_mux_put_tag(m
, req
->tag
);
804 static void p9_mux_flush_cb(struct p9_req
*freq
, void *a
)
806 p9_conn_req_callback cb
;
809 struct p9_req
*req
, *rreq
, *rptr
;
812 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p tc %p rc %p err %d oldtag %d\n", m
,
813 freq
->tcall
, freq
->rcall
, freq
->err
,
814 freq
->tcall
->params
.tflush
.oldtag
);
818 tag
= freq
->tcall
->params
.tflush
.oldtag
;
820 list_for_each_entry_safe(rreq
, rptr
, &m
->req_list
, req_list
) {
821 if (rreq
->tag
== tag
) {
823 list_del(&req
->req_list
);
827 spin_unlock(&m
->lock
);
830 spin_lock(&req
->lock
);
831 req
->flush
= Flushed
;
832 spin_unlock(&req
->lock
);
835 (*req
->cb
) (req
, req
->cba
);
844 p9_mux_free_request(m
, freq
);
848 p9_mux_flush_request(struct p9_conn
*m
, struct p9_req
*req
)
851 struct p9_req
*rreq
, *rptr
;
853 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p req %p tag %d\n", m
, req
, req
->tag
);
855 /* if a response was received for a request, do nothing */
856 spin_lock(&req
->lock
);
857 if (req
->rcall
|| req
->err
) {
858 spin_unlock(&req
->lock
);
859 P9_DPRINTK(P9_DEBUG_MUX
,
860 "mux %p req %p response already received\n", m
, req
);
864 req
->flush
= Flushing
;
865 spin_unlock(&req
->lock
);
868 /* if the request is not sent yet, just remove it from the list */
869 list_for_each_entry_safe(rreq
, rptr
, &m
->unsent_req_list
, req_list
) {
870 if (rreq
->tag
== req
->tag
) {
871 P9_DPRINTK(P9_DEBUG_MUX
,
872 "mux %p req %p request is not sent yet\n", m
, req
);
873 list_del(&rreq
->req_list
);
874 req
->flush
= Flushed
;
875 spin_unlock(&m
->lock
);
877 (*req
->cb
) (req
, req
->cba
);
881 spin_unlock(&m
->lock
);
883 clear_thread_flag(TIF_SIGPENDING
);
884 fc
= p9_create_tflush(req
->tag
);
885 p9_send_request(m
, fc
, p9_mux_flush_cb
, m
);
890 p9_conn_rpc_cb(struct p9_req
*req
, void *a
)
892 struct p9_mux_rpc
*r
;
894 P9_DPRINTK(P9_DEBUG_MUX
, "req %p r %p\n", req
, a
);
896 r
->rcall
= req
->rcall
;
899 if (req
->flush
!= None
&& !req
->err
)
900 r
->err
= -ERESTARTSYS
;
906 * p9_mux_rpc - sends 9P request and waits until a response is available.
907 * The function can be interrupted.
909 * @tc: request to be sent
910 * @rc: pointer where a pointer to the response is stored
913 p9_conn_rpc(struct p9_conn
*m
, struct p9_fcall
*tc
,
914 struct p9_fcall
**rc
)
925 init_waitqueue_head(&r
.wqueue
);
931 if (signal_pending(current
)) {
933 clear_thread_flag(TIF_SIGPENDING
);
936 req
= p9_send_request(m
, tc
, p9_conn_rpc_cb
, &r
);
939 P9_DPRINTK(P9_DEBUG_MUX
, "error %d\n", err
);
943 err
= wait_event_interruptible(r
.wqueue
, r
.rcall
!= NULL
|| r
.err
< 0);
947 if (err
== -ERESTARTSYS
&& m
->trans
->status
== Connected
949 if (p9_mux_flush_request(m
, req
)) {
950 /* wait until we get response of the flush message */
952 clear_thread_flag(TIF_SIGPENDING
);
953 err
= wait_event_interruptible(r
.wqueue
,
955 } while (!r
.rcall
&& !r
.err
&& err
== -ERESTARTSYS
&&
956 m
->trans
->status
== Connected
&& !m
->err
);
964 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
966 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
974 p9_mux_free_request(m
, req
);
980 EXPORT_SYMBOL(p9_conn_rpc
);
984 * p9_conn_rpcnb - sends 9P request without waiting for response.
986 * @tc: request to be sent
987 * @cb: callback function to be called when response arrives
988 * @cba: value to pass to the callback function
990 int p9_conn_rpcnb(struct p9_conn
*m
, struct p9_fcall
*tc
,
991 p9_conn_req_callback cb
, void *a
)
996 req
= p9_send_request(m
, tc
, cb
, a
);
999 P9_DPRINTK(P9_DEBUG_MUX
, "error %d\n", err
);
1000 return PTR_ERR(req
);
1003 P9_DPRINTK(P9_DEBUG_MUX
, "mux %p tc %p tag %d\n", m
, tc
, req
->tag
);
1006 EXPORT_SYMBOL(p9_conn_rpcnb
);
1007 #endif /* P9_NONBLOCK */
1010 * p9_conn_cancel - cancel all pending requests with error
1014 void p9_conn_cancel(struct p9_conn
*m
, int err
)
1016 struct p9_req
*req
, *rtmp
;
1017 LIST_HEAD(cancel_list
);
1019 P9_DPRINTK(P9_DEBUG_ERROR
, "mux %p err %d\n", m
, err
);
1021 spin_lock(&m
->lock
);
1022 list_for_each_entry_safe(req
, rtmp
, &m
->req_list
, req_list
) {
1023 list_move(&req
->req_list
, &cancel_list
);
1025 list_for_each_entry_safe(req
, rtmp
, &m
->unsent_req_list
, req_list
) {
1026 list_move(&req
->req_list
, &cancel_list
);
1028 spin_unlock(&m
->lock
);
1030 list_for_each_entry_safe(req
, rtmp
, &cancel_list
, req_list
) {
1031 list_del(&req
->req_list
);
1036 (*req
->cb
) (req
, req
->cba
);
1041 wake_up(&m
->equeue
);
1043 EXPORT_SYMBOL(p9_conn_cancel
);
1045 static u16
p9_mux_get_tag(struct p9_conn
*m
)
1049 tag
= p9_idpool_get(m
->tagpool
);
1056 static void p9_mux_put_tag(struct p9_conn
*m
, u16 tag
)
1058 if (tag
!= P9_NOTAG
&& p9_idpool_check(tag
, m
->tagpool
))
1059 p9_idpool_put(tag
, m
->tagpool
);