blob: 0b4eb5f78356332f9172d98ecc4f7140623112bc [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>
42#include <net/9p/transport.h>
43
44#define P9_PORT 564
Eric Van Hensbergena80d9232007-10-17 14:31:07 -050045#define MAX_SOCK_BUF (64*1024)
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -060046#define ERREQFLUSH 1
47#define SCHED_TIMEOUT 10
48#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
89static match_table_t tokens = {
90 {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
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600138struct p9_mux_poll_task {
139 struct task_struct *task;
140 struct list_head mux_list;
141 int muxnum;
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 (?)
148 * @poll_task: task polling on this connection
149 * @msize: maximum size for connection (dup)
150 * @extended: 9p2000.u flag (dup)
151 * @trans: reference to transport instance for this connection
152 * @tagpool: id accounting for transactions
153 * @err: error state
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600154 * @req_list: accounting for requests which have been sent
155 * @unsent_req_list: accounting for requests that haven't been sent
156 * @rcall: current response &p9_fcall structure
157 * @rpos: read position in current frame
158 * @rbuf: current read buffer
159 * @wpos: write position for current frame
160 * @wsize: amount of data to write for current frame
161 * @wbuf: current write buffer
162 * @poll_wait: array of wait_q's for various worker threads
163 * @poll_waddr: ????
164 * @pt: poll state
165 * @rq: current read work
166 * @wq: current write work
167 * @wsched: ????
168 *
169 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600170
171struct p9_conn {
172 spinlock_t lock; /* protect lock structure */
173 struct list_head mux_list;
174 struct p9_mux_poll_task *poll_task;
175 int msize;
176 unsigned char extended;
177 struct p9_trans *trans;
178 struct p9_idpool *tagpool;
179 int err;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600180 struct list_head req_list;
181 struct list_head unsent_req_list;
182 struct p9_fcall *rcall;
183 int rpos;
184 char *rbuf;
185 int wpos;
186 int wsize;
187 char *wbuf;
188 wait_queue_t poll_wait[MAXPOLLWADDR];
189 wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
190 poll_table pt;
191 struct work_struct rq;
192 struct work_struct wq;
193 unsigned long wsched;
194};
195
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600196/**
197 * struct p9_mux_rpc - fd mux rpc accounting structure
198 * @m: connection this request was issued on
199 * @err: error state
200 * @tcall: request &p9_fcall
201 * @rcall: response &p9_fcall
202 * @wqueue: wait queue that client is blocked on for this rpc
203 *
204 * Bug: isn't this information duplicated elsewhere like &p9_req
205 */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600206
207struct p9_mux_rpc {
208 struct p9_conn *m;
209 int err;
210 struct p9_fcall *tcall;
211 struct p9_fcall *rcall;
212 wait_queue_head_t wqueue;
213};
214
215static int p9_poll_proc(void *);
216static void p9_read_work(struct work_struct *work);
217static void p9_write_work(struct work_struct *work);
218static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
219 poll_table *p);
220static int p9_fd_write(struct p9_trans *trans, void *v, int len);
221static int p9_fd_read(struct p9_trans *trans, void *v, int len);
222
223static DEFINE_MUTEX(p9_mux_task_lock);
224static struct workqueue_struct *p9_mux_wq;
225
226static int p9_mux_num;
227static int p9_mux_poll_task_num;
228static struct p9_mux_poll_task p9_mux_poll_tasks[100];
229
230static void p9_conn_destroy(struct p9_conn *);
231static unsigned int p9_fd_poll(struct p9_trans *trans,
232 struct poll_table_struct *pt);
233
234#ifdef P9_NONBLOCK
235static int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
236 p9_conn_req_callback cb, void *a);
237#endif /* P9_NONBLOCK */
238
239static void p9_conn_cancel(struct p9_conn *m, int err);
240
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600241static u16 p9_mux_get_tag(struct p9_conn *m)
242{
243 int tag;
244
245 tag = p9_idpool_get(m->tagpool);
246 if (tag < 0)
247 return P9_NOTAG;
248 else
249 return (u16) tag;
250}
251
252static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
253{
254 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
255 p9_idpool_put(tag, m->tagpool);
256}
257
258/**
259 * p9_mux_calc_poll_procs - calculates the number of polling procs
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600260 * @muxnum: number of mounts
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600261 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600262 * Calculation is based on the number of mounted v9fs filesystems.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600263 * The current implementation returns sqrt of the number of mounts.
264 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600265
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600266static int p9_mux_calc_poll_procs(int muxnum)
267{
268 int n;
269
270 if (p9_mux_poll_task_num)
271 n = muxnum / p9_mux_poll_task_num +
272 (muxnum % p9_mux_poll_task_num ? 1 : 0);
273 else
274 n = 1;
275
276 if (n > ARRAY_SIZE(p9_mux_poll_tasks))
277 n = ARRAY_SIZE(p9_mux_poll_tasks);
278
279 return n;
280}
281
282static int p9_mux_poll_start(struct p9_conn *m)
283{
284 int i, n;
285 struct p9_mux_poll_task *vpt, *vptlast;
286 struct task_struct *pproc;
287
288 P9_DPRINTK(P9_DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, p9_mux_num,
289 p9_mux_poll_task_num);
290 mutex_lock(&p9_mux_task_lock);
291
292 n = p9_mux_calc_poll_procs(p9_mux_num + 1);
293 if (n > p9_mux_poll_task_num) {
294 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
295 if (p9_mux_poll_tasks[i].task == NULL) {
296 vpt = &p9_mux_poll_tasks[i];
297 P9_DPRINTK(P9_DEBUG_MUX, "create proc %p\n",
298 vpt);
299 pproc = kthread_create(p9_poll_proc, vpt,
300 "v9fs-poll");
301
302 if (!IS_ERR(pproc)) {
303 vpt->task = pproc;
304 INIT_LIST_HEAD(&vpt->mux_list);
305 vpt->muxnum = 0;
306 p9_mux_poll_task_num++;
307 wake_up_process(vpt->task);
308 }
309 break;
310 }
311 }
312
313 if (i >= ARRAY_SIZE(p9_mux_poll_tasks))
314 P9_DPRINTK(P9_DEBUG_ERROR,
315 "warning: no free poll slots\n");
316 }
317
318 n = (p9_mux_num + 1) / p9_mux_poll_task_num +
319 ((p9_mux_num + 1) % p9_mux_poll_task_num ? 1 : 0);
320
321 vptlast = NULL;
322 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++) {
323 vpt = &p9_mux_poll_tasks[i];
324 if (vpt->task != NULL) {
325 vptlast = vpt;
326 if (vpt->muxnum < n) {
327 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
328 list_add(&m->mux_list, &vpt->mux_list);
329 vpt->muxnum++;
330 m->poll_task = vpt;
331 memset(&m->poll_waddr, 0,
332 sizeof(m->poll_waddr));
333 init_poll_funcptr(&m->pt, p9_pollwait);
334 break;
335 }
336 }
337 }
338
339 if (i >= ARRAY_SIZE(p9_mux_poll_tasks)) {
340 if (vptlast == NULL) {
341 mutex_unlock(&p9_mux_task_lock);
342 return -ENOMEM;
343 }
344
345 P9_DPRINTK(P9_DEBUG_MUX, "put in proc %d\n", i);
346 list_add(&m->mux_list, &vptlast->mux_list);
347 vptlast->muxnum++;
348 m->poll_task = vptlast;
349 memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
350 init_poll_funcptr(&m->pt, p9_pollwait);
351 }
352
353 p9_mux_num++;
354 mutex_unlock(&p9_mux_task_lock);
355
356 return 0;
357}
358
359static void p9_mux_poll_stop(struct p9_conn *m)
360{
361 int i;
362 struct p9_mux_poll_task *vpt;
363
364 mutex_lock(&p9_mux_task_lock);
365 vpt = m->poll_task;
366 list_del(&m->mux_list);
367 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
368 if (m->poll_waddr[i] != NULL) {
369 remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
370 m->poll_waddr[i] = NULL;
371 }
372 }
373 vpt->muxnum--;
374 if (!vpt->muxnum) {
375 P9_DPRINTK(P9_DEBUG_MUX, "destroy proc %p\n", vpt);
376 kthread_stop(vpt->task);
377 vpt->task = NULL;
378 p9_mux_poll_task_num--;
379 }
380 p9_mux_num--;
381 mutex_unlock(&p9_mux_task_lock);
382}
383
384/**
385 * p9_conn_create - allocate and initialize the per-session mux data
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600386 * @trans: transport structure
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600387 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600388 * Note: Creates the polling task if this is the first session.
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600389 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600390
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600391static struct p9_conn *p9_conn_create(struct p9_trans *trans)
392{
393 int i, n;
Tejun Heo571ffea2008-09-24 16:22:23 -0500394 struct p9_conn *m;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600395
396 P9_DPRINTK(P9_DEBUG_MUX, "transport %p msize %d\n", trans,
397 trans->msize);
Tejun Heo571ffea2008-09-24 16:22:23 -0500398 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600399 if (!m)
400 return ERR_PTR(-ENOMEM);
401
402 spin_lock_init(&m->lock);
403 INIT_LIST_HEAD(&m->mux_list);
404 m->msize = trans->msize;
405 m->extended = trans->extended;
406 m->trans = trans;
407 m->tagpool = p9_idpool_create();
408 if (IS_ERR(m->tagpool)) {
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600409 kfree(m);
Tejun Heo571ffea2008-09-24 16:22:23 -0500410 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600411 }
412
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600413 INIT_LIST_HEAD(&m->req_list);
414 INIT_LIST_HEAD(&m->unsent_req_list);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600415 INIT_WORK(&m->rq, p9_read_work);
416 INIT_WORK(&m->wq, p9_write_work);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600417 n = p9_mux_poll_start(m);
418 if (n) {
419 kfree(m);
420 return ERR_PTR(n);
421 }
422
423 n = p9_fd_poll(trans, &m->pt);
424 if (n & POLLIN) {
425 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
426 set_bit(Rpending, &m->wsched);
427 }
428
429 if (n & POLLOUT) {
430 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
431 set_bit(Wpending, &m->wsched);
432 }
433
434 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
435 if (IS_ERR(m->poll_waddr[i])) {
436 p9_mux_poll_stop(m);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600437 kfree(m);
Tejun Heo571ffea2008-09-24 16:22:23 -0500438 return (void *)m->poll_waddr; /* the error code */
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600439 }
440 }
441
442 return m;
443}
444
445/**
446 * p9_mux_destroy - cancels all pending requests and frees mux resources
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600447 * @m: mux to destroy
448 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600449 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600450
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600451static void p9_conn_destroy(struct p9_conn *m)
452{
453 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
454 m->mux_list.prev, m->mux_list.next);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600455
456 p9_mux_poll_stop(m);
Tejun Heo7dc5d242008-09-24 16:22:23 -0500457 cancel_work_sync(&m->rq);
458 cancel_work_sync(&m->wq);
459
460 p9_conn_cancel(m, -ECONNRESET);
461
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600462 m->trans = NULL;
463 p9_idpool_destroy(m->tagpool);
464 kfree(m);
465}
466
467/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600468 * p9_pollwait - add poll task to the wait queue
469 * @filp: file pointer being polled
470 * @wait_address: wait_q to block on
471 * @p: poll state
472 *
473 * called by files poll operation to add v9fs-poll task to files wait queue
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600474 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600475
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600476static void
477p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
478{
479 int i;
480 struct p9_conn *m;
481
482 m = container_of(p, struct p9_conn, pt);
483 for (i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
484 if (m->poll_waddr[i] == NULL)
485 break;
486
487 if (i >= ARRAY_SIZE(m->poll_waddr)) {
488 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
489 return;
490 }
491
492 m->poll_waddr[i] = wait_address;
493
494 if (!wait_address) {
495 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
496 m->poll_waddr[i] = ERR_PTR(-EIO);
497 return;
498 }
499
500 init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
501 add_wait_queue(wait_address, &m->poll_wait[i]);
502}
503
504/**
505 * p9_poll_mux - polls a mux and schedules read or write works if necessary
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600506 * @m: connection to poll
507 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600508 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600509
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600510static void p9_poll_mux(struct p9_conn *m)
511{
512 int n;
513
514 if (m->err < 0)
515 return;
516
517 n = p9_fd_poll(m->trans, NULL);
518 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
519 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
520 if (n >= 0)
521 n = -ECONNRESET;
522 p9_conn_cancel(m, n);
523 }
524
525 if (n & POLLIN) {
526 set_bit(Rpending, &m->wsched);
527 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
528 if (!test_and_set_bit(Rworksched, &m->wsched)) {
529 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
530 queue_work(p9_mux_wq, &m->rq);
531 }
532 }
533
534 if (n & POLLOUT) {
535 set_bit(Wpending, &m->wsched);
536 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
537 if ((m->wsize || !list_empty(&m->unsent_req_list))
538 && !test_and_set_bit(Wworksched, &m->wsched)) {
539 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
540 queue_work(p9_mux_wq, &m->wq);
541 }
542 }
543}
544
545/**
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600546 * p9_poll_proc - poll worker thread
547 * @a: thread state and arguments
548 *
549 * polls all v9fs transports for new events and queues the appropriate
550 * work to the work queue
551 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600552 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600553
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600554static int p9_poll_proc(void *a)
555{
556 struct p9_conn *m, *mtmp;
557 struct p9_mux_poll_task *vpt;
558
559 vpt = a;
560 P9_DPRINTK(P9_DEBUG_MUX, "start %p %p\n", current, vpt);
561 while (!kthread_should_stop()) {
562 set_current_state(TASK_INTERRUPTIBLE);
563
564 list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
565 p9_poll_mux(m);
566 }
567
568 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
569 schedule_timeout(SCHED_TIMEOUT * HZ);
570 }
571
572 __set_current_state(TASK_RUNNING);
573 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
574 return 0;
575}
576
577/**
578 * p9_write_work - called when a transport can send some data
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600579 * @work: container for work to be done
580 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600581 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600582
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600583static void p9_write_work(struct work_struct *work)
584{
585 int n, err;
586 struct p9_conn *m;
587 struct p9_req *req;
588
589 m = container_of(work, struct p9_conn, wq);
590
591 if (m->err < 0) {
592 clear_bit(Wworksched, &m->wsched);
593 return;
594 }
595
596 if (!m->wsize) {
597 if (list_empty(&m->unsent_req_list)) {
598 clear_bit(Wworksched, &m->wsched);
599 return;
600 }
601
602 spin_lock(&m->lock);
603again:
604 req = list_entry(m->unsent_req_list.next, struct p9_req,
605 req_list);
606 list_move_tail(&req->req_list, &m->req_list);
607 if (req->err == ERREQFLUSH)
608 goto again;
609
610 m->wbuf = req->tcall->sdata;
611 m->wsize = req->tcall->size;
612 m->wpos = 0;
613 spin_unlock(&m->lock);
614 }
615
616 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
617 m->wsize);
618 clear_bit(Wpending, &m->wsched);
619 err = p9_fd_write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
620 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
621 if (err == -EAGAIN) {
622 clear_bit(Wworksched, &m->wsched);
623 return;
624 }
625
626 if (err < 0)
627 goto error;
628 else if (err == 0) {
629 err = -EREMOTEIO;
630 goto error;
631 }
632
633 m->wpos += err;
634 if (m->wpos == m->wsize)
635 m->wpos = m->wsize = 0;
636
637 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
638 if (test_and_clear_bit(Wpending, &m->wsched))
639 n = POLLOUT;
640 else
641 n = p9_fd_poll(m->trans, NULL);
642
643 if (n & POLLOUT) {
644 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
645 queue_work(p9_mux_wq, &m->wq);
646 } else
647 clear_bit(Wworksched, &m->wsched);
648 } else
649 clear_bit(Wworksched, &m->wsched);
650
651 return;
652
653error:
654 p9_conn_cancel(m, err);
655 clear_bit(Wworksched, &m->wsched);
656}
657
658static void process_request(struct p9_conn *m, struct p9_req *req)
659{
660 int ecode;
661 struct p9_str *ename;
662
663 if (!req->err && req->rcall->id == P9_RERROR) {
664 ecode = req->rcall->params.rerror.errno;
665 ename = &req->rcall->params.rerror.error;
666
667 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
668 ename->str);
669
670 if (m->extended)
671 req->err = -ecode;
672
673 if (!req->err) {
674 req->err = p9_errstr2errno(ename->str, ename->len);
675
676 /* string match failed */
677 if (!req->err) {
678 PRINT_FCALL_ERROR("unknown error", req->rcall);
679 req->err = -ESERVERFAULT;
680 }
681 }
682 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
683 P9_DPRINTK(P9_DEBUG_ERROR,
684 "fcall mismatch: expected %d, got %d\n",
685 req->tcall->id + 1, req->rcall->id);
686 if (!req->err)
687 req->err = -EIO;
688 }
689}
690
691/**
692 * p9_read_work - called when there is some data to be read from a transport
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600693 * @work: container of work to be done
694 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600695 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600696
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600697static void p9_read_work(struct work_struct *work)
698{
699 int n, err;
700 struct p9_conn *m;
701 struct p9_req *req, *rptr, *rreq;
702 struct p9_fcall *rcall;
703 char *rbuf;
704
705 m = container_of(work, struct p9_conn, rq);
706
707 if (m->err < 0)
708 return;
709
710 rcall = NULL;
711 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
712
713 if (!m->rcall) {
714 m->rcall =
715 kmalloc(sizeof(struct p9_fcall) + m->msize, GFP_KERNEL);
716 if (!m->rcall) {
717 err = -ENOMEM;
718 goto error;
719 }
720
721 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
722 m->rpos = 0;
723 }
724
725 clear_bit(Rpending, &m->wsched);
726 err = p9_fd_read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
727 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
728 if (err == -EAGAIN) {
729 clear_bit(Rworksched, &m->wsched);
730 return;
731 }
732
733 if (err <= 0)
734 goto error;
735
736 m->rpos += err;
737 while (m->rpos > 4) {
738 n = le32_to_cpu(*(__le32 *) m->rbuf);
739 if (n >= m->msize) {
740 P9_DPRINTK(P9_DEBUG_ERROR,
741 "requested packet size too big: %d\n", n);
742 err = -EIO;
743 goto error;
744 }
745
746 if (m->rpos < n)
747 break;
748
749 err =
750 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->extended);
751 if (err < 0)
752 goto error;
753
754#ifdef CONFIG_NET_9P_DEBUG
755 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
756 char buf[150];
757
758 p9_printfcall(buf, sizeof(buf), m->rcall,
759 m->extended);
760 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
761 }
762#endif
763
764 rcall = m->rcall;
765 rbuf = m->rbuf;
766 if (m->rpos > n) {
767 m->rcall = kmalloc(sizeof(struct p9_fcall) + m->msize,
768 GFP_KERNEL);
769 if (!m->rcall) {
770 err = -ENOMEM;
771 goto error;
772 }
773
774 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
775 memmove(m->rbuf, rbuf + n, m->rpos - n);
776 m->rpos -= n;
777 } else {
778 m->rcall = NULL;
779 m->rbuf = NULL;
780 m->rpos = 0;
781 }
782
783 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
784 rcall->id, rcall->tag);
785
786 req = NULL;
787 spin_lock(&m->lock);
788 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
789 if (rreq->tag == rcall->tag) {
790 req = rreq;
791 if (req->flush != Flushing)
792 list_del(&req->req_list);
793 break;
794 }
795 }
796 spin_unlock(&m->lock);
797
798 if (req) {
799 req->rcall = rcall;
800 process_request(m, req);
801
802 if (req->flush != Flushing) {
803 if (req->cb)
804 (*req->cb) (req, req->cba);
805 else
806 kfree(req->rcall);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600807 }
808 } else {
809 if (err >= 0 && rcall->id != P9_RFLUSH)
810 P9_DPRINTK(P9_DEBUG_ERROR,
811 "unexpected response mux %p id %d tag %d\n",
812 m, rcall->id, rcall->tag);
813 kfree(rcall);
814 }
815 }
816
817 if (!list_empty(&m->req_list)) {
818 if (test_and_clear_bit(Rpending, &m->wsched))
819 n = POLLIN;
820 else
821 n = p9_fd_poll(m->trans, NULL);
822
823 if (n & POLLIN) {
824 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
825 queue_work(p9_mux_wq, &m->rq);
826 } else
827 clear_bit(Rworksched, &m->wsched);
828 } else
829 clear_bit(Rworksched, &m->wsched);
830
831 return;
832
833error:
834 p9_conn_cancel(m, err);
835 clear_bit(Rworksched, &m->wsched);
836}
837
838/**
839 * p9_send_request - send 9P request
840 * The function can sleep until the request is scheduled for sending.
841 * The function can be interrupted. Return from the function is not
842 * a guarantee that the request is sent successfully. Can return errors
843 * that can be retrieved by PTR_ERR macros.
844 *
845 * @m: mux data
846 * @tc: request to be sent
847 * @cb: callback function to call when response is received
848 * @cba: parameter to pass to the callback function
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600849 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600850 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600851
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600852static struct p9_req *p9_send_request(struct p9_conn *m,
853 struct p9_fcall *tc,
854 p9_conn_req_callback cb, void *cba)
855{
856 int n;
857 struct p9_req *req;
858
859 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
860 tc, tc->id);
861 if (m->err < 0)
862 return ERR_PTR(m->err);
863
864 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
865 if (!req)
866 return ERR_PTR(-ENOMEM);
867
868 if (tc->id == P9_TVERSION)
869 n = P9_NOTAG;
870 else
871 n = p9_mux_get_tag(m);
872
873 if (n < 0)
874 return ERR_PTR(-ENOMEM);
875
876 p9_set_tag(tc, n);
877
878#ifdef CONFIG_NET_9P_DEBUG
879 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
880 char buf[150];
881
882 p9_printfcall(buf, sizeof(buf), tc, m->extended);
883 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
884 }
885#endif
886
887 spin_lock_init(&req->lock);
888 req->tag = n;
889 req->tcall = tc;
890 req->rcall = NULL;
891 req->err = 0;
892 req->cb = cb;
893 req->cba = cba;
894 req->flush = None;
895
896 spin_lock(&m->lock);
897 list_add_tail(&req->req_list, &m->unsent_req_list);
898 spin_unlock(&m->lock);
899
900 if (test_and_clear_bit(Wpending, &m->wsched))
901 n = POLLOUT;
902 else
903 n = p9_fd_poll(m->trans, NULL);
904
905 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
906 queue_work(p9_mux_wq, &m->wq);
907
908 return req;
909}
910
911static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
912{
913 p9_mux_put_tag(m, req->tag);
914 kfree(req);
915}
916
917static void p9_mux_flush_cb(struct p9_req *freq, void *a)
918{
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600919 int tag;
920 struct p9_conn *m;
921 struct p9_req *req, *rreq, *rptr;
922
923 m = a;
924 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
925 freq->tcall, freq->rcall, freq->err,
926 freq->tcall->params.tflush.oldtag);
927
928 spin_lock(&m->lock);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600929 tag = freq->tcall->params.tflush.oldtag;
930 req = NULL;
931 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
932 if (rreq->tag == tag) {
933 req = rreq;
934 list_del(&req->req_list);
935 break;
936 }
937 }
938 spin_unlock(&m->lock);
939
940 if (req) {
941 spin_lock(&req->lock);
942 req->flush = Flushed;
943 spin_unlock(&req->lock);
944
945 if (req->cb)
946 (*req->cb) (req, req->cba);
947 else
948 kfree(req->rcall);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -0600949 }
950
951 kfree(freq->tcall);
952 kfree(freq->rcall);
953 p9_mux_free_request(m, freq);
954}
955
956static int
957p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
958{
959 struct p9_fcall *fc;
960 struct p9_req *rreq, *rptr;
961
962 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
963
964 /* if a response was received for a request, do nothing */
965 spin_lock(&req->lock);
966 if (req->rcall || req->err) {
967 spin_unlock(&req->lock);
968 P9_DPRINTK(P9_DEBUG_MUX,
969 "mux %p req %p response already received\n", m, req);
970 return 0;
971 }
972
973 req->flush = Flushing;
974 spin_unlock(&req->lock);
975
976 spin_lock(&m->lock);
977 /* if the request is not sent yet, just remove it from the list */
978 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
979 if (rreq->tag == req->tag) {
980 P9_DPRINTK(P9_DEBUG_MUX,
981 "mux %p req %p request is not sent yet\n", m, req);
982 list_del(&rreq->req_list);
983 req->flush = Flushed;
984 spin_unlock(&m->lock);
985 if (req->cb)
986 (*req->cb) (req, req->cba);
987 return 0;
988 }
989 }
990 spin_unlock(&m->lock);
991
992 clear_thread_flag(TIF_SIGPENDING);
993 fc = p9_create_tflush(req->tag);
994 p9_send_request(m, fc, p9_mux_flush_cb, m);
995 return 1;
996}
997
998static void
999p9_conn_rpc_cb(struct p9_req *req, void *a)
1000{
1001 struct p9_mux_rpc *r;
1002
1003 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
1004 r = a;
1005 r->rcall = req->rcall;
1006 r->err = req->err;
1007
1008 if (req->flush != None && !req->err)
1009 r->err = -ERESTARTSYS;
1010
1011 wake_up(&r->wqueue);
1012}
1013
1014/**
1015 * p9_fd_rpc- sends 9P request and waits until a response is available.
1016 * The function can be interrupted.
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001017 * @t: transport data
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001018 * @tc: request to be sent
1019 * @rc: pointer where a pointer to the response is stored
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001020 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001021 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001022
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001023int
1024p9_fd_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
1025{
1026 struct p9_trans_fd *p = t->priv;
1027 struct p9_conn *m = p->conn;
1028 int err, sigpending;
1029 unsigned long flags;
1030 struct p9_req *req;
1031 struct p9_mux_rpc r;
1032
1033 r.err = 0;
1034 r.tcall = tc;
1035 r.rcall = NULL;
1036 r.m = m;
1037 init_waitqueue_head(&r.wqueue);
1038
1039 if (rc)
1040 *rc = NULL;
1041
1042 sigpending = 0;
1043 if (signal_pending(current)) {
1044 sigpending = 1;
1045 clear_thread_flag(TIF_SIGPENDING);
1046 }
1047
1048 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
1049 if (IS_ERR(req)) {
1050 err = PTR_ERR(req);
1051 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1052 return err;
1053 }
1054
1055 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
1056 if (r.err < 0)
1057 err = r.err;
1058
1059 if (err == -ERESTARTSYS && m->trans->status == Connected
1060 && m->err == 0) {
1061 if (p9_mux_flush_request(m, req)) {
1062 /* wait until we get response of the flush message */
1063 do {
1064 clear_thread_flag(TIF_SIGPENDING);
1065 err = wait_event_interruptible(r.wqueue,
1066 r.rcall || r.err);
1067 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
1068 m->trans->status == Connected && !m->err);
1069
1070 err = -ERESTARTSYS;
1071 }
1072 sigpending = 1;
1073 }
1074
1075 if (sigpending) {
1076 spin_lock_irqsave(&current->sighand->siglock, flags);
1077 recalc_sigpending();
1078 spin_unlock_irqrestore(&current->sighand->siglock, flags);
1079 }
1080
1081 if (rc)
1082 *rc = r.rcall;
1083 else
1084 kfree(r.rcall);
1085
1086 p9_mux_free_request(m, req);
1087 if (err > 0)
1088 err = -EIO;
1089
1090 return err;
1091}
1092
1093#ifdef P9_NONBLOCK
1094/**
1095 * p9_conn_rpcnb - sends 9P request without waiting for response.
1096 * @m: mux data
1097 * @tc: request to be sent
1098 * @cb: callback function to be called when response arrives
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001099 * @a: value to pass to the callback function
1100 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001101 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001102
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001103int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1104 p9_conn_req_callback cb, void *a)
1105{
1106 int err;
1107 struct p9_req *req;
1108
1109 req = p9_send_request(m, tc, cb, a);
1110 if (IS_ERR(req)) {
1111 err = PTR_ERR(req);
1112 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1113 return PTR_ERR(req);
1114 }
1115
1116 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1117 return 0;
1118}
1119#endif /* P9_NONBLOCK */
1120
1121/**
1122 * p9_conn_cancel - cancel all pending requests with error
1123 * @m: mux data
1124 * @err: error code
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001125 *
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001126 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001127
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001128void p9_conn_cancel(struct p9_conn *m, int err)
1129{
1130 struct p9_req *req, *rtmp;
1131 LIST_HEAD(cancel_list);
1132
1133 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1134 m->err = err;
1135 spin_lock(&m->lock);
1136 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1137 list_move(&req->req_list, &cancel_list);
1138 }
1139 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1140 list_move(&req->req_list, &cancel_list);
1141 }
1142 spin_unlock(&m->lock);
1143
1144 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1145 list_del(&req->req_list);
1146 if (!req->err)
1147 req->err = err;
1148
1149 if (req->cb)
1150 (*req->cb) (req, req->cba);
1151 else
1152 kfree(req->rcall);
1153 }
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001154}
1155
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001156/**
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001157 * parse_options - parse mount options into session structure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001158 * @options: options string passed from mount
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001159 * @opts: transport-specific structure to parse options into
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001160 *
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001161 * Returns 0 upon success, -ERRNO upon failure
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001162 */
1163
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001164static int parse_opts(char *params, struct p9_fd_opts *opts)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001165{
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001166 char *p;
1167 substring_t args[MAX_OPT_ARGS];
1168 int option;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001169 char *options;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001170 int ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001171
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001172 opts->port = P9_PORT;
1173 opts->rfd = ~0;
1174 opts->wfd = ~0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001175
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001176 if (!params)
1177 return 0;
1178
1179 options = kstrdup(params, GFP_KERNEL);
1180 if (!options) {
1181 P9_DPRINTK(P9_DEBUG_ERROR,
1182 "failed to allocate copy of option string\n");
1183 return -ENOMEM;
1184 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001185
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001186 while ((p = strsep(&options, ",")) != NULL) {
1187 int token;
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001188 int r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001189 if (!*p)
1190 continue;
1191 token = match_token(p, tokens, args);
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001192 r = match_int(&args[0], &option);
1193 if (r < 0) {
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001194 P9_DPRINTK(P9_DEBUG_ERROR,
1195 "integer field, but no integer?\n");
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001196 ret = r;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001197 continue;
1198 }
1199 switch (token) {
1200 case Opt_port:
1201 opts->port = option;
1202 break;
1203 case Opt_rfdno:
1204 opts->rfd = option;
1205 break;
1206 case Opt_wfdno:
1207 opts->wfd = option;
1208 break;
1209 default:
1210 continue;
1211 }
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001212 }
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001213 kfree(options);
1214 return 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001215}
1216
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001217static int p9_fd_open(struct p9_trans *trans, int rfd, int wfd)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001218{
1219 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1220 GFP_KERNEL);
1221 if (!ts)
1222 return -ENOMEM;
1223
1224 ts->rd = fget(rfd);
1225 ts->wr = fget(wfd);
1226 if (!ts->rd || !ts->wr) {
1227 if (ts->rd)
1228 fput(ts->rd);
1229 if (ts->wr)
1230 fput(ts->wr);
1231 kfree(ts);
1232 return -EIO;
1233 }
1234
1235 trans->priv = ts;
1236 trans->status = Connected;
1237
1238 return 0;
1239}
1240
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001241static int p9_socket_open(struct p9_trans *trans, struct socket *csocket)
1242{
1243 int fd, ret;
1244
1245 csocket->sk->sk_allocation = GFP_NOIO;
Ulrich Dreppera677a032008-07-23 21:29:17 -07001246 fd = sock_map_fd(csocket, 0);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001247 if (fd < 0) {
1248 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1249 return fd;
1250 }
1251
1252 ret = p9_fd_open(trans, fd, fd);
1253 if (ret < 0) {
1254 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1255 sockfd_put(csocket);
1256 return ret;
1257 }
1258
1259 ((struct p9_trans_fd *)trans->priv)->rd->f_flags |= O_NONBLOCK;
1260
1261 return 0;
1262}
1263
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001264/**
1265 * p9_fd_read- read from a fd
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001266 * @trans: transport instance state
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001267 * @v: buffer to receive data into
1268 * @len: size of receive buffer
1269 *
1270 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001271
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001272static int p9_fd_read(struct p9_trans *trans, void *v, int len)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001273{
1274 int ret;
1275 struct p9_trans_fd *ts = NULL;
1276
1277 if (trans && trans->status != Disconnected)
1278 ts = trans->priv;
1279
1280 if (!ts)
1281 return -EREMOTEIO;
1282
1283 if (!(ts->rd->f_flags & O_NONBLOCK))
1284 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
1285
1286 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
1287 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1288 trans->status = Disconnected;
1289 return ret;
1290}
1291
1292/**
1293 * p9_fd_write - write to a socket
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001294 * @trans: transport instance state
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001295 * @v: buffer to send data from
1296 * @len: size of send buffer
1297 *
1298 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001299
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001300static int p9_fd_write(struct p9_trans *trans, void *v, int len)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001301{
1302 int ret;
1303 mm_segment_t oldfs;
1304 struct p9_trans_fd *ts = NULL;
1305
1306 if (trans && trans->status != Disconnected)
1307 ts = trans->priv;
1308
1309 if (!ts)
1310 return -EREMOTEIO;
1311
1312 if (!(ts->wr->f_flags & O_NONBLOCK))
1313 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
1314
1315 oldfs = get_fs();
1316 set_fs(get_ds());
1317 /* The cast to a user pointer is valid due to the set_fs() */
1318 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
1319 set_fs(oldfs);
1320
1321 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1322 trans->status = Disconnected;
1323 return ret;
1324}
1325
1326static unsigned int
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001327p9_fd_poll(struct p9_trans *trans, struct poll_table_struct *pt)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001328{
1329 int ret, n;
1330 struct p9_trans_fd *ts = NULL;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001331
1332 if (trans && trans->status == Connected)
1333 ts = trans->priv;
1334
1335 if (!ts)
1336 return -EREMOTEIO;
1337
1338 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1339 return -EIO;
1340
1341 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1342 return -EIO;
1343
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001344 ret = ts->rd->f_op->poll(ts->rd, pt);
1345 if (ret < 0)
Tejun Heoec3c68f2008-09-24 16:22:23 -05001346 return ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001347
1348 if (ts->rd != ts->wr) {
1349 n = ts->wr->f_op->poll(ts->wr, pt);
Tejun Heoec3c68f2008-09-24 16:22:23 -05001350 if (n < 0)
1351 return n;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001352 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1353 }
1354
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001355 return ret;
1356}
1357
1358/**
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001359 * p9_fd_close - shutdown socket
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001360 * @trans: private socket structure
1361 *
1362 */
Eric Van Hensbergenee443992008-03-05 07:08:09 -06001363
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001364static void p9_fd_close(struct p9_trans *trans)
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001365{
1366 struct p9_trans_fd *ts;
1367
1368 if (!trans)
1369 return;
1370
1371 ts = xchg(&trans->priv, NULL);
1372
1373 if (!ts)
1374 return;
1375
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001376 p9_conn_destroy(ts->conn);
1377
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -05001378 trans->status = Disconnected;
1379 if (ts->rd)
1380 fput(ts->rd);
1381 if (ts->wr)
1382 fput(ts->wr);
1383 kfree(ts);
1384}
1385
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001386/*
1387 * stolen from NFS - maybe should be made a generic function?
1388 */
1389static inline int valid_ipaddr4(const char *buf)
1390{
1391 int rc, count, in[4];
1392
1393 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
1394 if (rc != 4)
1395 return -EINVAL;
1396 for (count = 0; count < 4; count++) {
1397 if (in[count] > 255)
1398 return -EINVAL;
1399 }
1400 return 0;
1401}
1402
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001403static struct p9_trans *
1404p9_trans_create_tcp(const char *addr, char *args, int msize, unsigned char dotu)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001405{
1406 int err;
1407 struct p9_trans *trans;
1408 struct socket *csocket;
1409 struct sockaddr_in sin_server;
1410 struct p9_fd_opts opts;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001411 struct p9_trans_fd *p;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001412
Eric Van Hensbergenbb8ffdf2008-03-07 10:53:53 -06001413 err = parse_opts(args, &opts);
1414 if (err < 0)
1415 return ERR_PTR(err);
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001416
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001417 if (valid_ipaddr4(addr) < 0)
1418 return ERR_PTR(-EINVAL);
1419
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001420 csocket = NULL;
1421 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1422 if (!trans)
1423 return ERR_PTR(-ENOMEM);
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001424 trans->msize = msize;
1425 trans->extended = dotu;
1426 trans->rpc = p9_fd_rpc;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001427 trans->close = p9_fd_close;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001428
1429 sin_server.sin_family = AF_INET;
1430 sin_server.sin_addr.s_addr = in_aton(addr);
1431 sin_server.sin_port = htons(opts.port);
1432 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1433
1434 if (!csocket) {
1435 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1436 err = -EIO;
1437 goto error;
1438 }
1439
1440 err = csocket->ops->connect(csocket,
1441 (struct sockaddr *)&sin_server,
1442 sizeof(struct sockaddr_in), 0);
1443 if (err < 0) {
1444 P9_EPRINTK(KERN_ERR,
1445 "p9_trans_tcp: problem connecting socket to %s\n",
1446 addr);
1447 goto error;
1448 }
1449
1450 err = p9_socket_open(trans, csocket);
1451 if (err < 0)
1452 goto error;
1453
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001454 p = (struct p9_trans_fd *) trans->priv;
1455 p->conn = p9_conn_create(trans);
1456 if (IS_ERR(p->conn)) {
1457 err = PTR_ERR(p->conn);
1458 p->conn = NULL;
1459 goto error;
1460 }
1461
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001462 return trans;
1463
1464error:
1465 if (csocket)
1466 sock_release(csocket);
1467
1468 kfree(trans);
1469 return ERR_PTR(err);
1470}
1471
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001472static struct p9_trans *
1473p9_trans_create_unix(const char *addr, char *args, int msize,
1474 unsigned char dotu)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001475{
1476 int err;
1477 struct socket *csocket;
1478 struct sockaddr_un sun_server;
1479 struct p9_trans *trans;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001480 struct p9_trans_fd *p;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001481
1482 csocket = NULL;
1483 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1484 if (!trans)
1485 return ERR_PTR(-ENOMEM);
1486
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001487 trans->rpc = p9_fd_rpc;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001488 trans->close = p9_fd_close;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001489
1490 if (strlen(addr) > UNIX_PATH_MAX) {
1491 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1492 addr);
1493 err = -ENAMETOOLONG;
1494 goto error;
1495 }
1496
1497 sun_server.sun_family = PF_UNIX;
1498 strcpy(sun_server.sun_path, addr);
1499 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1500 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1501 sizeof(struct sockaddr_un) - 1, 0);
1502 if (err < 0) {
1503 P9_EPRINTK(KERN_ERR,
1504 "p9_trans_unix: problem connecting socket: %s: %d\n",
1505 addr, err);
1506 goto error;
1507 }
1508
1509 err = p9_socket_open(trans, csocket);
1510 if (err < 0)
1511 goto error;
1512
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001513 trans->msize = msize;
1514 trans->extended = dotu;
1515 p = (struct p9_trans_fd *) trans->priv;
1516 p->conn = p9_conn_create(trans);
1517 if (IS_ERR(p->conn)) {
1518 err = PTR_ERR(p->conn);
1519 p->conn = NULL;
1520 goto error;
1521 }
1522
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001523 return trans;
1524
1525error:
1526 if (csocket)
1527 sock_release(csocket);
1528
1529 kfree(trans);
1530 return ERR_PTR(err);
1531}
1532
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001533static struct p9_trans *
1534p9_trans_create_fd(const char *name, char *args, int msize,
1535 unsigned char extended)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001536{
1537 int err;
1538 struct p9_trans *trans;
1539 struct p9_fd_opts opts;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001540 struct p9_trans_fd *p;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001541
1542 parse_opts(args, &opts);
1543
1544 if (opts.rfd == ~0 || opts.wfd == ~0) {
1545 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
1546 return ERR_PTR(-ENOPROTOOPT);
1547 }
1548
1549 trans = kmalloc(sizeof(struct p9_trans), GFP_KERNEL);
1550 if (!trans)
1551 return ERR_PTR(-ENOMEM);
1552
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001553 trans->rpc = p9_fd_rpc;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001554 trans->close = p9_fd_close;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001555
1556 err = p9_fd_open(trans, opts.rfd, opts.wfd);
1557 if (err < 0)
1558 goto error;
1559
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001560 trans->msize = msize;
1561 trans->extended = extended;
1562 p = (struct p9_trans_fd *) trans->priv;
1563 p->conn = p9_conn_create(trans);
1564 if (IS_ERR(p->conn)) {
1565 err = PTR_ERR(p->conn);
1566 p->conn = NULL;
1567 goto error;
1568 }
1569
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001570 return trans;
1571
1572error:
1573 kfree(trans);
1574 return ERR_PTR(err);
1575}
1576
1577static struct p9_trans_module p9_tcp_trans = {
1578 .name = "tcp",
1579 .maxsize = MAX_SOCK_BUF,
1580 .def = 1,
1581 .create = p9_trans_create_tcp,
Tejun Heo72029fe2008-09-24 16:22:23 -05001582 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001583};
1584
1585static struct p9_trans_module p9_unix_trans = {
1586 .name = "unix",
1587 .maxsize = MAX_SOCK_BUF,
1588 .def = 0,
1589 .create = p9_trans_create_unix,
Tejun Heo72029fe2008-09-24 16:22:23 -05001590 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001591};
1592
1593static struct p9_trans_module p9_fd_trans = {
1594 .name = "fd",
1595 .maxsize = MAX_SOCK_BUF,
1596 .def = 0,
1597 .create = p9_trans_create_fd,
Tejun Heo72029fe2008-09-24 16:22:23 -05001598 .owner = THIS_MODULE,
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001599};
1600
Eric Van Hensbergen887b3ec2008-05-08 20:26:37 -05001601int p9_trans_fd_init(void)
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001602{
Tejun Heo206ca502008-09-24 16:22:23 -05001603 int i;
1604
1605 for (i = 0; i < ARRAY_SIZE(p9_mux_poll_tasks); i++)
1606 p9_mux_poll_tasks[i].task = NULL;
1607
1608 p9_mux_wq = create_workqueue("v9fs");
1609 if (!p9_mux_wq) {
1610 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
1611 return -ENOMEM;
Eric Van Hensbergen8a0dc952008-02-06 19:25:03 -06001612 }
1613
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001614 v9fs_register_trans(&p9_tcp_trans);
1615 v9fs_register_trans(&p9_unix_trans);
1616 v9fs_register_trans(&p9_fd_trans);
1617
Andrew Morton3387b802008-03-28 14:15:57 -07001618 return 0;
Eric Van Hensbergena80d9232007-10-17 14:31:07 -05001619}
Tejun Heo72029fe2008-09-24 16:22:23 -05001620
1621void p9_trans_fd_exit(void)
1622{
1623 v9fs_unregister_trans(&p9_tcp_trans);
1624 v9fs_unregister_trans(&p9_unix_trans);
1625 v9fs_unregister_trans(&p9_fd_trans);
Tejun Heo206ca502008-09-24 16:22:23 -05001626
1627 destroy_workqueue(p9_mux_wq);
Tejun Heo72029fe2008-09-24 16:22:23 -05001628}