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