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>
39 #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;
77 struct v9fs_fcall *rcall;
83 wait_queue_t poll_wait[MAXPOLLWADDR];
84 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
86 struct work_struct rq;
87 struct work_struct wq;
91 struct v9fs_mux_poll_task {
92 struct task_struct *task;
93 struct list_head mux_list;
98 struct v9fs_mux_data *m;
101 struct v9fs_fcall *rcall;
102 wait_queue_head_t wqueue;
105 extern int v9fs_errstr2errno(char *str, int len);
107 static int v9fs_poll_proc(void *);
108 static void v9fs_read_work(void *);
109 static void v9fs_write_work(void *);
110 static void v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
112 static u16 v9fs_mux_get_tag(struct v9fs_mux_data *);
113 static void v9fs_mux_put_tag(struct v9fs_mux_data *, u16);
115 static DECLARE_MUTEX(v9fs_mux_task_lock);
116 static struct workqueue_struct *v9fs_mux_wq;
118 static int v9fs_mux_num;
119 static int v9fs_mux_poll_task_num;
120 static struct v9fs_mux_poll_task v9fs_mux_poll_tasks[100];
122 void v9fs_mux_global_init(void)
126 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++)
127 v9fs_mux_poll_tasks[i].task = NULL;
129 v9fs_mux_wq = create_workqueue("v9fs");
132 void v9fs_mux_global_exit(void)
134 destroy_workqueue(v9fs_mux_wq);
138 * v9fs_mux_calc_poll_procs - calculates the number of polling procs
139 * based on the number of mounted v9fs filesystems.
141 * The current implementation returns sqrt of the number of mounts.
143 inline int v9fs_mux_calc_poll_procs(int muxnum)
147 if (v9fs_mux_poll_task_num)
148 n = muxnum / v9fs_mux_poll_task_num +
149 (muxnum % v9fs_mux_poll_task_num ? 1 : 0);
153 if (n > ARRAY_SIZE(v9fs_mux_poll_tasks))
154 n = ARRAY_SIZE(v9fs_mux_poll_tasks);
159 static void v9fs_mux_poll_start(struct v9fs_mux_data *m)
162 struct v9fs_mux_poll_task *vpt, *vptlast;
164 dprintk(DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, v9fs_mux_num,
165 v9fs_mux_poll_task_num);
166 up(&v9fs_mux_task_lock);
168 n = v9fs_mux_calc_poll_procs(v9fs_mux_num + 1);
169 if (n > v9fs_mux_poll_task_num) {
170 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
171 if (v9fs_mux_poll_tasks[i].task == NULL) {
172 vpt = &v9fs_mux_poll_tasks[i];
173 dprintk(DEBUG_MUX, "create proc %p\n", vpt);
175 kthread_create(v9fs_poll_proc, vpt,
177 INIT_LIST_HEAD(&vpt->mux_list);
179 v9fs_mux_poll_task_num++;
180 wake_up_process(vpt->task);
185 if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks))
186 dprintk(DEBUG_ERROR, "warning: no free poll slots\n");
189 n = (v9fs_mux_num + 1) / v9fs_mux_poll_task_num +
190 ((v9fs_mux_num + 1) % v9fs_mux_poll_task_num ? 1 : 0);
193 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
194 vpt = &v9fs_mux_poll_tasks[i];
195 if (vpt->task != NULL) {
197 if (vpt->muxnum < n) {
198 dprintk(DEBUG_MUX, "put in proc %d\n", i);
199 list_add(&m->mux_list, &vpt->mux_list);
202 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
203 init_poll_funcptr(&m->pt, v9fs_pollwait);
209 if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks)) {
210 dprintk(DEBUG_MUX, "put in proc %d\n", i);
211 list_add(&m->mux_list, &vptlast->mux_list);
214 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
215 init_poll_funcptr(&m->pt, v9fs_pollwait);
219 down(&v9fs_mux_task_lock);
222 static void v9fs_mux_poll_stop(struct v9fs_mux_data *m)
225 struct v9fs_mux_poll_task *vpt;
227 up(&v9fs_mux_task_lock);
229 list_del(&m->mux_list);
230 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
231 if (m->poll_waddr[i] != NULL) {
232 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
233 m->poll_waddr[i] = NULL;
238 dprintk(DEBUG_MUX, "destroy proc %p\n", vpt);
239 send_sig(SIGKILL, vpt->task, 1);
241 v9fs_mux_poll_task_num--;
244 down(&v9fs_mux_task_lock);
248 * v9fs_mux_init - allocate and initialize the per-session mux data
249 * Creates the polling task if this is the first session.
251 * @trans - transport structure
252 * @msize - maximum message size
253 * @extended - pointer to the extended flag
255 struct v9fs_mux_data *v9fs_mux_init(struct v9fs_transport *trans, int msize,
256 unsigned char *extended)
259 struct v9fs_mux_data *m, *mtmp;
261 dprintk(DEBUG_MUX, "transport %p msize %d\n", trans, msize);
262 m = kmalloc(sizeof(struct v9fs_mux_data), GFP_KERNEL);
264 return ERR_PTR(-ENOMEM);
266 spin_lock_init(&m->lock);
267 INIT_LIST_HEAD(&m->mux_list);
269 m->extended = extended;
271 idr_init(&m->tidpool.pool);
272 init_MUTEX(&m->tidpool.lock);
274 init_waitqueue_head(&m->equeue);
275 INIT_LIST_HEAD(&m->req_list);
276 INIT_LIST_HEAD(&m->unsent_req_list);
280 m->wpos = m->wsize = 0;
282 INIT_WORK(&m->rq, v9fs_read_work, m);
283 INIT_WORK(&m->wq, v9fs_write_work, m);
285 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
286 v9fs_mux_poll_start(m);
288 n = trans->poll(trans, &m->pt);
290 dprintk(DEBUG_MUX, "mux %p can read\n", m);
291 set_bit(Rpending, &m->wsched);
295 dprintk(DEBUG_MUX, "mux %p can write\n", m);
296 set_bit(Wpending, &m->wsched);
299 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
300 if (IS_ERR(m->poll_waddr[i])) {
301 v9fs_mux_poll_stop(m);
302 mtmp = (void *)m->poll_waddr; /* the error code */
313 * v9fs_mux_destroy - cancels all pending requests and frees mux resources
315 void v9fs_mux_destroy(struct v9fs_mux_data *m)
317 dprintk(DEBUG_MUX, "mux %p prev %p next %p\n", m,
318 m->mux_list.prev, m->mux_list.next);
319 v9fs_mux_cancel(m, -ECONNRESET);
321 if (!list_empty(&m->req_list)) {
322 /* wait until all processes waiting on this session exit */
323 dprintk(DEBUG_MUX, "mux %p waiting for empty request queue\n",
325 wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000);
326 dprintk(DEBUG_MUX, "mux %p request queue empty: %d\n", m,
327 list_empty(&m->req_list));
330 v9fs_mux_poll_stop(m);
337 * v9fs_pollwait - called by files poll operation to add v9fs-poll task
338 * to files wait queue
341 v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
345 struct v9fs_mux_data *m;
347 m = container_of(p, struct v9fs_mux_data, pt);
348 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
349 if (m->poll_waddr[i] == NULL)
352 if (i >= ARRAY_SIZE(m->poll_waddr)) {
353 dprintk(DEBUG_ERROR, "not enough wait_address slots\n");
357 m->poll_waddr[i] = wait_address;
360 dprintk(DEBUG_ERROR, "no wait_address\n");
361 m->poll_waddr[i] = ERR_PTR(-EIO);
365 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
366 add_wait_queue(wait_address, &m->poll_wait[i]);
370 * v9fs_poll_mux - polls a mux and schedules read or write works if necessary
372 static inline void v9fs_poll_mux(struct v9fs_mux_data *m)
379 n = m->trans->poll(m->trans, NULL);
380 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
381 dprintk(DEBUG_MUX, "error mux %p err %d\n", m, n);
384 v9fs_mux_cancel(m, n);
388 set_bit(Rpending, &m->wsched);
389 dprintk(DEBUG_MUX, "mux %p can read\n", m);
390 if (!test_and_set_bit(Rworksched, &m->wsched)) {
391 dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
392 queue_work(v9fs_mux_wq, &m->rq);
397 set_bit(Wpending, &m->wsched);
398 dprintk(DEBUG_MUX, "mux %p can write\n", m);
399 if ((m->wsize || !list_empty(&m->unsent_req_list))
400 && !test_and_set_bit(Wworksched, &m->wsched)) {
401 dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
402 queue_work(v9fs_mux_wq, &m->wq);
408 * v9fs_poll_proc - polls all v9fs transports for new events and queues
409 * the appropriate work to the work queue
411 static int v9fs_poll_proc(void *a)
413 struct v9fs_mux_data *m, *mtmp;
414 struct v9fs_mux_poll_task *vpt;
417 dprintk(DEBUG_MUX, "start %p %p\n", current, vpt);
418 allow_signal(SIGKILL);
419 while (!kthread_should_stop()) {
420 set_current_state(TASK_INTERRUPTIBLE);
421 if (signal_pending(current))
424 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
428 dprintk(DEBUG_MUX, "sleeping...\n");
429 schedule_timeout(SCHED_TIMEOUT * HZ);
432 __set_current_state(TASK_RUNNING);
433 dprintk(DEBUG_MUX, "finish\n");
438 * v9fs_write_work - called when a transport can send some data
440 static void v9fs_write_work(void *a)
443 struct v9fs_mux_data *m;
444 struct v9fs_req *req;
449 clear_bit(Wworksched, &m->wsched);
454 if (list_empty(&m->unsent_req_list)) {
455 clear_bit(Wworksched, &m->wsched);
461 list_entry(m->unsent_req_list.next, struct v9fs_req,
463 list_move_tail(&req->req_list, &m->req_list);
464 m->wbuf = req->tcall->sdata;
465 m->wsize = req->tcall->size;
467 dump_data(m->wbuf, m->wsize);
468 spin_unlock(&m->lock);
471 dprintk(DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, m->wsize);
472 clear_bit(Wpending, &m->wsched);
473 err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
474 dprintk(DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
475 if (err == -EAGAIN) {
476 clear_bit(Wworksched, &m->wsched);
484 if (m->wpos == m->wsize)
485 m->wpos = m->wsize = 0;
487 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
488 if (test_and_clear_bit(Wpending, &m->wsched))
491 n = m->trans->poll(m->trans, NULL);
494 dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
495 queue_work(v9fs_mux_wq, &m->wq);
497 clear_bit(Wworksched, &m->wsched);
499 clear_bit(Wworksched, &m->wsched);
504 v9fs_mux_cancel(m, err);
505 clear_bit(Wworksched, &m->wsched);
508 static void process_request(struct v9fs_mux_data *m, struct v9fs_req *req)
511 struct v9fs_str *ename;
514 if (req->rcall->id == RERROR && !req->err) {
515 ecode = req->rcall->params.rerror.errno;
516 ename = &req->rcall->params.rerror.error;
518 dprintk(DEBUG_MUX, "Rerror %.*s\n", ename->len, ename->str);
524 req->err = v9fs_errstr2errno(ename->str, ename->len);
526 if (!req->err) { /* string match failed */
527 PRINT_FCALL_ERROR("unknown error", req->rcall);
531 req->err = -ESERVERFAULT;
533 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
534 dprintk(DEBUG_ERROR, "fcall mismatch: expected %d, got %d\n",
535 req->tcall->id + 1, req->rcall->id);
540 if (req->cb && req->err != ERREQFLUSH) {
541 dprintk(DEBUG_MUX, "calling callback tcall %p rcall %p\n",
542 req->tcall, req->rcall);
544 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
549 v9fs_mux_put_tag(m, tag);
556 * v9fs_read_work - called when there is some data to be read from a transport
558 static void v9fs_read_work(void *a)
561 struct v9fs_mux_data *m;
562 struct v9fs_req *req, *rptr, *rreq;
563 struct v9fs_fcall *rcall;
572 dprintk(DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
576 kmalloc(sizeof(struct v9fs_fcall) + m->msize, GFP_KERNEL);
582 m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall);
586 clear_bit(Rpending, &m->wsched);
587 err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
588 dprintk(DEBUG_MUX, "mux %p got %d bytes\n", m, err);
589 if (err == -EAGAIN) {
590 clear_bit(Rworksched, &m->wsched);
598 while (m->rpos > 4) {
599 n = le32_to_cpu(*(__le32 *) m->rbuf);
602 "requested packet size too big: %d\n", n);
610 dump_data(m->rbuf, n);
612 v9fs_deserialize_fcall(m->rbuf, n, m->rcall, *m->extended);
620 m->rcall = kmalloc(sizeof(struct v9fs_fcall) + m->msize,
627 m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall);
628 memmove(m->rbuf, rbuf + n, m->rpos - n);
636 dprintk(DEBUG_MUX, "mux %p fcall id %d tag %d\n", m, rcall->id,
641 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
642 if (rreq->tag == rcall->tag) {
645 list_del(&req->req_list);
646 spin_unlock(&m->lock);
647 process_request(m, req);
654 spin_unlock(&m->lock);
655 if (err >= 0 && rcall->id != RFLUSH)
657 "unexpected response mux %p id %d tag %d\n",
658 m, rcall->id, rcall->tag);
663 if (!list_empty(&m->req_list)) {
664 if (test_and_clear_bit(Rpending, &m->wsched))
667 n = m->trans->poll(m->trans, NULL);
670 dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
671 queue_work(v9fs_mux_wq, &m->rq);
673 clear_bit(Rworksched, &m->wsched);
675 clear_bit(Rworksched, &m->wsched);
680 v9fs_mux_cancel(m, err);
681 clear_bit(Rworksched, &m->wsched);
685 * v9fs_send_request - send 9P request
686 * The function can sleep until the request is scheduled for sending.
687 * The function can be interrupted. Return from the function is not
688 * a guarantee that the request is sent succesfully. Can return errors
689 * that can be retrieved by PTR_ERR macros.
692 * @tc: request to be sent
693 * @cb: callback function to call when response is received
694 * @cba: parameter to pass to the callback function
696 static struct v9fs_req *v9fs_send_request(struct v9fs_mux_data *m,
697 struct v9fs_fcall *tc,
698 v9fs_mux_req_callback cb, void *cba)
701 struct v9fs_req *req;
703 dprintk(DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
706 return ERR_PTR(m->err);
708 req = kmalloc(sizeof(struct v9fs_req), GFP_KERNEL);
710 return ERR_PTR(-ENOMEM);
712 if (tc->id == TVERSION)
715 n = v9fs_mux_get_tag(m);
718 return ERR_PTR(-ENOMEM);
730 list_add_tail(&req->req_list, &m->unsent_req_list);
731 spin_unlock(&m->lock);
733 if (test_and_clear_bit(Wpending, &m->wsched))
736 n = m->trans->poll(m->trans, NULL);
738 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
739 queue_work(v9fs_mux_wq, &m->wq);
745 v9fs_mux_flush_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc,
748 v9fs_mux_req_callback cb;
750 struct v9fs_mux_data *m;
751 struct v9fs_req *req, *rptr;
754 dprintk(DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m, tc,
755 rc, err, tc->params.tflush.oldtag);
759 tag = tc->params.tflush.oldtag;
760 list_for_each_entry_safe(req, rptr, &m->req_list, req_list) {
761 if (req->tag == tag) {
762 list_del(&req->req_list);
766 spin_unlock(&m->lock);
767 (*cb) (req->cba, req->tcall, req->rcall,
777 spin_unlock(&m->lock);
779 v9fs_mux_put_tag(m, tag);
785 v9fs_mux_flush_request(struct v9fs_mux_data *m, struct v9fs_req *req)
787 struct v9fs_fcall *fc;
789 dprintk(DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
791 fc = v9fs_create_tflush(req->tag);
792 v9fs_send_request(m, fc, v9fs_mux_flush_cb, m);
796 v9fs_mux_rpc_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc, int err)
798 struct v9fs_mux_rpc *r;
800 if (err == ERREQFLUSH) {
801 dprintk(DEBUG_MUX, "err req flush\n");
806 dprintk(DEBUG_MUX, "mux %p req %p tc %p rc %p err %d\n", r->m, r->req,
814 * v9fs_mux_rpc - sends 9P request and waits until a response is available.
815 * The function can be interrupted.
817 * @tc: request to be sent
818 * @rc: pointer where a pointer to the response is stored
821 v9fs_mux_rpc(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
822 struct v9fs_fcall **rc)
826 struct v9fs_req *req;
827 struct v9fs_mux_rpc r;
832 init_waitqueue_head(&r.wqueue);
837 req = v9fs_send_request(m, tc, v9fs_mux_rpc_cb, &r);
840 dprintk(DEBUG_MUX, "error %d\n", err);
845 dprintk(DEBUG_MUX, "mux %p tc %p tag %d rpc %p req %p\n", m, tc,
847 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
851 if (err == -ERESTARTSYS && m->trans->status == Connected && m->err == 0) {
854 req->err = ERREQFLUSH;
855 spin_unlock(&m->lock);
857 clear_thread_flag(TIF_SIGPENDING);
858 v9fs_mux_flush_request(m, req);
859 spin_lock_irqsave(¤t->sighand->siglock, flags);
861 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
866 dprintk(DEBUG_MUX, "got response id %d tag %d\n",
867 r.rcall->id, r.rcall->tag);
875 dprintk(DEBUG_MUX, "got error %d\n", err);
884 * v9fs_mux_rpcnb - sends 9P request without waiting for response.
886 * @tc: request to be sent
887 * @cb: callback function to be called when response arrives
888 * @cba: value to pass to the callback function
890 int v9fs_mux_rpcnb(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
891 v9fs_mux_req_callback cb, void *a)
894 struct v9fs_req *req;
896 req = v9fs_send_request(m, tc, cb, a);
899 dprintk(DEBUG_MUX, "error %d\n", err);
903 dprintk(DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
908 * v9fs_mux_cancel - cancel all pending requests with error
912 void v9fs_mux_cancel(struct v9fs_mux_data *m, int err)
914 struct v9fs_req *req, *rtmp;
915 LIST_HEAD(cancel_list);
917 dprintk(DEBUG_MUX, "mux %p err %d\n", m, err);
920 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
921 list_move(&req->req_list, &cancel_list);
923 spin_unlock(&m->lock);
925 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
926 list_del(&req->req_list);
931 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
941 static u16 v9fs_mux_get_tag(struct v9fs_mux_data *m)
945 tag = v9fs_get_idpool(&m->tidpool);
952 static void v9fs_mux_put_tag(struct v9fs_mux_data *m, u16 tag)
954 if (tag != V9FS_NOTAG && v9fs_check_idpool(tag, &m->tidpool))
955 v9fs_put_idpool(tag, &m->tidpool);