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 as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/errno.h>
31 #include <linux/poll.h>
32 #include <linux/kthread.h>
33 #include <linux/idr.h>
38 #include "transport.h"
43 #define SCHED_TIMEOUT 10
44 #define MAXPOLLWADDR 2
47 Rworksched = 1, /* read work scheduled or running */
48 Rpending = 2, /* can read */
49 Wworksched = 4, /* write work scheduled or running */
50 Wpending = 8, /* can write */
53 struct v9fs_mux_poll_task;
57 struct v9fs_fcall *tcall;
58 struct v9fs_fcall *rcall;
60 v9fs_mux_req_callback cb;
62 struct list_head req_list;
65 struct v9fs_mux_data {
67 struct list_head mux_list;
68 struct v9fs_mux_poll_task *poll_task;
70 unsigned char *extended;
71 struct v9fs_transport *trans;
72 struct v9fs_idpool tidpool;
74 wait_queue_head_t equeue;
75 struct list_head req_list;
76 struct list_head unsent_req_list;
82 wait_queue_t poll_wait[MAXPOLLWADDR];
83 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
85 struct work_struct rq;
86 struct work_struct wq;
90 struct v9fs_mux_poll_task {
91 struct task_struct *task;
92 struct list_head mux_list;
97 struct v9fs_mux_data *m;
100 struct v9fs_fcall *rcall;
101 wait_queue_head_t wqueue;
104 static int v9fs_poll_proc(void *);
105 static void v9fs_read_work(void *);
106 static void v9fs_write_work(void *);
107 static void v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
110 static DECLARE_MUTEX(v9fs_mux_task_lock);
111 static struct workqueue_struct *v9fs_mux_wq;
113 static int v9fs_mux_num;
114 static int v9fs_mux_poll_task_num;
115 static struct v9fs_mux_poll_task v9fs_mux_poll_tasks[100];
117 void v9fs_mux_global_init(void)
121 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++)
122 v9fs_mux_poll_tasks[i].task = NULL;
124 v9fs_mux_wq = create_workqueue("v9fs");
127 void v9fs_mux_global_exit(void)
129 destroy_workqueue(v9fs_mux_wq);
133 * v9fs_mux_calc_poll_procs - calculates the number of polling procs
134 * based on the number of mounted v9fs filesystems.
136 * The current implementation returns sqrt of the number of mounts.
138 inline int v9fs_mux_calc_poll_procs(int muxnum)
142 if (v9fs_mux_poll_task_num)
143 n = muxnum / v9fs_mux_poll_task_num +
144 (muxnum % v9fs_mux_poll_task_num ? 1 : 0);
148 if (n > ARRAY_SIZE(v9fs_mux_poll_tasks))
149 n = ARRAY_SIZE(v9fs_mux_poll_tasks);
154 static void v9fs_mux_poll_start(struct v9fs_mux_data *m)
157 struct v9fs_mux_poll_task *vpt, *vptlast;
159 dprintk(DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, v9fs_mux_num,
160 v9fs_mux_poll_task_num);
161 up(&v9fs_mux_task_lock);
163 n = v9fs_mux_calc_poll_procs(v9fs_mux_num + 1);
164 if (n > v9fs_mux_poll_task_num) {
165 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
166 if (v9fs_mux_poll_tasks[i].task == NULL) {
167 vpt = &v9fs_mux_poll_tasks[i];
168 dprintk(DEBUG_MUX, "create proc %p\n", vpt);
169 vpt->task = kthread_create(v9fs_poll_proc,
171 INIT_LIST_HEAD(&vpt->mux_list);
173 v9fs_mux_poll_task_num++;
174 wake_up_process(vpt->task);
179 if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks))
180 dprintk(DEBUG_ERROR, "warning: no free poll slots\n");
183 n = (v9fs_mux_num + 1) / v9fs_mux_poll_task_num +
184 ((v9fs_mux_num + 1) % v9fs_mux_poll_task_num ? 1 : 0);
187 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
188 vpt = &v9fs_mux_poll_tasks[i];
189 if (vpt->task != NULL) {
191 if (vpt->muxnum < n) {
192 dprintk(DEBUG_MUX, "put in proc %d\n", i);
193 list_add(&m->mux_list, &vpt->mux_list);
196 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
197 init_poll_funcptr(&m->pt, v9fs_pollwait);
203 if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks)) {
204 dprintk(DEBUG_MUX, "put in proc %d\n", i);
205 list_add(&m->mux_list, &vptlast->mux_list);
208 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
209 init_poll_funcptr(&m->pt, v9fs_pollwait);
213 down(&v9fs_mux_task_lock);
216 static void v9fs_mux_poll_stop(struct v9fs_mux_data *m)
219 struct v9fs_mux_poll_task *vpt;
221 up(&v9fs_mux_task_lock);
223 list_del(&m->mux_list);
224 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
225 if (m->poll_waddr[i] != NULL) {
226 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
227 m->poll_waddr[i] = NULL;
232 dprintk(DEBUG_MUX, "destroy proc %p\n", vpt);
233 send_sig(SIGKILL, vpt->task, 1);
235 v9fs_mux_poll_task_num--;
238 down(&v9fs_mux_task_lock);
242 * v9fs_mux_init - allocate and initialize the per-session mux data
243 * Creates the polling task if this is the first session.
245 * @trans - transport structure
246 * @msize - maximum message size
247 * @extended - pointer to the extended flag
249 struct v9fs_mux_data *v9fs_mux_init(struct v9fs_transport *trans, int msize,
250 unsigned char *extended)
253 struct v9fs_mux_data *m, *mtmp;
255 dprintk(DEBUG_MUX, "transport %p msize %d\n", trans, msize);
256 m = kmalloc(sizeof(struct v9fs_mux_data) + 2 * msize, GFP_KERNEL);
258 return ERR_PTR(-ENOMEM);
260 spin_lock_init(&m->lock);
261 INIT_LIST_HEAD(&m->mux_list);
263 m->extended = extended;
265 idr_init(&m->tidpool.pool);
266 init_MUTEX(&m->tidpool.lock);
268 init_waitqueue_head(&m->equeue);
269 INIT_LIST_HEAD(&m->req_list);
270 INIT_LIST_HEAD(&m->unsent_req_list);
272 m->rbuf = (char *)m + sizeof(struct v9fs_mux_data);
273 m->wpos = m->wsize = 0;
274 m->wbuf = m->rbuf + msize;
275 INIT_WORK(&m->rq, v9fs_read_work, m);
276 INIT_WORK(&m->wq, v9fs_write_work, m);
278 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
279 v9fs_mux_poll_start(m);
281 n = trans->poll(trans, &m->pt);
283 dprintk(DEBUG_MUX, "mux %p can read\n", m);
284 set_bit(Rpending, &m->wsched);
288 dprintk(DEBUG_MUX, "mux %p can write\n", m);
289 set_bit(Wpending, &m->wsched);
292 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
293 if (IS_ERR(m->poll_waddr[i])) {
294 v9fs_mux_poll_stop(m);
295 mtmp = (void *)m->poll_waddr; /* the error code */
306 * v9fs_mux_destroy - cancels all pending requests and frees mux resources
308 void v9fs_mux_destroy(struct v9fs_mux_data *m)
310 dprintk(DEBUG_MUX, "mux %p prev %p next %p\n", m,
311 m->mux_list.prev, m->mux_list.next);
312 v9fs_mux_cancel(m, -ECONNRESET);
314 if (!list_empty(&m->req_list)) {
315 /* wait until all processes waiting on this session exit */
316 dprintk(DEBUG_MUX, "mux %p waiting for empty request queue\n",
318 wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000);
319 dprintk(DEBUG_MUX, "mux %p request queue empty: %d\n", m,
320 list_empty(&m->req_list));
323 v9fs_mux_poll_stop(m);
330 * v9fs_pollwait - called by files poll operation to add v9fs-poll task
331 * to files wait queue
334 v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
338 struct v9fs_mux_data *m;
340 m = container_of(p, struct v9fs_mux_data, pt);
341 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
342 if (m->poll_waddr[i] == NULL)
345 if (i >= ARRAY_SIZE(m->poll_waddr)) {
346 dprintk(DEBUG_ERROR, "not enough wait_address slots\n");
350 m->poll_waddr[i] = wait_address;
353 dprintk(DEBUG_ERROR, "no wait_address\n");
354 m->poll_waddr[i] = ERR_PTR(-EIO);
358 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
359 add_wait_queue(wait_address, &m->poll_wait[i]);
363 * v9fs_poll_mux - polls a mux and schedules read or write works if necessary
365 static inline void v9fs_poll_mux(struct v9fs_mux_data *m)
372 n = m->trans->poll(m->trans, NULL);
373 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
374 dprintk(DEBUG_MUX, "error mux %p err %d\n", m, n);
377 v9fs_mux_cancel(m, n);
381 set_bit(Rpending, &m->wsched);
382 dprintk(DEBUG_MUX, "mux %p can read\n", m);
383 if (!test_and_set_bit(Rworksched, &m->wsched)) {
384 dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
385 queue_work(v9fs_mux_wq, &m->rq);
390 set_bit(Wpending, &m->wsched);
391 dprintk(DEBUG_MUX, "mux %p can write\n", m);
392 if ((m->wsize || !list_empty(&m->unsent_req_list))
393 && !test_and_set_bit(Wworksched, &m->wsched)) {
394 dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
395 queue_work(v9fs_mux_wq, &m->wq);
401 * v9fs_poll_proc - polls all v9fs transports for new events and queues
402 * the appropriate work to the work queue
404 static int v9fs_poll_proc(void *a)
406 struct v9fs_mux_data *m, *mtmp;
407 struct v9fs_mux_poll_task *vpt;
410 dprintk(DEBUG_MUX, "start %p %p\n", current, vpt);
411 allow_signal(SIGKILL);
412 while (!kthread_should_stop()) {
413 set_current_state(TASK_INTERRUPTIBLE);
414 if (signal_pending(current))
417 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
421 dprintk(DEBUG_MUX, "sleeping...\n");
422 schedule_timeout(SCHED_TIMEOUT * HZ);
425 __set_current_state(TASK_RUNNING);
426 dprintk(DEBUG_MUX, "finish\n");
430 static inline int v9fs_write_req(struct v9fs_mux_data *m, struct v9fs_req *req)
434 list_move_tail(&req->req_list, &m->req_list);
435 n = v9fs_serialize_fcall(req->tcall, m->wbuf, m->msize, *m->extended);
438 list_del(&req->req_list);
440 spin_unlock(&m->lock);
441 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
454 * v9fs_write_work - called when a transport can send some data
456 static void v9fs_write_work(void *a)
459 struct v9fs_mux_data *m;
460 struct v9fs_req *req, *rtmp;
465 clear_bit(Wworksched, &m->wsched);
470 if (list_empty(&m->unsent_req_list)) {
471 clear_bit(Wworksched, &m->wsched);
477 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list,
479 err = v9fs_write_req(m, req);
486 spin_unlock(&m->lock);
489 dprintk(DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, m->wsize);
490 clear_bit(Wpending, &m->wsched);
491 err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
492 dprintk(DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
493 if (err == -EAGAIN) {
494 clear_bit(Wworksched, &m->wsched);
502 if (m->wpos == m->wsize)
503 m->wpos = m->wsize = 0;
505 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
506 if (test_and_clear_bit(Wpending, &m->wsched))
509 n = m->trans->poll(m->trans, NULL);
512 dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
513 queue_work(v9fs_mux_wq, &m->wq);
515 clear_bit(Wworksched, &m->wsched);
517 clear_bit(Wworksched, &m->wsched);
522 v9fs_mux_cancel(m, err);
523 clear_bit(Wworksched, &m->wsched);
526 static void process_request(struct v9fs_mux_data *m, struct v9fs_req *req)
532 if (req->rcall->id == RERROR && !req->err) {
533 ecode = req->rcall->params.rerror.errno;
534 ename = req->rcall->params.rerror.error;
536 dprintk(DEBUG_MUX, "Rerror %s\n", ename);
542 req->err = v9fs_errstr2errno(ename);
544 if (!req->err) { /* string match failed */
545 dprintk(DEBUG_ERROR, "unknown error: %s\n",
550 req->err = -ESERVERFAULT;
552 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
553 dprintk(DEBUG_ERROR, "fcall mismatch: expected %d, got %d\n",
554 req->tcall->id + 1, req->rcall->id);
559 if (req->cb && req->err != ERREQFLUSH) {
560 dprintk(DEBUG_MUX, "calling callback tcall %p rcall %p\n",
561 req->tcall, req->rcall);
563 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
568 if (tag != V9FS_NOTAG)
569 v9fs_put_idpool(tag, &m->tidpool);
576 * v9fs_read_work - called when there is some data to be read from a transport
578 static void v9fs_read_work(void *a)
581 struct v9fs_mux_data *m;
582 struct v9fs_req *req, *rptr, *rreq;
583 struct v9fs_fcall *rcall;
591 dprintk(DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
592 clear_bit(Rpending, &m->wsched);
593 err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
594 dprintk(DEBUG_MUX, "mux %p got %d bytes\n", m, err);
595 if (err == -EAGAIN) {
596 clear_bit(Rworksched, &m->wsched);
604 while (m->rpos > 4) {
605 n = le32_to_cpu(*(__le32 *) m->rbuf);
608 "requested packet size too big: %d\n", n);
616 rcallen = n + V9FS_FCALLHDRSZ;
617 rcall = kmalloc(rcallen, GFP_KERNEL);
623 dump_data(m->rbuf, n);
624 err = v9fs_deserialize_fcall(m->rbuf, n, rcall, rcallen,
631 dprintk(DEBUG_MUX, "mux %p fcall id %d tag %d\n", m, rcall->id,
636 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
637 if (rreq->tag == rcall->tag) {
640 list_del(&req->req_list);
641 spin_unlock(&m->lock);
642 process_request(m, req);
648 spin_unlock(&m->lock);
649 if (err >= 0 && rcall->id != RFLUSH)
651 "unexpected response mux %p id %d tag %d\n",
652 m, rcall->id, rcall->tag);
657 memmove(m->rbuf, m->rbuf + n, m->rpos - n);
661 if (!list_empty(&m->req_list)) {
662 if (test_and_clear_bit(Rpending, &m->wsched))
665 n = m->trans->poll(m->trans, NULL);
668 dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
669 queue_work(v9fs_mux_wq, &m->rq);
671 clear_bit(Rworksched, &m->wsched);
673 clear_bit(Rworksched, &m->wsched);
678 v9fs_mux_cancel(m, err);
679 clear_bit(Rworksched, &m->wsched);
683 * v9fs_send_request - send 9P request
684 * The function can sleep until the request is scheduled for sending.
685 * The function can be interrupted. Return from the function is not
686 * a guarantee that the request is sent succesfully. Can return errors
687 * that can be retrieved by PTR_ERR macros.
690 * @tc: request to be sent
691 * @cb: callback function to call when response is received
692 * @cba: parameter to pass to the callback function
694 static struct v9fs_req *v9fs_send_request(struct v9fs_mux_data *m,
695 struct v9fs_fcall *tc,
696 v9fs_mux_req_callback cb, void *cba)
699 struct v9fs_req *req;
701 dprintk(DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
704 return ERR_PTR(m->err);
706 req = kmalloc(sizeof(struct v9fs_req), GFP_KERNEL);
708 return ERR_PTR(-ENOMEM);
710 if (tc->id == TVERSION)
713 n = v9fs_get_idpool(&m->tidpool);
716 return ERR_PTR(-ENOMEM);
727 list_add_tail(&req->req_list, &m->unsent_req_list);
728 spin_unlock(&m->lock);
730 if (test_and_clear_bit(Wpending, &m->wsched))
733 n = m->trans->poll(m->trans, NULL);
735 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
736 queue_work(v9fs_mux_wq, &m->wq);
742 v9fs_mux_flush_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc,
745 v9fs_mux_req_callback cb;
747 struct v9fs_mux_data *m;
748 struct v9fs_req *req, *rptr;
751 dprintk(DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m, tc,
752 rc, err, tc->params.tflush.oldtag);
756 tag = tc->params.tflush.oldtag;
757 list_for_each_entry_safe(req, rptr, &m->req_list, req_list) {
758 if (req->tag == tag) {
759 list_del(&req->req_list);
763 spin_unlock(&m->lock);
764 (*cb) (req->cba, req->tcall, req->rcall,
774 spin_unlock(&m->lock);
776 if (v9fs_check_idpool(tag, &m->tidpool))
777 v9fs_put_idpool(tag, &m->tidpool);
784 v9fs_mux_flush_request(struct v9fs_mux_data *m, struct v9fs_req *req)
786 struct v9fs_fcall *fc;
788 dprintk(DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
790 fc = kmalloc(sizeof(struct v9fs_fcall), GFP_KERNEL);
792 fc->params.tflush.oldtag = req->tag;
794 v9fs_send_request(m, fc, v9fs_mux_flush_cb, m);
798 v9fs_mux_rpc_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc, int err)
800 struct v9fs_mux_rpc *r;
802 if (err == ERREQFLUSH) {
803 dprintk(DEBUG_MUX, "err req flush\n");
808 dprintk(DEBUG_MUX, "mux %p req %p tc %p rc %p err %d\n", r->m, r->req,
816 * v9fs_mux_rpc - sends 9P request and waits until a response is available.
817 * The function can be interrupted.
819 * @tc: request to be sent
820 * @rc: pointer where a pointer to the response is stored
823 v9fs_mux_rpc(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
824 struct v9fs_fcall **rc)
828 struct v9fs_req *req;
829 struct v9fs_mux_rpc r;
834 init_waitqueue_head(&r.wqueue);
839 req = v9fs_send_request(m, tc, v9fs_mux_rpc_cb, &r);
842 dprintk(DEBUG_MUX, "error %d\n", err);
847 dprintk(DEBUG_MUX, "mux %p tc %p tag %d rpc %p req %p\n", m, tc,
849 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
853 if (err == -ERESTARTSYS && m->trans->status == Connected && m->err == 0) {
856 req->err = ERREQFLUSH;
857 spin_unlock(&m->lock);
859 clear_thread_flag(TIF_SIGPENDING);
860 v9fs_mux_flush_request(m, req);
861 spin_lock_irqsave(¤t->sighand->siglock, flags);
863 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
868 dprintk(DEBUG_MUX, "got response id %d tag %d\n",
869 r.rcall->id, r.rcall->tag);
877 dprintk(DEBUG_MUX, "got error %d\n", err);
886 * v9fs_mux_rpcnb - sends 9P request without waiting for response.
888 * @tc: request to be sent
889 * @cb: callback function to be called when response arrives
890 * @cba: value to pass to the callback function
892 int v9fs_mux_rpcnb(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
893 v9fs_mux_req_callback cb, void *a)
896 struct v9fs_req *req;
898 req = v9fs_send_request(m, tc, cb, a);
901 dprintk(DEBUG_MUX, "error %d\n", err);
905 dprintk(DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
910 * v9fs_mux_cancel - cancel all pending requests with error
914 void v9fs_mux_cancel(struct v9fs_mux_data *m, int err)
916 struct v9fs_req *req, *rtmp;
917 LIST_HEAD(cancel_list);
919 dprintk(DEBUG_MUX, "mux %p err %d\n", m, err);
922 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
923 list_move(&req->req_list, &cancel_list);
925 spin_unlock(&m->lock);
927 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
928 list_del(&req->req_list);
933 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);