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