blob: bc5b6965981b9a34958659fa5eb74c4dd8beca53 [file] [log] [blame]
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001/*
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 Hensbergen8a0dc952008-02-06 19:25:03 -06008 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05009 * 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 Hensbergen8a0dc952008-02-06 19:25:03 -060032#include <linux/kthread.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050033#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 Hensbergena80d9232007-10-17 14:31:07 -050040#include <linux/parser.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050041#include <net/9p/9p.h>
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -050042#include <net/9p/client.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050043#include <net/9p/transport.h>
44
45#define P9_PORT 564
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050046#define MAX_SOCK_BUF (64*1024)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060047#define ERREQFLUSH 1
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060048#define MAXPOLLWADDR 2
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050049
Eric Van Hensbergenee443992008-03-05 07:08:09 -060050/**
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 Hensbergena80d9232007-10-17 14:31:07 -050058struct p9_fd_opts {
59 int rfd;
60 int wfd;
61 u16 port;
62};
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050063
Eric Van Hensbergenee443992008-03-05 07:08:09 -060064
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 Ionkovbd238fb2007-07-10 17:57:28 -050073struct p9_trans_fd {
74 struct file *rd;
75 struct file *wr;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060076 struct p9_conn *conn;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050077};
78
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050079/*
80 * Option Parsing (code inspired by NFS code)
81 * - a little lazy - parse all fd-transport options
82 */
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050083
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050084enum {
85 /* Options that take integer arguments */
Latchesar Ionkov55762692007-11-06 08:02:53 -060086 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050087};
88
Steven Whitehousea447c092008-10-13 10:46:57 +010089static const match_table_t tokens = {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050090 {Opt_port, "port=%u"},
91 {Opt_rfdno, "rfdno=%u"},
92 {Opt_wfdno, "wfdno=%u"},
Latchesar Ionkov55762692007-11-06 08:02:53 -060093 {Opt_err, NULL},
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050094};
95
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060096enum {
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
103enum {
104 None,
105 Flushing,
106 Flushed,
107};
108
109struct p9_req;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600110typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600111
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 Hensbergen8a0dc952008-02-06 19:25:03 -0600126struct p9_req {
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600127 spinlock_t lock;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600128 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 Heo992b3f12008-10-13 18:45:25 -0500138struct p9_poll_wait {
139 struct p9_conn *conn;
140 wait_queue_t wait;
141 wait_queue_head_t *wait_addr;
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600142};
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 Hensbergen8b81ef52008-10-13 18:45:25 -0500148 * @client: reference to client instance for this connection
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600149 * @tagpool: id accounting for transactions
150 * @err: error state
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600151 * @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 Hensbergen8a0dc952008-02-06 19:25:03 -0600167
168struct p9_conn {
169 spinlock_t lock; /* protect lock structure */
170 struct list_head mux_list;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500171 struct p9_client *client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600172 struct p9_idpool *tagpool;
173 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600174 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 Heo992b3f12008-10-13 18:45:25 -0500182 struct list_head poll_pending_link;
183 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600184 poll_table pt;
185 struct work_struct rq;
186 struct work_struct wq;
187 unsigned long wsched;
188};
189
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600190/**
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 Hensbergen8a0dc952008-02-06 19:25:03 -0600200
201struct 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
209static int p9_poll_proc(void *);
210static void p9_read_work(struct work_struct *work);
211static void p9_write_work(struct work_struct *work);
212static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
213 poll_table *p);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500214static int p9_fd_write(struct p9_client *client, void *v, int len);
215static int p9_fd_read(struct p9_client *client, void *v, int len);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600216
Tejun Heo992b3f12008-10-13 18:45:25 -0500217static DEFINE_SPINLOCK(p9_poll_lock);
218static LIST_HEAD(p9_poll_pending_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600219static struct workqueue_struct *p9_mux_wq;
Tejun Heo992b3f12008-10-13 18:45:25 -0500220static struct task_struct *p9_poll_task;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600221
222static void p9_conn_destroy(struct p9_conn *);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500223static unsigned int p9_fd_poll(struct p9_client *client,
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600224 struct poll_table_struct *pt);
225
226#ifdef P9_NONBLOCK
227static 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
231static void p9_conn_cancel(struct p9_conn *m, int err);
232
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600233static 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
244static 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600250static void p9_mux_poll_stop(struct p9_conn *m)
251{
Tejun Heo992b3f12008-10-13 18:45:25 -0500252 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600253 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600254
Tejun Heo992b3f12008-10-13 18:45:25 -0500255 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600261 }
262 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500263
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 Hensbergen8a0dc952008-02-06 19:25:03 -0600267}
268
269/**
270 * p9_conn_create - allocate and initialize the per-session mux data
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500271 * @client: client instance
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600272 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600273 * Note: Creates the polling task if this is the first session.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600274 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600275
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500276static struct p9_conn *p9_conn_create(struct p9_client *client)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600277{
278 int i, n;
Tejun Heo571ffea2008-09-24 16:22:23 -0500279 struct p9_conn *m;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600280
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500281 P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize);
Tejun Heo571ffea2008-09-24 16:22:23 -0500282 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600283 if (!m)
284 return ERR_PTR(-ENOMEM);
285
286 spin_lock_init(&m->lock);
287 INIT_LIST_HEAD(&m->mux_list);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500288 m->client = client;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600289 m->tagpool = p9_idpool_create();
290 if (IS_ERR(m->tagpool)) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600291 kfree(m);
Tejun Heo571ffea2008-09-24 16:22:23 -0500292 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600293 }
294
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600295 INIT_LIST_HEAD(&m->req_list);
296 INIT_LIST_HEAD(&m->unsent_req_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600297 INIT_WORK(&m->rq, p9_read_work);
298 INIT_WORK(&m->wq, p9_write_work);
Tejun Heo992b3f12008-10-13 18:45:25 -0500299 INIT_LIST_HEAD(&m->poll_pending_link);
300 init_poll_funcptr(&m->pt, p9_pollwait);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600301
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500302 n = p9_fd_poll(client, &m->pt);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600303 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 Heo992b3f12008-10-13 18:45:25 -0500313 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
314 if (IS_ERR(m->poll_wait[i].wait_addr)) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600315 p9_mux_poll_stop(m);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600316 kfree(m);
Tejun Heo992b3f12008-10-13 18:45:25 -0500317 /* return the error code */
318 return (void *)m->poll_wait[i].wait_addr;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600319 }
320 }
321
322 return m;
323}
324
325/**
326 * p9_mux_destroy - cancels all pending requests and frees mux resources
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600327 * @m: mux to destroy
328 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600329 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600330
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600331static 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600335
336 p9_mux_poll_stop(m);
Tejun Heo7dc5d242008-09-24 16:22:23 -0500337 cancel_work_sync(&m->rq);
338 cancel_work_sync(&m->wq);
339
340 p9_conn_cancel(m, -ECONNRESET);
341
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500342 m->client = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600343 p9_idpool_destroy(m->tagpool);
344 kfree(m);
345}
346
Tejun Heo992b3f12008-10-13 18:45:25 -0500347static 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600364/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600365 * 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600371 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600372
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600373static void
374p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
375{
Tejun Heo992b3f12008-10-13 18:45:25 -0500376 struct p9_conn *m = container_of(p, struct p9_conn, pt);
377 struct p9_poll_wait *pwait = NULL;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600378 int i;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600379
Tejun Heo992b3f12008-10-13 18:45:25 -0500380 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600383 break;
Tejun Heo992b3f12008-10-13 18:45:25 -0500384 }
385 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600386
Tejun Heo992b3f12008-10-13 18:45:25 -0500387 if (!pwait) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600388 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
389 return;
390 }
391
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600392 if (!wait_address) {
393 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
Tejun Heo992b3f12008-10-13 18:45:25 -0500394 pwait->wait_addr = ERR_PTR(-EIO);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600395 return;
396 }
397
Tejun Heo992b3f12008-10-13 18:45:25 -0500398 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600402}
403
404/**
405 * p9_poll_mux - polls a mux and schedules read or write works if necessary
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600406 * @m: connection to poll
407 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600408 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600409
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600410static void p9_poll_mux(struct p9_conn *m)
411{
412 int n;
413
414 if (m->err < 0)
415 return;
416
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500417 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600418 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 Hensbergenee443992008-03-05 07:08:09 -0600446 * 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600452 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600453
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600454static int p9_poll_proc(void *a)
455{
Tejun Heo992b3f12008-10-13 18:45:25 -0500456 unsigned long flags;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600457
Tejun Heo992b3f12008-10-13 18:45:25 -0500458 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600467
Tejun Heo992b3f12008-10-13 18:45:25 -0500468 p9_poll_mux(conn);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600469
Tejun Heo992b3f12008-10-13 18:45:25 -0500470 spin_lock_irqsave(&p9_poll_lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600471 }
Tejun Heo992b3f12008-10-13 18:45:25 -0500472 spin_unlock_irqrestore(&p9_poll_lock, flags);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600473
Tejun Heo992b3f12008-10-13 18:45:25 -0500474 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600479 __set_current_state(TASK_RUNNING);
Tejun Heo992b3f12008-10-13 18:45:25 -0500480
481 if (!kthread_should_stop())
482 goto repeat;
483
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600484 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 Hensbergenee443992008-03-05 07:08:09 -0600490 * @work: container for work to be done
491 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600492 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600493
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600494static 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);
514again:
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 Hensbergen8b81ef52008-10-13 18:45:25 -0500530 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600531 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 Hensbergen8b81ef52008-10-13 18:45:25 -0500552 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600553
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
564error:
565 p9_conn_cancel(m, err);
566 clear_bit(Wworksched, &m->wsched);
567}
568
569static 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 Hensbergenbead27f2008-10-13 18:45:24 -0500581 if (m->client->dotu)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600582 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 Hensbergenee443992008-03-05 07:08:09 -0600604 * @work: container of work to be done
605 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600606 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600607
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600608static 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 Hensbergenbead27f2008-10-13 18:45:24 -0500626 kmalloc(sizeof(struct p9_fcall) + m->client->msize,
627 GFP_KERNEL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600628 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 Hensbergenbead27f2008-10-13 18:45:24 -0500638 err = p9_fd_read(m->client, m->rbuf + m->rpos,
639 m->client->msize - m->rpos);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600640 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 Hensbergenbead27f2008-10-13 18:45:24 -0500652 if (n >= m->client->msize) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600653 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 Hensbergenbead27f2008-10-13 18:45:24 -0500663 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->client->dotu);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600664 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 Hensbergenbead27f2008-10-13 18:45:24 -0500672 m->client->dotu);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600673 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 Hensbergenbead27f2008-10-13 18:45:24 -0500680 m->rcall = kmalloc(sizeof(struct p9_fcall) +
681 m->client->msize, GFP_KERNEL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600682 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600720 }
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 Hensbergen8b81ef52008-10-13 18:45:25 -0500734 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600735
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
746error:
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 Hensbergenee443992008-03-05 07:08:09 -0600762 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600763 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600764
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600765static 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 Lawall62067822008-09-24 16:22:22 -0500786 if (n < 0) {
787 kfree(req);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600788 return ERR_PTR(-ENOMEM);
Julia Lawall62067822008-09-24 16:22:22 -0500789 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600790
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 Hensbergenbead27f2008-10-13 18:45:24 -0500797 p9_printfcall(buf, sizeof(buf), tc, m->client->dotu);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600798 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 Hensbergen8b81ef52008-10-13 18:45:25 -0500818 n = p9_fd_poll(m->client, NULL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600819
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
826static 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
832static void p9_mux_flush_cb(struct p9_req *freq, void *a)
833{
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600834 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600844 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 Hensbergen8a0dc952008-02-06 19:25:03 -0600864 }
865
866 kfree(freq->tcall);
867 kfree(freq->rcall);
868 p9_mux_free_request(m, freq);
869}
870
871static int
872p9_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
913static void
914p9_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 Hensbergen8b81ef52008-10-13 18:45:25 -0500932 * @client: client instance
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600933 * @tc: request to be sent
934 * @rc: pointer where a pointer to the response is stored
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600935 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600936 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600937
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600938int
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500939p9_fd_rpc(struct p9_client *client, struct p9_fcall *tc, struct p9_fcall **rc)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600940{
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -0500941 struct p9_trans_fd *p = client->trans;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600942 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 Hensbergen8b81ef52008-10-13 18:45:25 -0500974 if (err == -ERESTARTSYS && client->status == Connected
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600975 && 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 Hensbergen8b81ef52008-10-13 18:45:25 -0500983 client->status == Connected && !m->err);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600984
985 err = -ERESTARTSYS;
986 }
987 sigpending = 1;
988 }
989
990 if (sigpending) {
991 spin_lock_irqsave(&current->sighand->siglock, flags);
992 recalc_sigpending();
993 spin_unlock_irqrestore(&current->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 Hensbergenee443992008-03-05 07:08:09 -06001014 * @a: value to pass to the callback function
1015 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001016 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001017
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001018int 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 Hensbergenee443992008-03-05 07:08:09 -06001040 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001041 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001042
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001043void 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 Hensbergen8a0dc952008-02-06 19:25:03 -06001069}
1070
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001071/**
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001072 * parse_options - parse mount options into session structure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001073 * @options: options string passed from mount
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001074 * @opts: transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001075 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001076 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001077 */
1078
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001079static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001080{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001081 char *p;
1082 substring_t args[MAX_OPT_ARGS];
1083 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001084 char *options;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001085 int ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001086
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001087 opts->port = P9_PORT;
1088 opts->rfd = ~0;
1089 opts->wfd = ~0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001090
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001091 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 Ionkovbd238fb2007-07-10 17:57:28 -05001100
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001101 while ((p = strsep(&options, ",")) != NULL) {
1102 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001103 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001104 if (!*p)
1105 continue;
1106 token = match_token(p, tokens, args);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001107 r = match_int(&args[0], &option);
1108 if (r < 0) {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001109 P9_DPRINTK(P9_DEBUG_ERROR,
1110 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001111 ret = r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001112 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 Ionkovbd238fb2007-07-10 17:57:28 -05001127 }
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001128 kfree(options);
1129 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001130}
1131
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001132static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001133{
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 Hensbergen8b81ef52008-10-13 18:45:25 -05001150 client->trans = ts;
1151 client->status = Connected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001152
1153 return 0;
1154}
1155
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001156static int p9_socket_open(struct p9_client *client, struct socket *csocket)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001157{
1158 int fd, ret;
1159
1160 csocket->sk->sk_allocation = GFP_NOIO;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001161 fd = sock_map_fd(csocket, 0);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001162 if (fd < 0) {
1163 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1164 return fd;
1165 }
1166
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001167 ret = p9_fd_open(client, fd, fd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001168 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 Hensbergen8b81ef52008-10-13 18:45:25 -05001174 ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001175
1176 return 0;
1177}
1178
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001179/**
1180 * p9_fd_read- read from a fd
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001181 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001182 * @v: buffer to receive data into
1183 * @len: size of receive buffer
1184 *
1185 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001186
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001187static int p9_fd_read(struct p9_client *client, void *v, int len)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001188{
1189 int ret;
1190 struct p9_trans_fd *ts = NULL;
1191
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001192 if (client && client->status != Disconnected)
1193 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001194
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 Hensbergen8b81ef52008-10-13 18:45:25 -05001203 client->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001204 return ret;
1205}
1206
1207/**
1208 * p9_fd_write - write to a socket
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001209 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001210 * @v: buffer to send data from
1211 * @len: size of send buffer
1212 *
1213 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001214
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001215static int p9_fd_write(struct p9_client *client, void *v, int len)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001216{
1217 int ret;
1218 mm_segment_t oldfs;
1219 struct p9_trans_fd *ts = NULL;
1220
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001221 if (client && client->status != Disconnected)
1222 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001223
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 Hensbergen8b81ef52008-10-13 18:45:25 -05001237 client->status = Disconnected;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001238 return ret;
1239}
1240
1241static unsigned int
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001242p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001243{
1244 int ret, n;
1245 struct p9_trans_fd *ts = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001246
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001247 if (client && client->status == Connected)
1248 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001249
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 Ionkovbd238fb2007-07-10 17:57:28 -05001259 ret = ts->rd->f_op->poll(ts->rd, pt);
1260 if (ret < 0)
Tejun Heoec3c68f2008-09-24 16:22:23 -05001261 return ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001262
1263 if (ts->rd != ts->wr) {
1264 n = ts->wr->f_op->poll(ts->wr, pt);
Tejun Heoec3c68f2008-09-24 16:22:23 -05001265 if (n < 0)
1266 return n;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001267 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1268 }
1269
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001270 return ret;
1271}
1272
1273/**
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001274 * p9_fd_close - shutdown file descriptor transport
1275 * @client: client instance
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001276 *
1277 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001278
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001279static void p9_fd_close(struct p9_client *client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001280{
1281 struct p9_trans_fd *ts;
1282
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001283 if (!client)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001284 return;
1285
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001286 ts = client->trans;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001287 if (!ts)
1288 return;
1289
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001290 client->status = Disconnected;
1291
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001292 p9_conn_destroy(ts->conn);
1293
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001294 if (ts->rd)
1295 fput(ts->rd);
1296 if (ts->wr)
1297 fput(ts->wr);
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001298
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001299 kfree(ts);
1300}
1301
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001302/*
1303 * stolen from NFS - maybe should be made a generic function?
1304 */
1305static 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 Hensbergen8b81ef52008-10-13 18:45:25 -05001319static int
1320p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001321{
1322 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001323 struct socket *csocket;
1324 struct sockaddr_in sin_server;
1325 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001326 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001327
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001328 err = parse_opts(args, &opts);
1329 if (err < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001330 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001331
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001332 if (valid_ipaddr4(addr) < 0)
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001333 return -EINVAL;
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001334
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001335 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001336
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 Hensbergen8b81ef52008-10-13 18:45:25 -05001358 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001359 if (err < 0)
1360 goto error;
1361
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001362 p = (struct p9_trans_fd *) client->trans;
1363 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001364 if (IS_ERR(p->conn)) {
1365 err = PTR_ERR(p->conn);
1366 p->conn = NULL;
1367 goto error;
1368 }
1369
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001370 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001371
1372error:
1373 if (csocket)
1374 sock_release(csocket);
1375
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001376 kfree(p);
1377
1378 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001379}
1380
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001381static int
1382p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001383{
1384 int err;
1385 struct socket *csocket;
1386 struct sockaddr_un sun_server;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001387 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001388
1389 csocket = NULL;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001390
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 Hensbergen8b81ef52008-10-13 18:45:25 -05001410 err = p9_socket_open(client, csocket);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001411 if (err < 0)
1412 goto error;
1413
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001414 p = (struct p9_trans_fd *) client->trans;
1415 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001416 if (IS_ERR(p->conn)) {
1417 err = PTR_ERR(p->conn);
1418 p->conn = NULL;
1419 goto error;
1420 }
1421
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001422 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001423
1424error:
1425 if (csocket)
1426 sock_release(csocket);
1427
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001428 kfree(p);
1429 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001430}
1431
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001432static int
1433p9_fd_create(struct p9_client *client, const char *addr, char *args)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001434{
1435 int err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001436 struct p9_fd_opts opts;
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001437 struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001438
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 Hensbergen8b81ef52008-10-13 18:45:25 -05001443 return -ENOPROTOOPT;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001444 }
1445
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001446 err = p9_fd_open(client, opts.rfd, opts.wfd);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001447 if (err < 0)
1448 goto error;
1449
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001450 p = (struct p9_trans_fd *) client->trans;
1451 p->conn = p9_conn_create(client);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001452 if (IS_ERR(p->conn)) {
1453 err = PTR_ERR(p->conn);
1454 p->conn = NULL;
1455 goto error;
1456 }
1457
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001458 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001459
1460error:
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001461 kfree(p);
1462 return err;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001463}
1464
1465static struct p9_trans_module p9_tcp_trans = {
1466 .name = "tcp",
1467 .maxsize = MAX_SOCK_BUF,
1468 .def = 1,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001469 .create = p9_fd_create_tcp,
1470 .close = p9_fd_close,
1471 .rpc = p9_fd_rpc,
Tejun Heo72029fe2008-09-24 16:22:23 -05001472 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001473};
1474
1475static struct p9_trans_module p9_unix_trans = {
1476 .name = "unix",
1477 .maxsize = MAX_SOCK_BUF,
1478 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001479 .create = p9_fd_create_unix,
1480 .close = p9_fd_close,
1481 .rpc = p9_fd_rpc,
Tejun Heo72029fe2008-09-24 16:22:23 -05001482 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001483};
1484
1485static struct p9_trans_module p9_fd_trans = {
1486 .name = "fd",
1487 .maxsize = MAX_SOCK_BUF,
1488 .def = 0,
Eric Van Hensbergen8b81ef52008-10-13 18:45:25 -05001489 .create = p9_fd_create,
1490 .close = p9_fd_close,
1491 .rpc = p9_fd_rpc,
Tejun Heo72029fe2008-09-24 16:22:23 -05001492 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001493};
1494
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001495int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001496{
Tejun Heo206ca502008-09-24 16:22:23 -05001497 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 Hensbergen8a0dc952008-02-06 19:25:03 -06001501 }
1502
Tejun Heo992b3f12008-10-13 18:45:25 -05001503 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 Hensbergena80d9232007-10-17 14:31:07 -05001510 v9fs_register_trans(&p9_tcp_trans);
1511 v9fs_register_trans(&p9_unix_trans);
1512 v9fs_register_trans(&p9_fd_trans);
1513
Andrew Morton3387b802008-03-28 14:15:57 -07001514 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001515}
Tejun Heo72029fe2008-09-24 16:22:23 -05001516
1517void p9_trans_fd_exit(void)
1518{
Tejun Heo992b3f12008-10-13 18:45:25 -05001519 kthread_stop(p9_poll_task);
Tejun Heo72029fe2008-09-24 16:22:23 -05001520 v9fs_unregister_trans(&p9_tcp_trans);
1521 v9fs_unregister_trans(&p9_unix_trans);
1522 v9fs_unregister_trans(&p9_fd_trans);
Tejun Heo206ca502008-09-24 16:22:23 -05001523
1524 destroy_workqueue(p9_mux_wq);
Tejun Heo72029fe2008-09-24 16:22:23 -05001525}