aboutsummaryrefslogtreecommitdiffstats
path: root/fs/9p
diff options
context:
space:
mode:
authorEric Van Hensbergen <ericvh@gmail.com>2005-09-09 16:04:22 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2005-09-09 16:57:57 -0400
commit426cc91aa651b50713d06d45e5c3c3e90cfd40d9 (patch)
tree56124d6564412d5f88cf12ad4b216ec5a0a6867e /fs/9p
parentb8cf945b3166c4394386f162a527c9950f396ce2 (diff)
[PATCH] v9fs: transport modules
This part of the patch contains transport routines. Signed-off-by: Eric Van Hensbergen <ericvh@gmail.com> Signed-off-by: Latchesar Ionkov <lucho@ionkov.net> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'fs/9p')
-rw-r--r--fs/9p/mux.c440
-rw-r--r--fs/9p/mux.h39
-rw-r--r--fs/9p/trans_fd.c172
-rw-r--r--fs/9p/trans_sock.c282
-rw-r--r--fs/9p/transport.h46
-rw-r--r--fs/9p/v9fs.c5
6 files changed, 979 insertions, 5 deletions
diff --git a/fs/9p/mux.c b/fs/9p/mux.c
new file mode 100644
index 000000000000..8ebc1af2c245
--- /dev/null
+++ b/fs/9p/mux.c
@@ -0,0 +1,440 @@
1/*
2 * linux/fs/9p/mux.c
3 *
4 * Protocol Multiplexer
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2004 by Latchesar Ionkov <lucho@ionkov.net>
8 *
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
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
24 *
25 */
26
27#include <linux/config.h>
28#include <linux/module.h>
29#include <linux/errno.h>
30#include <linux/fs.h>
31#include <linux/kthread.h>
32#include <linux/idr.h>
33
34#include "debug.h"
35#include "v9fs.h"
36#include "9p.h"
37#include "transport.h"
38#include "conv.h"
39#include "mux.h"
40
41/**
42 * dprintcond - print condition of session info
43 * @v9ses: session info structure
44 * @req: RPC request structure
45 *
46 */
47
48static inline int
49dprintcond(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
50{
51 dprintk(DEBUG_MUX, "condition: %d, %p\n", v9ses->transport->status,
52 req->rcall);
53 return 0;
54}
55
56/**
57 * xread - force read of a certain number of bytes
58 * @v9ses: session info structure
59 * @ptr: pointer to buffer
60 * @sz: number of bytes to read
61 *
62 * Chuck Cranor CS-533 project1
63 */
64
65static int xread(struct v9fs_session_info *v9ses, void *ptr, unsigned long sz)
66{
67 int rd = 0;
68 int ret = 0;
69 while (rd < sz) {
70 ret = v9ses->transport->read(v9ses->transport, ptr, sz - rd);
71 if (ret <= 0) {
72 dprintk(DEBUG_ERROR, "xread errno %d\n", ret);
73 return ret;
74 }
75 rd += ret;
76 ptr += ret;
77 }
78 return (rd);
79}
80
81/**
82 * read_message - read a full 9P2000 fcall packet
83 * @v9ses: session info structure
84 * @rcall: fcall structure to read into
85 * @rcalllen: size of fcall buffer
86 *
87 */
88
89static int
90read_message(struct v9fs_session_info *v9ses,
91 struct v9fs_fcall *rcall, int rcalllen)
92{
93 unsigned char buf[4];
94 void *data;
95 int size = 0;
96 int res = 0;
97
98 res = xread(v9ses, buf, sizeof(buf));
99 if (res < 0) {
100 dprintk(DEBUG_ERROR,
101 "Reading of count field failed returned: %d\n", res);
102 return res;
103 }
104
105 if (res < 4) {
106 dprintk(DEBUG_ERROR,
107 "Reading of count field failed returned: %d\n", res);
108 return -EIO;
109 }
110
111 size = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
112 dprintk(DEBUG_MUX, "got a packet count: %d\n", size);
113
114 /* adjust for the four bytes of size */
115 size -= 4;
116
117 if (size > v9ses->maxdata) {
118 dprintk(DEBUG_ERROR, "packet too big: %d\n", size);
119 return -E2BIG;
120 }
121
122 data = kmalloc(size, GFP_KERNEL);
123 if (!data) {
124 eprintk(KERN_WARNING, "out of memory\n");
125 return -ENOMEM;
126 }
127
128 res = xread(v9ses, data, size);
129 if (res < size) {
130 dprintk(DEBUG_ERROR, "Reading of fcall failed returned: %d\n",
131 res);
132 kfree(data);
133 return res;
134 }
135
136 /* we now have an in-memory string that is the reply.
137 * deserialize it. There is very little to go wrong at this point
138 * save for v9fs_alloc errors.
139 */
140 res = v9fs_deserialize_fcall(v9ses, size, data, v9ses->maxdata,
141 rcall, rcalllen);
142
143 kfree(data);
144
145 if (res < 0)
146 return res;
147
148 return 0;
149}
150
151/**
152 * v9fs_recv - receive an RPC response for a particular tag
153 * @v9ses: session info structure
154 * @req: RPC request structure
155 *
156 */
157
158static int v9fs_recv(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
159{
160 int ret = 0;
161
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) || dprintcond(v9ses, req)));
166
167 dprintk(DEBUG_MUX, "got it: rcall %p\n", req->rcall);
168 if (v9ses->transport->status == Disconnected)
169 return -ECONNRESET;
170
171 if (ret == 0) {
172 spin_lock(&v9ses->muxlock);
173 list_del(&req->next);
174 spin_unlock(&v9ses->muxlock);
175 }
176
177 return ret;
178}
179
180/**
181 * v9fs_send - send a 9P request
182 * @v9ses: session info structure
183 * @req: RPC request to send
184 *
185 */
186
187static int v9fs_send(struct v9fs_session_info *v9ses, struct v9fs_rpcreq *req)
188{
189 int ret = -1;
190 void *data = NULL;
191 struct v9fs_fcall *tcall = req->tcall;
192
193 data = kmalloc(v9ses->maxdata + V9FS_IOHDRSZ, GFP_KERNEL);
194 if (!data)
195 return -ENOMEM;
196
197 tcall->size = 0; /* enforce size recalculation */
198 ret =
199 v9fs_serialize_fcall(v9ses, tcall, data,
200 v9ses->maxdata + V9FS_IOHDRSZ);
201 if (ret < 0)
202 goto free_data;
203
204 spin_lock(&v9ses->muxlock);
205 list_add(&req->next, &v9ses->mux_fcalls);
206 spin_unlock(&v9ses->muxlock);
207
208 dprintk(DEBUG_MUX, "sending message: tag %d size %d\n", tcall->tag,
209 tcall->size);
210 ret = v9ses->transport->write(v9ses->transport, data, tcall->size);
211
212 if (ret != tcall->size) {
213 spin_lock(&v9ses->muxlock);
214 list_del(&req->next);
215 kfree(req->rcall);
216
217 spin_unlock(&v9ses->muxlock);
218 if (ret >= 0)
219 ret = -EREMOTEIO;
220 } else
221 ret = 0;
222
223 free_data:
224 kfree(data);
225 return ret;
226}
227
228/**
229 * v9fs_mux_rpc - send a request, receive a response
230 * @v9ses: session info structure
231 * @tcall: fcall to send
232 * @rcall: buffer to place response into
233 *
234 */
235
236long
237v9fs_mux_rpc(struct v9fs_session_info *v9ses, struct v9fs_fcall *tcall,
238 struct v9fs_fcall **rcall)
239{
240 int tid = -1;
241 struct v9fs_fcall *fcall = NULL;
242 struct v9fs_rpcreq req;
243 int ret = -1;
244
245 if (!v9ses)
246 return -EINVAL;
247
248 if (rcall)
249 *rcall = NULL;
250
251 if (tcall->id != TVERSION) {
252 tid = v9fs_get_idpool(&v9ses->tidpool);
253 if (tid < 0)
254 return -ENOMEM;
255 }
256
257 tcall->tag = tid;
258
259 req.tcall = tcall;
260 req.rcall = NULL;
261
262 ret = v9fs_send(v9ses, &req);
263
264 if (ret < 0) {
265 if (tcall->id != TVERSION)
266 v9fs_put_idpool(tid, &v9ses->tidpool);
267 dprintk(DEBUG_MUX, "error %d\n", ret);
268 return ret;
269 }
270
271 ret = v9fs_recv(v9ses, &req);
272
273 fcall = req.rcall;
274
275 dprintk(DEBUG_MUX, "received: tag=%x, ret=%d\n", tcall->tag, ret);
276 if (ret == -ERESTARTSYS) {
277 if (v9ses->transport->status != Disconnected
278 && tcall->id != TFLUSH) {
279 unsigned long flags;
280
281 dprintk(DEBUG_MUX, "flushing the tag: %d\n",
282 tcall->tag);
283 clear_thread_flag(TIF_SIGPENDING);
284 v9fs_t_flush(v9ses, tcall->tag);
285 spin_lock_irqsave(&current->sighand->siglock, flags);
286 recalc_sigpending();
287 spin_unlock_irqrestore(&current->sighand->siglock,
288 flags);
289 dprintk(DEBUG_MUX, "flushing done\n");
290 }
291
292 goto release_req;
293 } else if (ret < 0)
294 goto release_req;
295
296 if (!fcall)
297 ret = -EIO;
298 else {
299 if (fcall->id == RERROR) {
300 ret = v9fs_errstr2errno(fcall->params.rerror.error);
301 if (ret == 0) { /* string match failed */
302 if (fcall->params.rerror.errno)
303 ret = -(fcall->params.rerror.errno);
304 else
305 ret = -ESERVERFAULT;
306 }
307 } else if (fcall->id != tcall->id + 1) {
308 dprintk(DEBUG_ERROR,
309 "fcall mismatch: expected %d, got %d\n",
310 tcall->id + 1, fcall->id);
311 ret = -EIO;
312 }
313 }
314
315 release_req:
316 if (tcall->id != TVERSION)
317 v9fs_put_idpool(tid, &v9ses->tidpool);
318 if (rcall)
319 *rcall = fcall;
320 else
321 kfree(fcall);
322
323 return ret;
324}
325
326/**
327 * v9fs_recvproc - kproc to handle demultiplexing responses
328 * @data: session info structure
329 *
330 */
331
332static int v9fs_recvproc(void *data)
333{
334 struct v9fs_session_info *v9ses = (struct v9fs_session_info *)data;
335 struct v9fs_fcall *rcall = NULL;
336 struct v9fs_rpcreq *rptr;
337 struct v9fs_rpcreq *req;
338 struct v9fs_rpcreq *rreq;
339 int err = 0;
340
341 allow_signal(SIGKILL);
342 set_current_state(TASK_INTERRUPTIBLE);
343 complete(&v9ses->proccmpl);
344 while (!kthread_should_stop() && err >= 0) {
345 req = rptr = rreq = NULL;
346
347 rcall = kmalloc(v9ses->maxdata + V9FS_IOHDRSZ, GFP_KERNEL);
348 if (!rcall) {
349 eprintk(KERN_ERR, "no memory for buffers\n");
350 break;
351 }
352
353 err = read_message(v9ses, rcall, v9ses->maxdata + V9FS_IOHDRSZ);
354 if (err < 0) {
355 kfree(rcall);
356 break;
357 }
358 spin_lock(&v9ses->muxlock);
359 list_for_each_entry_safe(rreq, rptr, &v9ses->mux_fcalls, next) {
360 if (rreq->tcall->tag == rcall->tag) {
361 req = rreq;
362 req->rcall = rcall;
363 break;
364 }
365 }
366
367 if (req && (req->tcall->id == TFLUSH)) {
368 struct v9fs_rpcreq *treq = NULL;
369 list_for_each_entry_safe(treq, rptr, &v9ses->mux_fcalls, next) {
370 if (treq->tcall->tag ==
371 req->tcall->params.tflush.oldtag) {
372 list_del(&rptr->next);
373 kfree(treq->rcall);
374 break;
375 }
376 }
377 }
378
379 spin_unlock(&v9ses->muxlock);
380
381 if (!req) {
382 dprintk(DEBUG_ERROR,
383 "unexpected response: id %d tag %d\n",
384 rcall->id, rcall->tag);
385
386 kfree(rcall);
387 }
388
389 wake_up_all(&v9ses->read_wait);
390 set_current_state(TASK_INTERRUPTIBLE);
391 }
392
393 /* Inform all pending processes about the failure */
394 wake_up_all(&v9ses->read_wait);
395
396 if (signal_pending(current))
397 complete(&v9ses->proccmpl);
398
399 dprintk(DEBUG_MUX, "recvproc: end\n");
400 v9ses->recvproc = NULL;
401
402 return err >= 0;
403}
404
405/**
406 * v9fs_mux_init - initialize multiplexer (spawn kproc)
407 * @v9ses: session info structure
408 * @dev_name: mount device information (to create unique kproc)
409 *
410 */
411
412int v9fs_mux_init(struct v9fs_session_info *v9ses, const char *dev_name)
413{
414 char procname[60];
415
416 strncpy(procname, dev_name, sizeof(procname));
417 procname[sizeof(procname) - 1] = 0;
418
419 init_waitqueue_head(&v9ses->read_wait);
420 init_completion(&v9ses->fcread);
421 init_completion(&v9ses->proccmpl);
422 spin_lock_init(&v9ses->muxlock);
423 INIT_LIST_HEAD(&v9ses->mux_fcalls);
424 v9ses->recvproc = NULL;
425 v9ses->curfcall = NULL;
426
427 v9ses->recvproc = kthread_create(v9fs_recvproc, v9ses,
428 "v9fs_recvproc %s", procname);
429
430 if (IS_ERR(v9ses->recvproc)) {
431 eprintk(KERN_ERR, "cannot create receiving thread\n");
432 v9fs_session_close(v9ses);
433 return -ECONNABORTED;
434 }
435
436 wake_up_process(v9ses->recvproc);
437 wait_for_completion(&v9ses->proccmpl);
438
439 return 0;
440}
diff --git a/fs/9p/mux.h b/fs/9p/mux.h
new file mode 100644
index 000000000000..d7d8fa1c1529
--- /dev/null
+++ b/fs/9p/mux.h
@@ -0,0 +1,39 @@
1/*
2 * linux/fs/9p/mux.h
3 *
4 * Multiplexer Definitions
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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/* structure to manage each RPC transaction */
27
28struct v9fs_rpcreq {
29 struct v9fs_fcall *tcall;
30 struct v9fs_fcall *rcall;
31
32 /* XXX - could we put scatter/gather buffers here? */
33
34 struct list_head next;
35};
36
37int v9fs_mux_init(struct v9fs_session_info *v9ses, const char *dev_name);
38long v9fs_mux_rpc(struct v9fs_session_info *v9ses,
39 struct v9fs_fcall *tcall, struct v9fs_fcall **rcall);
diff --git a/fs/9p/trans_fd.c b/fs/9p/trans_fd.c
new file mode 100644
index 000000000000..63b58ce98ff4
--- /dev/null
+++ b/fs/9p/trans_fd.c
@@ -0,0 +1,172 @@
1/*
2 * linux/fs/9p/trans_fd.c
3 *
4 * File Descriptor Transport Layer
5 *
6 * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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/net.h>
29#include <linux/ipv6.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
32#include <linux/un.h>
33#include <asm/uaccess.h>
34#include <linux/inet.h>
35#include <linux/idr.h>
36#include <linux/file.h>
37
38#include "debug.h"
39#include "v9fs.h"
40#include "transport.h"
41
42struct v9fs_trans_fd {
43 struct file *in_file;
44 struct file *out_file;
45};
46
47/**
48 * v9fs_fd_recv - receive from a socket
49 * @v9ses: session information
50 * @v: buffer to receive data into
51 * @len: size of receive buffer
52 *
53 */
54
55static int v9fs_fd_recv(struct v9fs_transport *trans, void *v, int len)
56{
57 struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
58
59 if (!trans || trans->status != Connected || !ts)
60 return -EIO;
61
62 return kernel_read(ts->in_file, ts->in_file->f_pos, v, len);
63}
64
65/**
66 * v9fs_fd_send - send to a socket
67 * @v9ses: session information
68 * @v: buffer to send data from
69 * @len: size of send buffer
70 *
71 */
72
73static int v9fs_fd_send(struct v9fs_transport *trans, void *v, int len)
74{
75 struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
76 mm_segment_t oldfs = get_fs();
77 int ret = 0;
78
79 if (!trans || trans->status != Connected || !ts)
80 return -EIO;
81
82 set_fs(get_ds());
83 /* The cast to a user pointer is valid due to the set_fs() */
84 ret = vfs_write(ts->out_file, (void __user *)v, len, &ts->out_file->f_pos);
85 set_fs(oldfs);
86
87 return ret;
88}
89
90/**
91 * v9fs_fd_init - initialize file descriptor transport
92 * @v9ses: session information
93 * @addr: address of server to mount
94 * @data: mount options
95 *
96 */
97
98static int
99v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
100{
101 struct v9fs_trans_fd *ts = NULL;
102 struct v9fs_transport *trans = v9ses->transport;
103
104 if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) {
105 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
106 return -ENOPROTOOPT;
107 }
108
109 sema_init(&trans->writelock, 1);
110 sema_init(&trans->readlock, 1);
111
112 ts = kmalloc(sizeof(struct v9fs_trans_fd), GFP_KERNEL);
113
114 if (!ts)
115 return -ENOMEM;
116
117 ts->in_file = fget( v9ses->rfdno );
118 ts->out_file = fget( v9ses->wfdno );
119
120 if (!ts->in_file || !ts->out_file) {
121 if (ts->in_file)
122 fput(ts->in_file);
123
124 if (ts->out_file)
125 fput(ts->out_file);
126
127 kfree(ts);
128 return -EIO;
129 }
130
131 trans->priv = ts;
132 trans->status = Connected;
133
134 return 0;
135}
136
137
138/**
139 * v9fs_fd_close - shutdown file descriptor
140 * @trans: private socket structure
141 *
142 */
143
144static void v9fs_fd_close(struct v9fs_transport *trans)
145{
146 struct v9fs_trans_fd *ts;
147
148 if (!trans)
149 return;
150
151 trans->status = Disconnected;
152 ts = trans->priv;
153
154 if (!ts)
155 return;
156
157 if (ts->in_file)
158 fput(ts->in_file);
159
160 if (ts->out_file)
161 fput(ts->out_file);
162
163 kfree(ts);
164}
165
166struct v9fs_transport v9fs_trans_fd = {
167 .init = v9fs_fd_init,
168 .write = v9fs_fd_send,
169 .read = v9fs_fd_recv,
170 .close = v9fs_fd_close,
171};
172
diff --git a/fs/9p/trans_sock.c b/fs/9p/trans_sock.c
new file mode 100644
index 000000000000..081d1c847803
--- /dev/null
+++ b/fs/9p/trans_sock.c
@@ -0,0 +1,282 @@
1/*
2 * linux/fs/9p/trans_socket.c
3 *
4 * Socket Transport Layer
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
8 * Copyright (C) 1995, 1996 by Olaf Kirch <okir@monad.swb.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to:
22 * Free Software Foundation
23 * 51 Franklin Street, Fifth Floor
24 * Boston, MA 02111-1301 USA
25 *
26 */
27
28#include <linux/config.h>
29#include <linux/module.h>
30#include <linux/net.h>
31#include <linux/ipv6.h>
32#include <linux/errno.h>
33#include <linux/kernel.h>
34#include <linux/un.h>
35#include <asm/uaccess.h>
36#include <linux/inet.h>
37#include <linux/idr.h>
38
39#include "debug.h"
40#include "v9fs.h"
41#include "transport.h"
42
43#define V9FS_PORT 564
44
45struct v9fs_trans_sock {
46 struct socket *s;
47};
48
49/**
50 * v9fs_sock_recv - receive from a socket
51 * @v9ses: session information
52 * @v: buffer to receive data into
53 * @len: size of receive buffer
54 *
55 */
56
57static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len)
58{
59 struct msghdr msg;
60 struct kvec iov;
61 int result;
62 mm_segment_t oldfs;
63 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
64
65 if (trans->status == Disconnected)
66 return -EREMOTEIO;
67
68 result = -EINVAL;
69
70 oldfs = get_fs();
71 set_fs(get_ds());
72
73 iov.iov_base = v;
74 iov.iov_len = len;
75 msg.msg_name = NULL;
76 msg.msg_namelen = 0;
77 msg.msg_iovlen = 1;
78 msg.msg_control = NULL;
79 msg.msg_controllen = 0;
80 msg.msg_namelen = 0;
81 msg.msg_flags = MSG_NOSIGNAL;
82
83 result = kernel_recvmsg(ts->s, &msg, &iov, 1, len, 0);
84
85 dprintk(DEBUG_TRANS, "socket state %d\n", ts->s->state);
86 set_fs(oldfs);
87
88 if (result <= 0) {
89 if (result != -ERESTARTSYS)
90 trans->status = Disconnected;
91 }
92
93 return result;
94}
95
96/**
97 * v9fs_sock_send - send to a socket
98 * @v9ses: session information
99 * @v: buffer to send data from
100 * @len: size of send buffer
101 *
102 */
103
104static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len)
105{
106 struct kvec iov;
107 struct msghdr msg;
108 int result = -1;
109 mm_segment_t oldfs;
110 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
111
112 dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len);
113 dump_data(v, len);
114
115 down(&trans->writelock);
116
117 oldfs = get_fs();
118 set_fs(get_ds());
119 iov.iov_base = v;
120 iov.iov_len = len;
121 msg.msg_name = NULL;
122 msg.msg_namelen = 0;
123 msg.msg_iovlen = 1;
124 msg.msg_control = NULL;
125 msg.msg_controllen = 0;
126 msg.msg_namelen = 0;
127 msg.msg_flags = MSG_NOSIGNAL;
128 result = kernel_sendmsg(ts->s, &msg, &iov, 1, len);
129 set_fs(oldfs);
130
131 if (result < 0) {
132 if (result != -ERESTARTSYS)
133 trans->status = Disconnected;
134 }
135
136 up(&trans->writelock);
137 return result;
138}
139
140/**
141 * v9fs_tcp_init - initialize TCP socket
142 * @v9ses: session information
143 * @addr: address of server to mount
144 * @data: mount options
145 *
146 */
147
148static int
149v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
150{
151 struct socket *csocket = NULL;
152 struct sockaddr_in sin_server;
153 int rc = 0;
154 struct v9fs_trans_sock *ts = NULL;
155 struct v9fs_transport *trans = v9ses->transport;
156
157 sema_init(&trans->writelock, 1);
158 sema_init(&trans->readlock, 1);
159
160 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
161
162 if (!ts)
163 return -ENOMEM;
164
165 trans->priv = ts;
166 ts->s = NULL;
167
168 if (!addr)
169 return -EINVAL;
170
171 dprintk(DEBUG_TRANS, "Connecting to %s\n", addr);
172
173 sin_server.sin_family = AF_INET;
174 sin_server.sin_addr.s_addr = in_aton(addr);
175 sin_server.sin_port = htons(v9ses->port);
176 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
177 rc = csocket->ops->connect(csocket,
178 (struct sockaddr *)&sin_server,
179 sizeof(struct sockaddr_in), 0);
180 if (rc < 0) {
181 eprintk(KERN_ERR,
182 "v9fs_trans_tcp: problem connecting socket to %s\n",
183 addr);
184 return rc;
185 }
186 csocket->sk->sk_allocation = GFP_NOIO;
187 ts->s = csocket;
188 trans->status = Connected;
189
190 return 0;
191}
192
193/**
194 * v9fs_unix_init - initialize UNIX domain socket
195 * @v9ses: session information
196 * @dev_name: path to named pipe
197 * @data: mount options
198 *
199 */
200
201static int
202v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
203 char *data)
204{
205 int rc;
206 struct socket *csocket;
207 struct sockaddr_un sun_server;
208 struct v9fs_transport *trans;
209 struct v9fs_trans_sock *ts;
210
211 rc = 0;
212 csocket = NULL;
213 trans = v9ses->transport;
214
215 if (strlen(dev_name) > UNIX_PATH_MAX) {
216 eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n",
217 dev_name);
218 return -ENOMEM;
219 }
220
221 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
222 if (!ts)
223 return -ENOMEM;
224
225 trans->priv = ts;
226 ts->s = NULL;
227
228 sema_init(&trans->writelock, 1);
229 sema_init(&trans->readlock, 1);
230
231 sun_server.sun_family = PF_UNIX;
232 strcpy(sun_server.sun_path, dev_name);
233 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
234 rc = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
235 sizeof(struct sockaddr_un) - 1, 0); /* -1 *is* important */
236 if (rc < 0) {
237 eprintk(KERN_ERR,
238 "v9fs_trans_unix: problem connecting socket: %s: %d\n",
239 dev_name, rc);
240 return rc;
241 }
242 csocket->sk->sk_allocation = GFP_NOIO;
243 ts->s = csocket;
244 trans->status = Connected;
245
246 return 0;
247}
248
249/**
250 * v9fs_sock_close - shutdown socket
251 * @trans: private socket structure
252 *
253 */
254
255static void v9fs_sock_close(struct v9fs_transport *trans)
256{
257 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
258
259 if ((ts) && (ts->s)) {
260 dprintk(DEBUG_TRANS, "closing the socket %p\n", ts->s);
261 sock_release(ts->s);
262 ts->s = NULL;
263 trans->status = Disconnected;
264 dprintk(DEBUG_TRANS, "socket closed\n");
265 }
266
267 kfree(ts);
268}
269
270struct v9fs_transport v9fs_trans_tcp = {
271 .init = v9fs_tcp_init,
272 .write = v9fs_sock_send,
273 .read = v9fs_sock_recv,
274 .close = v9fs_sock_close,
275};
276
277struct v9fs_transport v9fs_trans_unix = {
278 .init = v9fs_unix_init,
279 .write = v9fs_sock_send,
280 .read = v9fs_sock_recv,
281 .close = v9fs_sock_close,
282};
diff --git a/fs/9p/transport.h b/fs/9p/transport.h
new file mode 100644
index 000000000000..9e9cd418efd5
--- /dev/null
+++ b/fs/9p/transport.h
@@ -0,0 +1,46 @@
1/*
2 * linux/fs/9p/transport.h
3 *
4 * Transport Definition
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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
26enum v9fs_transport_status {
27 Connected,
28 Disconnected,
29 Hung,
30};
31
32struct v9fs_transport {
33 enum v9fs_transport_status status;
34 struct semaphore writelock;
35 struct semaphore readlock;
36 void *priv;
37
38 int (*init) (struct v9fs_session_info *, const char *, char *);
39 int (*write) (struct v9fs_transport *, void *, int);
40 int (*read) (struct v9fs_transport *, void *, int);
41 void (*close) (struct v9fs_transport *);
42};
43
44extern struct v9fs_transport v9fs_trans_tcp;
45extern struct v9fs_transport v9fs_trans_unix;
46extern struct v9fs_transport v9fs_trans_fd;
diff --git a/fs/9p/v9fs.c b/fs/9p/v9fs.c
index 14d663ebfcbc..a573b751dd9a 100644
--- a/fs/9p/v9fs.c
+++ b/fs/9p/v9fs.c
@@ -296,11 +296,6 @@ v9fs_session_init(struct v9fs_session_info *v9ses,
296 case PROTO_FD: 296 case PROTO_FD:
297 trans_proto = &v9fs_trans_fd; 297 trans_proto = &v9fs_trans_fd;
298 *v9ses->remotename = 0; 298 *v9ses->remotename = 0;
299 if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) {
300 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
301 retval = -ENOPROTOOPT;
302 goto SessCleanUp;
303 }
304 break; 299 break;
305 default: 300 default:
306 printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto); 301 printk(KERN_ERR "v9fs: Bad mount protocol %d\n", v9ses->proto);