[PATCH] tpm: add HID module parameter
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / 9p / trans_fd.c
blob94e0a7fd9fc2c30b6954001620a975005a742e29
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-2005 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/config.h>
29 #include <linux/in.h>
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>
41 #include "debug.h"
42 #include "v9fs.h"
43 #include "transport.h"
45 #define V9FS_PORT 564
47 struct v9fs_trans_fd {
48 struct file *rd;
49 struct file *wr;
52 /**
53 * v9fs_fd_read- read from a fd
54 * @v9ses: session information
55 * @v: buffer to receive data into
56 * @len: size of receive buffer
59 static int v9fs_fd_read(struct v9fs_transport *trans, void *v, int len)
61 int ret;
62 struct v9fs_trans_fd *ts;
64 if (!trans || trans->status == Disconnected || !(ts = trans->priv))
65 return -EREMOTEIO;
67 if (!(ts->rd->f_flags & O_NONBLOCK))
68 dprintk(DEBUG_ERROR, "blocking read ...\n");
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;
76 /**
77 * v9fs_fd_write - write to a socket
78 * @v9ses: session information
79 * @v: buffer to send data from
80 * @len: size of send buffer
83 static int v9fs_fd_write(struct v9fs_transport *trans, void *v, int len)
85 int ret;
86 mm_segment_t oldfs;
87 struct v9fs_trans_fd *ts;
89 if (!trans || trans->status == Disconnected || !(ts = trans->priv))
90 return -EREMOTEIO;
92 if (!(ts->wr->f_flags & O_NONBLOCK))
93 dprintk(DEBUG_ERROR, "blocking write ...\n");
95 oldfs = get_fs();
96 set_fs(get_ds());
97 /* The cast to a user pointer is valid due to the set_fs() */
98 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
99 set_fs(oldfs);
101 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
102 trans->status = Disconnected;
103 return ret;
106 static unsigned int
107 v9fs_fd_poll(struct v9fs_transport *trans, struct poll_table_struct *pt)
109 int ret, n;
110 struct v9fs_trans_fd *ts;
111 mm_segment_t oldfs;
113 if (!trans || trans->status != Connected || !(ts = trans->priv))
114 return -EREMOTEIO;
116 if (!ts->rd->f_op || !ts->rd->f_op->poll)
117 return -EIO;
119 if (!ts->wr->f_op || !ts->wr->f_op->poll)
120 return -EIO;
122 oldfs = get_fs();
123 set_fs(get_ds());
125 ret = ts->rd->f_op->poll(ts->rd, pt);
126 if (ret < 0)
127 goto end;
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;
135 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
138 end:
139 set_fs(oldfs);
140 return ret;
143 static int v9fs_fd_open(struct v9fs_session_info *v9ses, int rfd, int wfd)
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;
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);
158 kfree(ts);
159 return -EIO;
162 trans->priv = ts;
163 trans->status = Connected;
165 return 0;
168 static int v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr,
169 char *data)
171 if (v9ses->rfdno == ~0 || v9ses->wfdno == ~0) {
172 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
173 return -ENOPROTOOPT;
176 return v9fs_fd_open(v9ses, v9ses->rfdno, v9ses->wfdno);
179 static int v9fs_socket_open(struct v9fs_session_info *v9ses,
180 struct socket *csocket)
182 int fd, ret;
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;
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;
199 ((struct v9fs_trans_fd *)v9ses->transport->priv)->rd->f_flags |=
200 O_NONBLOCK;
201 return 0;
204 static int v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr,
205 char *data)
207 int ret;
208 struct socket *csocket = NULL;
209 struct sockaddr_in sin_server;
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);
216 if (!csocket) {
217 eprintk(KERN_ERR, "v9fs_trans_tcp: problem creating socket\n");
218 return -1;
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;
231 return v9fs_socket_open(v9ses, csocket);
234 static int
235 v9fs_unix_init(struct v9fs_session_info *v9ses, const char *addr, char *data)
237 int ret;
238 struct socket *csocket;
239 struct sockaddr_un sun_server;
241 if (strlen(addr) > UNIX_PATH_MAX) {
242 eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n",
243 addr);
244 return -ENAMETOOLONG;
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;
259 return v9fs_socket_open(v9ses, csocket);
263 * v9fs_sock_close - shutdown socket
264 * @trans: private socket structure
267 static void v9fs_fd_close(struct v9fs_transport *trans)
269 struct v9fs_trans_fd *ts;
271 if (!trans)
272 return;
274 ts = xchg(&trans->priv, NULL);
276 if (!ts)
277 return;
279 trans->status = Disconnected;
280 if (ts->rd)
281 fput(ts->rd);
282 if (ts->wr)
283 fput(ts->wr);
284 kfree(ts);
287 struct v9fs_transport v9fs_trans_fd = {
288 .init = v9fs_fd_init,
289 .write = v9fs_fd_write,
290 .read = v9fs_fd_read,
291 .close = v9fs_fd_close,
292 .poll = v9fs_fd_poll,
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,
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,