Commit | Line | Data |
---|---|---|
426cc91a EVH |
1 | /* |
2 | * linux/fs/9p/trans_fd.c | |
3 | * | |
27979bb2 | 4 | * Fd transport layer. Includes deprecated socket layer. |
426cc91a | 5 | * |
27979bb2 RC |
6 | * Copyright (C) 2006 by Russ Cox <rsc@swtch.com> |
7 | * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net> | |
8 | * Copyright (C) 2004-2005 by Eric Van Hensbergen <ericvh@gmail.com> | |
9 | * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com> | |
426cc91a EVH |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify | |
42e8c509 EVH |
12 | * it under the terms of the GNU General Public License version 2 |
13 | * as published by the Free Software Foundation. | |
426cc91a EVH |
14 | * |
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. | |
19 | * | |
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 | |
25 | * | |
26 | */ | |
27 | ||
27979bb2 | 28 | #include <linux/in.h> |
426cc91a EVH |
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> | |
38 | #include <linux/file.h> | |
39 | ||
40 | #include "debug.h" | |
41 | #include "v9fs.h" | |
42 | #include "transport.h" | |
43 | ||
27979bb2 RC |
44 | #define V9FS_PORT 564 |
45 | ||
426cc91a | 46 | struct v9fs_trans_fd { |
27979bb2 RC |
47 | struct file *rd; |
48 | struct file *wr; | |
426cc91a EVH |
49 | }; |
50 | ||
51 | /** | |
27979bb2 | 52 | * v9fs_fd_read- read from a fd |
426cc91a EVH |
53 | * @v9ses: session information |
54 | * @v: buffer to receive data into | |
55 | * @len: size of receive buffer | |
56 | * | |
57 | */ | |
27979bb2 | 58 | static int v9fs_fd_read(struct v9fs_transport *trans, void *v, int len) |
426cc91a | 59 | { |
27979bb2 RC |
60 | int ret; |
61 | struct v9fs_trans_fd *ts; | |
426cc91a | 62 | |
27979bb2 RC |
63 | if (!trans || trans->status == Disconnected || !(ts = trans->priv)) |
64 | return -EREMOTEIO; | |
426cc91a | 65 | |
27979bb2 RC |
66 | if (!(ts->rd->f_flags & O_NONBLOCK)) |
67 | dprintk(DEBUG_ERROR, "blocking read ...\n"); | |
68 | ||
69 | ret = kernel_read(ts->rd, ts->rd->f_pos, v, len); | |
70 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) | |
71 | trans->status = Disconnected; | |
72 | return ret; | |
426cc91a EVH |
73 | } |
74 | ||
75 | /** | |
27979bb2 | 76 | * v9fs_fd_write - write to a socket |
426cc91a EVH |
77 | * @v9ses: session information |
78 | * @v: buffer to send data from | |
79 | * @len: size of send buffer | |
80 | * | |
81 | */ | |
27979bb2 | 82 | static int v9fs_fd_write(struct v9fs_transport *trans, void *v, int len) |
426cc91a | 83 | { |
27979bb2 RC |
84 | int ret; |
85 | mm_segment_t oldfs; | |
86 | struct v9fs_trans_fd *ts; | |
426cc91a | 87 | |
27979bb2 RC |
88 | if (!trans || trans->status == Disconnected || !(ts = trans->priv)) |
89 | return -EREMOTEIO; | |
90 | ||
91 | if (!(ts->wr->f_flags & O_NONBLOCK)) | |
92 | dprintk(DEBUG_ERROR, "blocking write ...\n"); | |
426cc91a | 93 | |
731805b4 | 94 | oldfs = get_fs(); |
426cc91a EVH |
95 | set_fs(get_ds()); |
96 | /* The cast to a user pointer is valid due to the set_fs() */ | |
27979bb2 | 97 | ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos); |
426cc91a EVH |
98 | set_fs(oldfs); |
99 | ||
27979bb2 RC |
100 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
101 | trans->status = Disconnected; | |
426cc91a EVH |
102 | return ret; |
103 | } | |
104 | ||
27979bb2 RC |
105 | static unsigned int |
106 | v9fs_fd_poll(struct v9fs_transport *trans, struct poll_table_struct *pt) | |
426cc91a | 107 | { |
27979bb2 RC |
108 | int ret, n; |
109 | struct v9fs_trans_fd *ts; | |
110 | mm_segment_t oldfs; | |
426cc91a | 111 | |
27979bb2 RC |
112 | if (!trans || trans->status != Connected || !(ts = trans->priv)) |
113 | return -EREMOTEIO; | |
426cc91a | 114 | |
27979bb2 RC |
115 | if (!ts->rd->f_op || !ts->rd->f_op->poll) |
116 | return -EIO; | |
426cc91a | 117 | |
27979bb2 RC |
118 | if (!ts->wr->f_op || !ts->wr->f_op->poll) |
119 | return -EIO; | |
426cc91a | 120 | |
27979bb2 RC |
121 | oldfs = get_fs(); |
122 | set_fs(get_ds()); | |
426cc91a | 123 | |
27979bb2 RC |
124 | ret = ts->rd->f_op->poll(ts->rd, pt); |
125 | if (ret < 0) | |
126 | goto end; | |
426cc91a | 127 | |
27979bb2 RC |
128 | if (ts->rd != ts->wr) { |
129 | n = ts->wr->f_op->poll(ts->wr, pt); | |
130 | if (n < 0) { | |
131 | ret = n; | |
132 | goto end; | |
133 | } | |
134 | ret = (ret & ~POLLOUT) | (n & ~POLLIN); | |
135 | } | |
426cc91a | 136 | |
27979bb2 RC |
137 | end: |
138 | set_fs(oldfs); | |
139 | return ret; | |
140 | } | |
141 | ||
142 | static int v9fs_fd_open(struct v9fs_session_info *v9ses, int rfd, int wfd) | |
143 | { | |
144 | struct v9fs_transport *trans = v9ses->transport; | |
145 | struct v9fs_trans_fd *ts = kmalloc(sizeof(struct v9fs_trans_fd), | |
146 | GFP_KERNEL); | |
147 | if (!ts) | |
148 | return -ENOMEM; | |
149 | ||
150 | ts->rd = fget(rfd); | |
151 | ts->wr = fget(wfd); | |
152 | if (!ts->rd || !ts->wr) { | |
153 | if (ts->rd) | |
154 | fput(ts->rd); | |
155 | if (ts->wr) | |
156 | fput(ts->wr); | |
426cc91a EVH |
157 | kfree(ts); |
158 | return -EIO; | |
159 | } | |
160 | ||
161 | trans->priv = ts; | |
162 | trans->status = Connected; | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
27979bb2 RC |
167 | static int v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, |
168 | char *data) | |
426cc91a | 169 | { |
27979bb2 RC |
170 | if (v9ses->rfdno == ~0 || v9ses->wfdno == ~0) { |
171 | printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n"); | |
172 | return -ENOPROTOOPT; | |
173 | } | |
426cc91a | 174 | |
27979bb2 RC |
175 | return v9fs_fd_open(v9ses, v9ses->rfdno, v9ses->wfdno); |
176 | } | |
426cc91a | 177 | |
27979bb2 RC |
178 | static int v9fs_socket_open(struct v9fs_session_info *v9ses, |
179 | struct socket *csocket) | |
180 | { | |
181 | int fd, ret; | |
182 | ||
183 | csocket->sk->sk_allocation = GFP_NOIO; | |
184 | if ((fd = sock_map_fd(csocket)) < 0) { | |
185 | eprintk(KERN_ERR, "v9fs_socket_open: failed to map fd\n"); | |
186 | ret = fd; | |
187 | release_csocket: | |
188 | sock_release(csocket); | |
189 | return ret; | |
190 | } | |
426cc91a | 191 | |
27979bb2 RC |
192 | if ((ret = v9fs_fd_open(v9ses, fd, fd)) < 0) { |
193 | sockfd_put(csocket); | |
194 | eprintk(KERN_ERR, "v9fs_socket_open: failed to open fd\n"); | |
195 | goto release_csocket; | |
196 | } | |
426cc91a | 197 | |
27979bb2 RC |
198 | ((struct v9fs_trans_fd *)v9ses->transport->priv)->rd->f_flags |= |
199 | O_NONBLOCK; | |
200 | return 0; | |
426cc91a EVH |
201 | } |
202 | ||
27979bb2 RC |
203 | static int v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, |
204 | char *data) | |
3cf6429a | 205 | { |
27979bb2 RC |
206 | int ret; |
207 | struct socket *csocket = NULL; | |
208 | struct sockaddr_in sin_server; | |
209 | ||
210 | sin_server.sin_family = AF_INET; | |
211 | sin_server.sin_addr.s_addr = in_aton(addr); | |
212 | sin_server.sin_port = htons(v9ses->port); | |
213 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); | |
214 | ||
215 | if (!csocket) { | |
216 | eprintk(KERN_ERR, "v9fs_trans_tcp: problem creating socket\n"); | |
217 | return -1; | |
218 | } | |
3cf6429a | 219 | |
27979bb2 RC |
220 | ret = csocket->ops->connect(csocket, |
221 | (struct sockaddr *)&sin_server, | |
222 | sizeof(struct sockaddr_in), 0); | |
223 | if (ret < 0) { | |
224 | eprintk(KERN_ERR, | |
225 | "v9fs_trans_tcp: problem connecting socket to %s\n", | |
226 | addr); | |
227 | return ret; | |
228 | } | |
3cf6429a | 229 | |
27979bb2 RC |
230 | return v9fs_socket_open(v9ses, csocket); |
231 | } | |
3cf6429a | 232 | |
27979bb2 RC |
233 | static int |
234 | v9fs_unix_init(struct v9fs_session_info *v9ses, const char *addr, char *data) | |
235 | { | |
236 | int ret; | |
237 | struct socket *csocket; | |
238 | struct sockaddr_un sun_server; | |
239 | ||
240 | if (strlen(addr) > UNIX_PATH_MAX) { | |
241 | eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n", | |
242 | addr); | |
243 | return -ENAMETOOLONG; | |
244 | } | |
3cf6429a | 245 | |
27979bb2 RC |
246 | sun_server.sun_family = PF_UNIX; |
247 | strcpy(sun_server.sun_path, addr); | |
248 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); | |
249 | ret = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, | |
250 | sizeof(struct sockaddr_un) - 1, 0); | |
251 | if (ret < 0) { | |
252 | eprintk(KERN_ERR, | |
253 | "v9fs_trans_unix: problem connecting socket: %s: %d\n", | |
254 | addr, ret); | |
255 | return ret; | |
3cf6429a LI |
256 | } |
257 | ||
27979bb2 RC |
258 | return v9fs_socket_open(v9ses, csocket); |
259 | } | |
3cf6429a | 260 | |
27979bb2 RC |
261 | /** |
262 | * v9fs_sock_close - shutdown socket | |
263 | * @trans: private socket structure | |
264 | * | |
265 | */ | |
266 | static void v9fs_fd_close(struct v9fs_transport *trans) | |
267 | { | |
268 | struct v9fs_trans_fd *ts; | |
3cf6429a | 269 | |
27979bb2 RC |
270 | if (!trans) |
271 | return; | |
3cf6429a | 272 | |
27979bb2 | 273 | ts = xchg(&trans->priv, NULL); |
3cf6429a | 274 | |
27979bb2 RC |
275 | if (!ts) |
276 | return; | |
3cf6429a | 277 | |
27979bb2 RC |
278 | trans->status = Disconnected; |
279 | if (ts->rd) | |
280 | fput(ts->rd); | |
281 | if (ts->wr) | |
282 | fput(ts->wr); | |
283 | kfree(ts); | |
3cf6429a LI |
284 | } |
285 | ||
426cc91a EVH |
286 | struct v9fs_transport v9fs_trans_fd = { |
287 | .init = v9fs_fd_init, | |
27979bb2 RC |
288 | .write = v9fs_fd_write, |
289 | .read = v9fs_fd_read, | |
426cc91a | 290 | .close = v9fs_fd_close, |
3cf6429a | 291 | .poll = v9fs_fd_poll, |
426cc91a EVH |
292 | }; |
293 | ||
27979bb2 RC |
294 | struct v9fs_transport v9fs_trans_tcp = { |
295 | .init = v9fs_tcp_init, | |
296 | .write = v9fs_fd_write, | |
297 | .read = v9fs_fd_read, | |
298 | .close = v9fs_fd_close, | |
299 | .poll = v9fs_fd_poll, | |
300 | }; | |
301 | ||
302 | struct v9fs_transport v9fs_trans_unix = { | |
303 | .init = v9fs_unix_init, | |
304 | .write = v9fs_fd_write, | |
305 | .read = v9fs_fd_read, | |
306 | .close = v9fs_fd_close, | |
307 | .poll = v9fs_fd_poll, | |
308 | }; |