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