b7ffb98595881f31f0c40cb7b6d519ee3050c3ed
[pandora-kernel.git] / fs / 9p / trans_fd.c
1 /*
2  * linux/fs/9p/trans_fd.c
3  *
4  * File Descriptor Transport Layer
5  *
6  *  Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to:
20  *  Free Software Foundation
21  *  51 Franklin Street, Fifth Floor
22  *  Boston, MA  02111-1301  USA
23  *
24  */
25
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/net.h>
29 #include <linux/ipv6.h>
30 #include <linux/errno.h>
31 #include <linux/kernel.h>
32 #include <linux/un.h>
33 #include <asm/uaccess.h>
34 #include <linux/inet.h>
35 #include <linux/idr.h>
36 #include <linux/file.h>
37
38 #include "debug.h"
39 #include "v9fs.h"
40 #include "transport.h"
41
42 struct v9fs_trans_fd {
43         struct file *in_file;
44         struct file *out_file;
45 };
46
47 /**
48  * v9fs_fd_recv - receive from a socket
49  * @v9ses: session information
50  * @v: buffer to receive data into
51  * @len: size of receive buffer
52  *
53  */
54
55 static int v9fs_fd_recv(struct v9fs_transport *trans, void *v, int len)
56 {
57         struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
58
59         if (!trans || trans->status != Connected || !ts)
60                 return -EIO;
61
62         return kernel_read(ts->in_file, ts->in_file->f_pos, v, len);
63 }
64
65 /**
66  * v9fs_fd_send - send to a socket
67  * @v9ses: session information
68  * @v: buffer to send data from
69  * @len: size of send buffer
70  *
71  */
72
73 static int v9fs_fd_send(struct v9fs_transport *trans, void *v, int len)
74 {
75         struct v9fs_trans_fd *ts = trans ? trans->priv : NULL;
76         mm_segment_t oldfs = get_fs();
77         int ret = 0;
78
79         if (!trans || trans->status != Connected || !ts)
80                 return -EIO;
81
82         set_fs(get_ds());
83         /* The cast to a user pointer is valid due to the set_fs() */
84         ret = vfs_write(ts->out_file, (void __user *)v, len, &ts->out_file->f_pos);
85         set_fs(oldfs);
86
87         return ret;
88 }
89
90 /**
91  * v9fs_fd_init - initialize file descriptor transport
92  * @v9ses: session information
93  * @addr: address of server to mount
94  * @data: mount options
95  *
96  */
97
98 static int
99 v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
100 {
101         struct v9fs_trans_fd *ts = NULL;
102         struct v9fs_transport *trans = v9ses->transport;
103
104         if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) {
105                 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
106                 return -ENOPROTOOPT;
107         }
108
109         sema_init(&trans->writelock, 1);
110         sema_init(&trans->readlock, 1);
111
112         ts = kmalloc(sizeof(struct v9fs_trans_fd), GFP_KERNEL);
113
114         if (!ts)
115                 return -ENOMEM;
116
117         ts->in_file = fget( v9ses->rfdno );
118         ts->out_file = fget( v9ses->wfdno );
119
120         if (!ts->in_file || !ts->out_file) {
121                 if (ts->in_file)
122                         fput(ts->in_file);
123
124                 if (ts->out_file)
125                         fput(ts->out_file);
126
127                 kfree(ts);
128                 return -EIO;
129         }
130
131         trans->priv = ts;
132         trans->status = Connected;
133
134         return 0;
135 }
136
137
138 /**
139  * v9fs_fd_close - shutdown file descriptor
140  * @trans: private socket structure
141  *
142  */
143
144 static void v9fs_fd_close(struct v9fs_transport *trans)
145 {
146         struct v9fs_trans_fd *ts;
147
148         if (!trans)
149                 return;
150
151         ts = xchg(&trans->priv, NULL);
152
153         if (!ts)
154                 return;
155
156         trans->status = Disconnected;
157         if (ts->in_file)
158                 fput(ts->in_file);
159
160         if (ts->out_file)
161                 fput(ts->out_file);
162
163         kfree(ts);
164 }
165
166 struct v9fs_transport v9fs_trans_fd = {
167         .init = v9fs_fd_init,
168         .write = v9fs_fd_send,
169         .read = v9fs_fd_recv,
170         .close = v9fs_fd_close,
171 };
172