diff options
Diffstat (limited to 'fs/9p/trans_sock.c')
-rw-r--r-- | fs/9p/trans_sock.c | 282 |
1 files changed, 282 insertions, 0 deletions
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 | |||
45 | struct 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 | |||
57 | static 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 | |||
104 | static 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 | |||
148 | static int | ||
149 | v9fs_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 | |||
201 | static int | ||
202 | v9fs_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 | |||
255 | static 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 | |||
270 | struct 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 | |||
277 | struct 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 | }; | ||