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