[PATCH] uml: SIGWINCH handling cleanup
[linux-2.6/x86.git] / fs / 9p / trans_sock.c
blob6a9a75d40f735c209c4e95aefab71178f07990fd
1 /*
2 * linux/fs/9p/trans_socket.c
4 * Socket Transport Layer
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>
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.
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.
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
28 #include <linux/config.h>
29 #include <linux/in.h>
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>
40 #include "debug.h"
41 #include "v9fs.h"
42 #include "transport.h"
44 #define V9FS_PORT 564
46 struct v9fs_trans_sock {
47 struct socket *s;
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
58 static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len)
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;
66 if (trans->status == Disconnected)
67 return -EREMOTEIO;
69 result = -EINVAL;
71 oldfs = get_fs();
72 set_fs(get_ds());
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;
84 result = kernel_recvmsg(ts->s, &msg, &iov, 1, len, 0);
86 dprintk(DEBUG_TRANS, "socket state %d\n", ts->s->state);
87 set_fs(oldfs);
89 if (result <= 0) {
90 if (result != -ERESTARTSYS)
91 trans->status = Disconnected;
94 return result;
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
105 static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len)
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;
113 dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len);
114 dump_data(v, len);
116 down(&trans->writelock);
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);
132 if (result < 0) {
133 if (result != -ERESTARTSYS)
134 trans->status = Disconnected;
137 up(&trans->writelock);
138 return result;
142 * v9fs_tcp_init - initialize TCP socket
143 * @v9ses: session information
144 * @addr: address of server to mount
145 * @data: mount options
149 static int
150 v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
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;
158 sema_init(&trans->writelock, 1);
159 sema_init(&trans->readlock, 1);
161 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
163 if (!ts)
164 return -ENOMEM;
166 trans->priv = ts;
167 ts->s = NULL;
169 if (!addr)
170 return -EINVAL;
172 dprintk(DEBUG_TRANS, "Connecting to %s\n", addr);
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;
187 csocket->sk->sk_allocation = GFP_NOIO;
188 ts->s = csocket;
189 trans->status = Connected;
191 return 0;
195 * v9fs_unix_init - initialize UNIX domain socket
196 * @v9ses: session information
197 * @dev_name: path to named pipe
198 * @data: mount options
202 static int
203 v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
204 char *data)
206 int rc;
207 struct socket *csocket;
208 struct sockaddr_un sun_server;
209 struct v9fs_transport *trans;
210 struct v9fs_trans_sock *ts;
212 rc = 0;
213 csocket = NULL;
214 trans = v9ses->transport;
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;
222 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
223 if (!ts)
224 return -ENOMEM;
226 trans->priv = ts;
227 ts->s = NULL;
229 sema_init(&trans->writelock, 1);
230 sema_init(&trans->readlock, 1);
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;
243 csocket->sk->sk_allocation = GFP_NOIO;
244 ts->s = csocket;
245 trans->status = Connected;
247 return 0;
251 * v9fs_sock_close - shutdown socket
252 * @trans: private socket structure
256 static void v9fs_sock_close(struct v9fs_transport *trans)
258 struct v9fs_trans_sock *ts;
260 if (!trans)
261 return;
263 ts = trans->priv;
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");
273 kfree(ts);
275 trans->priv = NULL;
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,
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,