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