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 | |
| 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>
| -rw-r--r-- | fs/9p/Makefile | 1 | ||||
| -rw-r--r-- | fs/9p/trans_fd.c | 297 | ||||
| -rw-r--r-- | fs/9p/trans_sock.c | 334 |
3 files changed, 196 insertions, 436 deletions
diff --git a/fs/9p/Makefile b/fs/9p/Makefile index 2f4ce43f7b6c..5db5af9dbf25 100644 --- a/fs/9p/Makefile +++ b/fs/9p/Makefile | |||
| @@ -2,7 +2,6 @@ obj-$(CONFIG_9P_FS) := 9p2000.o | |||
| 2 | 2 | ||
| 3 | 9p2000-objs := \ | 3 | 9p2000-objs := \ |
| 4 | trans_fd.o \ | 4 | trans_fd.o \ |
| 5 | trans_sock.o \ | ||
| 6 | mux.o \ | 5 | mux.o \ |
| 7 | 9p.o \ | 6 | 9p.o \ |
| 8 | conv.o \ | 7 | conv.o \ |
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 | }; | ||
diff --git a/fs/9p/trans_sock.c b/fs/9p/trans_sock.c deleted file mode 100644 index 44e830697acb..000000000000 --- a/fs/9p/trans_sock.c +++ /dev/null | |||
| @@ -1,334 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * linux/fs/9p/trans_socket.c | ||
| 3 | * | ||
| 4 | * Socket Transport Layer | ||
| 5 | * | ||
| 6 | * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net> | ||
| 7 | * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> | ||
| 8 | * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com> | ||
| 9 | * Copyright (C) 1995, 1996 by Olaf Kirch <okir@monad.swb.de> | ||
| 10 | * | ||
| 11 | * This program is free software; you can redistribute it and/or modify | ||
| 12 | * it under the terms of the GNU General Public License as published by | ||
| 13 | * the Free Software Foundation; either version 2 of the License, or | ||
| 14 | * (at your option) any later version. | ||
| 15 | * | ||
| 16 | * This program is distributed in the hope that it will be useful, | ||
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 19 | * GNU General Public License for more details. | ||
| 20 | * | ||
| 21 | * You should have received a copy of the GNU General Public License | ||
| 22 | * along with this program; if not, write to: | ||
| 23 | * Free Software Foundation | ||
| 24 | * 51 Franklin Street, Fifth Floor | ||
| 25 | * Boston, MA 02111-1301 USA | ||
| 26 | * | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include <linux/config.h> | ||
| 30 | #include <linux/in.h> | ||
| 31 | #include <linux/module.h> | ||
| 32 | #include <linux/net.h> | ||
| 33 | #include <linux/ipv6.h> | ||
| 34 | #include <linux/errno.h> | ||
| 35 | #include <linux/kernel.h> | ||
| 36 | #include <linux/un.h> | ||
| 37 | #include <asm/uaccess.h> | ||
| 38 | #include <linux/inet.h> | ||
| 39 | #include <linux/idr.h> | ||
| 40 | #include <linux/file.h> | ||
| 41 | |||
| 42 | #include "debug.h" | ||
| 43 | #include "v9fs.h" | ||
| 44 | #include "transport.h" | ||
| 45 | |||
| 46 | #define V9FS_PORT 564 | ||
| 47 | |||
| 48 | struct v9fs_trans_sock { | ||
| 49 | struct socket *s; | ||
| 50 | struct file *filp; | ||
| 51 | }; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * v9fs_sock_recv - receive from a socket | ||
| 55 | * @v9ses: session information | ||
| 56 | * @v: buffer to receive data into | ||
| 57 | * @len: size of receive buffer | ||
| 58 | * | ||
| 59 | */ | ||
| 60 | |||
| 61 | static int v9fs_sock_recv(struct v9fs_transport *trans, void *v, int len) | ||
| 62 | { | ||
| 63 | int ret; | ||
| 64 | struct v9fs_trans_sock *ts; | ||
| 65 | |||
| 66 | if (!trans || trans->status == Disconnected) { | ||
| 67 | dprintk(DEBUG_ERROR, "disconnected ...\n"); | ||
| 68 | return -EREMOTEIO; | ||
| 69 | } | ||
| 70 | |||
| 71 | ts = trans->priv; | ||
| 72 | |||
| 73 | if (!(ts->filp->f_flags & O_NONBLOCK)) | ||
| 74 | dprintk(DEBUG_ERROR, "blocking read ...\n"); | ||
| 75 | |||
| 76 | ret = kernel_read(ts->filp, ts->filp->f_pos, v, len); | ||
| 77 | if (ret <= 0) { | ||
| 78 | if (ret != -ERESTARTSYS && ret != -EAGAIN) | ||
| 79 | trans->status = Disconnected; | ||
| 80 | } | ||
| 81 | |||
| 82 | return ret; | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * v9fs_sock_send - send to a socket | ||
| 87 | * @v9ses: session information | ||
| 88 | * @v: buffer to send data from | ||
| 89 | * @len: size of send buffer | ||
| 90 | * | ||
| 91 | */ | ||
| 92 | |||
| 93 | static int v9fs_sock_send(struct v9fs_transport *trans, void *v, int len) | ||
| 94 | { | ||
| 95 | int ret; | ||
| 96 | mm_segment_t oldfs; | ||
| 97 | struct v9fs_trans_sock *ts; | ||
| 98 | |||
| 99 | if (!trans || trans->status == Disconnected) { | ||
| 100 | dprintk(DEBUG_ERROR, "disconnected ...\n"); | ||
| 101 | return -EREMOTEIO; | ||
| 102 | } | ||
| 103 | |||
| 104 | ts = trans->priv; | ||
| 105 | if (!ts) { | ||
| 106 | dprintk(DEBUG_ERROR, "no transport ...\n"); | ||
| 107 | return -EREMOTEIO; | ||
| 108 | } | ||
| 109 | |||
| 110 | if (!(ts->filp->f_flags & O_NONBLOCK)) | ||
| 111 | dprintk(DEBUG_ERROR, "blocking write ...\n"); | ||
| 112 | |||
| 113 | oldfs = get_fs(); | ||
| 114 | set_fs(get_ds()); | ||
| 115 | ret = vfs_write(ts->filp, (void __user *)v, len, &ts->filp->f_pos); | ||
| 116 | set_fs(oldfs); | ||
| 117 | |||
| 118 | if (ret < 0) { | ||
| 119 | if (ret != -ERESTARTSYS) | ||
| 120 | trans->status = Disconnected; | ||
| 121 | } | ||
| 122 | |||
| 123 | return ret; | ||
| 124 | } | ||
| 125 | |||
| 126 | static unsigned int v9fs_sock_poll(struct v9fs_transport *trans, | ||
| 127 | struct poll_table_struct *pt) { | ||
| 128 | |||
| 129 | int ret; | ||
| 130 | struct v9fs_trans_sock *ts; | ||
| 131 | mm_segment_t oldfs; | ||
| 132 | |||
| 133 | if (!trans) { | ||
| 134 | dprintk(DEBUG_ERROR, "no transport\n"); | ||
| 135 | return -EIO; | ||
| 136 | } | ||
| 137 | |||
| 138 | ts = trans->priv; | ||
| 139 | if (trans->status != Connected || !ts) { | ||
| 140 | dprintk(DEBUG_ERROR, "transport disconnected: %d\n", trans->status); | ||
| 141 | return -EIO; | ||
| 142 | } | ||
| 143 | |||
| 144 | oldfs = get_fs(); | ||
| 145 | set_fs(get_ds()); | ||
| 146 | |||
| 147 | if (!ts->filp->f_op || !ts->filp->f_op->poll) { | ||
| 148 | dprintk(DEBUG_ERROR, "no poll operation\n"); | ||
| 149 | ret = -EIO; | ||
| 150 | goto end; | ||
| 151 | } | ||
| 152 | |||
| 153 | ret = ts->filp->f_op->poll(ts->filp, pt); | ||
| 154 | |||
| 155 | end: | ||
| 156 | set_fs(oldfs); | ||
| 157 | return ret; | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | /** | ||
| 162 | * v9fs_tcp_init - initialize TCP socket | ||
| 163 | * @v9ses: session information | ||
| 164 | * @addr: address of server to mount | ||
| 165 | * @data: mount options | ||
| 166 | * | ||
| 167 | */ | ||
| 168 | |||
| 169 | static int | ||
| 170 | v9fs_tcp_init(struct v9fs_session_info *v9ses, const char *addr, char *data) | ||
| 171 | { | ||
| 172 | struct socket *csocket = NULL; | ||
| 173 | struct sockaddr_in sin_server; | ||
| 174 | int rc = 0; | ||
| 175 | struct v9fs_trans_sock *ts = NULL; | ||
| 176 | struct v9fs_transport *trans = v9ses->transport; | ||
| 177 | int fd; | ||
| 178 | |||
| 179 | trans->status = Disconnected; | ||
| 180 | |||
| 181 | ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL); | ||
| 182 | |||
| 183 | if (!ts) | ||
| 184 | return -ENOMEM; | ||
| 185 | |||
| 186 | trans->priv = ts; | ||
| 187 | ts->s = NULL; | ||
| 188 | ts->filp = NULL; | ||
| 189 | |||
| 190 | if (!addr) | ||
| 191 | return -EINVAL; | ||
| 192 | |||
| 193 | dprintk(DEBUG_TRANS, "Connecting to %s\n", addr); | ||
| 194 | |||
| 195 | sin_server.sin_family = AF_INET; | ||
| 196 | sin_server.sin_addr.s_addr = in_aton(addr); | ||
| 197 | sin_server.sin_port = htons(v9ses->port); | ||
| 198 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); | ||
| 199 | rc = csocket->ops->connect(csocket, | ||
| 200 | (struct sockaddr *)&sin_server, | ||
| 201 | sizeof(struct sockaddr_in), 0); | ||
| 202 | if (rc < 0) { | ||
| 203 | eprintk(KERN_ERR, | ||
| 204 | "v9fs_trans_tcp: problem connecting socket to %s\n", | ||
| 205 | addr); | ||
| 206 | return rc; | ||
| 207 | } | ||
| 208 | csocket->sk->sk_allocation = GFP_NOIO; | ||
| 209 | |||
| 210 | fd = sock_map_fd(csocket); | ||
| 211 | if (fd < 0) { | ||
| 212 | sock_release(csocket); | ||
| 213 | kfree(ts); | ||
| 214 | trans->priv = NULL; | ||
| 215 | return fd; | ||
| 216 | } | ||
| 217 | |||
| 218 | ts->s = csocket; | ||
| 219 | ts->filp = fget(fd); | ||
| 220 | ts->filp->f_flags |= O_NONBLOCK; | ||
| 221 | trans->status = Connected; | ||
| 222 | |||
| 223 | return 0; | ||
| 224 | } | ||
| 225 | |||
| 226 | /** | ||
| 227 | * v9fs_unix_init - initialize UNIX domain socket | ||
| 228 | * @v9ses: session information | ||
| 229 | * @dev_name: path to named pipe | ||
| 230 | * @data: mount options | ||
| 231 | * | ||
| 232 | */ | ||
| 233 | |||
| 234 | static int | ||
| 235 | v9fs_unix_init(struct v9fs_session_info *v9ses, const char *dev_name, | ||
| 236 | char *data) | ||
| 237 | { | ||
| 238 | int rc, fd; | ||
| 239 | struct socket *csocket; | ||
| 240 | struct sockaddr_un sun_server; | ||
| 241 | struct v9fs_transport *trans; | ||
| 242 | struct v9fs_trans_sock *ts; | ||
| 243 | |||
| 244 | rc = 0; | ||
| 245 | csocket = NULL; | ||
| 246 | trans = v9ses->transport; | ||
| 247 | |||
| 248 | trans->status = Disconnected; | ||
| 249 | |||
| 250 | if (strlen(dev_name) > UNIX_PATH_MAX) { | ||
| 251 | eprintk(KERN_ERR, "v9fs_trans_unix: address too long: %s\n", | ||
| 252 | dev_name); | ||
| 253 | return -ENOMEM; | ||
| 254 | } | ||
| 255 | |||
| 256 | ts = kmalloc(sizeof(struct v9fs_trans_sock), GFP_KERNEL); | ||
| 257 | if (!ts) | ||
| 258 | return -ENOMEM; | ||
| 259 | |||
| 260 | trans->priv = ts; | ||
| 261 | ts->s = NULL; | ||
| 262 | ts->filp = NULL; | ||
| 263 | |||
| 264 | sun_server.sun_family = PF_UNIX; | ||
| 265 | strcpy(sun_server.sun_path, dev_name); | ||
| 266 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); | ||
| 267 | rc = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, | ||
| 268 | sizeof(struct sockaddr_un) - 1, 0); /* -1 *is* important */ | ||
| 269 | if (rc < 0) { | ||
| 270 | eprintk(KERN_ERR, | ||
| 271 | "v9fs_trans_unix: problem connecting socket: %s: %d\n", | ||
| 272 | dev_name, rc); | ||
| 273 | return rc; | ||
| 274 | } | ||
| 275 | csocket->sk->sk_allocation = GFP_NOIO; | ||
| 276 | |||
| 277 | fd = sock_map_fd(csocket); | ||
| 278 | if (fd < 0) { | ||
| 279 | sock_release(csocket); | ||
| 280 | kfree(ts); | ||
| 281 | trans->priv = NULL; | ||
| 282 | return fd; | ||
| 283 | } | ||
| 284 | |||
| 285 | ts->s = csocket; | ||
| 286 | ts->filp = fget(fd); | ||
| 287 | ts->filp->f_flags |= O_NONBLOCK; | ||
| 288 | trans->status = Connected; | ||
| 289 | |||
| 290 | return 0; | ||
| 291 | } | ||
| 292 | |||
| 293 | /** | ||
| 294 | * v9fs_sock_close - shutdown socket | ||
| 295 | * @trans: private socket structure | ||
| 296 | * | ||
| 297 | */ | ||
| 298 | |||
| 299 | static void v9fs_sock_close(struct v9fs_transport *trans) | ||
| 300 | { | ||
| 301 | struct v9fs_trans_sock *ts; | ||
| 302 | |||
| 303 | if (!trans) | ||
| 304 | return; | ||
| 305 | |||
| 306 | ts = trans->priv; | ||
| 307 | |||
| 308 | if ((ts) && (ts->filp)) { | ||
| 309 | fput(ts->filp); | ||
| 310 | ts->filp = NULL; | ||
| 311 | ts->s = NULL; | ||
| 312 | trans->status = Disconnected; | ||
| 313 | } | ||
| 314 | |||
| 315 | kfree(ts); | ||
| 316 | |||
| 317 | trans->priv = NULL; | ||
| 318 | } | ||
| 319 | |||
| 320 | struct v9fs_transport v9fs_trans_tcp = { | ||
| 321 | .init = v9fs_tcp_init, | ||
| 322 | .write = v9fs_sock_send, | ||
| 323 | .read = v9fs_sock_recv, | ||
| 324 | .close = v9fs_sock_close, | ||
| 325 | .poll = v9fs_sock_poll, | ||
| 326 | }; | ||
| 327 | |||
| 328 | struct v9fs_transport v9fs_trans_unix = { | ||
| 329 | .init = v9fs_unix_init, | ||
| 330 | .write = v9fs_sock_send, | ||
| 331 | .read = v9fs_sock_recv, | ||
| 332 | .close = v9fs_sock_close, | ||
| 333 | .poll = v9fs_sock_poll, | ||
| 334 | }; | ||
