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