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 ERREQFLUSH 1 |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 48 | #define MAXPOLLWADDR 2 |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 49 | |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 50 | /** |
| 51 | * struct p9_fd_opts - per-transport options |
| 52 | * @rfd: file descriptor for reading (trans=fd) |
| 53 | * @wfd: file descriptor for writing (trans=fd) |
| 54 | * @port: port to connect to (trans=tcp) |
| 55 | * |
| 56 | */ |
| 57 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 58 | struct p9_fd_opts { |
| 59 | int rfd; |
| 60 | int wfd; |
| 61 | u16 port; |
| 62 | }; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 63 | |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * struct p9_trans_fd - transport state |
| 67 | * @rd: reference to file to read from |
| 68 | * @wr: reference of file to write to |
| 69 | * @conn: connection state reference |
| 70 | * |
| 71 | */ |
| 72 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 73 | struct p9_trans_fd { |
| 74 | struct file *rd; |
| 75 | struct file *wr; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 76 | struct p9_conn *conn; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 79 | /* |
| 80 | * Option Parsing (code inspired by NFS code) |
| 81 | * - a little lazy - parse all fd-transport options |
| 82 | */ |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 83 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 84 | enum { |
| 85 | /* Options that take integer arguments */ |
Latchesar Ionkov | 5576269 | 2007-11-06 08:02:53 -0600 | [diff] [blame] | 86 | Opt_port, Opt_rfdno, Opt_wfdno, Opt_err, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 89 | static const match_table_t tokens = { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 90 | {Opt_port, "port=%u"}, |
| 91 | {Opt_rfdno, "rfdno=%u"}, |
| 92 | {Opt_wfdno, "wfdno=%u"}, |
Latchesar Ionkov | 5576269 | 2007-11-06 08:02:53 -0600 | [diff] [blame] | 93 | {Opt_err, NULL}, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 94 | }; |
| 95 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 96 | enum { |
| 97 | Rworksched = 1, /* read work scheduled or running */ |
| 98 | Rpending = 2, /* can read */ |
| 99 | Wworksched = 4, /* write work scheduled or running */ |
| 100 | Wpending = 8, /* can write */ |
| 101 | }; |
| 102 | |
| 103 | enum { |
| 104 | None, |
| 105 | Flushing, |
| 106 | Flushed, |
| 107 | }; |
| 108 | |
| 109 | struct p9_req; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 110 | typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a); |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * struct p9_req - fd mux encoding of an rpc transaction |
| 114 | * @lock: protects req_list |
| 115 | * @tag: numeric tag for rpc transaction |
| 116 | * @tcall: request &p9_fcall structure |
| 117 | * @rcall: response &p9_fcall structure |
| 118 | * @err: error state |
| 119 | * @cb: callback for when response is received |
| 120 | * @cba: argument to pass to callback |
| 121 | * @flush: flag to indicate RPC has been flushed |
| 122 | * @req_list: list link for higher level objects to chain requests |
| 123 | * |
| 124 | */ |
| 125 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 126 | struct p9_req { |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 127 | spinlock_t lock; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 128 | int tag; |
| 129 | struct p9_fcall *tcall; |
| 130 | struct p9_fcall *rcall; |
| 131 | int err; |
| 132 | p9_conn_req_callback cb; |
| 133 | void *cba; |
| 134 | int flush; |
| 135 | struct list_head req_list; |
| 136 | }; |
| 137 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 138 | struct p9_poll_wait { |
| 139 | struct p9_conn *conn; |
| 140 | wait_queue_t wait; |
| 141 | wait_queue_head_t *wait_addr; |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | /** |
| 145 | * struct p9_conn - fd mux connection state information |
| 146 | * @lock: protects mux_list (?) |
| 147 | * @mux_list: list link for mux to manage multiple connections (?) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 148 | * @client: reference to client instance for this connection |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 149 | * @tagpool: id accounting for transactions |
| 150 | * @err: error state |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 151 | * @req_list: accounting for requests which have been sent |
| 152 | * @unsent_req_list: accounting for requests that haven't been sent |
| 153 | * @rcall: current response &p9_fcall structure |
| 154 | * @rpos: read position in current frame |
| 155 | * @rbuf: current read buffer |
| 156 | * @wpos: write position for current frame |
| 157 | * @wsize: amount of data to write for current frame |
| 158 | * @wbuf: current write buffer |
| 159 | * @poll_wait: array of wait_q's for various worker threads |
| 160 | * @poll_waddr: ???? |
| 161 | * @pt: poll state |
| 162 | * @rq: current read work |
| 163 | * @wq: current write work |
| 164 | * @wsched: ???? |
| 165 | * |
| 166 | */ |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 167 | |
| 168 | struct p9_conn { |
| 169 | spinlock_t lock; /* protect lock structure */ |
| 170 | struct list_head mux_list; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 171 | struct p9_client *client; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 172 | struct p9_idpool *tagpool; |
| 173 | int err; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 174 | struct list_head req_list; |
| 175 | struct list_head unsent_req_list; |
| 176 | struct p9_fcall *rcall; |
| 177 | int rpos; |
| 178 | char *rbuf; |
| 179 | int wpos; |
| 180 | int wsize; |
| 181 | char *wbuf; |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 182 | struct list_head poll_pending_link; |
| 183 | struct p9_poll_wait poll_wait[MAXPOLLWADDR]; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 184 | poll_table pt; |
| 185 | struct work_struct rq; |
| 186 | struct work_struct wq; |
| 187 | unsigned long wsched; |
| 188 | }; |
| 189 | |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 190 | /** |
| 191 | * struct p9_mux_rpc - fd mux rpc accounting structure |
| 192 | * @m: connection this request was issued on |
| 193 | * @err: error state |
| 194 | * @tcall: request &p9_fcall |
| 195 | * @rcall: response &p9_fcall |
| 196 | * @wqueue: wait queue that client is blocked on for this rpc |
| 197 | * |
| 198 | * Bug: isn't this information duplicated elsewhere like &p9_req |
| 199 | */ |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 200 | |
| 201 | struct p9_mux_rpc { |
| 202 | struct p9_conn *m; |
| 203 | int err; |
| 204 | struct p9_fcall *tcall; |
| 205 | struct p9_fcall *rcall; |
| 206 | wait_queue_head_t wqueue; |
| 207 | }; |
| 208 | |
| 209 | static int p9_poll_proc(void *); |
| 210 | static void p9_read_work(struct work_struct *work); |
| 211 | static void p9_write_work(struct work_struct *work); |
| 212 | static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, |
| 213 | poll_table *p); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 214 | static int p9_fd_write(struct p9_client *client, void *v, int len); |
| 215 | static int p9_fd_read(struct p9_client *client, void *v, int len); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 216 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 217 | static DEFINE_SPINLOCK(p9_poll_lock); |
| 218 | static LIST_HEAD(p9_poll_pending_list); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 219 | static struct workqueue_struct *p9_mux_wq; |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 220 | static struct task_struct *p9_poll_task; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 221 | |
| 222 | static void p9_conn_destroy(struct p9_conn *); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 223 | static unsigned int p9_fd_poll(struct p9_client *client, |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 224 | struct poll_table_struct *pt); |
| 225 | |
| 226 | #ifdef P9_NONBLOCK |
| 227 | static int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc, |
| 228 | p9_conn_req_callback cb, void *a); |
| 229 | #endif /* P9_NONBLOCK */ |
| 230 | |
| 231 | static void p9_conn_cancel(struct p9_conn *m, int err); |
| 232 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 233 | static u16 p9_mux_get_tag(struct p9_conn *m) |
| 234 | { |
| 235 | int tag; |
| 236 | |
| 237 | tag = p9_idpool_get(m->tagpool); |
| 238 | if (tag < 0) |
| 239 | return P9_NOTAG; |
| 240 | else |
| 241 | return (u16) tag; |
| 242 | } |
| 243 | |
| 244 | static void p9_mux_put_tag(struct p9_conn *m, u16 tag) |
| 245 | { |
| 246 | if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool)) |
| 247 | p9_idpool_put(tag, m->tagpool); |
| 248 | } |
| 249 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 250 | static void p9_mux_poll_stop(struct p9_conn *m) |
| 251 | { |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 252 | unsigned long flags; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 253 | int i; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 254 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 255 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 256 | struct p9_poll_wait *pwait = &m->poll_wait[i]; |
| 257 | |
| 258 | if (pwait->wait_addr) { |
| 259 | remove_wait_queue(pwait->wait_addr, &pwait->wait); |
| 260 | pwait->wait_addr = NULL; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 261 | } |
| 262 | } |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 263 | |
| 264 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 265 | list_del_init(&m->poll_pending_link); |
| 266 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
| 270 | * p9_conn_create - allocate and initialize the per-session mux data |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 271 | * @client: client instance |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 272 | * |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 273 | * Note: Creates the polling task if this is the first session. |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 274 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 275 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 276 | static struct p9_conn *p9_conn_create(struct p9_client *client) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 277 | { |
| 278 | int i, n; |
Tejun Heo | 571ffea | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 279 | struct p9_conn *m; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 280 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 281 | P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize); |
Tejun Heo | 571ffea | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 282 | m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 283 | if (!m) |
| 284 | return ERR_PTR(-ENOMEM); |
| 285 | |
| 286 | spin_lock_init(&m->lock); |
| 287 | INIT_LIST_HEAD(&m->mux_list); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 288 | m->client = client; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 289 | m->tagpool = p9_idpool_create(); |
| 290 | if (IS_ERR(m->tagpool)) { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 291 | kfree(m); |
Tejun Heo | 571ffea | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 292 | return ERR_PTR(-ENOMEM); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 293 | } |
| 294 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 295 | INIT_LIST_HEAD(&m->req_list); |
| 296 | INIT_LIST_HEAD(&m->unsent_req_list); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 297 | INIT_WORK(&m->rq, p9_read_work); |
| 298 | INIT_WORK(&m->wq, p9_write_work); |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 299 | INIT_LIST_HEAD(&m->poll_pending_link); |
| 300 | init_poll_funcptr(&m->pt, p9_pollwait); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 301 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 302 | n = p9_fd_poll(client, &m->pt); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 303 | if (n & POLLIN) { |
| 304 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m); |
| 305 | set_bit(Rpending, &m->wsched); |
| 306 | } |
| 307 | |
| 308 | if (n & POLLOUT) { |
| 309 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m); |
| 310 | set_bit(Wpending, &m->wsched); |
| 311 | } |
| 312 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 313 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 314 | if (IS_ERR(m->poll_wait[i].wait_addr)) { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 315 | p9_mux_poll_stop(m); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 316 | kfree(m); |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 317 | /* return the error code */ |
| 318 | return (void *)m->poll_wait[i].wait_addr; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | return m; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * p9_mux_destroy - cancels all pending requests and frees mux resources |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 327 | * @m: mux to destroy |
| 328 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 329 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 330 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 331 | static void p9_conn_destroy(struct p9_conn *m) |
| 332 | { |
| 333 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m, |
| 334 | m->mux_list.prev, m->mux_list.next); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 335 | |
| 336 | p9_mux_poll_stop(m); |
Tejun Heo | 7dc5d24 | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 337 | cancel_work_sync(&m->rq); |
| 338 | cancel_work_sync(&m->wq); |
| 339 | |
| 340 | p9_conn_cancel(m, -ECONNRESET); |
| 341 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 342 | m->client = NULL; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 343 | p9_idpool_destroy(m->tagpool); |
| 344 | kfree(m); |
| 345 | } |
| 346 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 347 | static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key) |
| 348 | { |
| 349 | struct p9_poll_wait *pwait = |
| 350 | container_of(wait, struct p9_poll_wait, wait); |
| 351 | struct p9_conn *m = pwait->conn; |
| 352 | unsigned long flags; |
| 353 | DECLARE_WAITQUEUE(dummy_wait, p9_poll_task); |
| 354 | |
| 355 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 356 | if (list_empty(&m->poll_pending_link)) |
| 357 | list_add_tail(&m->poll_pending_link, &p9_poll_pending_list); |
| 358 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
| 359 | |
| 360 | /* perform the default wake up operation */ |
| 361 | return default_wake_function(&dummy_wait, mode, sync, key); |
| 362 | } |
| 363 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 364 | /** |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 365 | * p9_pollwait - add poll task to the wait queue |
| 366 | * @filp: file pointer being polled |
| 367 | * @wait_address: wait_q to block on |
| 368 | * @p: poll state |
| 369 | * |
| 370 | * called by files poll operation to add v9fs-poll task to files wait queue |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 371 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 372 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 373 | static void |
| 374 | p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p) |
| 375 | { |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 376 | struct p9_conn *m = container_of(p, struct p9_conn, pt); |
| 377 | struct p9_poll_wait *pwait = NULL; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 378 | int i; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 379 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 380 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
| 381 | if (m->poll_wait[i].wait_addr == NULL) { |
| 382 | pwait = &m->poll_wait[i]; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 383 | break; |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 384 | } |
| 385 | } |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 386 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 387 | if (!pwait) { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 388 | P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n"); |
| 389 | return; |
| 390 | } |
| 391 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 392 | if (!wait_address) { |
| 393 | P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n"); |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 394 | pwait->wait_addr = ERR_PTR(-EIO); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 398 | pwait->conn = m; |
| 399 | pwait->wait_addr = wait_address; |
| 400 | init_waitqueue_func_entry(&pwait->wait, p9_pollwake); |
| 401 | add_wait_queue(wait_address, &pwait->wait); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
| 405 | * p9_poll_mux - polls a mux and schedules read or write works if necessary |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 406 | * @m: connection to poll |
| 407 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 408 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 409 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 410 | static void p9_poll_mux(struct p9_conn *m) |
| 411 | { |
| 412 | int n; |
| 413 | |
| 414 | if (m->err < 0) |
| 415 | return; |
| 416 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 417 | n = p9_fd_poll(m->client, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 418 | if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) { |
| 419 | P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n); |
| 420 | if (n >= 0) |
| 421 | n = -ECONNRESET; |
| 422 | p9_conn_cancel(m, n); |
| 423 | } |
| 424 | |
| 425 | if (n & POLLIN) { |
| 426 | set_bit(Rpending, &m->wsched); |
| 427 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m); |
| 428 | if (!test_and_set_bit(Rworksched, &m->wsched)) { |
| 429 | P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m); |
| 430 | queue_work(p9_mux_wq, &m->rq); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (n & POLLOUT) { |
| 435 | set_bit(Wpending, &m->wsched); |
| 436 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m); |
| 437 | if ((m->wsize || !list_empty(&m->unsent_req_list)) |
| 438 | && !test_and_set_bit(Wworksched, &m->wsched)) { |
| 439 | P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m); |
| 440 | queue_work(p9_mux_wq, &m->wq); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 446 | * p9_poll_proc - poll worker thread |
| 447 | * @a: thread state and arguments |
| 448 | * |
| 449 | * polls all v9fs transports for new events and queues the appropriate |
| 450 | * work to the work queue |
| 451 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 452 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 453 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 454 | static int p9_poll_proc(void *a) |
| 455 | { |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 456 | unsigned long flags; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 457 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 458 | P9_DPRINTK(P9_DEBUG_MUX, "start %p\n", current); |
| 459 | repeat: |
| 460 | spin_lock_irqsave(&p9_poll_lock, flags); |
| 461 | while (!list_empty(&p9_poll_pending_list)) { |
| 462 | struct p9_conn *conn = list_first_entry(&p9_poll_pending_list, |
| 463 | struct p9_conn, |
| 464 | poll_pending_link); |
| 465 | list_del_init(&conn->poll_pending_link); |
| 466 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 467 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 468 | p9_poll_mux(conn); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 469 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 470 | spin_lock_irqsave(&p9_poll_lock, flags); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 471 | } |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 472 | spin_unlock_irqrestore(&p9_poll_lock, flags); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 473 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 474 | set_current_state(TASK_INTERRUPTIBLE); |
| 475 | if (list_empty(&p9_poll_pending_list)) { |
| 476 | P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n"); |
| 477 | schedule(); |
| 478 | } |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 479 | __set_current_state(TASK_RUNNING); |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 480 | |
| 481 | if (!kthread_should_stop()) |
| 482 | goto repeat; |
| 483 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 484 | P9_DPRINTK(P9_DEBUG_MUX, "finish\n"); |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * p9_write_work - called when a transport can send some data |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 490 | * @work: container for work to be done |
| 491 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 492 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 493 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 494 | static void p9_write_work(struct work_struct *work) |
| 495 | { |
| 496 | int n, err; |
| 497 | struct p9_conn *m; |
| 498 | struct p9_req *req; |
| 499 | |
| 500 | m = container_of(work, struct p9_conn, wq); |
| 501 | |
| 502 | if (m->err < 0) { |
| 503 | clear_bit(Wworksched, &m->wsched); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | if (!m->wsize) { |
| 508 | if (list_empty(&m->unsent_req_list)) { |
| 509 | clear_bit(Wworksched, &m->wsched); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | spin_lock(&m->lock); |
| 514 | again: |
| 515 | req = list_entry(m->unsent_req_list.next, struct p9_req, |
| 516 | req_list); |
| 517 | list_move_tail(&req->req_list, &m->req_list); |
| 518 | if (req->err == ERREQFLUSH) |
| 519 | goto again; |
| 520 | |
| 521 | m->wbuf = req->tcall->sdata; |
| 522 | m->wsize = req->tcall->size; |
| 523 | m->wpos = 0; |
| 524 | spin_unlock(&m->lock); |
| 525 | } |
| 526 | |
| 527 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, |
| 528 | m->wsize); |
| 529 | clear_bit(Wpending, &m->wsched); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 530 | err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 531 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err); |
| 532 | if (err == -EAGAIN) { |
| 533 | clear_bit(Wworksched, &m->wsched); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | if (err < 0) |
| 538 | goto error; |
| 539 | else if (err == 0) { |
| 540 | err = -EREMOTEIO; |
| 541 | goto error; |
| 542 | } |
| 543 | |
| 544 | m->wpos += err; |
| 545 | if (m->wpos == m->wsize) |
| 546 | m->wpos = m->wsize = 0; |
| 547 | |
| 548 | if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) { |
| 549 | if (test_and_clear_bit(Wpending, &m->wsched)) |
| 550 | n = POLLOUT; |
| 551 | else |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 552 | n = p9_fd_poll(m->client, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 553 | |
| 554 | if (n & POLLOUT) { |
| 555 | P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m); |
| 556 | queue_work(p9_mux_wq, &m->wq); |
| 557 | } else |
| 558 | clear_bit(Wworksched, &m->wsched); |
| 559 | } else |
| 560 | clear_bit(Wworksched, &m->wsched); |
| 561 | |
| 562 | return; |
| 563 | |
| 564 | error: |
| 565 | p9_conn_cancel(m, err); |
| 566 | clear_bit(Wworksched, &m->wsched); |
| 567 | } |
| 568 | |
| 569 | static void process_request(struct p9_conn *m, struct p9_req *req) |
| 570 | { |
| 571 | int ecode; |
| 572 | struct p9_str *ename; |
| 573 | |
| 574 | if (!req->err && req->rcall->id == P9_RERROR) { |
| 575 | ecode = req->rcall->params.rerror.errno; |
| 576 | ename = &req->rcall->params.rerror.error; |
| 577 | |
| 578 | P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len, |
| 579 | ename->str); |
| 580 | |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 581 | if (m->client->dotu) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 582 | req->err = -ecode; |
| 583 | |
| 584 | if (!req->err) { |
| 585 | req->err = p9_errstr2errno(ename->str, ename->len); |
| 586 | |
| 587 | /* string match failed */ |
| 588 | if (!req->err) { |
| 589 | PRINT_FCALL_ERROR("unknown error", req->rcall); |
| 590 | req->err = -ESERVERFAULT; |
| 591 | } |
| 592 | } |
| 593 | } else if (req->tcall && req->rcall->id != req->tcall->id + 1) { |
| 594 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 595 | "fcall mismatch: expected %d, got %d\n", |
| 596 | req->tcall->id + 1, req->rcall->id); |
| 597 | if (!req->err) |
| 598 | req->err = -EIO; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * 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] | 604 | * @work: container of work to be done |
| 605 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 606 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 607 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 608 | static void p9_read_work(struct work_struct *work) |
| 609 | { |
| 610 | int n, err; |
| 611 | struct p9_conn *m; |
| 612 | struct p9_req *req, *rptr, *rreq; |
| 613 | struct p9_fcall *rcall; |
| 614 | char *rbuf; |
| 615 | |
| 616 | m = container_of(work, struct p9_conn, rq); |
| 617 | |
| 618 | if (m->err < 0) |
| 619 | return; |
| 620 | |
| 621 | rcall = NULL; |
| 622 | P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos); |
| 623 | |
| 624 | if (!m->rcall) { |
| 625 | m->rcall = |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 626 | kmalloc(sizeof(struct p9_fcall) + m->client->msize, |
| 627 | GFP_KERNEL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 628 | if (!m->rcall) { |
| 629 | err = -ENOMEM; |
| 630 | goto error; |
| 631 | } |
| 632 | |
| 633 | m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall); |
| 634 | m->rpos = 0; |
| 635 | } |
| 636 | |
| 637 | clear_bit(Rpending, &m->wsched); |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 638 | err = p9_fd_read(m->client, m->rbuf + m->rpos, |
| 639 | m->client->msize - m->rpos); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 640 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err); |
| 641 | if (err == -EAGAIN) { |
| 642 | clear_bit(Rworksched, &m->wsched); |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | if (err <= 0) |
| 647 | goto error; |
| 648 | |
| 649 | m->rpos += err; |
| 650 | while (m->rpos > 4) { |
| 651 | n = le32_to_cpu(*(__le32 *) m->rbuf); |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 652 | if (n >= m->client->msize) { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 653 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 654 | "requested packet size too big: %d\n", n); |
| 655 | err = -EIO; |
| 656 | goto error; |
| 657 | } |
| 658 | |
| 659 | if (m->rpos < n) |
| 660 | break; |
| 661 | |
| 662 | err = |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 663 | p9_deserialize_fcall(m->rbuf, n, m->rcall, m->client->dotu); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 664 | if (err < 0) |
| 665 | goto error; |
| 666 | |
| 667 | #ifdef CONFIG_NET_9P_DEBUG |
| 668 | if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) { |
| 669 | char buf[150]; |
| 670 | |
| 671 | p9_printfcall(buf, sizeof(buf), m->rcall, |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 672 | m->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 | |
| 677 | rcall = m->rcall; |
| 678 | rbuf = m->rbuf; |
| 679 | if (m->rpos > n) { |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 680 | m->rcall = kmalloc(sizeof(struct p9_fcall) + |
| 681 | m->client->msize, GFP_KERNEL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 682 | if (!m->rcall) { |
| 683 | err = -ENOMEM; |
| 684 | goto error; |
| 685 | } |
| 686 | |
| 687 | m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall); |
| 688 | memmove(m->rbuf, rbuf + n, m->rpos - n); |
| 689 | m->rpos -= n; |
| 690 | } else { |
| 691 | m->rcall = NULL; |
| 692 | m->rbuf = NULL; |
| 693 | m->rpos = 0; |
| 694 | } |
| 695 | |
| 696 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m, |
| 697 | rcall->id, rcall->tag); |
| 698 | |
| 699 | req = NULL; |
| 700 | spin_lock(&m->lock); |
| 701 | list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) { |
| 702 | if (rreq->tag == rcall->tag) { |
| 703 | req = rreq; |
| 704 | if (req->flush != Flushing) |
| 705 | list_del(&req->req_list); |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | spin_unlock(&m->lock); |
| 710 | |
| 711 | if (req) { |
| 712 | req->rcall = rcall; |
| 713 | process_request(m, req); |
| 714 | |
| 715 | if (req->flush != Flushing) { |
| 716 | if (req->cb) |
| 717 | (*req->cb) (req, req->cba); |
| 718 | else |
| 719 | kfree(req->rcall); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 720 | } |
| 721 | } else { |
| 722 | if (err >= 0 && rcall->id != P9_RFLUSH) |
| 723 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 724 | "unexpected response mux %p id %d tag %d\n", |
| 725 | m, rcall->id, rcall->tag); |
| 726 | kfree(rcall); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | if (!list_empty(&m->req_list)) { |
| 731 | if (test_and_clear_bit(Rpending, &m->wsched)) |
| 732 | n = POLLIN; |
| 733 | else |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 734 | n = p9_fd_poll(m->client, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 735 | |
| 736 | if (n & POLLIN) { |
| 737 | P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m); |
| 738 | queue_work(p9_mux_wq, &m->rq); |
| 739 | } else |
| 740 | clear_bit(Rworksched, &m->wsched); |
| 741 | } else |
| 742 | clear_bit(Rworksched, &m->wsched); |
| 743 | |
| 744 | return; |
| 745 | |
| 746 | error: |
| 747 | p9_conn_cancel(m, err); |
| 748 | clear_bit(Rworksched, &m->wsched); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * p9_send_request - send 9P request |
| 753 | * The function can sleep until the request is scheduled for sending. |
| 754 | * The function can be interrupted. Return from the function is not |
| 755 | * a guarantee that the request is sent successfully. Can return errors |
| 756 | * that can be retrieved by PTR_ERR macros. |
| 757 | * |
| 758 | * @m: mux data |
| 759 | * @tc: request to be sent |
| 760 | * @cb: callback function to call when response is received |
| 761 | * @cba: parameter to pass to the callback function |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 762 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 763 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 764 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 765 | static struct p9_req *p9_send_request(struct p9_conn *m, |
| 766 | struct p9_fcall *tc, |
| 767 | p9_conn_req_callback cb, void *cba) |
| 768 | { |
| 769 | int n; |
| 770 | struct p9_req *req; |
| 771 | |
| 772 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current, |
| 773 | tc, tc->id); |
| 774 | if (m->err < 0) |
| 775 | return ERR_PTR(m->err); |
| 776 | |
| 777 | req = kmalloc(sizeof(struct p9_req), GFP_KERNEL); |
| 778 | if (!req) |
| 779 | return ERR_PTR(-ENOMEM); |
| 780 | |
| 781 | if (tc->id == P9_TVERSION) |
| 782 | n = P9_NOTAG; |
| 783 | else |
| 784 | n = p9_mux_get_tag(m); |
| 785 | |
Julia Lawall | 6206782 | 2008-09-24 16:22:22 -0500 | [diff] [blame] | 786 | if (n < 0) { |
| 787 | kfree(req); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 788 | return ERR_PTR(-ENOMEM); |
Julia Lawall | 6206782 | 2008-09-24 16:22:22 -0500 | [diff] [blame] | 789 | } |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 790 | |
| 791 | p9_set_tag(tc, n); |
| 792 | |
| 793 | #ifdef CONFIG_NET_9P_DEBUG |
| 794 | if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) { |
| 795 | char buf[150]; |
| 796 | |
Eric Van Hensbergen | bead27f | 2008-10-13 18:45:24 -0500 | [diff] [blame^] | 797 | p9_printfcall(buf, sizeof(buf), tc, m->client->dotu); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 798 | printk(KERN_NOTICE "<<< %p %s\n", m, buf); |
| 799 | } |
| 800 | #endif |
| 801 | |
| 802 | spin_lock_init(&req->lock); |
| 803 | req->tag = n; |
| 804 | req->tcall = tc; |
| 805 | req->rcall = NULL; |
| 806 | req->err = 0; |
| 807 | req->cb = cb; |
| 808 | req->cba = cba; |
| 809 | req->flush = None; |
| 810 | |
| 811 | spin_lock(&m->lock); |
| 812 | list_add_tail(&req->req_list, &m->unsent_req_list); |
| 813 | spin_unlock(&m->lock); |
| 814 | |
| 815 | if (test_and_clear_bit(Wpending, &m->wsched)) |
| 816 | n = POLLOUT; |
| 817 | else |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 818 | n = p9_fd_poll(m->client, NULL); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 819 | |
| 820 | if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched)) |
| 821 | queue_work(p9_mux_wq, &m->wq); |
| 822 | |
| 823 | return req; |
| 824 | } |
| 825 | |
| 826 | static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req) |
| 827 | { |
| 828 | p9_mux_put_tag(m, req->tag); |
| 829 | kfree(req); |
| 830 | } |
| 831 | |
| 832 | static void p9_mux_flush_cb(struct p9_req *freq, void *a) |
| 833 | { |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 834 | int tag; |
| 835 | struct p9_conn *m; |
| 836 | struct p9_req *req, *rreq, *rptr; |
| 837 | |
| 838 | m = a; |
| 839 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m, |
| 840 | freq->tcall, freq->rcall, freq->err, |
| 841 | freq->tcall->params.tflush.oldtag); |
| 842 | |
| 843 | spin_lock(&m->lock); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 844 | tag = freq->tcall->params.tflush.oldtag; |
| 845 | req = NULL; |
| 846 | list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) { |
| 847 | if (rreq->tag == tag) { |
| 848 | req = rreq; |
| 849 | list_del(&req->req_list); |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | spin_unlock(&m->lock); |
| 854 | |
| 855 | if (req) { |
| 856 | spin_lock(&req->lock); |
| 857 | req->flush = Flushed; |
| 858 | spin_unlock(&req->lock); |
| 859 | |
| 860 | if (req->cb) |
| 861 | (*req->cb) (req, req->cba); |
| 862 | else |
| 863 | kfree(req->rcall); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | kfree(freq->tcall); |
| 867 | kfree(freq->rcall); |
| 868 | p9_mux_free_request(m, freq); |
| 869 | } |
| 870 | |
| 871 | static int |
| 872 | p9_mux_flush_request(struct p9_conn *m, struct p9_req *req) |
| 873 | { |
| 874 | struct p9_fcall *fc; |
| 875 | struct p9_req *rreq, *rptr; |
| 876 | |
| 877 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag); |
| 878 | |
| 879 | /* if a response was received for a request, do nothing */ |
| 880 | spin_lock(&req->lock); |
| 881 | if (req->rcall || req->err) { |
| 882 | spin_unlock(&req->lock); |
| 883 | P9_DPRINTK(P9_DEBUG_MUX, |
| 884 | "mux %p req %p response already received\n", m, req); |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | req->flush = Flushing; |
| 889 | spin_unlock(&req->lock); |
| 890 | |
| 891 | spin_lock(&m->lock); |
| 892 | /* if the request is not sent yet, just remove it from the list */ |
| 893 | list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) { |
| 894 | if (rreq->tag == req->tag) { |
| 895 | P9_DPRINTK(P9_DEBUG_MUX, |
| 896 | "mux %p req %p request is not sent yet\n", m, req); |
| 897 | list_del(&rreq->req_list); |
| 898 | req->flush = Flushed; |
| 899 | spin_unlock(&m->lock); |
| 900 | if (req->cb) |
| 901 | (*req->cb) (req, req->cba); |
| 902 | return 0; |
| 903 | } |
| 904 | } |
| 905 | spin_unlock(&m->lock); |
| 906 | |
| 907 | clear_thread_flag(TIF_SIGPENDING); |
| 908 | fc = p9_create_tflush(req->tag); |
| 909 | p9_send_request(m, fc, p9_mux_flush_cb, m); |
| 910 | return 1; |
| 911 | } |
| 912 | |
| 913 | static void |
| 914 | p9_conn_rpc_cb(struct p9_req *req, void *a) |
| 915 | { |
| 916 | struct p9_mux_rpc *r; |
| 917 | |
| 918 | P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a); |
| 919 | r = a; |
| 920 | r->rcall = req->rcall; |
| 921 | r->err = req->err; |
| 922 | |
| 923 | if (req->flush != None && !req->err) |
| 924 | r->err = -ERESTARTSYS; |
| 925 | |
| 926 | wake_up(&r->wqueue); |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * p9_fd_rpc- sends 9P request and waits until a response is available. |
| 931 | * The function can be interrupted. |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 932 | * @client: client instance |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 933 | * @tc: request to be sent |
| 934 | * @rc: pointer where a pointer to the response is stored |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 935 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 936 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 937 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 938 | int |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 939 | p9_fd_rpc(struct p9_client *client, struct p9_fcall *tc, struct p9_fcall **rc) |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 940 | { |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 941 | struct p9_trans_fd *p = client->trans; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 942 | struct p9_conn *m = p->conn; |
| 943 | int err, sigpending; |
| 944 | unsigned long flags; |
| 945 | struct p9_req *req; |
| 946 | struct p9_mux_rpc r; |
| 947 | |
| 948 | r.err = 0; |
| 949 | r.tcall = tc; |
| 950 | r.rcall = NULL; |
| 951 | r.m = m; |
| 952 | init_waitqueue_head(&r.wqueue); |
| 953 | |
| 954 | if (rc) |
| 955 | *rc = NULL; |
| 956 | |
| 957 | sigpending = 0; |
| 958 | if (signal_pending(current)) { |
| 959 | sigpending = 1; |
| 960 | clear_thread_flag(TIF_SIGPENDING); |
| 961 | } |
| 962 | |
| 963 | req = p9_send_request(m, tc, p9_conn_rpc_cb, &r); |
| 964 | if (IS_ERR(req)) { |
| 965 | err = PTR_ERR(req); |
| 966 | P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err); |
| 967 | return err; |
| 968 | } |
| 969 | |
| 970 | err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0); |
| 971 | if (r.err < 0) |
| 972 | err = r.err; |
| 973 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 974 | if (err == -ERESTARTSYS && client->status == Connected |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 975 | && m->err == 0) { |
| 976 | if (p9_mux_flush_request(m, req)) { |
| 977 | /* wait until we get response of the flush message */ |
| 978 | do { |
| 979 | clear_thread_flag(TIF_SIGPENDING); |
| 980 | err = wait_event_interruptible(r.wqueue, |
| 981 | r.rcall || r.err); |
| 982 | } while (!r.rcall && !r.err && err == -ERESTARTSYS && |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 983 | client->status == Connected && !m->err); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 984 | |
| 985 | err = -ERESTARTSYS; |
| 986 | } |
| 987 | sigpending = 1; |
| 988 | } |
| 989 | |
| 990 | if (sigpending) { |
| 991 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 992 | recalc_sigpending(); |
| 993 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 994 | } |
| 995 | |
| 996 | if (rc) |
| 997 | *rc = r.rcall; |
| 998 | else |
| 999 | kfree(r.rcall); |
| 1000 | |
| 1001 | p9_mux_free_request(m, req); |
| 1002 | if (err > 0) |
| 1003 | err = -EIO; |
| 1004 | |
| 1005 | return err; |
| 1006 | } |
| 1007 | |
| 1008 | #ifdef P9_NONBLOCK |
| 1009 | /** |
| 1010 | * p9_conn_rpcnb - sends 9P request without waiting for response. |
| 1011 | * @m: mux data |
| 1012 | * @tc: request to be sent |
| 1013 | * @cb: callback function to be called when response arrives |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1014 | * @a: value to pass to the callback function |
| 1015 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1016 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1017 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1018 | int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc, |
| 1019 | p9_conn_req_callback cb, void *a) |
| 1020 | { |
| 1021 | int err; |
| 1022 | struct p9_req *req; |
| 1023 | |
| 1024 | req = p9_send_request(m, tc, cb, a); |
| 1025 | if (IS_ERR(req)) { |
| 1026 | err = PTR_ERR(req); |
| 1027 | P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err); |
| 1028 | return PTR_ERR(req); |
| 1029 | } |
| 1030 | |
| 1031 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag); |
| 1032 | return 0; |
| 1033 | } |
| 1034 | #endif /* P9_NONBLOCK */ |
| 1035 | |
| 1036 | /** |
| 1037 | * p9_conn_cancel - cancel all pending requests with error |
| 1038 | * @m: mux data |
| 1039 | * @err: error code |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1040 | * |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1041 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1042 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1043 | void p9_conn_cancel(struct p9_conn *m, int err) |
| 1044 | { |
| 1045 | struct p9_req *req, *rtmp; |
| 1046 | LIST_HEAD(cancel_list); |
| 1047 | |
| 1048 | P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err); |
| 1049 | m->err = err; |
| 1050 | spin_lock(&m->lock); |
| 1051 | list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) { |
| 1052 | list_move(&req->req_list, &cancel_list); |
| 1053 | } |
| 1054 | list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) { |
| 1055 | list_move(&req->req_list, &cancel_list); |
| 1056 | } |
| 1057 | spin_unlock(&m->lock); |
| 1058 | |
| 1059 | list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) { |
| 1060 | list_del(&req->req_list); |
| 1061 | if (!req->err) |
| 1062 | req->err = err; |
| 1063 | |
| 1064 | if (req->cb) |
| 1065 | (*req->cb) (req, req->cba); |
| 1066 | else |
| 1067 | kfree(req->rcall); |
| 1068 | } |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1069 | } |
| 1070 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1071 | /** |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1072 | * parse_options - parse mount options into session structure |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1073 | * @options: options string passed from mount |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1074 | * @opts: transport-specific structure to parse options into |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1075 | * |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1076 | * Returns 0 upon success, -ERRNO upon failure |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1077 | */ |
| 1078 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1079 | static int parse_opts(char *params, struct p9_fd_opts *opts) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1080 | { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1081 | char *p; |
| 1082 | substring_t args[MAX_OPT_ARGS]; |
| 1083 | int option; |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1084 | char *options; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1085 | int ret; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1086 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1087 | opts->port = P9_PORT; |
| 1088 | opts->rfd = ~0; |
| 1089 | opts->wfd = ~0; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1090 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1091 | if (!params) |
| 1092 | return 0; |
| 1093 | |
| 1094 | options = kstrdup(params, GFP_KERNEL); |
| 1095 | if (!options) { |
| 1096 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 1097 | "failed to allocate copy of option string\n"); |
| 1098 | return -ENOMEM; |
| 1099 | } |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1100 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1101 | while ((p = strsep(&options, ",")) != NULL) { |
| 1102 | int token; |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1103 | int r; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1104 | if (!*p) |
| 1105 | continue; |
| 1106 | token = match_token(p, tokens, args); |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1107 | r = match_int(&args[0], &option); |
| 1108 | if (r < 0) { |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1109 | P9_DPRINTK(P9_DEBUG_ERROR, |
| 1110 | "integer field, but no integer?\n"); |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1111 | ret = r; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1112 | continue; |
| 1113 | } |
| 1114 | switch (token) { |
| 1115 | case Opt_port: |
| 1116 | opts->port = option; |
| 1117 | break; |
| 1118 | case Opt_rfdno: |
| 1119 | opts->rfd = option; |
| 1120 | break; |
| 1121 | case Opt_wfdno: |
| 1122 | opts->wfd = option; |
| 1123 | break; |
| 1124 | default: |
| 1125 | continue; |
| 1126 | } |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1127 | } |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1128 | kfree(options); |
| 1129 | return 0; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1130 | } |
| 1131 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1132 | 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] | 1133 | { |
| 1134 | struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd), |
| 1135 | GFP_KERNEL); |
| 1136 | if (!ts) |
| 1137 | return -ENOMEM; |
| 1138 | |
| 1139 | ts->rd = fget(rfd); |
| 1140 | ts->wr = fget(wfd); |
| 1141 | if (!ts->rd || !ts->wr) { |
| 1142 | if (ts->rd) |
| 1143 | fput(ts->rd); |
| 1144 | if (ts->wr) |
| 1145 | fput(ts->wr); |
| 1146 | kfree(ts); |
| 1147 | return -EIO; |
| 1148 | } |
| 1149 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1150 | client->trans = ts; |
| 1151 | client->status = Connected; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1156 | 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] | 1157 | { |
| 1158 | int fd, ret; |
| 1159 | |
| 1160 | csocket->sk->sk_allocation = GFP_NOIO; |
Ulrich Drepper | a677a03 | 2008-07-23 21:29:17 -0700 | [diff] [blame] | 1161 | fd = sock_map_fd(csocket, 0); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1162 | if (fd < 0) { |
| 1163 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n"); |
| 1164 | return fd; |
| 1165 | } |
| 1166 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1167 | ret = p9_fd_open(client, fd, fd); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1168 | if (ret < 0) { |
| 1169 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n"); |
| 1170 | sockfd_put(csocket); |
| 1171 | return ret; |
| 1172 | } |
| 1173 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1174 | ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1175 | |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1179 | /** |
| 1180 | * p9_fd_read- read from a fd |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1181 | * @client: client instance |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1182 | * @v: buffer to receive data into |
| 1183 | * @len: size of receive buffer |
| 1184 | * |
| 1185 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1186 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1187 | static int p9_fd_read(struct p9_client *client, void *v, int len) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1188 | { |
| 1189 | int ret; |
| 1190 | struct p9_trans_fd *ts = NULL; |
| 1191 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1192 | if (client && client->status != Disconnected) |
| 1193 | ts = client->trans; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1194 | |
| 1195 | if (!ts) |
| 1196 | return -EREMOTEIO; |
| 1197 | |
| 1198 | if (!(ts->rd->f_flags & O_NONBLOCK)) |
| 1199 | P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n"); |
| 1200 | |
| 1201 | ret = kernel_read(ts->rd, ts->rd->f_pos, v, len); |
| 1202 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1203 | client->status = Disconnected; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | /** |
| 1208 | * p9_fd_write - write to a socket |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1209 | * @client: client instance |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1210 | * @v: buffer to send data from |
| 1211 | * @len: size of send buffer |
| 1212 | * |
| 1213 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1214 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1215 | static int p9_fd_write(struct p9_client *client, void *v, int len) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1216 | { |
| 1217 | int ret; |
| 1218 | mm_segment_t oldfs; |
| 1219 | struct p9_trans_fd *ts = NULL; |
| 1220 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1221 | if (client && client->status != Disconnected) |
| 1222 | ts = client->trans; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1223 | |
| 1224 | if (!ts) |
| 1225 | return -EREMOTEIO; |
| 1226 | |
| 1227 | if (!(ts->wr->f_flags & O_NONBLOCK)) |
| 1228 | P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n"); |
| 1229 | |
| 1230 | oldfs = get_fs(); |
| 1231 | set_fs(get_ds()); |
| 1232 | /* The cast to a user pointer is valid due to the set_fs() */ |
| 1233 | ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos); |
| 1234 | set_fs(oldfs); |
| 1235 | |
| 1236 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1237 | client->status = Disconnected; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | static unsigned int |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1242 | p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1243 | { |
| 1244 | int ret, n; |
| 1245 | struct p9_trans_fd *ts = NULL; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1246 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1247 | if (client && client->status == Connected) |
| 1248 | ts = client->trans; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1249 | |
| 1250 | if (!ts) |
| 1251 | return -EREMOTEIO; |
| 1252 | |
| 1253 | if (!ts->rd->f_op || !ts->rd->f_op->poll) |
| 1254 | return -EIO; |
| 1255 | |
| 1256 | if (!ts->wr->f_op || !ts->wr->f_op->poll) |
| 1257 | return -EIO; |
| 1258 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1259 | ret = ts->rd->f_op->poll(ts->rd, pt); |
| 1260 | if (ret < 0) |
Tejun Heo | ec3c68f | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1261 | return ret; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1262 | |
| 1263 | if (ts->rd != ts->wr) { |
| 1264 | n = ts->wr->f_op->poll(ts->wr, pt); |
Tejun Heo | ec3c68f | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1265 | if (n < 0) |
| 1266 | return n; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1267 | ret = (ret & ~POLLOUT) | (n & ~POLLIN); |
| 1268 | } |
| 1269 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1270 | return ret; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1274 | * p9_fd_close - shutdown file descriptor transport |
| 1275 | * @client: client instance |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1276 | * |
| 1277 | */ |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 1278 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1279 | static void p9_fd_close(struct p9_client *client) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1280 | { |
| 1281 | struct p9_trans_fd *ts; |
| 1282 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1283 | if (!client) |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1284 | return; |
| 1285 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1286 | ts = client->trans; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1287 | if (!ts) |
| 1288 | return; |
| 1289 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1290 | client->status = Disconnected; |
| 1291 | |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1292 | p9_conn_destroy(ts->conn); |
| 1293 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1294 | if (ts->rd) |
| 1295 | fput(ts->rd); |
| 1296 | if (ts->wr) |
| 1297 | fput(ts->wr); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1298 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 1299 | kfree(ts); |
| 1300 | } |
| 1301 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 1302 | /* |
| 1303 | * stolen from NFS - maybe should be made a generic function? |
| 1304 | */ |
| 1305 | static inline int valid_ipaddr4(const char *buf) |
| 1306 | { |
| 1307 | int rc, count, in[4]; |
| 1308 | |
| 1309 | rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]); |
| 1310 | if (rc != 4) |
| 1311 | return -EINVAL; |
| 1312 | for (count = 0; count < 4; count++) { |
| 1313 | if (in[count] > 255) |
| 1314 | return -EINVAL; |
| 1315 | } |
| 1316 | return 0; |
| 1317 | } |
| 1318 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1319 | static int |
| 1320 | 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] | 1321 | { |
| 1322 | int err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1323 | struct socket *csocket; |
| 1324 | struct sockaddr_in sin_server; |
| 1325 | struct p9_fd_opts opts; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1326 | 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] | 1327 | |
Eric Van Hensbergen | bb8ffdf | 2008-03-07 10:53:53 -0600 | [diff] [blame] | 1328 | err = parse_opts(args, &opts); |
| 1329 | if (err < 0) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1330 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1331 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 1332 | if (valid_ipaddr4(addr) < 0) |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1333 | return -EINVAL; |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 1334 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1335 | csocket = NULL; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1336 | |
| 1337 | sin_server.sin_family = AF_INET; |
| 1338 | sin_server.sin_addr.s_addr = in_aton(addr); |
| 1339 | sin_server.sin_port = htons(opts.port); |
| 1340 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); |
| 1341 | |
| 1342 | if (!csocket) { |
| 1343 | P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n"); |
| 1344 | err = -EIO; |
| 1345 | goto error; |
| 1346 | } |
| 1347 | |
| 1348 | err = csocket->ops->connect(csocket, |
| 1349 | (struct sockaddr *)&sin_server, |
| 1350 | sizeof(struct sockaddr_in), 0); |
| 1351 | if (err < 0) { |
| 1352 | P9_EPRINTK(KERN_ERR, |
| 1353 | "p9_trans_tcp: problem connecting socket to %s\n", |
| 1354 | addr); |
| 1355 | goto error; |
| 1356 | } |
| 1357 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1358 | err = p9_socket_open(client, csocket); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1359 | if (err < 0) |
| 1360 | goto error; |
| 1361 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1362 | p = (struct p9_trans_fd *) client->trans; |
| 1363 | p->conn = p9_conn_create(client); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1364 | if (IS_ERR(p->conn)) { |
| 1365 | err = PTR_ERR(p->conn); |
| 1366 | p->conn = NULL; |
| 1367 | goto error; |
| 1368 | } |
| 1369 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1370 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1371 | |
| 1372 | error: |
| 1373 | if (csocket) |
| 1374 | sock_release(csocket); |
| 1375 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1376 | kfree(p); |
| 1377 | |
| 1378 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1379 | } |
| 1380 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1381 | static int |
| 1382 | 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] | 1383 | { |
| 1384 | int err; |
| 1385 | struct socket *csocket; |
| 1386 | struct sockaddr_un sun_server; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1387 | 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] | 1388 | |
| 1389 | csocket = NULL; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1390 | |
| 1391 | if (strlen(addr) > UNIX_PATH_MAX) { |
| 1392 | P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n", |
| 1393 | addr); |
| 1394 | err = -ENAMETOOLONG; |
| 1395 | goto error; |
| 1396 | } |
| 1397 | |
| 1398 | sun_server.sun_family = PF_UNIX; |
| 1399 | strcpy(sun_server.sun_path, addr); |
| 1400 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); |
| 1401 | err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, |
| 1402 | sizeof(struct sockaddr_un) - 1, 0); |
| 1403 | if (err < 0) { |
| 1404 | P9_EPRINTK(KERN_ERR, |
| 1405 | "p9_trans_unix: problem connecting socket: %s: %d\n", |
| 1406 | addr, err); |
| 1407 | goto error; |
| 1408 | } |
| 1409 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1410 | err = p9_socket_open(client, csocket); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1411 | if (err < 0) |
| 1412 | goto error; |
| 1413 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1414 | p = (struct p9_trans_fd *) client->trans; |
| 1415 | p->conn = p9_conn_create(client); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1416 | if (IS_ERR(p->conn)) { |
| 1417 | err = PTR_ERR(p->conn); |
| 1418 | p->conn = NULL; |
| 1419 | goto error; |
| 1420 | } |
| 1421 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1422 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1423 | |
| 1424 | error: |
| 1425 | if (csocket) |
| 1426 | sock_release(csocket); |
| 1427 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1428 | kfree(p); |
| 1429 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1430 | } |
| 1431 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1432 | static int |
| 1433 | 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] | 1434 | { |
| 1435 | int err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1436 | struct p9_fd_opts opts; |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1437 | 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] | 1438 | |
| 1439 | parse_opts(args, &opts); |
| 1440 | |
| 1441 | if (opts.rfd == ~0 || opts.wfd == ~0) { |
| 1442 | printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n"); |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1443 | return -ENOPROTOOPT; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1444 | } |
| 1445 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1446 | err = p9_fd_open(client, opts.rfd, opts.wfd); |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1447 | if (err < 0) |
| 1448 | goto error; |
| 1449 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1450 | p = (struct p9_trans_fd *) client->trans; |
| 1451 | p->conn = p9_conn_create(client); |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1452 | if (IS_ERR(p->conn)) { |
| 1453 | err = PTR_ERR(p->conn); |
| 1454 | p->conn = NULL; |
| 1455 | goto error; |
| 1456 | } |
| 1457 | |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1458 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1459 | |
| 1460 | error: |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1461 | kfree(p); |
| 1462 | return err; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | static struct p9_trans_module p9_tcp_trans = { |
| 1466 | .name = "tcp", |
| 1467 | .maxsize = MAX_SOCK_BUF, |
| 1468 | .def = 1, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1469 | .create = p9_fd_create_tcp, |
| 1470 | .close = p9_fd_close, |
| 1471 | .rpc = p9_fd_rpc, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1472 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1473 | }; |
| 1474 | |
| 1475 | static struct p9_trans_module p9_unix_trans = { |
| 1476 | .name = "unix", |
| 1477 | .maxsize = MAX_SOCK_BUF, |
| 1478 | .def = 0, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1479 | .create = p9_fd_create_unix, |
| 1480 | .close = p9_fd_close, |
| 1481 | .rpc = p9_fd_rpc, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1482 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1483 | }; |
| 1484 | |
| 1485 | static struct p9_trans_module p9_fd_trans = { |
| 1486 | .name = "fd", |
| 1487 | .maxsize = MAX_SOCK_BUF, |
| 1488 | .def = 0, |
Eric Van Hensbergen | 8b81ef5 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1489 | .create = p9_fd_create, |
| 1490 | .close = p9_fd_close, |
| 1491 | .rpc = p9_fd_rpc, |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1492 | .owner = THIS_MODULE, |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1493 | }; |
| 1494 | |
Eric Van Hensbergen | 887b3ec | 2008-05-08 20:26:37 -0500 | [diff] [blame] | 1495 | int p9_trans_fd_init(void) |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1496 | { |
Tejun Heo | 206ca50 | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1497 | p9_mux_wq = create_workqueue("v9fs"); |
| 1498 | if (!p9_mux_wq) { |
| 1499 | printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n"); |
| 1500 | return -ENOMEM; |
Eric Van Hensbergen | 8a0dc95 | 2008-02-06 19:25:03 -0600 | [diff] [blame] | 1501 | } |
| 1502 | |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1503 | p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll"); |
| 1504 | if (IS_ERR(p9_poll_task)) { |
| 1505 | destroy_workqueue(p9_mux_wq); |
| 1506 | printk(KERN_WARNING "v9fs: mux: creating poll task failed\n"); |
| 1507 | return PTR_ERR(p9_poll_task); |
| 1508 | } |
| 1509 | |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1510 | v9fs_register_trans(&p9_tcp_trans); |
| 1511 | v9fs_register_trans(&p9_unix_trans); |
| 1512 | v9fs_register_trans(&p9_fd_trans); |
| 1513 | |
Andrew Morton | 3387b80 | 2008-03-28 14:15:57 -0700 | [diff] [blame] | 1514 | return 0; |
Eric Van Hensbergen | a80d923 | 2007-10-17 14:31:07 -0500 | [diff] [blame] | 1515 | } |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1516 | |
| 1517 | void p9_trans_fd_exit(void) |
| 1518 | { |
Tejun Heo | 992b3f1 | 2008-10-13 18:45:25 -0500 | [diff] [blame] | 1519 | kthread_stop(p9_poll_task); |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1520 | v9fs_unregister_trans(&p9_tcp_trans); |
| 1521 | v9fs_unregister_trans(&p9_unix_trans); |
| 1522 | v9fs_unregister_trans(&p9_fd_trans); |
Tejun Heo | 206ca50 | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1523 | |
| 1524 | destroy_workqueue(p9_mux_wq); |
Tejun Heo | 72029fe | 2008-09-24 16:22:23 -0500 | [diff] [blame] | 1525 | } |