2 * linux/fs/9p/trans_socket.c
4 * Socket Transport Layer
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>
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>
36 #include <asm/uaccess.h>
37 #include <linux/inet.h>
38 #include <linux/idr.h>
42 #include "transport.h"
46 struct v9fs_trans_sock {
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)
64 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
66 if (trans->status == Disconnected)
79 msg.msg_control = NULL;
80 msg.msg_controllen = 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);
90 if (result != -ERESTARTSYS)
91 trans->status = Disconnected;
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)
111 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
113 dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len);
116 down(&trans->writelock);
125 msg.msg_control = NULL;
126 msg.msg_controllen = 0;
128 msg.msg_flags = MSG_NOSIGNAL;
129 result = kernel_sendmsg(ts->s, &msg, &iov, 1, len);
133 if (result != -ERESTARTSYS)
134 trans->status = Disconnected;
137 up(&trans->writelock);
142 * v9fs_tcp_init - initialize TCP socket
143 * @v9ses: session information
144 * @addr: address of server to mount
145 * @data: mount options
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;
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);
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);
183 "v9fs_trans_tcp: problem connecting socket to %s\n",
187 csocket->sk->sk_allocation = GFP_NOIO;
189 trans->status = Connected;
195 * v9fs_unix_init - initialize UNIX domain socket
196 * @v9ses: session information
197 * @dev_name: path to named pipe
198 * @data: mount options
203 v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
207 struct socket *csocket;
208 struct sockaddr_un sun_server;
209 struct v9fs_transport *trans;
210 struct v9fs_trans_sock *ts;
214 trans = v9ses->transport;
216 if (strlen(dev_name) > UNIX_PATH_MAX) {
217 eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n",
222 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
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 */
239 "v9fs_trans_unix: problem connecting socket: %s: %d\n",
243 csocket->sk->sk_allocation = GFP_NOIO;
245 trans->status = Connected;
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;
265 if ((ts) && (ts->s)) {
266 dprintk(DEBUG_TRANS, "closing the socket %p\n", ts->s);
269 trans->status = Disconnected;
270 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,