aboutsummaryrefslogtreecommitdiffstats
path: root/fs/9p/mux.c
diff options
context:
space:
mode:
authorLatchesar Ionkov <lucho@ionkov.net>2006-01-08 04:04:58 -0500
committerLinus Torvalds <torvalds@g5.osdl.org>2006-01-08 23:14:05 -0500
commit3cf6429a26da5c4d7b795e6d0f8f56ed2e4fdfc0 (patch)
treea8d856763fd9a0536519634c93ab92da684107fa /fs/9p/mux.c
parentf5ef3c105bee3a52486d7b55cef3330fcde9bca6 (diff)
[PATCH] v9fs: new multiplexer implementation
New multiplexer implementation. Decreases the number of kernel threads required. Better handling when the user process receives a signal. Signed-off-by: Latchesar Ionkov <lucho@ionkov.net> Cc: Eric Van Hensbergen <ericvh@ericvh.myip.org> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'fs/9p/mux.c')
-rw-r--r--fs/9p/mux.c1122
1 files changed, 794 insertions, 328 deletions
diff --git a/fs/9p/mux.c b/fs/9p/mux.c
index 8835b576f744..62b6ad0767e1 100644
--- a/fs/9p/mux.c
+++ b/fs/9p/mux.c
@@ -4,7 +4,7 @@
4 * Protocol Multiplexer 4 * Protocol Multiplexer
5 * 5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> 6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2004 by Latchesar Ionkov <lucho@ionkov.net> 7 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
@@ -28,6 +28,7 @@
28#include <linux/module.h> 28#include <linux/module.h>
29#include <linux/errno.h> 29#include <linux/errno.h>
30#include <linux/fs.h> 30#include <linux/fs.h>
31#include <linux/poll.h>
31#include <linux/kthread.h> 32#include <linux/kthread.h>
32#include <linux/idr.h> 33#include <linux/idr.h>
33 34
@@ -38,438 +39,903 @@
38#include "conv.h" 39#include "conv.h"
39#include "mux.h" 40#include "mux.h"
40 41
41/** 42#define ERREQFLUSH 1
42 * dprintcond - print condition of session info 43#define SCHED_TIMEOUT 10
43 * @v9ses: session info structure 44#define MAXPOLLWADDR 2
44 * @req: RPC request structure 45
45 * 46enum {
46 */ 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
53struct v9fs_mux_poll_task;
54
55struct v9fs_req {
56 int tag;
57 struct v9fs_fcall *tcall;
58 struct v9fs_fcall *rcall;
59 int err;
60 v9fs_mux_req_callback cb;
61 void *cba;
62 struct list_head req_list;
63};
64
65struct v9fs_mux_data {
66 spinlock_t lock;
67 struct list_head mux_list;
68 struct v9fs_mux_poll_task *poll_task;
69 int msize;
70 unsigned char *extended;
71 struct v9fs_transport *trans;
72 struct v9fs_idpool tidpool;
73 int err;
74 wait_queue_head_t equeue;
75 struct list_head req_list;
76 struct list_head unsent_req_list;
77 int rpos;
78 char *rbuf;
79 int wpos;
80 int wsize;
81 char *wbuf;
82 wait_queue_t poll_wait[MAXPOLLWADDR];
83 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
84 poll_table pt;
85 struct work_struct rq;
86 struct work_struct wq;
87 unsigned long wsched;
88};
89
90struct v9fs_mux_poll_task {
91 struct task_struct *task;
92 struct list_head mux_list;
93 int muxnum;
94};
95
96struct v9fs_mux_rpc {
97 struct v9fs_mux_data *m;
98 struct v9fs_req *req;
99 int err;
100 struct v9fs_fcall *rcall;
101 wait_queue_head_t wqueue;
102};
103
104static int v9fs_poll_proc(void *);
105static void v9fs_read_work(void *);
106static void v9fs_write_work(void *);
107static void v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
108 poll_table * p);
109
110static DECLARE_MUTEX(v9fs_mux_task_lock);
111static struct workqueue_struct *v9fs_mux_wq;
112
113static int v9fs_mux_num;
114static int v9fs_mux_poll_task_num;
115static struct v9fs_mux_poll_task v9fs_mux_poll_tasks[100];
116
117void v9fs_mux_global_init(void)
118{
119 int i;
120
121 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++)
122 v9fs_mux_poll_tasks[i].task = NULL;
123
124 v9fs_mux_wq = create_workqueue("v9fs");
125}
47 126
48static inline int 127void v9fs_mux_global_exit(void)
49dprintcond(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
50{ 128{
51 dprintk(DEBUG_MUX, "condition: %d, %p\n", v9ses->transport->status, 129 destroy_workqueue(v9fs_mux_wq);
52 req->rcall);
53 return 0;
54} 130}
55 131
56/** 132/**
57 * xread - force read of a certain number of bytes 133 * v9fs_mux_calc_poll_procs - calculates the number of polling procs
58 * @v9ses: session info structure 134 * based on the number of mounted v9fs filesystems.
59 * @ptr: pointer to buffer
60 * @sz: number of bytes to read
61 * 135 *
62 * Chuck Cranor CS-533 project1 136 * The current implementation returns sqrt of the number of mounts.
63 */ 137 */
138inline int v9fs_mux_calc_poll_procs(int muxnum)
139{
140 int n;
141
142 if (v9fs_mux_poll_task_num)
143 n = muxnum / v9fs_mux_poll_task_num +
144 (muxnum % v9fs_mux_poll_task_num ? 1 : 0);
145 else
146 n = 1;
147
148 if (n > ARRAY_SIZE(v9fs_mux_poll_tasks))
149 n = ARRAY_SIZE(v9fs_mux_poll_tasks);
64 150
65static int xread(struct v9fs_session_info *v9ses, void *ptr, unsigned long sz) 151 return n;
152}
153
154static void v9fs_mux_poll_start(struct v9fs_mux_data *m)
66{ 155{
67 int rd = 0; 156 int i, n;
68 int ret = 0; 157 struct v9fs_mux_poll_task *vpt, *vptlast;
69 while (rd < sz) { 158
70 ret = v9ses->transport->read(v9ses->transport, ptr, sz - rd); 159 dprintk(DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, v9fs_mux_num,
71 if (ret <= 0) { 160 v9fs_mux_poll_task_num);
72 dprintk(DEBUG_ERROR, "xread errno %d\n", ret); 161 up(&v9fs_mux_task_lock);
73 return ret; 162
163 n = v9fs_mux_calc_poll_procs(v9fs_mux_num + 1);
164 if (n > v9fs_mux_poll_task_num) {
165 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
166 if (v9fs_mux_poll_tasks[i].task == NULL) {
167 vpt = &v9fs_mux_poll_tasks[i];
168 dprintk(DEBUG_MUX, "create proc %p\n", vpt);
169 vpt->task = kthread_create(v9fs_poll_proc,
170 vpt, "v9fs-poll");
171 INIT_LIST_HEAD(&vpt->mux_list);
172 vpt->muxnum = 0;
173 v9fs_mux_poll_task_num++;
174 wake_up_process(vpt->task);
175 break;
176 }
74 } 177 }
75 rd += ret;
76 ptr += ret;
77 }
78 return (rd);
79}
80 178
81/** 179 if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks))
82 * read_message - read a full 9P2000 fcall packet 180 dprintk(DEBUG_ERROR, "warning: no free poll slots\n");
83 * @v9ses: session info structure 181 }
84 * @rcall: fcall structure to read into
85 * @rcalllen: size of fcall buffer
86 *
87 */
88 182
89static int 183 n = (v9fs_mux_num + 1) / v9fs_mux_poll_task_num +
90read_message(struct v9fs_session_info *v9ses, 184 ((v9fs_mux_num + 1) % v9fs_mux_poll_task_num ? 1 : 0);
91 struct v9fs_fcall *rcall, int rcalllen) 185
92{ 186 vptlast = NULL;
93 unsigned char buf[4]; 187 for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
94 void *data; 188 vpt = &v9fs_mux_poll_tasks[i];
95 int size = 0; 189 if (vpt->task != NULL) {
96 int res = 0; 190 vptlast = vpt;
97 191 if (vpt->muxnum < n) {
98 res = xread(v9ses, buf, sizeof(buf)); 192 dprintk(DEBUG_MUX, "put in proc %d\n", i);
99 if (res < 0) { 193 list_add(&m->mux_list, &vpt->mux_list);
100 dprintk(DEBUG_ERROR, 194 vpt->muxnum++;
101 "Reading of count field failed returned: %d\n", res); 195 m->poll_task = vpt;
102 return res; 196 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
197 init_poll_funcptr(&m->pt, v9fs_pollwait);
198 break;
199 }
200 }
103 } 201 }
104 202
105 if (res < 4) { 203 if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks)) {
106 dprintk(DEBUG_ERROR, 204 dprintk(DEBUG_MUX, "put in proc %d\n", i);
107 "Reading of count field failed returned: %d\n", res); 205 list_add(&m->mux_list, &vptlast->mux_list);
108 return -EIO; 206 vptlast->muxnum++;
207 m->poll_task = vpt;
208 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
209 init_poll_funcptr(&m->pt, v9fs_pollwait);
109 } 210 }
110 211
111 size = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); 212 v9fs_mux_num++;
112 dprintk(DEBUG_MUX, "got a packet count: %d\n", size); 213 down(&v9fs_mux_task_lock);
214}
113 215
114 /* adjust for the four bytes of size */ 216static void v9fs_mux_poll_stop(struct v9fs_mux_data *m)
115 size -= 4; 217{
218 int i;
219 struct v9fs_mux_poll_task *vpt;
220
221 up(&v9fs_mux_task_lock);
222 vpt = m->poll_task;
223 list_del(&m->mux_list);
224 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
225 if (m->poll_waddr[i] != NULL) {
226 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
227 m->poll_waddr[i] = NULL;
228 }
229 }
230 vpt->muxnum--;
231 if (!vpt->muxnum) {
232 dprintk(DEBUG_MUX, "destroy proc %p\n", vpt);
233 send_sig(SIGKILL, vpt->task, 1);
234 vpt->task = NULL;
235 v9fs_mux_poll_task_num--;
236 }
237 v9fs_mux_num--;
238 down(&v9fs_mux_task_lock);
239}
116 240
117 if (size > v9ses->maxdata) { 241/**
118 dprintk(DEBUG_ERROR, "packet too big: %d\n", size); 242 * v9fs_mux_init - allocate and initialize the per-session mux data
119 return -E2BIG; 243 * Creates the polling task if this is the first session.
244 *
245 * @trans - transport structure
246 * @msize - maximum message size
247 * @extended - pointer to the extended flag
248 */
249struct v9fs_mux_data *v9fs_mux_init(struct v9fs_transport *trans, int msize,
250 unsigned char *extended)
251{
252 int i, n;
253 struct v9fs_mux_data *m, *mtmp;
254
255 dprintk(DEBUG_MUX, "transport %p msize %d\n", trans, msize);
256 m = kmalloc(sizeof(struct v9fs_mux_data) + 2 * msize, GFP_KERNEL);
257 if (!m)
258 return ERR_PTR(-ENOMEM);
259
260 spin_lock_init(&m->lock);
261 INIT_LIST_HEAD(&m->mux_list);
262 m->msize = msize;
263 m->extended = extended;
264 m->trans = trans;
265 idr_init(&m->tidpool.pool);
266 init_MUTEX(&m->tidpool.lock);
267 m->err = 0;
268 init_waitqueue_head(&m->equeue);
269 INIT_LIST_HEAD(&m->req_list);
270 INIT_LIST_HEAD(&m->unsent_req_list);
271 m->rpos = 0;
272 m->rbuf = (char *)m + sizeof(struct v9fs_mux_data);
273 m->wpos = m->wsize = 0;
274 m->wbuf = m->rbuf + msize;
275 INIT_WORK(&m->rq, v9fs_read_work, m);
276 INIT_WORK(&m->wq, v9fs_write_work, m);
277 m->wsched = 0;
278 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
279 v9fs_mux_poll_start(m);
280
281 n = trans->poll(trans, &m->pt);
282 if (n & POLLIN) {
283 dprintk(DEBUG_MUX, "mux %p can read\n", m);
284 set_bit(Rpending, &m->wsched);
120 } 285 }
121 286
122 data = kmalloc(size, GFP_KERNEL); 287 if (n & POLLOUT) {
123 if (!data) { 288 dprintk(DEBUG_MUX, "mux %p can write\n", m);
124 eprintk(KERN_WARNING, "out of memory\n"); 289 set_bit(Wpending, &m->wsched);
125 return -ENOMEM;
126 } 290 }
127 291
128 res = xread(v9ses, data, size); 292 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
129 if (res < size) { 293 if (IS_ERR(m->poll_waddr[i])) {
130 dprintk(DEBUG_ERROR, "Reading of fcall failed returned: %d\n", 294 v9fs_mux_poll_stop(m);
131 res); 295 mtmp = (void *)m->poll_waddr; /* the error code */
132 kfree(data); 296 kfree(m);
133 return res; 297 m = mtmp;
298 break;
299 }
134 } 300 }
135 301
136 /* we now have an in-memory string that is the reply. 302 return m;
137 * deserialize it. There is very little to go wrong at this point 303}
138 * save for v9fs_alloc errors.
139 */
140 res = v9fs_deserialize_fcall(v9ses, size, data, v9ses->maxdata,
141 rcall, rcalllen);
142 304
143 kfree(data); 305/**
306 * v9fs_mux_destroy - cancels all pending requests and frees mux resources
307 */
308void v9fs_mux_destroy(struct v9fs_mux_data *m)
309{
310 dprintk(DEBUG_MUX, "mux %p prev %p next %p\n", m,
311 m->mux_list.prev, m->mux_list.next);
312 v9fs_mux_cancel(m, -ECONNRESET);
313
314 if (!list_empty(&m->req_list)) {
315 /* wait until all processes waiting on this session exit */
316 dprintk(DEBUG_MUX, "mux %p waiting for empty request queue\n",
317 m);
318 wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000);
319 dprintk(DEBUG_MUX, "mux %p request queue empty: %d\n", m,
320 list_empty(&m->req_list));
321 }
144 322
145 if (res < 0) 323 v9fs_mux_poll_stop(m);
146 return res; 324 m->trans = NULL;
147 325
148 return 0; 326 kfree(m);
149} 327}
150 328
151/** 329/**
152 * v9fs_recv - receive an RPC response for a particular tag 330 * v9fs_pollwait - called by files poll operation to add v9fs-poll task
153 * @v9ses: session info structure 331 * to files wait queue
154 * @req: RPC request structure
155 *
156 */ 332 */
157 333static void
158static int v9fs_recv(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req) 334v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
335 poll_table * p)
159{ 336{
160 int ret = 0; 337 int i;
161 338 struct v9fs_mux_data *m;
162 dprintk(DEBUG_MUX, "waiting for response: %d\n", req->tcall->tag);
163 ret = wait_event_interruptible(v9ses->read_wait,
164 ((v9ses->transport->status != Connected) ||
165 (req->rcall != 0) || (req->err < 0) ||
166 dprintcond(v9ses, req)));
167 339
168 dprintk(DEBUG_MUX, "got it: rcall %p\n", req->rcall); 340 m = container_of(p, struct v9fs_mux_data, pt);
341 for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
342 if (m->poll_waddr[i] == NULL)
343 break;
169 344
170 spin_lock(&v9ses->muxlock); 345 if (i >= ARRAY_SIZE(m->poll_waddr)) {
171 list_del(&req->next); 346 dprintk(DEBUG_ERROR, "not enough wait_address slots\n");
172 spin_unlock(&v9ses->muxlock); 347 return;
348 }
173 349
174 if (req->err < 0) 350 m->poll_waddr[i] = wait_address;
175 return req->err;
176 351
177 if (v9ses->transport->status == Disconnected) 352 if (!wait_address) {
178 return -ECONNRESET; 353 dprintk(DEBUG_ERROR, "no wait_address\n");
354 m->poll_waddr[i] = ERR_PTR(-EIO);
355 return;
356 }
179 357
180 return ret; 358 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
359 add_wait_queue(wait_address, &m->poll_wait[i]);
181} 360}
182 361
183/** 362/**
184 * v9fs_send - send a 9P request 363 * v9fs_poll_mux - polls a mux and schedules read or write works if necessary
185 * @v9ses: session info structure
186 * @req: RPC request to send
187 *
188 */ 364 */
189 365static inline void v9fs_poll_mux(struct v9fs_mux_data *m)
190static int v9fs_send(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
191{ 366{
192 int ret = -1; 367 int n;
193 void *data = NULL;
194 struct v9fs_fcall *tcall = req->tcall;
195
196 data = kmalloc(v9ses->maxdata + V9FS_IOHDRSZ, GFP_KERNEL);
197 if (!data)
198 return -ENOMEM;
199
200 tcall->size = 0; /* enforce size recalculation */
201 ret =
202 v9fs_serialize_fcall(v9ses, tcall, data,
203 v9ses->maxdata + V9FS_IOHDRSZ);
204 if (ret < 0)
205 goto free_data;
206
207 spin_lock(&v9ses->muxlock);
208 list_add(&req->next, &v9ses->mux_fcalls);
209 spin_unlock(&v9ses->muxlock);
210
211 dprintk(DEBUG_MUX, "sending message: tag %d size %d\n", tcall->tag,
212 tcall->size);
213 ret = v9ses->transport->write(v9ses->transport, data, tcall->size);
214
215 if (ret != tcall->size) {
216 spin_lock(&v9ses->muxlock);
217 list_del(&req->next);
218 kfree(req->rcall);
219 368
220 spin_unlock(&v9ses->muxlock); 369 if (m->err < 0)
221 if (ret >= 0) 370 return;
222 ret = -EREMOTEIO; 371
223 } else 372 n = m->trans->poll(m->trans, NULL);
224 ret = 0; 373 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
374 dprintk(DEBUG_MUX, "error mux %p err %d\n", m, n);
375 if (n >= 0)
376 n = -ECONNRESET;
377 v9fs_mux_cancel(m, n);
378 }
379
380 if (n & POLLIN) {
381 set_bit(Rpending, &m->wsched);
382 dprintk(DEBUG_MUX, "mux %p can read\n", m);
383 if (!test_and_set_bit(Rworksched, &m->wsched)) {
384 dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
385 queue_work(v9fs_mux_wq, &m->rq);
386 }
387 }
225 388
226 free_data: 389 if (n & POLLOUT) {
227 kfree(data); 390 set_bit(Wpending, &m->wsched);
228 return ret; 391 dprintk(DEBUG_MUX, "mux %p can write\n", m);
392 if ((m->wsize || !list_empty(&m->unsent_req_list))
393 && !test_and_set_bit(Wworksched, &m->wsched)) {
394 dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
395 queue_work(v9fs_mux_wq, &m->wq);
396 }
397 }
229} 398}
230 399
231/** 400/**
232 * v9fs_mux_rpc - send a request, receive a response 401 * v9fs_poll_proc - polls all v9fs transports for new events and queues
233 * @v9ses: session info structure 402 * the appropriate work to the work queue
234 * @tcall: fcall to send
235 * @rcall: buffer to place response into
236 *
237 */ 403 */
238 404static int v9fs_poll_proc(void *a)
239long
240v9fs_mux_rpc(struct v9fs_session_info *v9ses, struct v9fs_fcall *tcall,
241 struct v9fs_fcall **rcall)
242{ 405{
243 int tid = -1; 406 struct v9fs_mux_data *m, *mtmp;
244 struct v9fs_fcall *fcall = NULL; 407 struct v9fs_mux_poll_task *vpt;
245 struct v9fs_rpcreq req;
246 int ret = -1;
247 408
248 if (!v9ses) 409 vpt = a;
249 return -EINVAL; 410 dprintk(DEBUG_MUX, "start %p %p\n", current, vpt);
411 allow_signal(SIGKILL);
412 while (!kthread_should_stop()) {
413 set_current_state(TASK_INTERRUPTIBLE);
414 if (signal_pending(current))
415 break;
250 416
251 if (!v9ses->transport || v9ses->transport->status != Connected) 417 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
252 return -EIO; 418 v9fs_poll_mux(m);
419 }
420
421 dprintk(DEBUG_MUX, "sleeping...\n");
422 schedule_timeout(SCHED_TIMEOUT * HZ);
423 }
253 424
254 if (rcall) 425 __set_current_state(TASK_RUNNING);
255 *rcall = NULL; 426 dprintk(DEBUG_MUX, "finish\n");
427 return 0;
428}
256 429
257 if (tcall->id != TVERSION) { 430static inline int v9fs_write_req(struct v9fs_mux_data *m, struct v9fs_req *req)
258 tid = v9fs_get_idpool(&v9ses->tidpool); 431{
259 if (tid < 0) 432 int n;
260 return -ENOMEM; 433
434 list_move_tail(&req->req_list, &m->req_list);
435 n = v9fs_serialize_fcall(req->tcall, m->wbuf, m->msize, *m->extended);
436 if (n < 0) {
437 req->err = n;
438 list_del(&req->req_list);
439 if (req->cb) {
440 spin_unlock(&m->lock);
441 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
442 req->cb = NULL;
443 spin_lock(&m->lock);
444 } else
445 kfree(req->rcall);
446
447 kfree(req);
261 } 448 }
262 449
263 tcall->tag = tid; 450 return n;
451}
264 452
265 req.tcall = tcall; 453/**
266 req.err = 0; 454 * v9fs_write_work - called when a transport can send some data
267 req.rcall = NULL; 455 */
456static void v9fs_write_work(void *a)
457{
458 int n, err;
459 struct v9fs_mux_data *m;
460 struct v9fs_req *req, *rtmp;
268 461
269 ret = v9fs_send(v9ses, &req); 462 m = a;
270 463
271 if (ret < 0) { 464 if (m->err < 0) {
272 if (tcall->id != TVERSION) 465 clear_bit(Wworksched, &m->wsched);
273 v9fs_put_idpool(tid, &v9ses->tidpool); 466 return;
274 dprintk(DEBUG_MUX, "error %d\n", ret);
275 return ret;
276 } 467 }
277 468
278 ret = v9fs_recv(v9ses, &req); 469 if (!m->wsize) {
279 470 if (list_empty(&m->unsent_req_list)) {
280 fcall = req.rcall; 471 clear_bit(Wworksched, &m->wsched);
281 472 return;
282 dprintk(DEBUG_MUX, "received: tag=%x, ret=%d\n", tcall->tag, ret);
283 if (ret == -ERESTARTSYS) {
284 if (v9ses->transport->status != Disconnected
285 && tcall->id != TFLUSH) {
286 unsigned long flags;
287
288 dprintk(DEBUG_MUX, "flushing the tag: %d\n",
289 tcall->tag);
290 clear_thread_flag(TIF_SIGPENDING);
291 v9fs_t_flush(v9ses, tcall->tag);
292 spin_lock_irqsave(&current->sighand->siglock, flags);
293 recalc_sigpending();
294 spin_unlock_irqrestore(&current->sighand->siglock,
295 flags);
296 dprintk(DEBUG_MUX, "flushing done\n");
297 } 473 }
298 474
299 goto release_req; 475 err = 0;
300 } else if (ret < 0) 476 spin_lock(&m->lock);
301 goto release_req; 477 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list,
302 478 req_list) {
303 if (!fcall) 479 err = v9fs_write_req(m, req);
304 ret = -EIO; 480 if (err > 0)
305 else { 481 break;
306 if (fcall->id == RERROR) {
307 ret = v9fs_errstr2errno(fcall->params.rerror.error);
308 if (ret == 0) { /* string match failed */
309 if (fcall->params.rerror.errno)
310 ret = -(fcall->params.rerror.errno);
311 else
312 ret = -ESERVERFAULT;
313 }
314 } else if (fcall->id != tcall->id + 1) {
315 dprintk(DEBUG_ERROR,
316 "fcall mismatch: expected %d, got %d\n",
317 tcall->id + 1, fcall->id);
318 ret = -EIO;
319 } 482 }
483
484 m->wsize = err;
485 m->wpos = 0;
486 spin_unlock(&m->lock);
320 } 487 }
321 488
322 release_req: 489 dprintk(DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, m->wsize);
323 if (tcall->id != TVERSION) 490 clear_bit(Wpending, &m->wsched);
324 v9fs_put_idpool(tid, &v9ses->tidpool); 491 err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
325 if (rcall) 492 dprintk(DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
326 *rcall = fcall; 493 if (err == -EAGAIN) {
327 else 494 clear_bit(Wworksched, &m->wsched);
328 kfree(fcall); 495 return;
496 }
497
498 if (err <= 0)
499 goto error;
500
501 m->wpos += err;
502 if (m->wpos == m->wsize)
503 m->wpos = m->wsize = 0;
504
505 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
506 if (test_and_clear_bit(Wpending, &m->wsched))
507 n = POLLOUT;
508 else
509 n = m->trans->poll(m->trans, NULL);
510
511 if (n & POLLOUT) {
512 dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
513 queue_work(v9fs_mux_wq, &m->wq);
514 } else
515 clear_bit(Wworksched, &m->wsched);
516 } else
517 clear_bit(Wworksched, &m->wsched);
329 518
330 return ret; 519 return;
520
521 error:
522 v9fs_mux_cancel(m, err);
523 clear_bit(Wworksched, &m->wsched);
331} 524}
332 525
333/** 526static void process_request(struct v9fs_mux_data *m, struct v9fs_req *req)
334 * v9fs_mux_cancel_requests - cancels all pending requests
335 *
336 * @v9ses: session info structure
337 * @err: error code to return to the requests
338 */
339void v9fs_mux_cancel_requests(struct v9fs_session_info *v9ses, int err)
340{ 527{
341 struct v9fs_rpcreq *rptr; 528 int ecode, tag;
342 struct v9fs_rpcreq *rreq; 529 char *ename;
530
531 tag = req->tag;
532 if (req->rcall->id == RERROR && !req->err) {
533 ecode = req->rcall->params.rerror.errno;
534 ename = req->rcall->params.rerror.error;
343 535
344 dprintk(DEBUG_MUX, " %d\n", err); 536 dprintk(DEBUG_MUX, "Rerror %s\n", ename);
345 spin_lock(&v9ses->muxlock); 537
346 list_for_each_entry_safe(rreq, rptr, &v9ses->mux_fcalls, next) { 538 if (*m->extended)
347 rreq->err = err; 539 req->err = -ecode;
540
541 if (!req->err) {
542 req->err = v9fs_errstr2errno(ename);
543
544 if (!req->err) { /* string match failed */
545 dprintk(DEBUG_ERROR, "unknown error: %s\n",
546 ename);
547 }
548
549 if (!req->err)
550 req->err = -ESERVERFAULT;
551 }
552 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
553 dprintk(DEBUG_ERROR, "fcall mismatch: expected %d, got %d\n",
554 req->tcall->id + 1, req->rcall->id);
555 if (!req->err)
556 req->err = -EIO;
348 } 557 }
349 spin_unlock(&v9ses->muxlock); 558
350 wake_up_all(&v9ses->read_wait); 559 if (req->cb && req->err != ERREQFLUSH) {
560 dprintk(DEBUG_MUX, "calling callback tcall %p rcall %p\n",
561 req->tcall, req->rcall);
562
563 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
564 req->cb = NULL;
565 } else
566 kfree(req->rcall);
567
568 if (tag != V9FS_NOTAG)
569 v9fs_put_idpool(tag, &m->tidpool);
570
571 wake_up(&m->equeue);
572 kfree(req);
351} 573}
352 574
353/** 575/**
354 * v9fs_recvproc - kproc to handle demultiplexing responses 576 * v9fs_read_work - called when there is some data to be read from a transport
355 * @data: session info structure
356 *
357 */ 577 */
358 578static void v9fs_read_work(void *a)
359static int v9fs_recvproc(void *data)
360{ 579{
361 struct v9fs_session_info *v9ses = (struct v9fs_session_info *)data; 580 int n, err, rcallen;
362 struct v9fs_fcall *rcall = NULL; 581 struct v9fs_mux_data *m;
363 struct v9fs_rpcreq *rptr; 582 struct v9fs_req *req, *rptr, *rreq;
364 struct v9fs_rpcreq *req; 583 struct v9fs_fcall *rcall;
365 struct v9fs_rpcreq *rreq; 584
366 int err = 0; 585 m = a;
586
587 if (m->err < 0)
588 return;
589
590 rcall = NULL;
591 dprintk(DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
592 clear_bit(Rpending, &m->wsched);
593 err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
594 dprintk(DEBUG_MUX, "mux %p got %d bytes\n", m, err);
595 if (err == -EAGAIN) {
596 clear_bit(Rworksched, &m->wsched);
597 return;
598 }
367 599
368 allow_signal(SIGKILL); 600 if (err <= 0)
369 set_current_state(TASK_INTERRUPTIBLE); 601 goto error;
370 complete(&v9ses->proccmpl);
371 while (!kthread_should_stop() && err >= 0) {
372 req = rptr = rreq = NULL;
373 602
374 rcall = kmalloc(v9ses->maxdata + V9FS_IOHDRSZ, GFP_KERNEL); 603 m->rpos += err;
375 if (!rcall) { 604 while (m->rpos > 4) {
376 eprintk(KERN_ERR, "no memory for buffers\n"); 605 n = le32_to_cpu(*(__le32 *) m->rbuf);
606 if (n >= m->msize) {
607 dprintk(DEBUG_ERROR,
608 "requested packet size too big: %d\n", n);
609 err = -EIO;
610 goto error;
611 }
612
613 if (m->rpos < n)
377 break; 614 break;
615
616 rcallen = n + V9FS_FCALLHDRSZ;
617 rcall = kmalloc(rcallen, GFP_KERNEL);
618 if (!rcall) {
619 err = -ENOMEM;
620 goto error;
378 } 621 }
379 622
380 err = read_message(v9ses, rcall, v9ses->maxdata + V9FS_IOHDRSZ); 623 dump_data(m->rbuf, n);
381 spin_lock(&v9ses->muxlock); 624 err = v9fs_deserialize_fcall(m->rbuf, n, rcall, rcallen,
625 *m->extended);
382 if (err < 0) { 626 if (err < 0) {
383 list_for_each_entry_safe(rreq, rptr, &v9ses->mux_fcalls, next) { 627 kfree(rcall);
384 rreq->err = err; 628 goto error;
385 }
386 if(err != -ERESTARTSYS)
387 eprintk(KERN_ERR,
388 "Transport error while reading message %d\n", err);
389 } else {
390 list_for_each_entry_safe(rreq, rptr, &v9ses->mux_fcalls, next) {
391 if (rreq->tcall->tag == rcall->tag) {
392 req = rreq;
393 req->rcall = rcall;
394 break;
395 }
396 }
397 } 629 }
398 630
399 if (req && (req->tcall->id == TFLUSH)) { 631 dprintk(DEBUG_MUX, "mux %p fcall id %d tag %d\n", m, rcall->id,
400 struct v9fs_rpcreq *treq = NULL; 632 rcall->tag);
401 list_for_each_entry_safe(treq, rptr, &v9ses->mux_fcalls, next) { 633
402 if (treq->tcall->tag == 634 req = NULL;
403 req->tcall->params.tflush.oldtag) { 635 spin_lock(&m->lock);
404 list_del(&rptr->next); 636 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
405 kfree(treq->rcall); 637 if (rreq->tag == rcall->tag) {
406 break; 638 req = rreq;
407 } 639 req->rcall = rcall;
640 list_del(&req->req_list);
641 spin_unlock(&m->lock);
642 process_request(m, req);
643 break;
408 } 644 }
409 } 645 }
410 646
411 spin_unlock(&v9ses->muxlock);
412
413 if (!req) { 647 if (!req) {
414 if (err >= 0) 648 spin_unlock(&m->lock);
649 if (err >= 0 && rcall->id != RFLUSH)
415 dprintk(DEBUG_ERROR, 650 dprintk(DEBUG_ERROR,
416 "unexpected response: id %d tag %d\n", 651 "unexpected response mux %p id %d tag %d\n",
417 rcall->id, rcall->tag); 652 m, rcall->id, rcall->tag);
418
419 kfree(rcall); 653 kfree(rcall);
420 } 654 }
421 655
422 wake_up_all(&v9ses->read_wait); 656 if (m->rpos > n)
423 set_current_state(TASK_INTERRUPTIBLE); 657 memmove(m->rbuf, m->rbuf + n, m->rpos - n);
658 m->rpos -= n;
424 } 659 }
425 660
426 v9ses->transport->close(v9ses->transport); 661 if (!list_empty(&m->req_list)) {
427 662 if (test_and_clear_bit(Rpending, &m->wsched))
428 /* Inform all pending processes about the failure */ 663 n = POLLIN;
429 wake_up_all(&v9ses->read_wait); 664 else
430 665 n = m->trans->poll(m->trans, NULL);
431 if (signal_pending(current)) 666
432 complete(&v9ses->proccmpl); 667 if (n & POLLIN) {
668 dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
669 queue_work(v9fs_mux_wq, &m->rq);
670 } else
671 clear_bit(Rworksched, &m->wsched);
672 } else
673 clear_bit(Rworksched, &m->wsched);
433 674
434 dprintk(DEBUG_MUX, "recvproc: end\n"); 675 return;
435 v9ses->recvproc = NULL;
436 676
437 return err >= 0; 677 error:
678 v9fs_mux_cancel(m, err);
679 clear_bit(Rworksched, &m->wsched);
438} 680}
439 681
440/** 682/**
441 * v9fs_mux_init - initialize multiplexer (spawn kproc) 683 * v9fs_send_request - send 9P request
442 * @v9ses: session info structure 684 * The function can sleep until the request is scheduled for sending.
443 * @dev_name: mount device information (to create unique kproc) 685 * The function can be interrupted. Return from the function is not
686 * a guarantee that the request is sent succesfully. Can return errors
687 * that can be retrieved by PTR_ERR macros.
444 * 688 *
689 * @m: mux data
690 * @tc: request to be sent
691 * @cb: callback function to call when response is received
692 * @cba: parameter to pass to the callback function
445 */ 693 */
694static struct v9fs_req *v9fs_send_request(struct v9fs_mux_data *m,
695 struct v9fs_fcall *tc,
696 v9fs_mux_req_callback cb, void *cba)
697{
698 int n;
699 struct v9fs_req *req;
700
701 dprintk(DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
702 tc, tc->id);
703 if (m->err < 0)
704 return ERR_PTR(m->err);
705
706 req = kmalloc(sizeof(struct v9fs_req), GFP_KERNEL);
707 if (!req)
708 return ERR_PTR(-ENOMEM);
446 709
447int v9fs_mux_init(struct v9fs_session_info *v9ses, const char *dev_name) 710 if (tc->id == TVERSION)
711 n = V9FS_NOTAG;
712 else
713 n = v9fs_get_idpool(&m->tidpool);
714
715 if (n < 0)
716 return ERR_PTR(-ENOMEM);
717
718 tc->tag = n;
719 req->tag = n;
720 req->tcall = tc;
721 req->rcall = NULL;
722 req->err = 0;
723 req->cb = cb;
724 req->cba = cba;
725
726 spin_lock(&m->lock);
727 list_add_tail(&req->req_list, &m->unsent_req_list);
728 spin_unlock(&m->lock);
729
730 if (test_and_clear_bit(Wpending, &m->wsched))
731 n = POLLOUT;
732 else
733 n = m->trans->poll(m->trans, NULL);
734
735 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
736 queue_work(v9fs_mux_wq, &m->wq);
737
738 return req;
739}
740
741static inline void
742v9fs_mux_flush_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc,
743 int err)
448{ 744{
449 char procname[60]; 745 v9fs_mux_req_callback cb;
450 746 int tag;
451 strncpy(procname, dev_name, sizeof(procname)); 747 struct v9fs_mux_data *m;
452 procname[sizeof(procname) - 1] = 0; 748 struct v9fs_req *req, *rptr;
453 749
454 init_waitqueue_head(&v9ses->read_wait); 750 m = a;
455 init_completion(&v9ses->fcread); 751 dprintk(DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m, tc,
456 init_completion(&v9ses->proccmpl); 752 rc, err, tc->params.tflush.oldtag);
457 spin_lock_init(&v9ses->muxlock); 753
458 INIT_LIST_HEAD(&v9ses->mux_fcalls); 754 spin_lock(&m->lock);
459 v9ses->recvproc = NULL; 755 cb = NULL;
460 v9ses->curfcall = NULL; 756 tag = tc->params.tflush.oldtag;
461 757 list_for_each_entry_safe(req, rptr, &m->req_list, req_list) {
462 v9ses->recvproc = kthread_create(v9fs_recvproc, v9ses, 758 if (req->tag == tag) {
463 "v9fs_recvproc %s", procname); 759 list_del(&req->req_list);
464 760 if (req->cb) {
465 if (IS_ERR(v9ses->recvproc)) { 761 cb = req->cb;
466 eprintk(KERN_ERR, "cannot create receiving thread\n"); 762 req->cb = NULL;
467 v9fs_session_close(v9ses); 763 spin_unlock(&m->lock);
468 return -ECONNABORTED; 764 (*cb) (req->cba, req->tcall, req->rcall,
765 req->err);
766 }
767 kfree(req);
768 wake_up(&m->equeue);
769 break;
770 }
771 }
772
773 if (!cb)
774 spin_unlock(&m->lock);
775
776 if (v9fs_check_idpool(tag, &m->tidpool))
777 v9fs_put_idpool(tag, &m->tidpool);
778
779 kfree(tc);
780 kfree(rc);
781}
782
783static void
784v9fs_mux_flush_request(struct v9fs_mux_data *m, struct v9fs_req *req)
785{
786 struct v9fs_fcall *fc;
787
788 dprintk(DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
789
790 fc = kmalloc(sizeof(struct v9fs_fcall), GFP_KERNEL);
791 fc->id = TFLUSH;
792 fc->params.tflush.oldtag = req->tag;
793
794 v9fs_send_request(m, fc, v9fs_mux_flush_cb, m);
795}
796
797static void
798v9fs_mux_rpc_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc, int err)
799{
800 struct v9fs_mux_rpc *r;
801
802 if (err == ERREQFLUSH) {
803 dprintk(DEBUG_MUX, "err req flush\n");
804 return;
805 }
806
807 r = a;
808 dprintk(DEBUG_MUX, "mux %p req %p tc %p rc %p err %d\n", r->m, r->req,
809 tc, rc, err);
810 r->rcall = rc;
811 r->err = err;
812 wake_up(&r->wqueue);
813}
814
815/**
816 * v9fs_mux_rpc - sends 9P request and waits until a response is available.
817 * The function can be interrupted.
818 * @m: mux data
819 * @tc: request to be sent
820 * @rc: pointer where a pointer to the response is stored
821 */
822int
823v9fs_mux_rpc(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
824 struct v9fs_fcall **rc)
825{
826 int err;
827 unsigned long flags;
828 struct v9fs_req *req;
829 struct v9fs_mux_rpc r;
830
831 r.err = 0;
832 r.rcall = NULL;
833 r.m = m;
834 init_waitqueue_head(&r.wqueue);
835
836 if (rc)
837 *rc = NULL;
838
839 req = v9fs_send_request(m, tc, v9fs_mux_rpc_cb, &r);
840 if (IS_ERR(req)) {
841 err = PTR_ERR(req);
842 dprintk(DEBUG_MUX, "error %d\n", err);
843 return PTR_ERR(req);
844 }
845
846 r.req = req;
847 dprintk(DEBUG_MUX, "mux %p tc %p tag %d rpc %p req %p\n", m, tc,
848 req->tag, &r, req);
849 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
850 if (r.err < 0)
851 err = r.err;
852
853 if (err == -ERESTARTSYS && m->trans->status == Connected && m->err == 0) {
854 spin_lock(&m->lock);
855 req->tcall = NULL;
856 req->err = ERREQFLUSH;
857 spin_unlock(&m->lock);
858
859 clear_thread_flag(TIF_SIGPENDING);
860 v9fs_mux_flush_request(m, req);
861 spin_lock_irqsave(&current->sighand->siglock, flags);
862 recalc_sigpending();
863 spin_unlock_irqrestore(&current->sighand->siglock, flags);
469 } 864 }
470 865
471 wake_up_process(v9ses->recvproc); 866 if (!err) {
472 wait_for_completion(&v9ses->proccmpl); 867 if (r.rcall)
868 dprintk(DEBUG_MUX, "got response id %d tag %d\n",
869 r.rcall->id, r.rcall->tag);
870
871 if (rc)
872 *rc = r.rcall;
873 else
874 kfree(r.rcall);
875 } else {
876 kfree(r.rcall);
877 dprintk(DEBUG_MUX, "got error %d\n", err);
878 if (err > 0)
879 err = -EIO;
880 }
881
882 return err;
883}
884
885/**
886 * v9fs_mux_rpcnb - sends 9P request without waiting for response.
887 * @m: mux data
888 * @tc: request to be sent
889 * @cb: callback function to be called when response arrives
890 * @cba: value to pass to the callback function
891 */
892int v9fs_mux_rpcnb(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
893 v9fs_mux_req_callback cb, void *a)
894{
895 int err;
896 struct v9fs_req *req;
897
898 req = v9fs_send_request(m, tc, cb, a);
899 if (IS_ERR(req)) {
900 err = PTR_ERR(req);
901 dprintk(DEBUG_MUX, "error %d\n", err);
902 return PTR_ERR(req);
903 }
473 904
905 dprintk(DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
474 return 0; 906 return 0;
475} 907}
908
909/**
910 * v9fs_mux_cancel - cancel all pending requests with error
911 * @m: mux data
912 * @err: error code
913 */
914void v9fs_mux_cancel(struct v9fs_mux_data *m, int err)
915{
916 struct v9fs_req *req, *rtmp;
917 LIST_HEAD(cancel_list);
918
919 dprintk(DEBUG_MUX, "mux %p err %d\n", m, err);
920 m->err = err;
921 spin_lock(&m->lock);
922 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
923 list_move(&req->req_list, &cancel_list);
924 }
925 spin_unlock(&m->lock);
926
927 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
928 list_del(&req->req_list);
929 if (!req->err)
930 req->err = err;
931
932 if (req->cb)
933 (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
934 else
935 kfree(req->rcall);
936
937 kfree(req);
938 }
939
940 wake_up(&m->equeue);
941}