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