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>
35 #include <asm/uaccess.h>
36 #include <linux/inet.h>
37 #include <linux/idr.h>
41 #include "transport.h"
45 struct v9fs_trans_sock {
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)
63 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
65 if (trans->status == Disconnected)
78 msg.msg_control = NULL;
79 msg.msg_controllen = 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);
89 if (result != -ERESTARTSYS)
90 trans->status = Disconnected;
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)
110 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
112 dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len);
115 down(&trans->writelock);
124 msg.msg_control = NULL;
125 msg.msg_controllen = 0;
127 msg.msg_flags = MSG_NOSIGNAL;
128 result = kernel_sendmsg(ts->s, &msg, &iov, 1, len);
132 if (result != -ERESTARTSYS)
133 trans->status = Disconnected;
136 up(&trans->writelock);
141 * v9fs_tcp_init - initialize TCP socket
142 * @v9ses: session information
143 * @addr: address of server to mount
144 * @data: mount options
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;
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);
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);
182 "v9fs_trans_tcp: problem connecting socket to %s\n",
186 csocket->sk->sk_allocation = GFP_NOIO;
188 trans->status = Connected;
194 * v9fs_unix_init - initialize UNIX domain socket
195 * @v9ses: session information
196 * @dev_name: path to named pipe
197 * @data: mount options
202 v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
206 struct socket *csocket;
207 struct sockaddr_un sun_server;
208 struct v9fs_transport *trans;
209 struct v9fs_trans_sock *ts;
213 trans = v9ses->transport;
215 if (strlen(dev_name) > UNIX_PATH_MAX) {
216 eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n",
221 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
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 */
238 "v9fs_trans_unix: problem connecting socket: %s: %d\n",
242 csocket->sk->sk_allocation = GFP_NOIO;
244 trans->status = Connected;
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;
264 if ((ts) && (ts->s)) {
265 dprintk(DEBUG_TRANS, "closing the socket %p\n", ts->s);
268 trans->status = Disconnected;
269 dprintk(DEBUG_TRANS, "socket closed\n");
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,