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