diff options
author | Russ Cox <rsc@swtch.com> | 2006-03-25 06:07:23 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-03-25 11:22:54 -0500 |
commit | 27979bb2ff748613dba96ae66392a76fb0678527 (patch) | |
tree | 9f98e2b3c0efdc9db23eee70ce2f6831e9eb65bd /fs/9p/trans_fd.c | |
parent | 4a2f0acf0f951599fd9e4af95cf9483449970c26 (diff) |
[PATCH] v9fs: consolidate trans_sock into trans_fd
Here is a new trans_fd.c that replaces the current trans_fd.c and
trans_sock.c.
Signed-off-by: Eric Van Hensbergen <ericvh@gmail.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'fs/9p/trans_fd.c')
-rw-r--r-- | fs/9p/trans_fd.c | 297 |
1 files changed, 196 insertions, 101 deletions
diff --git a/fs/9p/trans_fd.c b/fs/9p/trans_fd.c index 5b2ce21b10fa..8029844d6b28 100644 --- a/fs/9p/trans_fd.c +++ b/fs/9p/trans_fd.c | |||
@@ -1,10 +1,13 @@ | |||
1 | /* | 1 | /* |
2 | * linux/fs/9p/trans_fd.c | 2 | * linux/fs/9p/trans_fd.c |
3 | * | 3 | * |
4 | * File Descriptor Transport Layer | 4 | * Fd transport layer. Includes deprecated socket layer. |
5 | * | 5 | * |
6 | * Copyright (C) 2005 by Latchesar Ionkov <lucho@ionkov.net> | 6 | * Copyright (C) 2006 by Russ Cox <rsc@swtch.com> |
7 | * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.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> | ||
10 | * Copyright (C) 1995, 1996 by Olaf Kirch <okir@monad.swb.de> | ||
8 | * | 11 | * |
9 | * This program is free software; you can redistribute it and/or modify | 12 | * 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 | 13 | * it under the terms of the GNU General Public License as published by |
@@ -25,6 +28,7 @@ | |||
25 | */ | 28 | */ |
26 | 29 | ||
27 | #include <linux/config.h> | 30 | #include <linux/config.h> |
31 | #include <linux/in.h> | ||
28 | #include <linux/module.h> | 32 | #include <linux/module.h> |
29 | #include <linux/net.h> | 33 | #include <linux/net.h> |
30 | #include <linux/ipv6.h> | 34 | #include <linux/ipv6.h> |
@@ -40,89 +44,119 @@ | |||
40 | #include "v9fs.h" | 44 | #include "v9fs.h" |
41 | #include "transport.h" | 45 | #include "transport.h" |
42 | 46 | ||
47 | #define V9FS_PORT 564 | ||
48 | |||
43 | struct v9fs_trans_fd { | 49 | struct v9fs_trans_fd { |
44 | struct file *in_file; | 50 | struct file *rd; |
45 | struct file *out_file; | 51 | struct file *wr; |
46 | }; | 52 | }; |
47 | 53 | ||
48 | /** | 54 | /** |
49 | * v9fs_fd_recv - receive from a socket | 55 | * v9fs_fd_read- read from a fd |
50 | * @v9ses: session information | 56 | * @v9ses: session information |
51 | * @v: buffer to receive data into | 57 | * @v: buffer to receive data into |
52 | * @len: size of receive buffer | 58 | * @len: size of receive buffer |
53 | * | 59 | * |
54 | */ | 60 | */ |
55 | 61 | static int v9fs_fd_read(struct v9fs_transport *trans, void *v, int len) | |
56 | static int v9fs_fd_recv(struct v9fs_transport *trans, void *v, int len) | ||
57 | { | 62 | { |
58 | struct v9fs_trans_fd *ts = trans ? trans->priv : NULL; | 63 | int ret; |
64 | struct v9fs_trans_fd *ts; | ||
59 | 65 | ||
60 | if (!trans || trans->status != Connected || !ts) | 66 | if (!trans || trans->status == Disconnected || !(ts = trans->priv)) |
61 | return -EIO; | 67 | return -EREMOTEIO; |
62 | 68 | ||
63 | return kernel_read(ts->in_file, ts->in_file->f_pos, v, len); | 69 | if (!(ts->rd->f_flags & O_NONBLOCK)) |
70 | dprintk(DEBUG_ERROR, "blocking read ...\n"); | ||
71 | |||
72 | ret = kernel_read(ts->rd, ts->rd->f_pos, v, len); | ||
73 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) | ||
74 | trans->status = Disconnected; | ||
75 | return ret; | ||
64 | } | 76 | } |
65 | 77 | ||
66 | /** | 78 | /** |
67 | * v9fs_fd_send - send to a socket | 79 | * v9fs_fd_write - write to a socket |
68 | * @v9ses: session information | 80 | * @v9ses: session information |
69 | * @v: buffer to send data from | 81 | * @v: buffer to send data from |
70 | * @len: size of send buffer | 82 | * @len: size of send buffer |
71 | * | 83 | * |
72 | */ | 84 | */ |
73 | 85 | static int v9fs_fd_write(struct v9fs_transport *trans, void *v, int len) | |
74 | static int v9fs_fd_send(struct v9fs_transport *trans, void *v, int len) | ||
75 | { | 86 | { |
76 | struct v9fs_trans_fd *ts = trans ? trans->priv : NULL; | 87 | int ret; |
77 | mm_segment_t oldfs = get_fs(); | 88 | mm_segment_t oldfs; |
78 | int ret = 0; | 89 | struct v9fs_trans_fd *ts; |
79 | 90 | ||
80 | if (!trans || trans->status != Connected || !ts) | 91 | if (!trans || trans->status == Disconnected || !(ts = trans->priv)) |
81 | return -EIO; | 92 | return -EREMOTEIO; |
93 | |||
94 | if (!(ts->wr->f_flags & O_NONBLOCK)) | ||
95 | dprintk(DEBUG_ERROR, "blocking write ...\n"); | ||
82 | 96 | ||
83 | oldfs = get_fs(); | 97 | oldfs = get_fs(); |
84 | set_fs(get_ds()); | 98 | set_fs(get_ds()); |
85 | /* The cast to a user pointer is valid due to the set_fs() */ | 99 | /* 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); | 100 | ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos); |
87 | set_fs(oldfs); | 101 | set_fs(oldfs); |
88 | 102 | ||
103 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) | ||
104 | trans->status = Disconnected; | ||
89 | return ret; | 105 | return ret; |
90 | } | 106 | } |
91 | 107 | ||
92 | /** | 108 | static unsigned int |
93 | * v9fs_fd_init - initialize file descriptor transport | 109 | v9fs_fd_poll(struct v9fs_transport *trans, struct poll_table_struct *pt) |
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 | { | 110 | { |
103 | struct v9fs_trans_fd *ts = NULL; | 111 | int ret, n; |
104 | struct v9fs_transport *trans = v9ses->transport; | 112 | struct v9fs_trans_fd *ts; |
113 | mm_segment_t oldfs; | ||
105 | 114 | ||
106 | if((v9ses->wfdno == ~0) || (v9ses->rfdno == ~0)) { | 115 | if (!trans || trans->status != Connected || !(ts = trans->priv)) |
107 | printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n"); | 116 | return -EREMOTEIO; |
108 | return -ENOPROTOOPT; | ||
109 | } | ||
110 | 117 | ||
111 | ts = kmalloc(sizeof(struct v9fs_trans_fd), GFP_KERNEL); | 118 | if (!ts->rd->f_op || !ts->rd->f_op->poll) |
119 | return -EIO; | ||
112 | 120 | ||
113 | if (!ts) | 121 | if (!ts->wr->f_op || !ts->wr->f_op->poll) |
114 | return -ENOMEM; | 122 | return -EIO; |
115 | 123 | ||
116 | ts->in_file = fget( v9ses->rfdno ); | 124 | oldfs = get_fs(); |
117 | ts->out_file = fget( v9ses->wfdno ); | 125 | set_fs(get_ds()); |
118 | 126 | ||
119 | if (!ts->in_file || !ts->out_file) { | 127 | ret = ts->rd->f_op->poll(ts->rd, pt); |
120 | if (ts->in_file) | 128 | if (ret < 0) |
121 | fput(ts->in_file); | 129 | goto end; |
122 | 130 | ||
123 | if (ts->out_file) | 131 | if (ts->rd != ts->wr) { |
124 | fput(ts->out_file); | 132 | n = ts->wr->f_op->poll(ts->wr, pt); |
133 | if (n < 0) { | ||
134 | ret = n; | ||
135 | goto end; | ||
136 | } | ||
137 | ret = (ret & ~POLLOUT) | (n & ~POLLIN); | ||
138 | } | ||
125 | 139 | ||
140 | end: | ||
141 | set_fs(oldfs); | ||
142 | return ret; | ||
143 | } | ||
144 | |||
145 | static int v9fs_fd_open(struct v9fs_session_info *v9ses, int rfd, int wfd) | ||
146 | { | ||
147 | struct v9fs_transport *trans = v9ses->transport; | ||
148 | struct v9fs_trans_fd *ts = kmalloc(sizeof(struct v9fs_trans_fd), | ||
149 | GFP_KERNEL); | ||
150 | if (!ts) | ||
151 | return -ENOMEM; | ||
152 | |||
153 | ts->rd = fget(rfd); | ||
154 | ts->wr = fget(wfd); | ||
155 | if (!ts->rd || !ts->wr) { | ||
156 | if (ts->rd) | ||
157 | fput(ts->rd); | ||
158 | if (ts->wr) | ||
159 | fput(ts->wr); | ||
126 | kfree(ts); | 160 | kfree(ts); |
127 | return -EIO; | 161 | return -EIO; |
128 | } | 162 | } |
@@ -133,84 +167,145 @@ v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, char *data) | |||
133 | return 0; | 167 | return 0; |
134 | } | 168 | } |
135 | 169 | ||
136 | 170 | static int v9fs_fd_init(struct v9fs_session_info *v9ses, const char *addr, | |
137 | /** | 171 | char *data) |
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 | { | 172 | { |
145 | struct v9fs_trans_fd *ts; | 173 | if (v9ses->rfdno == ~0 || v9ses->wfdno == ~0) { |
146 | 174 | printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n"); | |
147 | if (!trans) | 175 | return -ENOPROTOOPT; |
148 | return; | 176 | } |
149 | |||
150 | ts = xchg(&trans->priv, NULL); | ||
151 | 177 | ||
152 | if (!ts) | 178 | return v9fs_fd_open(v9ses, v9ses->rfdno, v9ses->wfdno); |
153 | return; | 179 | } |
154 | 180 | ||
155 | trans->status = Disconnected; | 181 | static int v9fs_socket_open(struct v9fs_session_info *v9ses, |
156 | if (ts->in_file) | 182 | struct socket *csocket) |
157 | fput(ts->in_file); | 183 | { |
184 | int fd, ret; | ||
185 | |||
186 | csocket->sk->sk_allocation = GFP_NOIO; | ||
187 | if ((fd = sock_map_fd(csocket)) < 0) { | ||
188 | eprintk(KERN_ERR, "v9fs_socket_open: failed to map fd\n"); | ||
189 | ret = fd; | ||
190 | release_csocket: | ||
191 | sock_release(csocket); | ||
192 | return ret; | ||
193 | } | ||
158 | 194 | ||
159 | if (ts->out_file) | 195 | if ((ret = v9fs_fd_open(v9ses, fd, fd)) < 0) { |
160 | fput(ts->out_file); | 196 | sockfd_put(csocket); |
197 | eprintk(KERN_ERR, "v9fs_socket_open: failed to open fd\n"); | ||
198 | goto release_csocket; | ||
199 | } | ||
161 | 200 | ||
162 | kfree(ts); | 201 | ((struct v9fs_trans_fd *)v9ses->transport->priv)->rd->f_flags |= |
202 | O_NONBLOCK; | ||
203 | return 0; | ||
163 | } | 204 | } |
164 | 205 | ||
165 | static unsigned int | 206 | static int v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, |
166 | v9fs_fd_poll(struct v9fs_transport *trans, struct poll_table_struct *pt) | 207 | char *data) |
167 | { | 208 | { |
168 | int ret, n; | 209 | int ret; |
169 | struct v9fs_trans_fd *ts; | 210 | struct socket *csocket = NULL; |
170 | mm_segment_t oldfs; | 211 | struct sockaddr_in sin_server; |
212 | |||
213 | sin_server.sin_family = AF_INET; | ||
214 | sin_server.sin_addr.s_addr = in_aton(addr); | ||
215 | sin_server.sin_port = htons(v9ses->port); | ||
216 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); | ||
217 | |||
218 | if (!csocket) { | ||
219 | eprintk(KERN_ERR, "v9fs_trans_tcp: problem creating socket\n"); | ||
220 | return -1; | ||
221 | } | ||
171 | 222 | ||
172 | if (!trans) | 223 | ret = csocket->ops->connect(csocket, |
173 | return -EIO; | 224 | (struct sockaddr *)&sin_server, |
225 | sizeof(struct sockaddr_in), 0); | ||
226 | if (ret < 0) { | ||
227 | eprintk(KERN_ERR, | ||
228 | "v9fs_trans_tcp: problem connecting socket to %s\n", | ||
229 | addr); | ||
230 | return ret; | ||
231 | } | ||
174 | 232 | ||
175 | ts = trans->priv; | 233 | return v9fs_socket_open(v9ses, csocket); |
176 | if (trans->status != Connected || !ts) | 234 | } |
177 | return -EIO; | ||
178 | 235 | ||
179 | oldfs = get_fs(); | 236 | static int |
180 | set_fs(get_ds()); | 237 | v9fs_unix_init(struct v9fs_session_info *v9ses, const char *addr, char *data) |
238 | { | ||
239 | int ret; | ||
240 | struct socket *csocket; | ||
241 | struct sockaddr_un sun_server; | ||
242 | |||
243 | if (strlen(addr) > UNIX_PATH_MAX) { | ||
244 | eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n", | ||
245 | addr); | ||
246 | return -ENAMETOOLONG; | ||
247 | } | ||
181 | 248 | ||
182 | if (!ts->in_file->f_op || !ts->in_file->f_op->poll) { | 249 | sun_server.sun_family = PF_UNIX; |
183 | ret = -EIO; | 250 | strcpy(sun_server.sun_path, addr); |
184 | goto end; | 251 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); |
252 | ret = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, | ||
253 | sizeof(struct sockaddr_un) - 1, 0); | ||
254 | if (ret < 0) { | ||
255 | eprintk(KERN_ERR, | ||
256 | "v9fs_trans_unix: problem connecting socket: %s: %d\n", | ||
257 | addr, ret); | ||
258 | return ret; | ||
185 | } | 259 | } |
186 | 260 | ||
187 | ret = ts->in_file->f_op->poll(ts->in_file, pt); | 261 | return v9fs_socket_open(v9ses, csocket); |
262 | } | ||
188 | 263 | ||
189 | if (ts->out_file != ts->in_file) { | 264 | /** |
190 | if (!ts->out_file->f_op || !ts->out_file->f_op->poll) { | 265 | * v9fs_sock_close - shutdown socket |
191 | ret = -EIO; | 266 | * @trans: private socket structure |
192 | goto end; | 267 | * |
193 | } | 268 | */ |
269 | static void v9fs_fd_close(struct v9fs_transport *trans) | ||
270 | { | ||
271 | struct v9fs_trans_fd *ts; | ||
194 | 272 | ||
195 | n = ts->out_file->f_op->poll(ts->out_file, pt); | 273 | if (!trans) |
274 | return; | ||
196 | 275 | ||
197 | ret &= ~POLLOUT; | 276 | ts = xchg(&trans->priv, NULL); |
198 | n &= ~POLLIN; | ||
199 | 277 | ||
200 | ret |= n; | 278 | if (!ts) |
201 | } | 279 | return; |
202 | 280 | ||
203 | end: | 281 | trans->status = Disconnected; |
204 | set_fs(oldfs); | 282 | if (ts->rd) |
205 | return ret; | 283 | fput(ts->rd); |
284 | if (ts->wr) | ||
285 | fput(ts->wr); | ||
286 | kfree(ts); | ||
206 | } | 287 | } |
207 | 288 | ||
208 | |||
209 | struct v9fs_transport v9fs_trans_fd = { | 289 | struct v9fs_transport v9fs_trans_fd = { |
210 | .init = v9fs_fd_init, | 290 | .init = v9fs_fd_init, |
211 | .write = v9fs_fd_send, | 291 | .write = v9fs_fd_write, |
212 | .read = v9fs_fd_recv, | 292 | .read = v9fs_fd_read, |
213 | .close = v9fs_fd_close, | 293 | .close = v9fs_fd_close, |
214 | .poll = v9fs_fd_poll, | 294 | .poll = v9fs_fd_poll, |
215 | }; | 295 | }; |
216 | 296 | ||
297 | struct v9fs_transport v9fs_trans_tcp = { | ||
298 | .init = v9fs_tcp_init, | ||
299 | .write = v9fs_fd_write, | ||
300 | .read = v9fs_fd_read, | ||
301 | .close = v9fs_fd_close, | ||
302 | .poll = v9fs_fd_poll, | ||
303 | }; | ||
304 | |||
305 | struct v9fs_transport v9fs_trans_unix = { | ||
306 | .init = v9fs_unix_init, | ||
307 | .write = v9fs_fd_write, | ||
308 | .read = v9fs_fd_read, | ||
309 | .close = v9fs_fd_close, | ||
310 | .poll = v9fs_fd_poll, | ||
311 | }; | ||