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