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