summaryrefslogtreecommitdiff
path: root/fs/9p/mux.c
blob: 8ebc1af2c245fef0f9b89644ee3a3dab099d28dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * linux/fs/9p/mux.c
 *
 * Protocol Multiplexer
 *
 *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
 *  Copyright (C) 2004 by Latchesar Ionkov <lucho@ionkov.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to:
 *  Free Software Foundation
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02111-1301  USA
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/kthread.h>
#include <linux/idr.h>

#include "debug.h"
#include "v9fs.h"
#include "9p.h"
#include "transport.h"
#include "conv.h"
#include "mux.h"

/**
 * dprintcond - print condition of session info
 * @v9ses: session info structure
 * @req: RPC request structure
 *
 */

static inline int
dprintcond(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
{
	dprintk(DEBUG_MUX, "condition: %d, %p\n", v9ses->transport->status,
		req->rcall);
	return 0;
}

/**
 * xread - force read of a certain number of bytes
 * @v9ses: session info structure
 * @ptr: pointer to buffer
 * @sz: number of bytes to read
 *
 * Chuck Cranor CS-533 project1
 */

static int xread(struct v9fs_session_info *v9ses, void *ptr, unsigned long sz)
{
	int rd = 0;
	int ret = 0;
	while (rd < sz) {
		ret = v9ses->transport->read(v9ses->transport, ptr, sz - rd);
		if (ret <= 0) {
			dprintk(DEBUG_ERROR, "xread errno %d\n", ret);
			return ret;
		}
		rd += ret;
		ptr += ret;
	}
	return (rd);
}

/**
 * read_message - read a full 9P2000 fcall packet
 * @v9ses: session info structure
 * @rcall: fcall structure to read into
 * @rcalllen: size of fcall buffer
 *
 */

static int
read_message(struct v9fs_session_info *v9ses,
	     struct v9fs_fcall *rcall, int rcalllen)
{
	unsigned char buf[4];
	void *data;
	int size = 0;
	int res = 0;

	res = xread(v9ses, buf, sizeof(buf));
	if (res < 0) {
		dprintk(DEBUG_ERROR,
			"Reading of count field failed returned: %d\n", res);
		return res;
	}

	if (res < 4) {
		dprintk(DEBUG_ERROR,
			"Reading of count field failed returned: %d\n", res);
		return -EIO;
	}

	size = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
	dprintk(DEBUG_MUX, "got a packet count: %d\n", size);

	/* adjust for the four bytes of size */
	size -= 4;

	if (size > v9ses->maxdata) {
		dprintk(DEBUG_ERROR, "packet too big: %d\n", size);
		return -E2BIG;
	}

	data = kmalloc(size, GFP_KERNEL);
	if (!data) {
		eprintk(KERN_WARNING, "out of memory\n");
		return -ENOMEM;
	}

	res = xread(v9ses, data, size);
	if (res < size) {
		dprintk(DEBUG_ERROR, "Reading of fcall failed returned: %d\n",
			res);
		kfree(data);
		return res;
	}

	/* we now have an in-memory string that is the reply.
	 * deserialize it. There is very little to go wrong at this point
	 * save for v9fs_alloc errors.
	 */
	res = v9fs_deserialize_fcall(v9ses, size, data, v9ses->maxdata,
				     rcall, rcalllen);

	kfree(data);

	if (res < 0)
		return res;

	return 0;
}

/**
 * v9fs_recv - receive an RPC response for a particular tag
 * @v9ses: session info structure
 * @req: RPC request structure
 *
 */

static int v9fs_recv(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
{
	int ret = 0;

	dprintk(DEBUG_MUX, "waiting for response: %d\n", req->tcall->tag);
	ret = wait_event_interruptible(v9ses->read_wait,
		       ((v9ses->transport->status != Connected) ||
			(req->rcall != 0) || dprintcond(v9ses, req)));

	dprintk(DEBUG_MUX, "got it: rcall %p\n", req->rcall);
	if (v9ses->transport->status == Disconnected)
		return -ECONNRESET;

	if (ret == 0) {
		spin_lock(&v9ses->muxlock);
		list_del(&req->next);
		spin_unlock(&v9ses->muxlock);
	}

	return ret;
}

/**
 * v9fs_send - send a 9P request
 * @v9ses: session info structure
 * @req: RPC request to send
 *
 */

static int v9fs_send(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
{
	int ret = -1;
	void *data = NULL;
	struct v9fs_fcall *tcall = req->tcall;

	data = kmalloc(v9ses->maxdata + V9FS_IOHDRSZ, GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	tcall->size = 0;	/* enforce size recalculation */
	ret =
	    v9fs_serialize_fcall(v9ses, tcall, data,
				 v9ses->maxdata + V9FS_IOHDRSZ);
	if (ret < 0)
		goto free_data;

	spin_lock(&v9ses->muxlock);
	list_add(&req->next, &v9ses->mux_fcalls);
	spin_unlock(&v9ses->muxlock);

	dprintk(DEBUG_MUX, "sending message: tag %d size %d\n", tcall->tag,
		tcall->size);
	ret = v9ses->transport->write(v9ses->transport, data, tcall->size);

	if (ret != tcall->size) {
		spin_lock(&v9ses->muxlock);
		list_del(&req->next);
		kfree(req->rcall);

		spin_unlock(&v9ses->muxlock);
		if (ret >= 0)
			ret = -EREMOTEIO;
	} else
		ret = 0;

      free_data:
	kfree(data);
	return ret;
}

/**
 * v9fs_mux_rpc - send a request, receive a response
 * @v9ses: session info structure
 * @tcall: fcall to send
 * @rcall: buffer to place response into
 *
 */

long
v9fs_mux_rpc(struct v9fs_session_info *v9ses, struct v9fs_fcall *tcall,
	     struct v9fs_fcall **rcall)
{
	int tid = -1;
	struct v9fs_fcall *fcall = NULL;
	struct v9fs_rpcreq req;
	int ret = -1;

	if (!v9ses)
		return -EINVAL;

	if (rcall)
		*rcall = NULL;

	if (tcall->id != TVERSION) {
		tid = v9fs_get_idpool(&v9ses->tidpool);
		if (tid < 0)
			return -ENOMEM;
	}

	tcall->tag = tid;

	req.tcall = tcall;
	req.rcall = NULL;

	ret = v9fs_send(v9ses, &req);

	if (ret < 0) {
		if (tcall->id != TVERSION)
			v9fs_put_idpool(tid, &v9ses->tidpool);
		dprintk(DEBUG_MUX, "error %d\n", ret);
		return ret;
	}

	ret = v9fs_recv(v9ses, &req);

	fcall = req.rcall;

	dprintk(DEBUG_MUX, "received: tag=%x, ret=%d\n", tcall->tag, ret);
	if (ret == -ERESTARTSYS) {
		if (v9ses->transport->status != Disconnected
		    && tcall->id != TFLUSH) {
			unsigned long flags;

			dprintk(DEBUG_MUX, "flushing the tag: %d\n",
				tcall->tag);
			clear_thread_flag(TIF_SIGPENDING);
			v9fs_t_flush(v9ses, tcall->tag);
			spin_lock_irqsave(&current->sighand->siglock, flags);
			recalc_sigpending();
			spin_unlock_irqrestore(&current->sighand->siglock,
					       flags);
			dprintk(DEBUG_MUX, "flushing done\n");
		}

		goto release_req;
	} else if (ret < 0)
		goto release_req;

	if (!fcall)
		ret = -EIO;
	else {
		if (fcall->id == RERROR) {
			ret = v9fs_errstr2errno(fcall->params.rerror.error);
			if (ret == 0) {	/* string match failed */
				if (fcall->params.rerror.errno)
					ret = -(fcall->params.rerror.errno);
				else
					ret = -ESERVERFAULT;
			}
		} else if (fcall->id != tcall->id + 1) {
			dprintk(DEBUG_ERROR,
				"fcall mismatch: expected %d, got %d\n",
				tcall->id + 1, fcall->id);
			ret = -EIO;
		}
	}

      release_req:
	if (tcall->id != TVERSION)
		v9fs_put_idpool(tid, &v9ses->tidpool);
	if (rcall)
		*rcall = fcall;
	else
		kfree(fcall);

	return ret;
}

/**
 * v9fs_recvproc - kproc to handle demultiplexing responses
 * @data: session info structure
 *
 */

static int v9fs_recvproc(void *data)
{
	struct v9fs_session_info *v9ses = (struct v9fs_session_info *)data;
	struct v9fs_fcall *rcall = NULL;
	struct v9fs_rpcreq *rptr;
	struct v9fs_rpcreq *req;
	struct v9fs_rpcreq *rreq;
	int err = 0;

	allow_signal(SIGKILL);
	set_current_state(TASK_INTERRUPTIBLE);
	complete(&v9ses->proccmpl);
	while (!kthread_should_stop() && err >= 0) {
		req = rptr = rreq = NULL;

		rcall = kmalloc(v9ses->maxdata + V9FS_IOHDRSZ, GFP_KERNEL);
		if (!rcall) {
			eprintk(KERN_ERR, "no memory for buffers\n");
			break;
		}

		err = read_message(v9ses, rcall, v9ses->maxdata + V9FS_IOHDRSZ);
		if (err < 0) {
			kfree(rcall);
			break;
		}
		spin_lock(&v9ses->muxlock);
		list_for_each_entry_safe(rreq, rptr, &v9ses->mux_fcalls, next) {
			if (rreq->tcall->tag == rcall->tag) {
				req = rreq;
				req->rcall = rcall;
				break;
			}
		}

		if (req && (req->tcall->id == TFLUSH)) {
			struct v9fs_rpcreq *treq = NULL;
			list_for_each_entry_safe(treq, rptr, &v9ses->mux_fcalls, next) {
				if (treq->tcall->tag ==
				    req->tcall->params.tflush.oldtag) {
					list_del(&rptr->next);
					kfree(treq->rcall);
					break;
				}
			}
		}

		spin_unlock(&v9ses->muxlock);

		if (!req) {
			dprintk(DEBUG_ERROR,
				"unexpected response: id %d tag %d\n",
				rcall->id, rcall->tag);

			kfree(rcall);
		}

		wake_up_all(&v9ses->read_wait);
		set_current_state(TASK_INTERRUPTIBLE);
	}

	/* Inform all pending processes about the failure */
	wake_up_all(&v9ses->read_wait);

	if (signal_pending(current))
		complete(&v9ses->proccmpl);

	dprintk(DEBUG_MUX, "recvproc: end\n");
	v9ses->recvproc = NULL;

	return err >= 0;
}

/**
 * v9fs_mux_init - initialize multiplexer (spawn kproc)
 * @v9ses: session info structure
 * @dev_name: mount device information (to create unique kproc)
 *
 */

int v9fs_mux_init(struct v9fs_session_info *v9ses, const char *dev_name)
{
	char procname[60];

	strncpy(procname, dev_name, sizeof(procname));
	procname[sizeof(procname) - 1] = 0;

	init_waitqueue_head(&v9ses->read_wait);
	init_completion(&v9ses->fcread);
	init_completion(&v9ses->proccmpl);
	spin_lock_init(&v9ses->muxlock);
	INIT_LIST_HEAD(&v9ses->mux_fcalls);
	v9ses->recvproc = NULL;
	v9ses->curfcall = NULL;

	v9ses->recvproc = kthread_create(v9fs_recvproc, v9ses,
					 "v9fs_recvproc %s", procname);

	if (IS_ERR(v9ses->recvproc)) {
		eprintk(KERN_ERR, "cannot create receiving thread\n");
		v9fs_session_close(v9ses);
		return -ECONNABORTED;
	}

	wake_up_process(v9ses->recvproc);
	wait_for_completion(&v9ses->proccmpl);

	return 0;
}