blob: c3aa87bc8b973c724e207a41ce54e9244620ad0d [file] [log] [blame]
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001/*
2 * net/9p/mux.c
3 *
4 * Protocol Multiplexer
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
8 *
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.
12 *
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.
17 *
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
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.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 <net/9p/transport.h>
35#include <net/9p/conn.h>
36
37#define ERREQFLUSH 1
38#define SCHED_TIMEOUT 10
39#define MAXPOLLWADDR 2
40
41enum {
42 Rworksched = 1, /* read work scheduled or running */
43 Rpending = 2, /* can read */
44 Wworksched = 4, /* write work scheduled or running */
45 Wpending = 8, /* can write */
46};
47
48enum {
49 None,
50 Flushing,
51 Flushed,
52};
53
54struct p9_mux_poll_task;
55
56struct p9_req {
57 spinlock_t lock; /* protect request structure */
58 int tag;
59 struct p9_fcall *tcall;
60 struct p9_fcall *rcall;
61 int err;
62 p9_conn_req_callback cb;
63 void *cba;
64 int flush;
65 struct list_head req_list;
66};
67
68struct p9_conn {
69 spinlock_t lock; /* protect lock structure */
70 struct list_head mux_list;
71 struct p9_mux_poll_task *poll_task;
72 int msize;
73 unsigned char *extended;
74 struct p9_transport *trans;
75 struct p9_idpool *tagpool;
76 int err;
77 wait_queue_head_t equeue;
78 struct list_head req_list;
79 struct list_head unsent_req_list;
80 struct p9_fcall *rcall;
81 int rpos;
82 char *rbuf;
83 int wpos;
84 int wsize;
85 char *wbuf;
86 wait_queue_t poll_wait[MAXPOLLWADDR];
87 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
88 poll_table pt;
89 struct work_struct rq;
90 struct work_struct wq;
91 unsigned long wsched;
92};
93
94struct p9_mux_poll_task {
95 struct task_struct *task;
96 struct list_head mux_list;
97 int muxnum;
98};
99
100struct p9_mux_rpc {
101 struct p9_conn *m;
102 int err;
103 struct p9_fcall *tcall;
104 struct p9_fcall *rcall;
105 wait_queue_head_t wqueue;
106};
107
108static int p9_poll_proc(void *);
109static void p9_read_work(struct work_struct *work);
110static void p9_write_work(struct work_struct *work);
111static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
112 poll_table * p);
113static u16 p9_mux_get_tag(struct p9_conn *);
114static void p9_mux_put_tag(struct p9_conn *, u16);
115
116static DEFINE_MUTEX(p9_mux_task_lock);
117static struct workqueue_struct *p9_mux_wq;
118
119static int p9_mux_num;
120static int p9_mux_poll_task_num;
121static struct p9_mux_poll_task p9_mux_poll_tasks[100];
122
123int p9_mux_global_init(void)
124{
125 int i;
126
127 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++)
128 p9_mux_poll_tasks[i].task = NULL;
129
130 p9_mux_wq = create_workqueue("v9fs");
131 if (!p9_mux_wq) {
132 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
133 return -ENOMEM;
134 }
135
136 return 0;
137}
138
139void p9_mux_global_exit(void)
140{
141 destroy_workqueue(p9_mux_wq);
142}
143
144/**
145 * p9_mux_calc_poll_procs - calculates the number of polling procs
146 * based on the number of mounted v9fs filesystems.
147 *
148 * The current implementation returns sqrt of the number of mounts.
149 */
150static int p9_mux_calc_poll_procs(int muxnum)
151{
152 int n;
153
154 if (p9_mux_poll_task_num)
155 n = muxnum / p9_mux_poll_task_num +
156 (muxnum % p9_mux_poll_task_num ? 1 : 0);
157 else
158 n = 1;
159
160 if (n > ARRAY_SIZE(p9_mux_poll_tasks))
161 n = ARRAY_SIZE(p9_mux_poll_tasks);
162
163 return n;
164}
165
166static int p9_mux_poll_start(struct p9_conn *m)
167{
168 int i, n;
169 struct p9_mux_poll_task *vpt, *vptlast;
170 struct task_struct *pproc;
171
172 P9_DPRINTK(P9_DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, p9_mux_num,
173 p9_mux_poll_task_num);
174 mutex_lock(&p9_mux_task_lock);
175
176 n = p9_mux_calc_poll_procs(p9_mux_num + 1);
177 if (n > p9_mux_poll_task_num) {
178 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
179 if (p9_mux_poll_tasks[i].task == NULL) {
180 vpt = &p9_mux_poll_tasks[i];
181 P9_DPRINTK(P9_DEBUG_MUX, "create proc %p\n",
182 vpt);
183 pproc = kthread_create(p9_poll_proc, vpt,
184 "v9fs-poll");
185
186 if (!IS_ERR(pproc)) {
187 vpt->task = pproc;
188 INIT_LIST_HEAD(&vpt->mux_list);
189 vpt->muxnum = 0;
190 p9_mux_poll_task_num++;
191 wake_up_process(vpt->task);
192 }
193 break;
194 }
195 }
196
197 if (i >= ARRAY_SIZE(p9_mux_poll_tasks))
198 P9_DPRINTK(P9_DEBUG_ERROR,
199 "warning: no free poll slots\n");
200 }
201
202 n = (p9_mux_num + 1) / p9_mux_poll_task_num +
203 ((p9_mux_num + 1) % p9_mux_poll_task_num ? 1 : 0);
204
205 vptlast = NULL;
206 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
207 vpt = &p9_mux_poll_tasks[i];
208 if (vpt->task != NULL) {
209 vptlast = vpt;
210 if (vpt->muxnum < n) {
211 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
212 list_add(&m->mux_list, &vpt->mux_list);
213 vpt->muxnum++;
214 m->poll_task = vpt;
215 memset(&m->poll_waddr, 0,
216 sizeof(m->poll_waddr));
217 init_poll_funcptr(&m->pt, p9_pollwait);
218 break;
219 }
220 }
221 }
222
223 if (i >= ARRAY_SIZE(p9_mux_poll_tasks)) {
224 if (vptlast == NULL)
225 return -ENOMEM;
226
227 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
228 list_add(&m->mux_list, &vptlast->mux_list);
229 vptlast->muxnum++;
230 m->poll_task = vptlast;
231 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
232 init_poll_funcptr(&m->pt, p9_pollwait);
233 }
234
235 p9_mux_num++;
236 mutex_unlock(&p9_mux_task_lock);
237
238 return 0;
239}
240
241static void p9_mux_poll_stop(struct p9_conn *m)
242{
243 int i;
244 struct p9_mux_poll_task *vpt;
245
246 mutex_lock(&p9_mux_task_lock);
247 vpt = m->poll_task;
248 list_del(&m->mux_list);
249 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
250 if (m->poll_waddr[i] != NULL) {
251 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
252 m->poll_waddr[i] = NULL;
253 }
254 }
255 vpt->muxnum--;
256 if (!vpt->muxnum) {
257 P9_DPRINTK(P9_DEBUG_MUX, "destroy proc %p\n", vpt);
258 kthread_stop(vpt->task);
259 vpt->task = NULL;
260 p9_mux_poll_task_num--;
261 }
262 p9_mux_num--;
263 mutex_unlock(&p9_mux_task_lock);
264}
265
266/**
267 * p9_conn_create - allocate and initialize the per-session mux data
268 * Creates the polling task if this is the first session.
269 *
270 * @trans - transport structure
271 * @msize - maximum message size
272 * @extended - pointer to the extended flag
273 */
274struct p9_conn *p9_conn_create(struct p9_transport *trans, int msize,
275 unsigned char *extended)
276{
277 int i, n;
278 struct p9_conn *m, *mtmp;
279
280 P9_DPRINTK(P9_DEBUG_MUX, "transport %p msize %d\n", trans, msize);
281 m = kmalloc(sizeof(struct p9_conn), GFP_KERNEL);
282 if (!m)
283 return ERR_PTR(-ENOMEM);
284
285 spin_lock_init(&m->lock);
286 INIT_LIST_HEAD(&m->mux_list);
287 m->msize = msize;
288 m->extended = extended;
289 m->trans = trans;
290 m->tagpool = p9_idpool_create();
291 if (!m->tagpool) {
292 kfree(m);
293 return ERR_PTR(PTR_ERR(m->tagpool));
294 }
295
296 m->err = 0;
297 init_waitqueue_head(&m->equeue);
298 INIT_LIST_HEAD(&m->req_list);
299 INIT_LIST_HEAD(&m->unsent_req_list);
300 m->rcall = NULL;
301 m->rpos = 0;
302 m->rbuf = NULL;
303 m->wpos = m->wsize = 0;
304 m->wbuf = NULL;
305 INIT_WORK(&m->rq, p9_read_work);
306 INIT_WORK(&m->wq, p9_write_work);
307 m->wsched = 0;
308 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
309 m->poll_task = NULL;
310 n = p9_mux_poll_start(m);
311 if (n)
312 return ERR_PTR(n);
313
314 n = trans->poll(trans, &m->pt);
315 if (n & POLLIN) {
316 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
317 set_bit(Rpending, &m->wsched);
318 }
319
320 if (n & POLLOUT) {
321 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
322 set_bit(Wpending, &m->wsched);
323 }
324
325 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
326 if (IS_ERR(m->poll_waddr[i])) {
327 p9_mux_poll_stop(m);
328 mtmp = (void *)m->poll_waddr; /* the error code */
329 kfree(m);
330 m = mtmp;
331 break;
332 }
333 }
334
335 return m;
336}
337EXPORT_SYMBOL(p9_conn_create);
338
339/**
340 * p9_mux_destroy - cancels all pending requests and frees mux resources
341 */
342void p9_conn_destroy(struct p9_conn *m)
343{
344 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
345 m->mux_list.prev, m->mux_list.next);
346 p9_conn_cancel(m, -ECONNRESET);
347
348 if (!list_empty(&m->req_list)) {
349 /* wait until all processes waiting on this session exit */
350 P9_DPRINTK(P9_DEBUG_MUX,
351 "mux %p waiting for empty request queue\n", m);
352 wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000);
353 P9_DPRINTK(P9_DEBUG_MUX, "mux %p request queue empty: %d\n", m,
354 list_empty(&m->req_list));
355 }
356
357 p9_mux_poll_stop(m);
358 m->trans = NULL;
359 p9_idpool_destroy(m->tagpool);
360 kfree(m);
361}
362EXPORT_SYMBOL(p9_conn_destroy);
363
364/**
365 * p9_pollwait - called by files poll operation to add v9fs-poll task
366 * to files wait queue
367 */
368static void
369p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
370 poll_table * p)
371{
372 int i;
373 struct p9_conn *m;
374
375 m = container_of(p, struct p9_conn, pt);
376 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
377 if (m->poll_waddr[i] == NULL)
378 break;
379
380 if (i >= ARRAY_SIZE(m->poll_waddr)) {
381 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
382 return;
383 }
384
385 m->poll_waddr[i] = wait_address;
386
387 if (!wait_address) {
388 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
389 m->poll_waddr[i] = ERR_PTR(-EIO);
390 return;
391 }
392
393 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
394 add_wait_queue(wait_address, &m->poll_wait[i]);
395}
396
397/**
398 * p9_poll_mux - polls a mux and schedules read or write works if necessary
399 */
400static void p9_poll_mux(struct p9_conn *m)
401{
402 int n;
403
404 if (m->err < 0)
405 return;
406
407 n = m->trans->poll(m->trans, NULL);
408 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
409 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
410 if (n >= 0)
411 n = -ECONNRESET;
412 p9_conn_cancel(m, n);
413 }
414
415 if (n & POLLIN) {
416 set_bit(Rpending, &m->wsched);
417 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
418 if (!test_and_set_bit(Rworksched, &m->wsched)) {
419 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
420 queue_work(p9_mux_wq, &m->rq);
421 }
422 }
423
424 if (n & POLLOUT) {
425 set_bit(Wpending, &m->wsched);
426 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
427 if ((m->wsize || !list_empty(&m->unsent_req_list))
428 && !test_and_set_bit(Wworksched, &m->wsched)) {
429 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
430 queue_work(p9_mux_wq, &m->wq);
431 }
432 }
433}
434
435/**
436 * p9_poll_proc - polls all v9fs transports for new events and queues
437 * the appropriate work to the work queue
438 */
439static int p9_poll_proc(void *a)
440{
441 struct p9_conn *m, *mtmp;
442 struct p9_mux_poll_task *vpt;
443
444 vpt = a;
445 P9_DPRINTK(P9_DEBUG_MUX, "start %p %p\n", current, vpt);
446 while (!kthread_should_stop()) {
447 set_current_state(TASK_INTERRUPTIBLE);
448
449 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
450 p9_poll_mux(m);
451 }
452
453 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
454 schedule_timeout(SCHED_TIMEOUT * HZ);
455 }
456
457 __set_current_state(TASK_RUNNING);
458 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
459 return 0;
460}
461
462/**
463 * p9_write_work - called when a transport can send some data
464 */
465static void p9_write_work(struct work_struct *work)
466{
467 int n, err;
468 struct p9_conn *m;
469 struct p9_req *req;
470
471 m = container_of(work, struct p9_conn, wq);
472
473 if (m->err < 0) {
474 clear_bit(Wworksched, &m->wsched);
475 return;
476 }
477
478 if (!m->wsize) {
479 if (list_empty(&m->unsent_req_list)) {
480 clear_bit(Wworksched, &m->wsched);
481 return;
482 }
483
484 spin_lock(&m->lock);
485again:
486 req = list_entry(m->unsent_req_list.next, struct p9_req,
487 req_list);
488 list_move_tail(&req->req_list, &m->req_list);
489 if (req->err == ERREQFLUSH)
490 goto again;
491
492 m->wbuf = req->tcall->sdata;
493 m->wsize = req->tcall->size;
494 m->wpos = 0;
495 spin_unlock(&m->lock);
496 }
497
498 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
499 m->wsize);
500 clear_bit(Wpending, &m->wsched);
501 err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
502 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
503 if (err == -EAGAIN) {
504 clear_bit(Wworksched, &m->wsched);
505 return;
506 }
507
508 if (err <= 0)
509 goto error;
510
511 m->wpos += err;
512 if (m->wpos == m->wsize)
513 m->wpos = m->wsize = 0;
514
515 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
516 if (test_and_clear_bit(Wpending, &m->wsched))
517 n = POLLOUT;
518 else
519 n = m->trans->poll(m->trans, NULL);
520
521 if (n & POLLOUT) {
522 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
523 queue_work(p9_mux_wq, &m->wq);
524 } else
525 clear_bit(Wworksched, &m->wsched);
526 } else
527 clear_bit(Wworksched, &m->wsched);
528
529 return;
530
531error:
532 p9_conn_cancel(m, err);
533 clear_bit(Wworksched, &m->wsched);
534}
535
536static void process_request(struct p9_conn *m, struct p9_req *req)
537{
538 int ecode;
539 struct p9_str *ename;
540
541 if (!req->err && req->rcall->id == P9_RERROR) {
542 ecode = req->rcall->params.rerror.errno;
543 ename = &req->rcall->params.rerror.error;
544
545 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
546 ename->str);
547
548 if (*m->extended)
549 req->err = -ecode;
550
551 if (!req->err) {
552 req->err = p9_errstr2errno(ename->str, ename->len);
553
554 if (!req->err) { /* string match failed */
555 PRINT_FCALL_ERROR("unknown error", req->rcall);
556 }
557
558 if (!req->err)
559 req->err = -ESERVERFAULT;
560 }
561 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
562 P9_DPRINTK(P9_DEBUG_ERROR,
563 "fcall mismatch: expected %d, got %d\n",
564 req->tcall->id + 1, req->rcall->id);
565 if (!req->err)
566 req->err = -EIO;
567 }
568}
569
570/**
571 * p9_read_work - called when there is some data to be read from a transport
572 */
573static void p9_read_work(struct work_struct *work)
574{
575 int n, err;
576 struct p9_conn *m;
577 struct p9_req *req, *rptr, *rreq;
578 struct p9_fcall *rcall;
579 char *rbuf;
580
581 m = container_of(work, struct p9_conn, rq);
582
583 if (m->err < 0)
584 return;
585
586 rcall = NULL;
587 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
588
589 if (!m->rcall) {
590 m->rcall =
591 kmalloc(sizeof(struct p9_fcall) + m->msize, GFP_KERNEL);
592 if (!m->rcall) {
593 err = -ENOMEM;
594 goto error;
595 }
596
597 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
598 m->rpos = 0;
599 }
600
601 clear_bit(Rpending, &m->wsched);
602 err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
603 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
604 if (err == -EAGAIN) {
605 clear_bit(Rworksched, &m->wsched);
606 return;
607 }
608
609 if (err <= 0)
610 goto error;
611
612 m->rpos += err;
613 while (m->rpos > 4) {
614 n = le32_to_cpu(*(__le32 *) m->rbuf);
615 if (n >= m->msize) {
616 P9_DPRINTK(P9_DEBUG_ERROR,
617 "requested packet size too big: %d\n", n);
618 err = -EIO;
619 goto error;
620 }
621
622 if (m->rpos < n)
623 break;
624
625 err =
626 p9_deserialize_fcall(m->rbuf, n, m->rcall, *m->extended);
627 if (err < 0) {
628 goto error;
629 }
630
631#ifdef CONFIG_NET_9P_DEBUG
632 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
633 char buf[150];
634
635 p9_printfcall(buf, sizeof(buf), m->rcall,
636 *m->extended);
637 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
638 }
639#endif
640
641 rcall = m->rcall;
642 rbuf = m->rbuf;
643 if (m->rpos > n) {
644 m->rcall = kmalloc(sizeof(struct p9_fcall) + m->msize,
645 GFP_KERNEL);
646 if (!m->rcall) {
647 err = -ENOMEM;
648 goto error;
649 }
650
651 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
652 memmove(m->rbuf, rbuf + n, m->rpos - n);
653 m->rpos -= n;
654 } else {
655 m->rcall = NULL;
656 m->rbuf = NULL;
657 m->rpos = 0;
658 }
659
660 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
661 rcall->id, rcall->tag);
662
663 req = NULL;
664 spin_lock(&m->lock);
665 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
666 if (rreq->tag == rcall->tag) {
667 req = rreq;
668 if (req->flush != Flushing)
669 list_del(&req->req_list);
670 break;
671 }
672 }
673 spin_unlock(&m->lock);
674
675 if (req) {
676 req->rcall = rcall;
677 process_request(m, req);
678
679 if (req->flush != Flushing) {
680 if (req->cb)
681 (*req->cb) (req, req->cba);
682 else
683 kfree(req->rcall);
684
685 wake_up(&m->equeue);
686 }
687 } else {
688 if (err >= 0 && rcall->id != P9_RFLUSH)
689 P9_DPRINTK(P9_DEBUG_ERROR,
690 "unexpected response mux %p id %d tag %d\n",
691 m, rcall->id, rcall->tag);
692 kfree(rcall);
693 }
694 }
695
696 if (!list_empty(&m->req_list)) {
697 if (test_and_clear_bit(Rpending, &m->wsched))
698 n = POLLIN;
699 else
700 n = m->trans->poll(m->trans, NULL);
701
702 if (n & POLLIN) {
703 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
704 queue_work(p9_mux_wq, &m->rq);
705 } else
706 clear_bit(Rworksched, &m->wsched);
707 } else
708 clear_bit(Rworksched, &m->wsched);
709
710 return;
711
712error:
713 p9_conn_cancel(m, err);
714 clear_bit(Rworksched, &m->wsched);
715}
716
717/**
718 * p9_send_request - send 9P request
719 * The function can sleep until the request is scheduled for sending.
720 * The function can be interrupted. Return from the function is not
721 * a guarantee that the request is sent successfully. Can return errors
722 * that can be retrieved by PTR_ERR macros.
723 *
724 * @m: mux data
725 * @tc: request to be sent
726 * @cb: callback function to call when response is received
727 * @cba: parameter to pass to the callback function
728 */
729static struct p9_req *p9_send_request(struct p9_conn *m,
730 struct p9_fcall *tc,
731 p9_conn_req_callback cb, void *cba)
732{
733 int n;
734 struct p9_req *req;
735
736 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
737 tc, tc->id);
738 if (m->err < 0)
739 return ERR_PTR(m->err);
740
741 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
742 if (!req)
743 return ERR_PTR(-ENOMEM);
744
745 if (tc->id == P9_TVERSION)
746 n = P9_NOTAG;
747 else
748 n = p9_mux_get_tag(m);
749
750 if (n < 0)
751 return ERR_PTR(-ENOMEM);
752
753 p9_set_tag(tc, n);
754
755#ifdef CONFIG_NET_9P_DEBUG
756 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
757 char buf[150];
758
759 p9_printfcall(buf, sizeof(buf), tc, *m->extended);
760 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
761 }
762#endif
763
764 spin_lock_init(&req->lock);
765 req->tag = n;
766 req->tcall = tc;
767 req->rcall = NULL;
768 req->err = 0;
769 req->cb = cb;
770 req->cba = cba;
771 req->flush = None;
772
773 spin_lock(&m->lock);
774 list_add_tail(&req->req_list, &m->unsent_req_list);
775 spin_unlock(&m->lock);
776
777 if (test_and_clear_bit(Wpending, &m->wsched))
778 n = POLLOUT;
779 else
780 n = m->trans->poll(m->trans, NULL);
781
782 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
783 queue_work(p9_mux_wq, &m->wq);
784
785 return req;
786}
787
788static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
789{
790 p9_mux_put_tag(m, req->tag);
791 kfree(req);
792}
793
794static void p9_mux_flush_cb(struct p9_req *freq, void *a)
795{
796 p9_conn_req_callback cb;
797 int tag;
798 struct p9_conn *m;
799 struct p9_req *req, *rreq, *rptr;
800
801 m = a;
802 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
803 freq->tcall, freq->rcall, freq->err,
804 freq->tcall->params.tflush.oldtag);
805
806 spin_lock(&m->lock);
807 cb = NULL;
808 tag = freq->tcall->params.tflush.oldtag;
809 req = NULL;
810 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
811 if (rreq->tag == tag) {
812 req = rreq;
813 list_del(&req->req_list);
814 break;
815 }
816 }
817 spin_unlock(&m->lock);
818
819 if (req) {
820 spin_lock(&req->lock);
821 req->flush = Flushed;
822 spin_unlock(&req->lock);
823
824 if (req->cb)
825 (*req->cb) (req, req->cba);
826 else
827 kfree(req->rcall);
828
829 wake_up(&m->equeue);
830 }
831
832 kfree(freq->tcall);
833 kfree(freq->rcall);
834 p9_mux_free_request(m, freq);
835}
836
837static int
838p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
839{
840 struct p9_fcall *fc;
841 struct p9_req *rreq, *rptr;
842
843 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
844
845 /* if a response was received for a request, do nothing */
846 spin_lock(&req->lock);
847 if (req->rcall || req->err) {
848 spin_unlock(&req->lock);
849 P9_DPRINTK(P9_DEBUG_MUX,
850 "mux %p req %p response already received\n", m, req);
851 return 0;
852 }
853
854 req->flush = Flushing;
855 spin_unlock(&req->lock);
856
857 spin_lock(&m->lock);
858 /* if the request is not sent yet, just remove it from the list */
859 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
860 if (rreq->tag == req->tag) {
861 P9_DPRINTK(P9_DEBUG_MUX,
862 "mux %p req %p request is not sent yet\n", m, req);
863 list_del(&rreq->req_list);
864 req->flush = Flushed;
865 spin_unlock(&m->lock);
866 if (req->cb)
867 (*req->cb) (req, req->cba);
868 return 0;
869 }
870 }
871 spin_unlock(&m->lock);
872
873 clear_thread_flag(TIF_SIGPENDING);
874 fc = p9_create_tflush(req->tag);
875 p9_send_request(m, fc, p9_mux_flush_cb, m);
876 return 1;
877}
878
879static void
880p9_conn_rpc_cb(struct p9_req *req, void *a)
881{
882 struct p9_mux_rpc *r;
883
884 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
885 r = a;
886 r->rcall = req->rcall;
887 r->err = req->err;
888
889 if (req->flush != None && !req->err)
890 r->err = -ERESTARTSYS;
891
892 wake_up(&r->wqueue);
893}
894
895/**
896 * p9_mux_rpc - sends 9P request and waits until a response is available.
897 * The function can be interrupted.
898 * @m: mux data
899 * @tc: request to be sent
900 * @rc: pointer where a pointer to the response is stored
901 */
902int
903p9_conn_rpc(struct p9_conn *m, struct p9_fcall *tc,
904 struct p9_fcall **rc)
905{
906 int err, sigpending;
907 unsigned long flags;
908 struct p9_req *req;
909 struct p9_mux_rpc r;
910
911 r.err = 0;
912 r.tcall = tc;
913 r.rcall = NULL;
914 r.m = m;
915 init_waitqueue_head(&r.wqueue);
916
917 if (rc)
918 *rc = NULL;
919
920 sigpending = 0;
921 if (signal_pending(current)) {
922 sigpending = 1;
923 clear_thread_flag(TIF_SIGPENDING);
924 }
925
926 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
927 if (IS_ERR(req)) {
928 err = PTR_ERR(req);
929 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
930 return err;
931 }
932
933 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
934 if (r.err < 0)
935 err = r.err;
936
937 if (err == -ERESTARTSYS && m->trans->status == Connected
938 && m->err == 0) {
939 if (p9_mux_flush_request(m, req)) {
940 /* wait until we get response of the flush message */
941 do {
942 clear_thread_flag(TIF_SIGPENDING);
943 err = wait_event_interruptible(r.wqueue,
944 r.rcall || r.err);
945 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
946 m->trans->status == Connected && !m->err);
947
948 err = -ERESTARTSYS;
949 }
950 sigpending = 1;
951 }
952
953 if (sigpending) {
954 spin_lock_irqsave(&current->sighand->siglock, flags);
955 recalc_sigpending();
956 spin_unlock_irqrestore(&current->sighand->siglock, flags);
957 }
958
959 if (rc)
960 *rc = r.rcall;
961 else
962 kfree(r.rcall);
963
964 p9_mux_free_request(m, req);
965 if (err > 0)
966 err = -EIO;
967
968 return err;
969}
970EXPORT_SYMBOL(p9_conn_rpc);
971
972#ifdef P9_NONBLOCK
973/**
974 * p9_conn_rpcnb - sends 9P request without waiting for response.
975 * @m: mux data
976 * @tc: request to be sent
977 * @cb: callback function to be called when response arrives
978 * @cba: value to pass to the callback function
979 */
980int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
981 p9_conn_req_callback cb, void *a)
982{
983 int err;
984 struct p9_req *req;
985
986 req = p9_send_request(m, tc, cb, a);
987 if (IS_ERR(req)) {
988 err = PTR_ERR(req);
989 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
990 return PTR_ERR(req);
991 }
992
993 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
994 return 0;
995}
996EXPORT_SYMBOL(p9_conn_rpcnb);
997#endif /* P9_NONBLOCK */
998
999/**
1000 * p9_conn_cancel - cancel all pending requests with error
1001 * @m: mux data
1002 * @err: error code
1003 */
1004void p9_conn_cancel(struct p9_conn *m, int err)
1005{
1006 struct p9_req *req, *rtmp;
1007 LIST_HEAD(cancel_list);
1008
1009 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1010 m->err = err;
1011 spin_lock(&m->lock);
1012 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1013 list_move(&req->req_list, &cancel_list);
1014 }
1015 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1016 list_move(&req->req_list, &cancel_list);
1017 }
1018 spin_unlock(&m->lock);
1019
1020 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1021 list_del(&req->req_list);
1022 if (!req->err)
1023 req->err = err;
1024
1025 if (req->cb)
1026 (*req->cb) (req, req->cba);
1027 else
1028 kfree(req->rcall);
1029 }
1030
1031 wake_up(&m->equeue);
1032}
1033EXPORT_SYMBOL(p9_conn_cancel);
1034
1035static u16 p9_mux_get_tag(struct p9_conn *m)
1036{
1037 int tag;
1038
1039 tag = p9_idpool_get(m->tagpool);
1040 if (tag < 0)
1041 return P9_NOTAG;
1042 else
1043 return (u16) tag;
1044}
1045
1046static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
1047{
1048 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
1049 p9_idpool_put(tag, m->tagpool);
1050}