Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/9p/trans_fd.c |
| 3 | * |
| 4 | * Fd transport layer. Includes deprecated socket layer. |
| 5 | * |
| 6 | * Copyright (C) 2006 by Russ Cox <rsc@swtch.com> |
| 7 | * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net> |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 8 | * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 9 | * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com> |
| 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 version 2 |
| 13 | * as published by the Free Software Foundation. |
| 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 | |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/net.h> |
| 31 | #include <linux/ipv6.h> |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 32 | #include <linux/kthread.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 33 | #include <linux/errno.h> |
| 34 | #include <linux/kernel.h> |
| 35 | #include <linux/un.h> |
| 36 | #include <linux/uaccess.h> |
| 37 | #include <linux/inet.h> |
| 38 | #include <linux/idr.h> |
| 39 | #include <linux/file.h> |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 40 | #include <linux/parser.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 41 | #include <net/9p/9p.h> |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 42 | #include <net/9p/client.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 43 | #include <net/9p/transport.h> |
| 44 | |
| 45 | #define P9_PORT 564 |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 46 | #define MAX_SOCK_BUF (64*1024) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 47 | #define MAXPOLLWADDR 2 |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 48 | |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 49 | /** |
| 50 | * struct p9_fd_opts - per-transport options |
| 51 | * @rfd: file descriptor for reading (trans=fd) |
| 52 | * @wfd: file descriptor for writing (trans=fd) |
| 53 | * @port: port to connect to (trans=tcp) |
| 54 | * |
| 55 | */ |
| 56 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 57 | struct p9_fd_opts { |
| 58 | int rfd; |
| 59 | int wfd; |
| 60 | u16 port; |
| 61 | }; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 62 | |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 63 | /** |
| 64 | * struct p9_trans_fd - transport state |
| 65 | * @rd: reference to file to read from |
| 66 | * @wr: reference of file to write to |
| 67 | * @conn: connection state reference |
| 68 | * |
| 69 | */ |
| 70 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 71 | struct p9_trans_fd { |
| 72 | struct file *rd; |
| 73 | struct file *wr; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 74 | struct p9_conn *conn; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 77 | /* |
| 78 | * Option Parsing (code inspired by NFS code) |
| 79 | * - a little lazy - parse all fd-transport options |
| 80 | */ |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 81 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 82 | enum { |
| 83 | /* Options that take integer arguments */ |
Latchesar Ionkov | 5576269 | 2007-11-06 08:02:53 -0600 | [diff] [blame] | 84 | Opt_port, Opt_rfdno, Opt_wfdno, Opt_err, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 85 | }; |
| 86 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 87 | static const match_table_t tokens = { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 88 | {Opt_port, "port=%u"}, |
| 89 | {Opt_rfdno, "rfdno=%u"}, |
| 90 | {Opt_wfdno, "wfdno=%u"}, |
Latchesar Ionkov | 5576269 | 2007-11-06 08:02:53 -0600 | [diff] [blame] | 91 | {Opt_err, NULL}, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 94 | enum { |
| 95 | Rworksched = 1, /* read work scheduled or running */ |
| 96 | Rpending = 2, /* can read */ |
| 97 | Wworksched = 4, /* write work scheduled or running */ |
| 98 | Wpending = 8, /* can write */ |
| 99 | }; |
| 100 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 101 | struct p9_poll_wait { |
| 102 | struct p9_conn *conn; |
| 103 | wait_queue_t wait; |
| 104 | wait_queue_head_t *wait_addr; |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * struct p9_conn - fd mux connection state information |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 109 | * @mux_list: list link for mux to manage multiple connections (?) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 110 | * @client: reference to client instance for this connection |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 111 | * @err: error state |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 112 | * @req_list: accounting for requests which have been sent |
| 113 | * @unsent_req_list: accounting for requests that haven't been sent |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 114 | * @req: current request being processed (if any) |
| 115 | * @tmp_buf: temporary buffer to read in header |
| 116 | * @rsize: amount to read for current frame |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 117 | * @rpos: read position in current frame |
| 118 | * @rbuf: current read buffer |
| 119 | * @wpos: write position for current frame |
| 120 | * @wsize: amount of data to write for current frame |
| 121 | * @wbuf: current write buffer |
| 122 | * @poll_wait: array of wait_q's for various worker threads |
| 123 | * @poll_waddr: ???? |
| 124 | * @pt: poll state |
| 125 | * @rq: current read work |
| 126 | * @wq: current write work |
| 127 | * @wsched: ???? |
| 128 | * |
| 129 | */ |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 130 | |
| 131 | struct p9_conn { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 132 | struct list_head mux_list; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 133 | struct p9_client *client; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 134 | int err; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 135 | struct list_head req_list; |
| 136 | struct list_head unsent_req_list; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 137 | struct p9_req_t *req; |
| 138 | char tmp_buf[7]; |
| 139 | int rsize; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 140 | int rpos; |
| 141 | char *rbuf; |
| 142 | int wpos; |
| 143 | int wsize; |
| 144 | char *wbuf; |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 145 | struct list_head poll_pending_link; |
| 146 | struct p9_poll_wait poll_wait[MAXPOLLWADDR]; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 147 | poll_table pt; |
| 148 | struct work_struct rq; |
| 149 | struct work_struct wq; |
| 150 | unsigned long wsched; |
| 151 | }; |
| 152 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 153 | static DEFINE_SPINLOCK(p9_poll_lock); |
| 154 | static LIST_HEAD(p9_poll_pending_list); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 155 | static struct workqueue_struct *p9_mux_wq; |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 156 | static struct task_struct *p9_poll_task; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 157 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 158 | static void p9_mux_poll_stop(struct p9_conn *m) |
| 159 | { |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 160 | unsigned long flags; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 161 | int i; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 162 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 163 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 164 | struct p9_poll_wait *pwait = &m->poll_wait[i]; |
| 165 | |
| 166 | if (pwait->wait_addr) { |
| 167 | remove_wait_queue(pwait->wait_addr, &pwait->wait); |
| 168 | pwait->wait_addr = NULL; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 169 | } |
| 170 | } |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 171 | |
| 172 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 173 | list_del_init(&m->poll_pending_link); |
| 174 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 178 | * p9_conn_cancel - cancel all pending requests with error |
| 179 | * @m: mux data |
| 180 | * @err: error code |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 181 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 182 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 183 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 184 | void p9_conn_cancel(struct p9_conn *m, int err) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 185 | { |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 186 | struct p9_req_t *req, *rtmp; |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 187 | unsigned long flags; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 188 | LIST_HEAD(cancel_list); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 189 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 190 | P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err); |
| 191 | m->err = err; |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 192 | spin_lock_irqsave(&m->client->lock, flags); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 193 | list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) { |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 194 | req->status = REQ_STATUS_ERROR; |
| 195 | if (!req->t_err) |
| 196 | req->t_err = err; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 197 | list_move(&req->req_list, &cancel_list); |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 198 | } |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 199 | list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) { |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 200 | req->status = REQ_STATUS_ERROR; |
| 201 | if (!req->t_err) |
| 202 | req->t_err = err; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 203 | list_move(&req->req_list, &cancel_list); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 204 | } |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 205 | spin_unlock_irqrestore(&m->client->lock, flags); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 206 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 207 | list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) { |
| 208 | list_del(&req->req_list); |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 209 | P9_DPRINTK(P9_DEBUG_ERROR, "call back req %p\n", req); |
| 210 | p9_client_cb(m->client, req); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 214 | static unsigned int |
| 215 | p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt) |
| 216 | { |
| 217 | int ret, n; |
| 218 | struct p9_trans_fd *ts = NULL; |
| 219 | |
| 220 | if (client && client->status == Connected) |
| 221 | ts = client->trans; |
| 222 | |
| 223 | if (!ts) |
| 224 | return -EREMOTEIO; |
| 225 | |
| 226 | if (!ts->rd->f_op || !ts->rd->f_op->poll) |
| 227 | return -EIO; |
| 228 | |
| 229 | if (!ts->wr->f_op || !ts->wr->f_op->poll) |
| 230 | return -EIO; |
| 231 | |
| 232 | ret = ts->rd->f_op->poll(ts->rd, pt); |
| 233 | if (ret < 0) |
| 234 | return ret; |
| 235 | |
| 236 | if (ts->rd != ts->wr) { |
| 237 | n = ts->wr->f_op->poll(ts->wr, pt); |
| 238 | if (n < 0) |
| 239 | return n; |
| 240 | ret = (ret & ~POLLOUT) | (n & ~POLLIN); |
| 241 | } |
| 242 | |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * p9_fd_read- read from a fd |
| 248 | * @client: client instance |
| 249 | * @v: buffer to receive data into |
| 250 | * @len: size of receive buffer |
| 251 | * |
| 252 | */ |
| 253 | |
| 254 | static int p9_fd_read(struct p9_client *client, void *v, int len) |
| 255 | { |
| 256 | int ret; |
| 257 | struct p9_trans_fd *ts = NULL; |
| 258 | |
| 259 | if (client && client->status != Disconnected) |
| 260 | ts = client->trans; |
| 261 | |
| 262 | if (!ts) |
| 263 | return -EREMOTEIO; |
| 264 | |
| 265 | if (!(ts->rd->f_flags & O_NONBLOCK)) |
| 266 | P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n"); |
| 267 | |
| 268 | ret = kernel_read(ts->rd, ts->rd->f_pos, v, len); |
| 269 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
| 270 | client->status = Disconnected; |
| 271 | return ret; |
| 272 | } |
| 273 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 274 | /** |
| 275 | * p9_read_work - called when there is some data to be read from a transport |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 276 | * @work: container of work to be done |
| 277 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 278 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 279 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 280 | static void p9_read_work(struct work_struct *work) |
| 281 | { |
| 282 | int n, err; |
| 283 | struct p9_conn *m; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 284 | |
| 285 | m = container_of(work, struct p9_conn, rq); |
| 286 | |
| 287 | if (m->err < 0) |
| 288 | return; |
| 289 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 290 | P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos); |
| 291 | |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 292 | if (!m->rbuf) { |
| 293 | m->rbuf = m->tmp_buf; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 294 | m->rpos = 0; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 295 | m->rsize = 7; /* start by reading header */ |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | clear_bit(Rpending, &m->wsched); |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 299 | P9_DPRINTK(P9_DEBUG_MUX, "read mux %p pos %d size: %d = %d\n", m, |
| 300 | m->rpos, m->rsize, m->rsize-m->rpos); |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 301 | err = p9_fd_read(m->client, m->rbuf + m->rpos, |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 302 | m->rsize - m->rpos); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 303 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err); |
| 304 | if (err == -EAGAIN) { |
| 305 | clear_bit(Rworksched, &m->wsched); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | if (err <= 0) |
| 310 | goto error; |
| 311 | |
| 312 | m->rpos += err; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 313 | |
| 314 | if ((!m->req) && (m->rpos == m->rsize)) { /* header read in */ |
| 315 | u16 tag; |
| 316 | P9_DPRINTK(P9_DEBUG_MUX, "got new header\n"); |
| 317 | |
| 318 | n = le32_to_cpu(*(__le32 *) m->rbuf); /* read packet size */ |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 319 | if (n >= m->client->msize) { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 320 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 321 | "requested packet size too big: %d\n", n); |
| 322 | err = -EIO; |
| 323 | goto error; |
| 324 | } |
| 325 | |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 326 | tag = le16_to_cpu(*(__le16 *) (m->rbuf+5)); /* read tag */ |
| 327 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p pkt: size: %d bytes tag: %d\n", |
| 328 | m, n, tag); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 329 | |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 330 | m->req = p9_tag_lookup(m->client, tag); |
| 331 | if (!m->req) { |
| 332 | P9_DPRINTK(P9_DEBUG_ERROR, "Unexpected packet tag %d\n", |
| 333 | tag); |
| 334 | err = -EIO; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 335 | goto error; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (m->req->rc == NULL) { |
| 339 | m->req->rc = kmalloc(sizeof(struct p9_fcall) + |
| 340 | m->client->msize, GFP_KERNEL); |
| 341 | if (!m->req->rc) { |
| 342 | m->req = NULL; |
| 343 | err = -ENOMEM; |
| 344 | goto error; |
| 345 | } |
| 346 | } |
| 347 | m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall); |
| 348 | memcpy(m->rbuf, m->tmp_buf, m->rsize); |
| 349 | m->rsize = n; |
| 350 | } |
| 351 | |
| 352 | /* not an else because some packets (like clunk) have no payload */ |
| 353 | if ((m->req) && (m->rpos == m->rsize)) { /* packet is read in */ |
| 354 | P9_DPRINTK(P9_DEBUG_MUX, "got new packet\n"); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 355 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 356 | list_del(&m->req->req_list); |
| 357 | p9_client_cb(m->client, m->req); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 358 | |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 359 | m->rbuf = NULL; |
| 360 | m->rpos = 0; |
| 361 | m->rsize = 0; |
Eric Van Hensbergen | 1b0a763 | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 362 | m->req = NULL; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | if (!list_empty(&m->req_list)) { |
| 366 | if (test_and_clear_bit(Rpending, &m->wsched)) |
| 367 | n = POLLIN; |
| 368 | else |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 369 | n = p9_fd_poll(m->client, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 370 | |
| 371 | if (n & POLLIN) { |
| 372 | P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m); |
| 373 | queue_work(p9_mux_wq, &m->rq); |
| 374 | } else |
| 375 | clear_bit(Rworksched, &m->wsched); |
| 376 | } else |
| 377 | clear_bit(Rworksched, &m->wsched); |
| 378 | |
| 379 | return; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 380 | error: |
| 381 | p9_conn_cancel(m, err); |
| 382 | clear_bit(Rworksched, &m->wsched); |
| 383 | } |
| 384 | |
| 385 | /** |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 386 | * p9_fd_write - write to a socket |
| 387 | * @client: client instance |
| 388 | * @v: buffer to send data from |
| 389 | * @len: size of send buffer |
| 390 | * |
| 391 | */ |
| 392 | |
| 393 | static int p9_fd_write(struct p9_client *client, void *v, int len) |
| 394 | { |
| 395 | int ret; |
| 396 | mm_segment_t oldfs; |
| 397 | struct p9_trans_fd *ts = NULL; |
| 398 | |
| 399 | if (client && client->status != Disconnected) |
| 400 | ts = client->trans; |
| 401 | |
| 402 | if (!ts) |
| 403 | return -EREMOTEIO; |
| 404 | |
| 405 | if (!(ts->wr->f_flags & O_NONBLOCK)) |
| 406 | P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n"); |
| 407 | |
| 408 | oldfs = get_fs(); |
| 409 | set_fs(get_ds()); |
| 410 | /* The cast to a user pointer is valid due to the set_fs() */ |
| 411 | ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos); |
| 412 | set_fs(oldfs); |
| 413 | |
| 414 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
| 415 | client->status = Disconnected; |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * p9_write_work - called when a transport can send some data |
| 421 | * @work: container for work to be done |
| 422 | * |
| 423 | */ |
| 424 | |
| 425 | static void p9_write_work(struct work_struct *work) |
| 426 | { |
| 427 | int n, err; |
| 428 | struct p9_conn *m; |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 429 | struct p9_req_t *req; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 430 | |
| 431 | m = container_of(work, struct p9_conn, wq); |
| 432 | |
| 433 | if (m->err < 0) { |
| 434 | clear_bit(Wworksched, &m->wsched); |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | if (!m->wsize) { |
| 439 | if (list_empty(&m->unsent_req_list)) { |
| 440 | clear_bit(Wworksched, &m->wsched); |
| 441 | return; |
| 442 | } |
| 443 | |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 444 | spin_lock(&m->client->lock); |
| 445 | req = list_entry(m->unsent_req_list.next, struct p9_req_t, |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 446 | req_list); |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 447 | req->status = REQ_STATUS_SENT; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 448 | list_move_tail(&req->req_list, &m->req_list); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 449 | |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 450 | m->wbuf = req->tc->sdata; |
| 451 | m->wsize = req->tc->size; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 452 | m->wpos = 0; |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 453 | spin_unlock(&m->client->lock); |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, |
| 457 | m->wsize); |
| 458 | clear_bit(Wpending, &m->wsched); |
| 459 | err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos); |
| 460 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err); |
| 461 | if (err == -EAGAIN) { |
| 462 | clear_bit(Wworksched, &m->wsched); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | if (err < 0) |
| 467 | goto error; |
| 468 | else if (err == 0) { |
| 469 | err = -EREMOTEIO; |
| 470 | goto error; |
| 471 | } |
| 472 | |
| 473 | m->wpos += err; |
| 474 | if (m->wpos == m->wsize) |
| 475 | m->wpos = m->wsize = 0; |
| 476 | |
| 477 | if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) { |
| 478 | if (test_and_clear_bit(Wpending, &m->wsched)) |
| 479 | n = POLLOUT; |
| 480 | else |
| 481 | n = p9_fd_poll(m->client, NULL); |
| 482 | |
| 483 | if (n & POLLOUT) { |
| 484 | P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m); |
| 485 | queue_work(p9_mux_wq, &m->wq); |
| 486 | } else |
| 487 | clear_bit(Wworksched, &m->wsched); |
| 488 | } else |
| 489 | clear_bit(Wworksched, &m->wsched); |
| 490 | |
| 491 | return; |
| 492 | |
| 493 | error: |
| 494 | p9_conn_cancel(m, err); |
| 495 | clear_bit(Wworksched, &m->wsched); |
| 496 | } |
| 497 | |
| 498 | static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key) |
| 499 | { |
| 500 | struct p9_poll_wait *pwait = |
| 501 | container_of(wait, struct p9_poll_wait, wait); |
| 502 | struct p9_conn *m = pwait->conn; |
| 503 | unsigned long flags; |
| 504 | DECLARE_WAITQUEUE(dummy_wait, p9_poll_task); |
| 505 | |
| 506 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 507 | if (list_empty(&m->poll_pending_link)) |
| 508 | list_add_tail(&m->poll_pending_link, &p9_poll_pending_list); |
| 509 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
| 510 | |
| 511 | /* perform the default wake up operation */ |
| 512 | return default_wake_function(&dummy_wait, mode, sync, key); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * p9_pollwait - add poll task to the wait queue |
| 517 | * @filp: file pointer being polled |
| 518 | * @wait_address: wait_q to block on |
| 519 | * @p: poll state |
| 520 | * |
| 521 | * called by files poll operation to add v9fs-poll task to files wait queue |
| 522 | */ |
| 523 | |
| 524 | static void |
| 525 | p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p) |
| 526 | { |
| 527 | struct p9_conn *m = container_of(p, struct p9_conn, pt); |
| 528 | struct p9_poll_wait *pwait = NULL; |
| 529 | int i; |
| 530 | |
| 531 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 532 | if (m->poll_wait[i].wait_addr == NULL) { |
| 533 | pwait = &m->poll_wait[i]; |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if (!pwait) { |
| 539 | P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n"); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | if (!wait_address) { |
| 544 | P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n"); |
| 545 | pwait->wait_addr = ERR_PTR(-EIO); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | pwait->conn = m; |
| 550 | pwait->wait_addr = wait_address; |
| 551 | init_waitqueue_func_entry(&pwait->wait, p9_pollwake); |
| 552 | add_wait_queue(wait_address, &pwait->wait); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * p9_conn_create - allocate and initialize the per-session mux data |
| 557 | * @client: client instance |
| 558 | * |
| 559 | * Note: Creates the polling task if this is the first session. |
| 560 | */ |
| 561 | |
| 562 | static struct p9_conn *p9_conn_create(struct p9_client *client) |
| 563 | { |
| 564 | int i, n; |
| 565 | struct p9_conn *m; |
| 566 | |
| 567 | P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize); |
| 568 | m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL); |
| 569 | if (!m) |
| 570 | return ERR_PTR(-ENOMEM); |
| 571 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 572 | INIT_LIST_HEAD(&m->mux_list); |
| 573 | m->client = client; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 574 | |
| 575 | INIT_LIST_HEAD(&m->req_list); |
| 576 | INIT_LIST_HEAD(&m->unsent_req_list); |
| 577 | INIT_WORK(&m->rq, p9_read_work); |
| 578 | INIT_WORK(&m->wq, p9_write_work); |
| 579 | INIT_LIST_HEAD(&m->poll_pending_link); |
| 580 | init_poll_funcptr(&m->pt, p9_pollwait); |
| 581 | |
| 582 | n = p9_fd_poll(client, &m->pt); |
| 583 | if (n & POLLIN) { |
| 584 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m); |
| 585 | set_bit(Rpending, &m->wsched); |
| 586 | } |
| 587 | |
| 588 | if (n & POLLOUT) { |
| 589 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m); |
| 590 | set_bit(Wpending, &m->wsched); |
| 591 | } |
| 592 | |
| 593 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 594 | if (IS_ERR(m->poll_wait[i].wait_addr)) { |
| 595 | p9_mux_poll_stop(m); |
| 596 | kfree(m); |
| 597 | /* return the error code */ |
| 598 | return (void *)m->poll_wait[i].wait_addr; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | return m; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * p9_poll_mux - polls a mux and schedules read or write works if necessary |
| 607 | * @m: connection to poll |
| 608 | * |
| 609 | */ |
| 610 | |
| 611 | static void p9_poll_mux(struct p9_conn *m) |
| 612 | { |
| 613 | int n; |
| 614 | |
| 615 | if (m->err < 0) |
| 616 | return; |
| 617 | |
| 618 | n = p9_fd_poll(m->client, NULL); |
| 619 | if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) { |
| 620 | P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n); |
| 621 | if (n >= 0) |
| 622 | n = -ECONNRESET; |
| 623 | p9_conn_cancel(m, n); |
| 624 | } |
| 625 | |
| 626 | if (n & POLLIN) { |
| 627 | set_bit(Rpending, &m->wsched); |
| 628 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m); |
| 629 | if (!test_and_set_bit(Rworksched, &m->wsched)) { |
| 630 | P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m); |
| 631 | queue_work(p9_mux_wq, &m->rq); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | if (n & POLLOUT) { |
| 636 | set_bit(Wpending, &m->wsched); |
| 637 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m); |
| 638 | if ((m->wsize || !list_empty(&m->unsent_req_list)) |
| 639 | && !test_and_set_bit(Wworksched, &m->wsched)) { |
| 640 | P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m); |
| 641 | queue_work(p9_mux_wq, &m->wq); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /** |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 647 | * p9_fd_request - send 9P request |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 648 | * The function can sleep until the request is scheduled for sending. |
| 649 | * The function can be interrupted. Return from the function is not |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 650 | * a guarantee that the request is sent successfully. |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 651 | * |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 652 | * @client: client instance |
| 653 | * @req: request to be sent |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 654 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 655 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 656 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 657 | static int p9_fd_request(struct p9_client *client, struct p9_req_t *req) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 658 | { |
| 659 | int n; |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 660 | struct p9_trans_fd *ts = client->trans; |
| 661 | struct p9_conn *m = ts->conn; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 662 | |
| 663 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current, |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 664 | req->tc, req->tc->id); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 665 | if (m->err < 0) |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 666 | return m->err; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 667 | |
| 668 | #ifdef CONFIG_NET_9P_DEBUG |
| 669 | if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) { |
| 670 | char buf[150]; |
| 671 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 672 | p9_printfcall(buf, sizeof(buf), req->tc, client->dotu); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 673 | printk(KERN_NOTICE "<<< %p %s\n", m, buf); |
| 674 | } |
| 675 | #endif |
| 676 | |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 677 | req->status = REQ_STATUS_UNSENT; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 678 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 679 | spin_lock(&client->lock); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 680 | list_add_tail(&req->req_list, &m->unsent_req_list); |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 681 | spin_unlock(&client->lock); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 682 | |
| 683 | if (test_and_clear_bit(Wpending, &m->wsched)) |
| 684 | n = POLLOUT; |
| 685 | else |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 686 | n = p9_fd_poll(m->client, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 687 | |
| 688 | if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched)) |
| 689 | queue_work(p9_mux_wq, &m->wq); |
| 690 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 691 | return 0; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 692 | } |
| 693 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 694 | static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 695 | { |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 696 | struct p9_trans_fd *ts = client->trans; |
| 697 | struct p9_conn *m = ts->conn; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 698 | |
| 699 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag); |
| 700 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 701 | spin_lock(&client->lock); |
| 702 | list_del(&req->req_list); |
| 703 | spin_unlock(&client->lock); |
| 704 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 705 | /* if a response was received for a request, do nothing */ |
Eric Van Hensbergen | 673d62cd | 2008-10-13 18:45:22 -0500 | [diff] [blame] | 706 | if (req->rc || req->t_err) { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 707 | P9_DPRINTK(P9_DEBUG_MUX, |
| 708 | "mux %p req %p response already received\n", m, req); |
| 709 | return 0; |
| 710 | } |
| 711 | |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 712 | if (req->status == REQ_STATUS_UNSENT) { |
| 713 | req->status = REQ_STATUS_FLSHD; |
| 714 | return 0; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 715 | } |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 716 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 717 | return 1; |
| 718 | } |
| 719 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 720 | /** |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 721 | * parse_options - parse mount options into session structure |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 722 | * @options: options string passed from mount |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 723 | * @opts: transport-specific structure to parse options into |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 724 | * |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 725 | * Returns 0 upon success, -ERRNO upon failure |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 726 | */ |
| 727 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 728 | static int parse_opts(char *params, struct p9_fd_opts *opts) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 729 | { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 730 | char *p; |
| 731 | substring_t args[MAX_OPT_ARGS]; |
| 732 | int option; |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 733 | char *options; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 734 | int ret; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 735 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 736 | opts->port = P9_PORT; |
| 737 | opts->rfd = ~0; |
| 738 | opts->wfd = ~0; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 739 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 740 | if (!params) |
| 741 | return 0; |
| 742 | |
| 743 | options = kstrdup(params, GFP_KERNEL); |
| 744 | if (!options) { |
| 745 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 746 | "failed to allocate copy of option string\n"); |
| 747 | return -ENOMEM; |
| 748 | } |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 749 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 750 | while ((p = strsep(&options, ",")) != NULL) { |
| 751 | int token; |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 752 | int r; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 753 | if (!*p) |
| 754 | continue; |
| 755 | token = match_token(p, tokens, args); |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 756 | r = match_int(&args[0], &option); |
| 757 | if (r < 0) { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 758 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 759 | "integer field, but no integer?\n"); |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 760 | ret = r; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 761 | continue; |
| 762 | } |
| 763 | switch (token) { |
| 764 | case Opt_port: |
| 765 | opts->port = option; |
| 766 | break; |
| 767 | case Opt_rfdno: |
| 768 | opts->rfd = option; |
| 769 | break; |
| 770 | case Opt_wfdno: |
| 771 | opts->wfd = option; |
| 772 | break; |
| 773 | default: |
| 774 | continue; |
| 775 | } |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 776 | } |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 777 | kfree(options); |
| 778 | return 0; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 779 | } |
| 780 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 781 | static int p9_fd_open(struct p9_client *client, int rfd, int wfd) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 782 | { |
| 783 | struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd), |
| 784 | GFP_KERNEL); |
| 785 | if (!ts) |
| 786 | return -ENOMEM; |
| 787 | |
| 788 | ts->rd = fget(rfd); |
| 789 | ts->wr = fget(wfd); |
| 790 | if (!ts->rd || !ts->wr) { |
| 791 | if (ts->rd) |
| 792 | fput(ts->rd); |
| 793 | if (ts->wr) |
| 794 | fput(ts->wr); |
| 795 | kfree(ts); |
| 796 | return -EIO; |
| 797 | } |
| 798 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 799 | client->trans = ts; |
| 800 | client->status = Connected; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 805 | static int p9_socket_open(struct p9_client *client, struct socket *csocket) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 806 | { |
| 807 | int fd, ret; |
| 808 | |
| 809 | csocket->sk->sk_allocation = GFP_NOIO; |
Ulrich Drepper | a677a03 | 2008-07-23 21:29:17 -0700 | [diff] [blame] | 810 | fd = sock_map_fd(csocket, 0); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 811 | if (fd < 0) { |
| 812 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n"); |
| 813 | return fd; |
| 814 | } |
| 815 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 816 | ret = p9_fd_open(client, fd, fd); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 817 | if (ret < 0) { |
| 818 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n"); |
| 819 | sockfd_put(csocket); |
| 820 | return ret; |
| 821 | } |
| 822 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 823 | ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 828 | /** |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 829 | * p9_mux_destroy - cancels all pending requests and frees mux resources |
| 830 | * @m: mux to destroy |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 831 | * |
| 832 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 833 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 834 | static void p9_conn_destroy(struct p9_conn *m) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 835 | { |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 836 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m, |
| 837 | m->mux_list.prev, m->mux_list.next); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 838 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 839 | p9_mux_poll_stop(m); |
| 840 | cancel_work_sync(&m->rq); |
| 841 | cancel_work_sync(&m->wq); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 842 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 843 | p9_conn_cancel(m, -ECONNRESET); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 844 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 845 | m->client = NULL; |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 846 | kfree(m); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | /** |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 850 | * p9_fd_close - shutdown file descriptor transport |
| 851 | * @client: client instance |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 852 | * |
| 853 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 854 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 855 | static void p9_fd_close(struct p9_client *client) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 856 | { |
| 857 | struct p9_trans_fd *ts; |
| 858 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 859 | if (!client) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 860 | return; |
| 861 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 862 | ts = client->trans; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 863 | if (!ts) |
| 864 | return; |
| 865 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 866 | client->status = Disconnected; |
| 867 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 868 | p9_conn_destroy(ts->conn); |
| 869 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 870 | if (ts->rd) |
| 871 | fput(ts->rd); |
| 872 | if (ts->wr) |
| 873 | fput(ts->wr); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 874 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 875 | kfree(ts); |
| 876 | } |
| 877 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 878 | /* |
| 879 | * stolen from NFS - maybe should be made a generic function? |
| 880 | */ |
| 881 | static inline int valid_ipaddr4(const char *buf) |
| 882 | { |
| 883 | int rc, count, in[4]; |
| 884 | |
| 885 | rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]); |
| 886 | if (rc != 4) |
| 887 | return -EINVAL; |
| 888 | for (count = 0; count < 4; count++) { |
| 889 | if (in[count] > 255) |
| 890 | return -EINVAL; |
| 891 | } |
| 892 | return 0; |
| 893 | } |
| 894 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 895 | static int |
| 896 | p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 897 | { |
| 898 | int err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 899 | struct socket *csocket; |
| 900 | struct sockaddr_in sin_server; |
| 901 | struct p9_fd_opts opts; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 902 | struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */ |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 903 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 904 | err = parse_opts(args, &opts); |
| 905 | if (err < 0) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 906 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 907 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 908 | if (valid_ipaddr4(addr) < 0) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 909 | return -EINVAL; |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 910 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 911 | csocket = NULL; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 912 | |
| 913 | sin_server.sin_family = AF_INET; |
| 914 | sin_server.sin_addr.s_addr = in_aton(addr); |
| 915 | sin_server.sin_port = htons(opts.port); |
| 916 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); |
| 917 | |
| 918 | if (!csocket) { |
| 919 | P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n"); |
| 920 | err = -EIO; |
| 921 | goto error; |
| 922 | } |
| 923 | |
| 924 | err = csocket->ops->connect(csocket, |
| 925 | (struct sockaddr *)&sin_server, |
| 926 | sizeof(struct sockaddr_in), 0); |
| 927 | if (err < 0) { |
| 928 | P9_EPRINTK(KERN_ERR, |
| 929 | "p9_trans_tcp: problem connecting socket to %s\n", |
| 930 | addr); |
| 931 | goto error; |
| 932 | } |
| 933 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 934 | err = p9_socket_open(client, csocket); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 935 | if (err < 0) |
| 936 | goto error; |
| 937 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 938 | p = (struct p9_trans_fd *) client->trans; |
| 939 | p->conn = p9_conn_create(client); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 940 | if (IS_ERR(p->conn)) { |
| 941 | err = PTR_ERR(p->conn); |
| 942 | p->conn = NULL; |
| 943 | goto error; |
| 944 | } |
| 945 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 946 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 947 | |
| 948 | error: |
| 949 | if (csocket) |
| 950 | sock_release(csocket); |
| 951 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 952 | kfree(p); |
| 953 | |
| 954 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 955 | } |
| 956 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 957 | static int |
| 958 | p9_fd_create_unix(struct p9_client *client, const char *addr, char *args) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 959 | { |
| 960 | int err; |
| 961 | struct socket *csocket; |
| 962 | struct sockaddr_un sun_server; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 963 | struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */ |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 964 | |
| 965 | csocket = NULL; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 966 | |
| 967 | if (strlen(addr) > UNIX_PATH_MAX) { |
| 968 | P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n", |
| 969 | addr); |
| 970 | err = -ENAMETOOLONG; |
| 971 | goto error; |
| 972 | } |
| 973 | |
| 974 | sun_server.sun_family = PF_UNIX; |
| 975 | strcpy(sun_server.sun_path, addr); |
| 976 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); |
| 977 | err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, |
| 978 | sizeof(struct sockaddr_un) - 1, 0); |
| 979 | if (err < 0) { |
| 980 | P9_EPRINTK(KERN_ERR, |
| 981 | "p9_trans_unix: problem connecting socket: %s: %d\n", |
| 982 | addr, err); |
| 983 | goto error; |
| 984 | } |
| 985 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 986 | err = p9_socket_open(client, csocket); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 987 | if (err < 0) |
| 988 | goto error; |
| 989 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 990 | p = (struct p9_trans_fd *) client->trans; |
| 991 | p->conn = p9_conn_create(client); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 992 | if (IS_ERR(p->conn)) { |
| 993 | err = PTR_ERR(p->conn); |
| 994 | p->conn = NULL; |
| 995 | goto error; |
| 996 | } |
| 997 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 998 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 999 | |
| 1000 | error: |
| 1001 | if (csocket) |
| 1002 | sock_release(csocket); |
| 1003 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1004 | kfree(p); |
| 1005 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1006 | } |
| 1007 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1008 | static int |
| 1009 | p9_fd_create(struct p9_client *client, const char *addr, char *args) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1010 | { |
| 1011 | int err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1012 | struct p9_fd_opts opts; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1013 | struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */ |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1014 | |
| 1015 | parse_opts(args, &opts); |
| 1016 | |
| 1017 | if (opts.rfd == ~0 || opts.wfd == ~0) { |
| 1018 | printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n"); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1019 | return -ENOPROTOOPT; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1020 | } |
| 1021 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1022 | err = p9_fd_open(client, opts.rfd, opts.wfd); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1023 | if (err < 0) |
| 1024 | goto error; |
| 1025 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1026 | p = (struct p9_trans_fd *) client->trans; |
| 1027 | p->conn = p9_conn_create(client); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1028 | if (IS_ERR(p->conn)) { |
| 1029 | err = PTR_ERR(p->conn); |
| 1030 | p->conn = NULL; |
| 1031 | goto error; |
| 1032 | } |
| 1033 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1034 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1035 | |
| 1036 | error: |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1037 | kfree(p); |
| 1038 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | static struct p9_trans_module p9_tcp_trans = { |
| 1042 | .name = "tcp", |
| 1043 | .maxsize = MAX_SOCK_BUF, |
| 1044 | .def = 1, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1045 | .create = p9_fd_create_tcp, |
| 1046 | .close = p9_fd_close, |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 1047 | .request = p9_fd_request, |
| 1048 | .cancel = p9_fd_cancel, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1049 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1050 | }; |
| 1051 | |
| 1052 | static struct p9_trans_module p9_unix_trans = { |
| 1053 | .name = "unix", |
| 1054 | .maxsize = MAX_SOCK_BUF, |
| 1055 | .def = 0, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1056 | .create = p9_fd_create_unix, |
| 1057 | .close = p9_fd_close, |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 1058 | .request = p9_fd_request, |
| 1059 | .cancel = p9_fd_cancel, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1060 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1061 | }; |
| 1062 | |
| 1063 | static struct p9_trans_module p9_fd_trans = { |
| 1064 | .name = "fd", |
| 1065 | .maxsize = MAX_SOCK_BUF, |
| 1066 | .def = 0, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1067 | .create = p9_fd_create, |
| 1068 | .close = p9_fd_close, |
Eric Van Hensbergen | 91b8534 | 2008-10-13 18:45:21 -0500 | [diff] [blame^] | 1069 | .request = p9_fd_request, |
| 1070 | .cancel = p9_fd_cancel, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1071 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1072 | }; |
| 1073 | |
Eric Van Hensbergen | 5503ac5 | 2008-10-13 18:45:24 -0500 | [diff] [blame] | 1074 | /** |
| 1075 | * p9_poll_proc - poll worker thread |
| 1076 | * @a: thread state and arguments |
| 1077 | * |
| 1078 | * polls all v9fs transports for new events and queues the appropriate |
| 1079 | * work to the work queue |
| 1080 | * |
| 1081 | */ |
| 1082 | |
| 1083 | static int p9_poll_proc(void *a) |
| 1084 | { |
| 1085 | unsigned long flags; |
| 1086 | |
| 1087 | P9_DPRINTK(P9_DEBUG_MUX, "start %p\n", current); |
| 1088 | repeat: |
| 1089 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 1090 | while (!list_empty(&p9_poll_pending_list)) { |
| 1091 | struct p9_conn *conn = list_first_entry(&p9_poll_pending_list, |
| 1092 | struct p9_conn, |
| 1093 | poll_pending_link); |
| 1094 | list_del_init(&conn->poll_pending_link); |
| 1095 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
| 1096 | |
| 1097 | p9_poll_mux(conn); |
| 1098 | |
| 1099 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 1100 | } |
| 1101 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
| 1102 | |
| 1103 | set_current_state(TASK_INTERRUPTIBLE); |
| 1104 | if (list_empty(&p9_poll_pending_list)) { |
| 1105 | P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n"); |
| 1106 | schedule(); |
| 1107 | } |
| 1108 | __set_current_state(TASK_RUNNING); |
| 1109 | |
| 1110 | if (!kthread_should_stop()) |
| 1111 | goto repeat; |
| 1112 | |
| 1113 | P9_DPRINTK(P9_DEBUG_MUX, "finish\n"); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 1117 | int p9_trans_fd_init(void) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1118 | { |
Tejun Heo | 206ca50 | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1119 | p9_mux_wq = create_workqueue("v9fs"); |
| 1120 | if (!p9_mux_wq) { |
| 1121 | printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n"); |
| 1122 | return -ENOMEM; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1123 | } |
| 1124 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1125 | p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll"); |
| 1126 | if (IS_ERR(p9_poll_task)) { |
| 1127 | destroy_workqueue(p9_mux_wq); |
| 1128 | printk(KERN_WARNING "v9fs: mux: creating poll task failed\n"); |
| 1129 | return PTR_ERR(p9_poll_task); |
| 1130 | } |
| 1131 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1132 | v9fs_register_trans(&p9_tcp_trans); |
| 1133 | v9fs_register_trans(&p9_unix_trans); |
| 1134 | v9fs_register_trans(&p9_fd_trans); |
| 1135 | |
Andrew Morton | 3387b80 | 2008-03-28 14:15:57 -0700 | [diff] [blame] | 1136 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1137 | } |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1138 | |
| 1139 | void p9_trans_fd_exit(void) |
| 1140 | { |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1141 | kthread_stop(p9_poll_task); |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1142 | v9fs_unregister_trans(&p9_tcp_trans); |
| 1143 | v9fs_unregister_trans(&p9_unix_trans); |
| 1144 | v9fs_unregister_trans(&p9_fd_trans); |
Tejun Heo | 206ca50 | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1145 | |
| 1146 | destroy_workqueue(p9_mux_wq); |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1147 | } |