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