m68knommu: remove local gettimeofday code
[wrt350n-kernel.git] / net / 9p / trans_fd.c
blob62332ed9da4afcd7e8b06577f0e4ec47d07ed171
1 /*
2 * linux/fs/9p/trans_fd.c
4 * Fd transport layer. Includes deprecated socket layer.
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-2007 by Eric Van Hensbergen <ericvh@gmail.com>
9 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
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/in.h>
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 <linux/uaccess.h>
36 #include <linux/inet.h>
37 #include <linux/idr.h>
38 #include <linux/file.h>
39 #include <linux/parser.h>
40 #include <net/9p/9p.h>
41 #include <net/9p/transport.h>
43 #define P9_PORT 564
44 #define MAX_SOCK_BUF (64*1024)
47 struct p9_fd_opts {
48 int rfd;
49 int wfd;
50 u16 port;
53 struct p9_trans_fd {
54 struct file *rd;
55 struct file *wr;
59 * Option Parsing (code inspired by NFS code)
60 * - a little lazy - parse all fd-transport options
63 enum {
64 /* Options that take integer arguments */
65 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
68 static match_table_t tokens = {
69 {Opt_port, "port=%u"},
70 {Opt_rfdno, "rfdno=%u"},
71 {Opt_wfdno, "wfdno=%u"},
72 {Opt_err, NULL},
75 /**
76 * v9fs_parse_options - parse mount options into session structure
77 * @options: options string passed from mount
78 * @v9ses: existing v9fs session information
82 static void parse_opts(char *options, struct p9_fd_opts *opts)
84 char *p;
85 substring_t args[MAX_OPT_ARGS];
86 int option;
87 int ret;
89 opts->port = P9_PORT;
90 opts->rfd = ~0;
91 opts->wfd = ~0;
93 if (!options)
94 return;
96 while ((p = strsep(&options, ",")) != NULL) {
97 int token;
98 if (!*p)
99 continue;
100 token = match_token(p, tokens, args);
101 ret = match_int(&args[0], &option);
102 if (ret < 0) {
103 P9_DPRINTK(P9_DEBUG_ERROR,
104 "integer field, but no integer?\n");
105 continue;
107 switch (token) {
108 case Opt_port:
109 opts->port = option;
110 break;
111 case Opt_rfdno:
112 opts->rfd = option;
113 break;
114 case Opt_wfdno:
115 opts->wfd = option;
116 break;
117 default:
118 continue;
123 static int p9_fd_open(struct p9_trans *trans, int rfd, int wfd)
125 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
126 GFP_KERNEL);
127 if (!ts)
128 return -ENOMEM;
130 ts->rd = fget(rfd);
131 ts->wr = fget(wfd);
132 if (!ts->rd || !ts->wr) {
133 if (ts->rd)
134 fput(ts->rd);
135 if (ts->wr)
136 fput(ts->wr);
137 kfree(ts);
138 return -EIO;
141 trans->priv = ts;
142 trans->status = Connected;
144 return 0;
147 static int p9_socket_open(struct p9_trans *trans, struct socket *csocket)
149 int fd, ret;
151 csocket->sk->sk_allocation = GFP_NOIO;
152 fd = sock_map_fd(csocket);
153 if (fd < 0) {
154 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
155 return fd;
158 ret = p9_fd_open(trans, fd, fd);
159 if (ret < 0) {
160 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
161 sockfd_put(csocket);
162 return ret;
165 ((struct p9_trans_fd *)trans->priv)->rd->f_flags |= O_NONBLOCK;
167 return 0;
171 * p9_fd_read- read from a fd
172 * @v9ses: session information
173 * @v: buffer to receive data into
174 * @len: size of receive buffer
177 static int p9_fd_read(struct p9_trans *trans, void *v, int len)
179 int ret;
180 struct p9_trans_fd *ts = NULL;
182 if (trans && trans->status != Disconnected)
183 ts = trans->priv;
185 if (!ts)
186 return -EREMOTEIO;
188 if (!(ts->rd->f_flags & O_NONBLOCK))
189 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
191 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
192 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
193 trans->status = Disconnected;
194 return ret;
198 * p9_fd_write - write to a socket
199 * @v9ses: session information
200 * @v: buffer to send data from
201 * @len: size of send buffer
204 static int p9_fd_write(struct p9_trans *trans, void *v, int len)
206 int ret;
207 mm_segment_t oldfs;
208 struct p9_trans_fd *ts = NULL;
210 if (trans && trans->status != Disconnected)
211 ts = trans->priv;
213 if (!ts)
214 return -EREMOTEIO;
216 if (!(ts->wr->f_flags & O_NONBLOCK))
217 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
219 oldfs = get_fs();
220 set_fs(get_ds());
221 /* The cast to a user pointer is valid due to the set_fs() */
222 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
223 set_fs(oldfs);
225 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
226 trans->status = Disconnected;
227 return ret;
230 static unsigned int
231 p9_fd_poll(struct p9_trans *trans, struct poll_table_struct *pt)
233 int ret, n;
234 struct p9_trans_fd *ts = NULL;
235 mm_segment_t oldfs;
237 if (trans && trans->status == Connected)
238 ts = trans->priv;
240 if (!ts)
241 return -EREMOTEIO;
243 if (!ts->rd->f_op || !ts->rd->f_op->poll)
244 return -EIO;
246 if (!ts->wr->f_op || !ts->wr->f_op->poll)
247 return -EIO;
249 oldfs = get_fs();
250 set_fs(get_ds());
252 ret = ts->rd->f_op->poll(ts->rd, pt);
253 if (ret < 0)
254 goto end;
256 if (ts->rd != ts->wr) {
257 n = ts->wr->f_op->poll(ts->wr, pt);
258 if (n < 0) {
259 ret = n;
260 goto end;
262 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
265 end:
266 set_fs(oldfs);
267 return ret;
271 * p9_sock_close - shutdown socket
272 * @trans: private socket structure
275 static void p9_fd_close(struct p9_trans *trans)
277 struct p9_trans_fd *ts;
279 if (!trans)
280 return;
282 ts = xchg(&trans->priv, NULL);
284 if (!ts)
285 return;
287 trans->status = Disconnected;
288 if (ts->rd)
289 fput(ts->rd);
290 if (ts->wr)
291 fput(ts->wr);
292 kfree(ts);
295 static struct p9_trans *p9_trans_create_tcp(const char *addr, char *args)
297 int err;
298 struct p9_trans *trans;
299 struct socket *csocket;
300 struct sockaddr_in sin_server;
301 struct p9_fd_opts opts;
303 parse_opts(args, &opts);
305 csocket = NULL;
306 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
307 if (!trans)
308 return ERR_PTR(-ENOMEM);
310 trans->write = p9_fd_write;
311 trans->read = p9_fd_read;
312 trans->close = p9_fd_close;
313 trans->poll = p9_fd_poll;
315 sin_server.sin_family = AF_INET;
316 sin_server.sin_addr.s_addr = in_aton(addr);
317 sin_server.sin_port = htons(opts.port);
318 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
320 if (!csocket) {
321 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
322 err = -EIO;
323 goto error;
326 err = csocket->ops->connect(csocket,
327 (struct sockaddr *)&sin_server,
328 sizeof(struct sockaddr_in), 0);
329 if (err < 0) {
330 P9_EPRINTK(KERN_ERR,
331 "p9_trans_tcp: problem connecting socket to %s\n",
332 addr);
333 goto error;
336 err = p9_socket_open(trans, csocket);
337 if (err < 0)
338 goto error;
340 return trans;
342 error:
343 if (csocket)
344 sock_release(csocket);
346 kfree(trans);
347 return ERR_PTR(err);
350 static struct p9_trans *p9_trans_create_unix(const char *addr, char *args)
352 int err;
353 struct socket *csocket;
354 struct sockaddr_un sun_server;
355 struct p9_trans *trans;
357 csocket = NULL;
358 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
359 if (!trans)
360 return ERR_PTR(-ENOMEM);
362 trans->write = p9_fd_write;
363 trans->read = p9_fd_read;
364 trans->close = p9_fd_close;
365 trans->poll = p9_fd_poll;
367 if (strlen(addr) > UNIX_PATH_MAX) {
368 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
369 addr);
370 err = -ENAMETOOLONG;
371 goto error;
374 sun_server.sun_family = PF_UNIX;
375 strcpy(sun_server.sun_path, addr);
376 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
377 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
378 sizeof(struct sockaddr_un) - 1, 0);
379 if (err < 0) {
380 P9_EPRINTK(KERN_ERR,
381 "p9_trans_unix: problem connecting socket: %s: %d\n",
382 addr, err);
383 goto error;
386 err = p9_socket_open(trans, csocket);
387 if (err < 0)
388 goto error;
390 return trans;
392 error:
393 if (csocket)
394 sock_release(csocket);
396 kfree(trans);
397 return ERR_PTR(err);
400 static struct p9_trans *p9_trans_create_fd(const char *name, char *args)
402 int err;
403 struct p9_trans *trans;
404 struct p9_fd_opts opts;
406 parse_opts(args, &opts);
408 if (opts.rfd == ~0 || opts.wfd == ~0) {
409 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
410 return ERR_PTR(-ENOPROTOOPT);
413 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
414 if (!trans)
415 return ERR_PTR(-ENOMEM);
417 trans->write = p9_fd_write;
418 trans->read = p9_fd_read;
419 trans->close = p9_fd_close;
420 trans->poll = p9_fd_poll;
422 err = p9_fd_open(trans, opts.rfd, opts.wfd);
423 if (err < 0)
424 goto error;
426 return trans;
428 error:
429 kfree(trans);
430 return ERR_PTR(err);
433 static struct p9_trans_module p9_tcp_trans = {
434 .name = "tcp",
435 .maxsize = MAX_SOCK_BUF,
436 .def = 1,
437 .create = p9_trans_create_tcp,
440 static struct p9_trans_module p9_unix_trans = {
441 .name = "unix",
442 .maxsize = MAX_SOCK_BUF,
443 .def = 0,
444 .create = p9_trans_create_unix,
447 static struct p9_trans_module p9_fd_trans = {
448 .name = "fd",
449 .maxsize = MAX_SOCK_BUF,
450 .def = 0,
451 .create = p9_trans_create_fd,
454 static int __init p9_trans_fd_init(void)
456 v9fs_register_trans(&p9_tcp_trans);
457 v9fs_register_trans(&p9_unix_trans);
458 v9fs_register_trans(&p9_fd_trans);
460 return 1;
463 static void __exit p9_trans_fd_exit(void) {
464 printk(KERN_ERR "Removal of 9p transports not implemented\n");
465 BUG();
468 module_init(p9_trans_fd_init);
469 module_exit(p9_trans_fd_exit);
471 MODULE_AUTHOR("Latchesar Ionkov <lucho@ionkov.net>");
472 MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>");
473 MODULE_LICENSE("GPL");