]>
Commit | Line | Data |
---|---|---|
426cc91a EVH |
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> | |
14c85021 | 29 | #include <linux/in.h> |
426cc91a EVH |
30 | #include <linux/module.h> |
31 | #include <linux/net.h> | |
32 | #include <linux/ipv6.h> | |
33 | #include <linux/errno.h> | |
34 | #include <linux/kernel.h> | |
35 | #include <linux/un.h> | |
36 | #include <asm/uaccess.h> | |
37 | #include <linux/inet.h> | |
38 | #include <linux/idr.h> | |
39 | ||
40 | #include "debug.h" | |
41 | #include "v9fs.h" | |
42 | #include "transport.h" | |
43 | ||
44 | #define V9FS_PORT 564 | |
45 | ||
46 | struct v9fs_trans_sock { | |
47 | struct socket *s; | |
48 | }; | |
49 | ||
50 | /** | |
51 | * v9fs_sock_recv - receive from a socket | |
52 | * @v9ses: session information | |
53 | * @v: buffer to receive data into | |
54 | * @len: size of receive buffer | |
55 | * | |
56 | */ | |
57 | ||
58 | static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len) | |
59 | { | |
60 | struct msghdr msg; | |
61 | struct kvec iov; | |
62 | int result; | |
63 | mm_segment_t oldfs; | |
64 | struct v9fs_trans_sock *ts = trans ? trans->priv : NULL; | |
65 | ||
66 | if (trans->status == Disconnected) | |
67 | return -EREMOTEIO; | |
68 | ||
69 | result = -EINVAL; | |
70 | ||
71 | oldfs = get_fs(); | |
72 | set_fs(get_ds()); | |
73 | ||
74 | iov.iov_base = v; | |
75 | iov.iov_len = len; | |
76 | msg.msg_name = NULL; | |
77 | msg.msg_namelen = 0; | |
78 | msg.msg_iovlen = 1; | |
79 | msg.msg_control = NULL; | |
80 | msg.msg_controllen = 0; | |
81 | msg.msg_namelen = 0; | |
82 | msg.msg_flags = MSG_NOSIGNAL; | |
83 | ||
84 | result = kernel_recvmsg(ts->s, &msg, &iov, 1, len, 0); | |
85 | ||
86 | dprintk(DEBUG_TRANS, "socket state %d\n", ts->s->state); | |
87 | set_fs(oldfs); | |
88 | ||
89 | if (result <= 0) { | |
90 | if (result != -ERESTARTSYS) | |
91 | trans->status = Disconnected; | |
92 | } | |
93 | ||
94 | return result; | |
95 | } | |
96 | ||
97 | /** | |
98 | * v9fs_sock_send - send to a socket | |
99 | * @v9ses: session information | |
100 | * @v: buffer to send data from | |
101 | * @len: size of send buffer | |
102 | * | |
103 | */ | |
104 | ||
105 | static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len) | |
106 | { | |
107 | struct kvec iov; | |
108 | struct msghdr msg; | |
109 | int result = -1; | |
110 | mm_segment_t oldfs; | |
111 | struct v9fs_trans_sock *ts = trans ? trans->priv : NULL; | |
112 | ||
113 | dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len); | |
114 | dump_data(v, len); | |
115 | ||
116 | down(&trans->writelock); | |
117 | ||
118 | oldfs = get_fs(); | |
119 | set_fs(get_ds()); | |
120 | iov.iov_base = v; | |
121 | iov.iov_len = len; | |
122 | msg.msg_name = NULL; | |
123 | msg.msg_namelen = 0; | |
124 | msg.msg_iovlen = 1; | |
125 | msg.msg_control = NULL; | |
126 | msg.msg_controllen = 0; | |
127 | msg.msg_namelen = 0; | |
128 | msg.msg_flags = MSG_NOSIGNAL; | |
129 | result = kernel_sendmsg(ts->s, &msg, &iov, 1, len); | |
130 | set_fs(oldfs); | |
131 | ||
132 | if (result < 0) { | |
133 | if (result != -ERESTARTSYS) | |
134 | trans->status = Disconnected; | |
135 | } | |
136 | ||
137 | up(&trans->writelock); | |
138 | return result; | |
139 | } | |
140 | ||
141 | /** | |
142 | * v9fs_tcp_init - initialize TCP socket | |
143 | * @v9ses: session information | |
144 | * @addr: address of server to mount | |
145 | * @data: mount options | |
146 | * | |
147 | */ | |
148 | ||
149 | static int | |
150 | v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data) | |
151 | { | |
152 | struct socket *csocket = NULL; | |
153 | struct sockaddr_in sin_server; | |
154 | int rc = 0; | |
155 | struct v9fs_trans_sock *ts = NULL; | |
156 | struct v9fs_transport *trans = v9ses->transport; | |
157 | ||
158 | sema_init(&trans->writelock, 1); | |
159 | sema_init(&trans->readlock, 1); | |
160 | ||
161 | ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL); | |
162 | ||
163 | if (!ts) | |
164 | return -ENOMEM; | |
165 | ||
166 | trans->priv = ts; | |
167 | ts->s = NULL; | |
168 | ||
169 | if (!addr) | |
170 | return -EINVAL; | |
171 | ||
172 | dprintk(DEBUG_TRANS, "Connecting to %s\n", addr); | |
173 | ||
174 | sin_server.sin_family = AF_INET; | |
175 | sin_server.sin_addr.s_addr = in_aton(addr); | |
176 | sin_server.sin_port = htons(v9ses->port); | |
177 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); | |
178 | rc = csocket->ops->connect(csocket, | |
179 | (struct sockaddr *)&sin_server, | |
180 | sizeof(struct sockaddr_in), 0); | |
181 | if (rc < 0) { | |
182 | eprintk(KERN_ERR, | |
183 | "v9fs_trans_tcp: problem connecting socket to %s\n", | |
184 | addr); | |
185 | return rc; | |
186 | } | |
187 | csocket->sk->sk_allocation = GFP_NOIO; | |
188 | ts->s = csocket; | |
189 | trans->status = Connected; | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | /** | |
195 | * v9fs_unix_init - initialize UNIX domain socket | |
196 | * @v9ses: session information | |
197 | * @dev_name: path to named pipe | |
198 | * @data: mount options | |
199 | * | |
200 | */ | |
201 | ||
202 | static int | |
203 | v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name, | |
204 | char *data) | |
205 | { | |
206 | int rc; | |
207 | struct socket *csocket; | |
208 | struct sockaddr_un sun_server; | |
209 | struct v9fs_transport *trans; | |
210 | struct v9fs_trans_sock *ts; | |
211 | ||
212 | rc = 0; | |
213 | csocket = NULL; | |
214 | trans = v9ses->transport; | |
215 | ||
216 | if (strlen(dev_name) > UNIX_PATH_MAX) { | |
217 | eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n", | |
218 | dev_name); | |
219 | return -ENOMEM; | |
220 | } | |
221 | ||
222 | ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL); | |
223 | if (!ts) | |
224 | return -ENOMEM; | |
225 | ||
226 | trans->priv = ts; | |
227 | ts->s = NULL; | |
228 | ||
229 | sema_init(&trans->writelock, 1); | |
230 | sema_init(&trans->readlock, 1); | |
231 | ||
232 | sun_server.sun_family = PF_UNIX; | |
233 | strcpy(sun_server.sun_path, dev_name); | |
234 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); | |
235 | rc = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, | |
236 | sizeof(struct sockaddr_un) - 1, 0); /* -1 *is* important */ | |
237 | if (rc < 0) { | |
238 | eprintk(KERN_ERR, | |
239 | "v9fs_trans_unix: problem connecting socket: %s: %d\n", | |
240 | dev_name, rc); | |
241 | return rc; | |
242 | } | |
243 | csocket->sk->sk_allocation = GFP_NOIO; | |
244 | ts->s = csocket; | |
245 | trans->status = Connected; | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | /** | |
251 | * v9fs_sock_close - shutdown socket | |
252 | * @trans: private socket structure | |
253 | * | |
254 | */ | |
255 | ||
256 | static void v9fs_sock_close(struct v9fs_transport *trans) | |
257 | { | |
cb2e87a6 EVH |
258 | struct v9fs_trans_sock *ts; |
259 | ||
260 | if (!trans) | |
261 | return; | |
262 | ||
263 | ts = trans->priv; | |
426cc91a EVH |
264 | |
265 | if ((ts) && (ts->s)) { | |
266 | dprintk(DEBUG_TRANS, "closing the socket %p\n", ts->s); | |
267 | sock_release(ts->s); | |
268 | ts->s = NULL; | |
269 | trans->status = Disconnected; | |
270 | dprintk(DEBUG_TRANS, "socket closed\n"); | |
271 | } | |
272 | ||
f99d49ad | 273 | kfree(ts); |
cb2e87a6 EVH |
274 | |
275 | trans->priv = NULL; | |
426cc91a EVH |
276 | } |
277 | ||
278 | struct v9fs_transport v9fs_trans_tcp = { | |
279 | .init = v9fs_tcp_init, | |
280 | .write = v9fs_sock_send, | |
281 | .read = v9fs_sock_recv, | |
282 | .close = v9fs_sock_close, | |
283 | }; | |
284 | ||
285 | struct v9fs_transport v9fs_trans_unix = { | |
286 | .init = v9fs_unix_init, | |
287 | .write = v9fs_sock_send, | |
288 | .read = v9fs_sock_recv, | |
289 | .close = v9fs_sock_close, | |
290 | }; |