[PATCH] aec62xxx: remove all dead (#if0'd) code
[linux-2.6/openmoko-kernel/knife-kernel.git] / fs / 9p / trans_sock.c
bloba93c2bf94c331a7b20326caa5a36c217fab3f023
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/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>
39 #include "debug.h"
40 #include "v9fs.h"
41 #include "transport.h"
43 #define V9FS_PORT 564
45 struct v9fs_trans_sock {
46 struct socket *s;
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
57 static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len)
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;
65 if (trans->status == Disconnected)
66 return -EREMOTEIO;
68 result = -EINVAL;
70 oldfs = get_fs();
71 set_fs(get_ds());
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;
83 result = kernel_recvmsg(ts->s, &msg, &iov, 1, len, 0);
85 dprintk(DEBUG_TRANS, "socket state %d\n", ts->s->state);
86 set_fs(oldfs);
88 if (result <= 0) {
89 if (result != -ERESTARTSYS)
90 trans->status = Disconnected;
93 return result;
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
104 static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len)
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;
112 dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len);
113 dump_data(v, len);
115 down(&trans->writelock);
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);
131 if (result < 0) {
132 if (result != -ERESTARTSYS)
133 trans->status = Disconnected;
136 up(&trans->writelock);
137 return result;
141 * v9fs_tcp_init - initialize TCP socket
142 * @v9ses: session information
143 * @addr: address of server to mount
144 * @data: mount options
148 static int
149 v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
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;
157 sema_init(&trans->writelock, 1);
158 sema_init(&trans->readlock, 1);
160 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
162 if (!ts)
163 return -ENOMEM;
165 trans->priv = ts;
166 ts->s = NULL;
168 if (!addr)
169 return -EINVAL;
171 dprintk(DEBUG_TRANS, "Connecting to %s\n", addr);
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;
186 csocket->sk->sk_allocation = GFP_NOIO;
187 ts->s = csocket;
188 trans->status = Connected;
190 return 0;
194 * v9fs_unix_init - initialize UNIX domain socket
195 * @v9ses: session information
196 * @dev_name: path to named pipe
197 * @data: mount options
201 static int
202 v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
203 char *data)
205 int rc;
206 struct socket *csocket;
207 struct sockaddr_un sun_server;
208 struct v9fs_transport *trans;
209 struct v9fs_trans_sock *ts;
211 rc = 0;
212 csocket = NULL;
213 trans = v9ses->transport;
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;
221 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
222 if (!ts)
223 return -ENOMEM;
225 trans->priv = ts;
226 ts->s = NULL;
228 sema_init(&trans->writelock, 1);
229 sema_init(&trans->readlock, 1);
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;
242 csocket->sk->sk_allocation = GFP_NOIO;
243 ts->s = csocket;
244 trans->status = Connected;
246 return 0;
250 * v9fs_sock_close - shutdown socket
251 * @trans: private socket structure
255 static void v9fs_sock_close(struct v9fs_transport *trans)
257 struct v9fs_trans_sock *ts;
259 if (!trans)
260 return;
262 ts = trans->priv;
264 if ((ts) && (ts->s)) {
265 dprintk(DEBUG_TRANS, "closing the socket %p\n", ts->s);
266 sock_release(ts->s);
267 ts->s = NULL;
268 trans->status = Disconnected;
269 dprintk(DEBUG_TRANS, "socket closed\n");
272 kfree(ts);
274 trans->priv = NULL;
277 struct v9fs_transport v9fs_trans_tcp = {
278 .init = v9fs_tcp_init,
279 .write = v9fs_sock_send,
280 .read = v9fs_sock_recv,
281 .close = v9fs_sock_close,
284 struct v9fs_transport v9fs_trans_unix = {
285 .init = v9fs_unix_init,
286 .write = v9fs_sock_send,
287 .read = v9fs_sock_recv,
288 .close = v9fs_sock_close,