aboutsummaryrefslogtreecommitdiffstats
path: root/fs/9p/trans_sock.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/9p/trans_sock.c')
-rw-r--r--fs/9p/trans_sock.c161
1 files changed, 103 insertions, 58 deletions
diff --git a/fs/9p/trans_sock.c b/fs/9p/trans_sock.c
index a93c2bf94c33..44e830697acb 100644
--- a/fs/9p/trans_sock.c
+++ b/fs/9p/trans_sock.c
@@ -3,6 +3,7 @@
3 * 3 *
4 * Socket Transport Layer 4 * Socket Transport Layer
5 * 5 *
6 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> 7 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com> 8 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
8 * Copyright (C) 1995, 1996 by Olaf Kirch <okir@monad.swb.de> 9 * Copyright (C) 1995, 1996 by Olaf Kirch <okir@monad.swb.de>
@@ -26,6 +27,7 @@
26 */ 27 */
27 28
28#include <linux/config.h> 29#include <linux/config.h>
30#include <linux/in.h>
29#include <linux/module.h> 31#include <linux/module.h>
30#include <linux/net.h> 32#include <linux/net.h>
31#include <linux/ipv6.h> 33#include <linux/ipv6.h>
@@ -35,6 +37,7 @@
35#include <asm/uaccess.h> 37#include <asm/uaccess.h>
36#include <linux/inet.h> 38#include <linux/inet.h>
37#include <linux/idr.h> 39#include <linux/idr.h>
40#include <linux/file.h>
38 41
39#include "debug.h" 42#include "debug.h"
40#include "v9fs.h" 43#include "v9fs.h"
@@ -44,6 +47,7 @@
44 47
45struct v9fs_trans_sock { 48struct v9fs_trans_sock {
46 struct socket *s; 49 struct socket *s;
50 struct file *filp;
47}; 51};
48 52
49/** 53/**
@@ -56,41 +60,26 @@ struct v9fs_trans_sock {
56 60
57static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len) 61static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len)
58{ 62{
59 struct msghdr msg; 63 int ret;
60 struct kvec iov; 64 struct v9fs_trans_sock *ts;
61 int result;
62 mm_segment_t oldfs;
63 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL;
64 65
65 if (trans->status == Disconnected) 66 if (!trans || trans->status == Disconnected) {
67 dprintk(DEBUG_ERROR, "disconnected ...\n");
66 return -EREMOTEIO; 68 return -EREMOTEIO;
69 }
67 70
68 result = -EINVAL; 71 ts = trans->priv;
69
70 oldfs = get_fs();
71 set_fs(get_ds());
72
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;
82 72
83 result = kernel_recvmsg(ts->s, &msg, &iov, 1, len, 0); 73 if (!(ts->filp->f_flags & O_NONBLOCK))
74 dprintk(DEBUG_ERROR, "blocking read ...\n");
84 75
85 dprintk(DEBUG_TRANS, "socket state %d\n", ts->s->state); 76 ret = kernel_read(ts->filp, ts->filp->f_pos, v, len);
86 set_fs(oldfs); 77 if (ret <= 0) {
87 78 if (ret != -ERESTARTSYS && ret != -EAGAIN)
88 if (result <= 0) {
89 if (result != -ERESTARTSYS)
90 trans->status = Disconnected; 79 trans->status = Disconnected;
91 } 80 }
92 81
93 return result; 82 return ret;
94} 83}
95 84
96/** 85/**
@@ -103,40 +92,72 @@ static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len)
103 92
104static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len) 93static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len)
105{ 94{
106 struct kvec iov; 95 int ret;
107 struct msghdr msg;
108 int result = -1;
109 mm_segment_t oldfs; 96 mm_segment_t oldfs;
110 struct v9fs_trans_sock *ts = trans ? trans->priv : NULL; 97 struct v9fs_trans_sock *ts;
111 98
112 dprintk(DEBUG_TRANS, "Sending packet size %d (%x)\n", len, len); 99 if (!trans || trans->status == Disconnected) {
113 dump_data(v, len); 100 dprintk(DEBUG_ERROR, "disconnected ...\n");
101 return -EREMOTEIO;
102 }
103
104 ts = trans->priv;
105 if (!ts) {
106 dprintk(DEBUG_ERROR, "no transport ...\n");
107 return -EREMOTEIO;
108 }
114 109
115 down(&trans->writelock); 110 if (!(ts->filp->f_flags & O_NONBLOCK))
111 dprintk(DEBUG_ERROR, "blocking write ...\n");
116 112
117 oldfs = get_fs(); 113 oldfs = get_fs();
118 set_fs(get_ds()); 114 set_fs(get_ds());
119 iov.iov_base = v; 115 ret = vfs_write(ts->filp, (void __user *)v, len, &ts->filp->f_pos);
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); 116 set_fs(oldfs);
130 117
131 if (result < 0) { 118 if (ret < 0) {
132 if (result != -ERESTARTSYS) 119 if (ret != -ERESTARTSYS)
133 trans->status = Disconnected; 120 trans->status = Disconnected;
134 } 121 }
135 122
136 up(&trans->writelock); 123 return ret;
137 return result; 124}
125
126static unsigned int v9fs_sock_poll(struct v9fs_transport *trans,
127 struct poll_table_struct *pt) {
128
129 int ret;
130 struct v9fs_trans_sock *ts;
131 mm_segment_t oldfs;
132
133 if (!trans) {
134 dprintk(DEBUG_ERROR, "no transport\n");
135 return -EIO;
136 }
137
138 ts = trans->priv;
139 if (trans->status != Connected || !ts) {
140 dprintk(DEBUG_ERROR, "transport disconnected: %d\n", trans->status);
141 return -EIO;
142 }
143
144 oldfs = get_fs();
145 set_fs(get_ds());
146
147 if (!ts->filp->f_op || !ts->filp->f_op->poll) {
148 dprintk(DEBUG_ERROR, "no poll operation\n");
149 ret = -EIO;
150 goto end;
151 }
152
153 ret = ts->filp->f_op->poll(ts->filp, pt);
154
155end:
156 set_fs(oldfs);
157 return ret;
138} 158}
139 159
160
140/** 161/**
141 * v9fs_tcp_init - initialize TCP socket 162 * v9fs_tcp_init - initialize TCP socket
142 * @v9ses: session information 163 * @v9ses: session information
@@ -153,9 +174,9 @@ v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
153 int rc = 0; 174 int rc = 0;
154 struct v9fs_trans_sock *ts = NULL; 175 struct v9fs_trans_sock *ts = NULL;
155 struct v9fs_transport *trans = v9ses->transport; 176 struct v9fs_transport *trans = v9ses->transport;
177 int fd;
156 178
157 sema_init(&trans->writelock, 1); 179 trans->status = Disconnected;
158 sema_init(&trans->readlock, 1);
159 180
160 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL); 181 ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL);
161 182
@@ -164,6 +185,7 @@ v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
164 185
165 trans->priv = ts; 186 trans->priv = ts;
166 ts->s = NULL; 187 ts->s = NULL;
188 ts->filp = NULL;
167 189
168 if (!addr) 190 if (!addr)
169 return -EINVAL; 191 return -EINVAL;
@@ -184,7 +206,18 @@ v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
184 return rc; 206 return rc;
185 } 207 }
186 csocket->sk->sk_allocation = GFP_NOIO; 208 csocket->sk->sk_allocation = GFP_NOIO;
209
210 fd = sock_map_fd(csocket);
211 if (fd < 0) {
212 sock_release(csocket);
213 kfree(ts);
214 trans->priv = NULL;
215 return fd;
216 }
217
187 ts->s = csocket; 218 ts->s = csocket;
219 ts->filp = fget(fd);
220 ts->filp->f_flags |= O_NONBLOCK;
188 trans->status = Connected; 221 trans->status = Connected;
189 222
190 return 0; 223 return 0;
@@ -202,7 +235,7 @@ static int
202v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name, 235v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
203 char *data) 236 char *data)
204{ 237{
205 int rc; 238 int rc, fd;
206 struct socket *csocket; 239 struct socket *csocket;
207 struct sockaddr_un sun_server; 240 struct sockaddr_un sun_server;
208 struct v9fs_transport *trans; 241 struct v9fs_transport *trans;
@@ -212,6 +245,8 @@ v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
212 csocket = NULL; 245 csocket = NULL;
213 trans = v9ses->transport; 246 trans = v9ses->transport;
214 247
248 trans->status = Disconnected;
249
215 if (strlen(dev_name) > UNIX_PATH_MAX) { 250 if (strlen(dev_name) > UNIX_PATH_MAX) {
216 eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n", 251 eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n",
217 dev_name); 252 dev_name);
@@ -224,9 +259,7 @@ v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
224 259
225 trans->priv = ts; 260 trans->priv = ts;
226 ts->s = NULL; 261 ts->s = NULL;
227 262 ts->filp = NULL;
228 sema_init(&trans->writelock, 1);
229 sema_init(&trans->readlock, 1);
230 263
231 sun_server.sun_family = PF_UNIX; 264 sun_server.sun_family = PF_UNIX;
232 strcpy(sun_server.sun_path, dev_name); 265 strcpy(sun_server.sun_path, dev_name);
@@ -240,7 +273,18 @@ v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name,
240 return rc; 273 return rc;
241 } 274 }
242 csocket->sk->sk_allocation = GFP_NOIO; 275 csocket->sk->sk_allocation = GFP_NOIO;
276
277 fd = sock_map_fd(csocket);
278 if (fd < 0) {
279 sock_release(csocket);
280 kfree(ts);
281 trans->priv = NULL;
282 return fd;
283 }
284
243 ts->s = csocket; 285 ts->s = csocket;
286 ts->filp = fget(fd);
287 ts->filp->f_flags |= O_NONBLOCK;
244 trans->status = Connected; 288 trans->status = Connected;
245 289
246 return 0; 290 return 0;
@@ -261,12 +305,11 @@ static void v9fs_sock_close(struct v9fs_transport *trans)
261 305
262 ts = trans->priv; 306 ts = trans->priv;
263 307
264 if ((ts) && (ts->s)) { 308 if ((ts) && (ts->filp)) {
265 dprintk(DEBUG_TRANS, "closing the socket %p\n", ts->s); 309 fput(ts->filp);
266 sock_release(ts->s); 310 ts->filp = NULL;
267 ts->s = NULL; 311 ts->s = NULL;
268 trans->status = Disconnected; 312 trans->status = Disconnected;
269 dprintk(DEBUG_TRANS, "socket closed\n");
270 } 313 }
271 314
272 kfree(ts); 315 kfree(ts);
@@ -279,6 +322,7 @@ struct v9fs_transport v9fs_trans_tcp = {
279 .write = v9fs_sock_send, 322 .write = v9fs_sock_send,
280 .read = v9fs_sock_recv, 323 .read = v9fs_sock_recv,
281 .close = v9fs_sock_close, 324 .close = v9fs_sock_close,
325 .poll = v9fs_sock_poll,
282}; 326};
283 327
284struct v9fs_transport v9fs_trans_unix = { 328struct v9fs_transport v9fs_trans_unix = {
@@ -286,4 +330,5 @@ struct v9fs_transport v9fs_trans_unix = {
286 .write = v9fs_sock_send, 330 .write = v9fs_sock_send,
287 .read = v9fs_sock_recv, 331 .read = v9fs_sock_recv,
288 .close = v9fs_sock_close, 332 .close = v9fs_sock_close,
333 .poll = v9fs_sock_poll,
289}; 334};