2 * linux/fs/9p/trans_fd.c
4 * Fd transport layer. Includes deprecated socket layer.
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>
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.
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.
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
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>
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>
46 #define MAX_SOCK_BUF (64*1024)
48 #define MAXPOLLWADDR 2
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)
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
80 * Option Parsing (code inspired by NFS code)
81 * - a little lazy - parse all fd-transport options
85 /* Options that take integer arguments */
86 Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
89 static const match_table_t tokens = {
90 {Opt_port, "port=%u"},
91 {Opt_rfdno, "rfdno=%u"},
92 {Opt_wfdno, "wfdno=%u"},
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 */
110 typedef void (*p9_conn_req_callback)(struct p9_req *req, void *a);
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
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
129 struct p9_fcall *tcall;
130 struct p9_fcall *rcall;
132 p9_conn_req_callback cb;
135 struct list_head req_list;
138 struct p9_poll_wait {
139 struct p9_conn *conn;
141 wait_queue_head_t *wait_addr;
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 * @client: reference to client instance for this connection
149 * @tagpool: id accounting for transactions
151 * @req_list: accounting for requests which have been sent
152 * @unsent_req_list: accounting for requests that haven't been sent
153 * @rcall: current response &p9_fcall structure
154 * @rpos: read position in current frame
155 * @rbuf: current read buffer
156 * @wpos: write position for current frame
157 * @wsize: amount of data to write for current frame
158 * @wbuf: current write buffer
159 * @poll_wait: array of wait_q's for various worker threads
162 * @rq: current read work
163 * @wq: current write work
169 spinlock_t lock; /* protect lock structure */
170 struct list_head mux_list;
171 struct p9_client *client;
172 struct p9_idpool *tagpool;
174 struct list_head req_list;
175 struct list_head unsent_req_list;
176 struct p9_fcall *rcall;
182 struct list_head poll_pending_link;
183 struct p9_poll_wait poll_wait[MAXPOLLWADDR];
185 struct work_struct rq;
186 struct work_struct wq;
187 unsigned long wsched;
191 * struct p9_mux_rpc - fd mux rpc accounting structure
192 * @m: connection this request was issued on
194 * @tcall: request &p9_fcall
195 * @rcall: response &p9_fcall
196 * @wqueue: wait queue that client is blocked on for this rpc
198 * Bug: isn't this information duplicated elsewhere like &p9_req
204 struct p9_fcall *tcall;
205 struct p9_fcall *rcall;
206 wait_queue_head_t wqueue;
209 static int p9_poll_proc(void *);
210 static void p9_read_work(struct work_struct *work);
211 static void p9_write_work(struct work_struct *work);
212 static void p9_pollwait(struct file *filp, wait_queue_head_t *wait_address,
214 static int p9_fd_write(struct p9_client *client, void *v, int len);
215 static int p9_fd_read(struct p9_client *client, void *v, int len);
217 static DEFINE_SPINLOCK(p9_poll_lock);
218 static LIST_HEAD(p9_poll_pending_list);
219 static struct workqueue_struct *p9_mux_wq;
220 static struct task_struct *p9_poll_task;
222 static void p9_conn_destroy(struct p9_conn *);
223 static unsigned int p9_fd_poll(struct p9_client *client,
224 struct poll_table_struct *pt);
227 static int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
228 p9_conn_req_callback cb, void *a);
229 #endif /* P9_NONBLOCK */
231 static void p9_conn_cancel(struct p9_conn *m, int err);
233 static u16 p9_mux_get_tag(struct p9_conn *m)
237 tag = p9_idpool_get(m->tagpool);
244 static void p9_mux_put_tag(struct p9_conn *m, u16 tag)
246 if (tag != P9_NOTAG && p9_idpool_check(tag, m->tagpool))
247 p9_idpool_put(tag, m->tagpool);
250 static void p9_mux_poll_stop(struct p9_conn *m)
255 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
256 struct p9_poll_wait *pwait = &m->poll_wait[i];
258 if (pwait->wait_addr) {
259 remove_wait_queue(pwait->wait_addr, &pwait->wait);
260 pwait->wait_addr = NULL;
264 spin_lock_irqsave(&p9_poll_lock, flags);
265 list_del_init(&m->poll_pending_link);
266 spin_unlock_irqrestore(&p9_poll_lock, flags);
270 * p9_conn_create - allocate and initialize the per-session mux data
271 * @client: client instance
273 * Note: Creates the polling task if this is the first session.
276 static struct p9_conn *p9_conn_create(struct p9_client *client)
281 P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize);
282 m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL);
284 return ERR_PTR(-ENOMEM);
286 spin_lock_init(&m->lock);
287 INIT_LIST_HEAD(&m->mux_list);
289 m->tagpool = p9_idpool_create();
290 if (IS_ERR(m->tagpool)) {
292 return ERR_PTR(-ENOMEM);
295 INIT_LIST_HEAD(&m->req_list);
296 INIT_LIST_HEAD(&m->unsent_req_list);
297 INIT_WORK(&m->rq, p9_read_work);
298 INIT_WORK(&m->wq, p9_write_work);
299 INIT_LIST_HEAD(&m->poll_pending_link);
300 init_poll_funcptr(&m->pt, p9_pollwait);
302 n = p9_fd_poll(client, &m->pt);
304 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
305 set_bit(Rpending, &m->wsched);
309 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
310 set_bit(Wpending, &m->wsched);
313 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
314 if (IS_ERR(m->poll_wait[i].wait_addr)) {
317 /* return the error code */
318 return (void *)m->poll_wait[i].wait_addr;
326 * p9_mux_destroy - cancels all pending requests and frees mux resources
331 static void p9_conn_destroy(struct p9_conn *m)
333 P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m,
334 m->mux_list.prev, m->mux_list.next);
337 cancel_work_sync(&m->rq);
338 cancel_work_sync(&m->wq);
340 p9_conn_cancel(m, -ECONNRESET);
343 p9_idpool_destroy(m->tagpool);
347 static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key)
349 struct p9_poll_wait *pwait =
350 container_of(wait, struct p9_poll_wait, wait);
351 struct p9_conn *m = pwait->conn;
353 DECLARE_WAITQUEUE(dummy_wait, p9_poll_task);
355 spin_lock_irqsave(&p9_poll_lock, flags);
356 if (list_empty(&m->poll_pending_link))
357 list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
358 spin_unlock_irqrestore(&p9_poll_lock, flags);
360 /* perform the default wake up operation */
361 return default_wake_function(&dummy_wait, mode, sync, key);
365 * p9_pollwait - add poll task to the wait queue
366 * @filp: file pointer being polled
367 * @wait_address: wait_q to block on
370 * called by files poll operation to add v9fs-poll task to files wait queue
374 p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
376 struct p9_conn *m = container_of(p, struct p9_conn, pt);
377 struct p9_poll_wait *pwait = NULL;
380 for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
381 if (m->poll_wait[i].wait_addr == NULL) {
382 pwait = &m->poll_wait[i];
388 P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n");
393 P9_DPRINTK(P9_DEBUG_ERROR, "no wait_address\n");
394 pwait->wait_addr = ERR_PTR(-EIO);
399 pwait->wait_addr = wait_address;
400 init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
401 add_wait_queue(wait_address, &pwait->wait);
405 * p9_poll_mux - polls a mux and schedules read or write works if necessary
406 * @m: connection to poll
410 static void p9_poll_mux(struct p9_conn *m)
417 n = p9_fd_poll(m->client, NULL);
418 if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
419 P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n);
422 p9_conn_cancel(m, n);
426 set_bit(Rpending, &m->wsched);
427 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m);
428 if (!test_and_set_bit(Rworksched, &m->wsched)) {
429 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
430 queue_work(p9_mux_wq, &m->rq);
435 set_bit(Wpending, &m->wsched);
436 P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m);
437 if ((m->wsize || !list_empty(&m->unsent_req_list))
438 && !test_and_set_bit(Wworksched, &m->wsched)) {
439 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
440 queue_work(p9_mux_wq, &m->wq);
446 * p9_poll_proc - poll worker thread
447 * @a: thread state and arguments
449 * polls all v9fs transports for new events and queues the appropriate
450 * work to the work queue
454 static int p9_poll_proc(void *a)
458 P9_DPRINTK(P9_DEBUG_MUX, "start %p\n", current);
460 spin_lock_irqsave(&p9_poll_lock, flags);
461 while (!list_empty(&p9_poll_pending_list)) {
462 struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
465 list_del_init(&conn->poll_pending_link);
466 spin_unlock_irqrestore(&p9_poll_lock, flags);
470 spin_lock_irqsave(&p9_poll_lock, flags);
472 spin_unlock_irqrestore(&p9_poll_lock, flags);
474 set_current_state(TASK_INTERRUPTIBLE);
475 if (list_empty(&p9_poll_pending_list)) {
476 P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n");
479 __set_current_state(TASK_RUNNING);
481 if (!kthread_should_stop())
484 P9_DPRINTK(P9_DEBUG_MUX, "finish\n");
489 * p9_write_work - called when a transport can send some data
490 * @work: container for work to be done
494 static void p9_write_work(struct work_struct *work)
500 m = container_of(work, struct p9_conn, wq);
503 clear_bit(Wworksched, &m->wsched);
508 if (list_empty(&m->unsent_req_list)) {
509 clear_bit(Wworksched, &m->wsched);
515 req = list_entry(m->unsent_req_list.next, struct p9_req,
517 list_move_tail(&req->req_list, &m->req_list);
518 if (req->err == ERREQFLUSH)
521 m->wbuf = req->tcall->sdata;
522 m->wsize = req->tcall->size;
524 spin_unlock(&m->lock);
527 P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos,
529 clear_bit(Wpending, &m->wsched);
530 err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
531 P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
532 if (err == -EAGAIN) {
533 clear_bit(Wworksched, &m->wsched);
545 if (m->wpos == m->wsize)
546 m->wpos = m->wsize = 0;
548 if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
549 if (test_and_clear_bit(Wpending, &m->wsched))
552 n = p9_fd_poll(m->client, NULL);
555 P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m);
556 queue_work(p9_mux_wq, &m->wq);
558 clear_bit(Wworksched, &m->wsched);
560 clear_bit(Wworksched, &m->wsched);
565 p9_conn_cancel(m, err);
566 clear_bit(Wworksched, &m->wsched);
569 static void process_request(struct p9_conn *m, struct p9_req *req)
572 struct p9_str *ename;
574 if (!req->err && req->rcall->id == P9_RERROR) {
575 ecode = req->rcall->params.rerror.errno;
576 ename = &req->rcall->params.rerror.error;
578 P9_DPRINTK(P9_DEBUG_MUX, "Rerror %.*s\n", ename->len,
585 req->err = p9_errstr2errno(ename->str, ename->len);
587 /* string match failed */
589 PRINT_FCALL_ERROR("unknown error", req->rcall);
590 req->err = -ESERVERFAULT;
593 } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
594 P9_DPRINTK(P9_DEBUG_ERROR,
595 "fcall mismatch: expected %d, got %d\n",
596 req->tcall->id + 1, req->rcall->id);
603 * p9_read_work - called when there is some data to be read from a transport
604 * @work: container of work to be done
608 static void p9_read_work(struct work_struct *work)
612 struct p9_req *req, *rptr, *rreq;
613 struct p9_fcall *rcall;
616 m = container_of(work, struct p9_conn, rq);
622 P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
626 kmalloc(sizeof(struct p9_fcall) + m->client->msize,
633 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
637 clear_bit(Rpending, &m->wsched);
638 err = p9_fd_read(m->client, m->rbuf + m->rpos,
639 m->client->msize - m->rpos);
640 P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err);
641 if (err == -EAGAIN) {
642 clear_bit(Rworksched, &m->wsched);
650 while (m->rpos > 4) {
651 n = le32_to_cpu(*(__le32 *) m->rbuf);
652 if (n >= m->client->msize) {
653 P9_DPRINTK(P9_DEBUG_ERROR,
654 "requested packet size too big: %d\n", n);
663 p9_deserialize_fcall(m->rbuf, n, m->rcall, m->client->dotu);
667 #ifdef CONFIG_NET_9P_DEBUG
668 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
671 p9_printfcall(buf, sizeof(buf), m->rcall,
673 printk(KERN_NOTICE ">>> %p %s\n", m, buf);
680 m->rcall = kmalloc(sizeof(struct p9_fcall) +
681 m->client->msize, GFP_KERNEL);
687 m->rbuf = (char *)m->rcall + sizeof(struct p9_fcall);
688 memmove(m->rbuf, rbuf + n, m->rpos - n);
696 P9_DPRINTK(P9_DEBUG_MUX, "mux %p fcall id %d tag %d\n", m,
697 rcall->id, rcall->tag);
701 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
702 if (rreq->tag == rcall->tag) {
704 if (req->flush != Flushing)
705 list_del(&req->req_list);
709 spin_unlock(&m->lock);
713 process_request(m, req);
715 if (req->flush != Flushing) {
717 (*req->cb) (req, req->cba);
722 if (err >= 0 && rcall->id != P9_RFLUSH)
723 P9_DPRINTK(P9_DEBUG_ERROR,
724 "unexpected response mux %p id %d tag %d\n",
725 m, rcall->id, rcall->tag);
730 if (!list_empty(&m->req_list)) {
731 if (test_and_clear_bit(Rpending, &m->wsched))
734 n = p9_fd_poll(m->client, NULL);
737 P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m);
738 queue_work(p9_mux_wq, &m->rq);
740 clear_bit(Rworksched, &m->wsched);
742 clear_bit(Rworksched, &m->wsched);
747 p9_conn_cancel(m, err);
748 clear_bit(Rworksched, &m->wsched);
752 * p9_send_request - send 9P request
753 * The function can sleep until the request is scheduled for sending.
754 * The function can be interrupted. Return from the function is not
755 * a guarantee that the request is sent successfully. Can return errors
756 * that can be retrieved by PTR_ERR macros.
759 * @tc: request to be sent
760 * @cb: callback function to call when response is received
761 * @cba: parameter to pass to the callback function
765 static struct p9_req *p9_send_request(struct p9_conn *m,
767 p9_conn_req_callback cb, void *cba)
772 P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
775 return ERR_PTR(m->err);
777 req = kmalloc(sizeof(struct p9_req), GFP_KERNEL);
779 return ERR_PTR(-ENOMEM);
781 if (tc->id == P9_TVERSION)
784 n = p9_mux_get_tag(m);
788 return ERR_PTR(-ENOMEM);
793 #ifdef CONFIG_NET_9P_DEBUG
794 if ((p9_debug_level&P9_DEBUG_FCALL) == P9_DEBUG_FCALL) {
797 p9_printfcall(buf, sizeof(buf), tc, m->client->dotu);
798 printk(KERN_NOTICE "<<< %p %s\n", m, buf);
802 spin_lock_init(&req->lock);
812 list_add_tail(&req->req_list, &m->unsent_req_list);
813 spin_unlock(&m->lock);
815 if (test_and_clear_bit(Wpending, &m->wsched))
818 n = p9_fd_poll(m->client, NULL);
820 if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
821 queue_work(p9_mux_wq, &m->wq);
826 static void p9_mux_free_request(struct p9_conn *m, struct p9_req *req)
828 p9_mux_put_tag(m, req->tag);
832 static void p9_mux_flush_cb(struct p9_req *freq, void *a)
836 struct p9_req *req, *rreq, *rptr;
839 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
840 freq->tcall, freq->rcall, freq->err,
841 freq->tcall->params.tflush.oldtag);
844 tag = freq->tcall->params.tflush.oldtag;
846 list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
847 if (rreq->tag == tag) {
849 list_del(&req->req_list);
853 spin_unlock(&m->lock);
856 spin_lock(&req->lock);
857 req->flush = Flushed;
858 spin_unlock(&req->lock);
861 (*req->cb) (req, req->cba);
868 p9_mux_free_request(m, freq);
872 p9_mux_flush_request(struct p9_conn *m, struct p9_req *req)
875 struct p9_req *rreq, *rptr;
877 P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
879 /* if a response was received for a request, do nothing */
880 spin_lock(&req->lock);
881 if (req->rcall || req->err) {
882 spin_unlock(&req->lock);
883 P9_DPRINTK(P9_DEBUG_MUX,
884 "mux %p req %p response already received\n", m, req);
888 req->flush = Flushing;
889 spin_unlock(&req->lock);
892 /* if the request is not sent yet, just remove it from the list */
893 list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
894 if (rreq->tag == req->tag) {
895 P9_DPRINTK(P9_DEBUG_MUX,
896 "mux %p req %p request is not sent yet\n", m, req);
897 list_del(&rreq->req_list);
898 req->flush = Flushed;
899 spin_unlock(&m->lock);
901 (*req->cb) (req, req->cba);
905 spin_unlock(&m->lock);
907 clear_thread_flag(TIF_SIGPENDING);
908 fc = p9_create_tflush(req->tag);
909 p9_send_request(m, fc, p9_mux_flush_cb, m);
914 p9_conn_rpc_cb(struct p9_req *req, void *a)
916 struct p9_mux_rpc *r;
918 P9_DPRINTK(P9_DEBUG_MUX, "req %p r %p\n", req, a);
920 r->rcall = req->rcall;
923 if (req->flush != None && !req->err)
924 r->err = -ERESTARTSYS;
930 * p9_fd_rpc- sends 9P request and waits until a response is available.
931 * The function can be interrupted.
932 * @client: client instance
933 * @tc: request to be sent
934 * @rc: pointer where a pointer to the response is stored
939 p9_fd_rpc(struct p9_client *client, struct p9_fcall *tc, struct p9_fcall **rc)
941 struct p9_trans_fd *p = client->trans;
942 struct p9_conn *m = p->conn;
952 init_waitqueue_head(&r.wqueue);
958 if (signal_pending(current)) {
960 clear_thread_flag(TIF_SIGPENDING);
963 req = p9_send_request(m, tc, p9_conn_rpc_cb, &r);
966 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
970 err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
974 if (err == -ERESTARTSYS && client->status == Connected
976 if (p9_mux_flush_request(m, req)) {
977 /* wait until we get response of the flush message */
979 clear_thread_flag(TIF_SIGPENDING);
980 err = wait_event_interruptible(r.wqueue,
982 } while (!r.rcall && !r.err && err == -ERESTARTSYS &&
983 client->status == Connected && !m->err);
991 spin_lock_irqsave(¤t->sighand->siglock, flags);
993 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
1001 p9_mux_free_request(m, req);
1010 * p9_conn_rpcnb - sends 9P request without waiting for response.
1012 * @tc: request to be sent
1013 * @cb: callback function to be called when response arrives
1014 * @a: value to pass to the callback function
1018 int p9_conn_rpcnb(struct p9_conn *m, struct p9_fcall *tc,
1019 p9_conn_req_callback cb, void *a)
1024 req = p9_send_request(m, tc, cb, a);
1027 P9_DPRINTK(P9_DEBUG_MUX, "error %d\n", err);
1028 return PTR_ERR(req);
1031 P9_DPRINTK(P9_DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
1034 #endif /* P9_NONBLOCK */
1037 * p9_conn_cancel - cancel all pending requests with error
1043 void p9_conn_cancel(struct p9_conn *m, int err)
1045 struct p9_req *req, *rtmp;
1046 LIST_HEAD(cancel_list);
1048 P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
1050 spin_lock(&m->lock);
1051 list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
1052 list_move(&req->req_list, &cancel_list);
1054 list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
1055 list_move(&req->req_list, &cancel_list);
1057 spin_unlock(&m->lock);
1059 list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
1060 list_del(&req->req_list);
1065 (*req->cb) (req, req->cba);
1072 * parse_options - parse mount options into session structure
1073 * @options: options string passed from mount
1074 * @opts: transport-specific structure to parse options into
1076 * Returns 0 upon success, -ERRNO upon failure
1079 static int parse_opts(char *params, struct p9_fd_opts *opts)
1082 substring_t args[MAX_OPT_ARGS];
1087 opts->port = P9_PORT;
1094 options = kstrdup(params, GFP_KERNEL);
1096 P9_DPRINTK(P9_DEBUG_ERROR,
1097 "failed to allocate copy of option string\n");
1101 while ((p = strsep(&options, ",")) != NULL) {
1106 token = match_token(p, tokens, args);
1107 r = match_int(&args[0], &option);
1109 P9_DPRINTK(P9_DEBUG_ERROR,
1110 "integer field, but no integer?\n");
1116 opts->port = option;
1132 static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
1134 struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd),
1141 if (!ts->rd || !ts->wr) {
1151 client->status = Connected;
1156 static int p9_socket_open(struct p9_client *client, struct socket *csocket)
1160 csocket->sk->sk_allocation = GFP_NOIO;
1161 fd = sock_map_fd(csocket, 0);
1163 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n");
1167 ret = p9_fd_open(client, fd, fd);
1169 P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n");
1170 sockfd_put(csocket);
1174 ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK;
1180 * p9_fd_read- read from a fd
1181 * @client: client instance
1182 * @v: buffer to receive data into
1183 * @len: size of receive buffer
1187 static int p9_fd_read(struct p9_client *client, void *v, int len)
1190 struct p9_trans_fd *ts = NULL;
1192 if (client && client->status != Disconnected)
1198 if (!(ts->rd->f_flags & O_NONBLOCK))
1199 P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n");
1201 ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
1202 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1203 client->status = Disconnected;
1208 * p9_fd_write - write to a socket
1209 * @client: client instance
1210 * @v: buffer to send data from
1211 * @len: size of send buffer
1215 static int p9_fd_write(struct p9_client *client, void *v, int len)
1219 struct p9_trans_fd *ts = NULL;
1221 if (client && client->status != Disconnected)
1227 if (!(ts->wr->f_flags & O_NONBLOCK))
1228 P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n");
1232 /* The cast to a user pointer is valid due to the set_fs() */
1233 ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos);
1236 if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
1237 client->status = Disconnected;
1242 p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
1245 struct p9_trans_fd *ts = NULL;
1247 if (client && client->status == Connected)
1253 if (!ts->rd->f_op || !ts->rd->f_op->poll)
1256 if (!ts->wr->f_op || !ts->wr->f_op->poll)
1259 ret = ts->rd->f_op->poll(ts->rd, pt);
1263 if (ts->rd != ts->wr) {
1264 n = ts->wr->f_op->poll(ts->wr, pt);
1267 ret = (ret & ~POLLOUT) | (n & ~POLLIN);
1274 * p9_fd_close - shutdown file descriptor transport
1275 * @client: client instance
1279 static void p9_fd_close(struct p9_client *client)
1281 struct p9_trans_fd *ts;
1290 client->status = Disconnected;
1292 p9_conn_destroy(ts->conn);
1303 * stolen from NFS - maybe should be made a generic function?
1305 static inline int valid_ipaddr4(const char *buf)
1307 int rc, count, in[4];
1309 rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
1312 for (count = 0; count < 4; count++) {
1313 if (in[count] > 255)
1320 p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
1323 struct socket *csocket;
1324 struct sockaddr_in sin_server;
1325 struct p9_fd_opts opts;
1326 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
1328 err = parse_opts(args, &opts);
1332 if (valid_ipaddr4(addr) < 0)
1337 sin_server.sin_family = AF_INET;
1338 sin_server.sin_addr.s_addr = in_aton(addr);
1339 sin_server.sin_port = htons(opts.port);
1340 sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket);
1343 P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n");
1348 err = csocket->ops->connect(csocket,
1349 (struct sockaddr *)&sin_server,
1350 sizeof(struct sockaddr_in), 0);
1352 P9_EPRINTK(KERN_ERR,
1353 "p9_trans_tcp: problem connecting socket to %s\n",
1358 err = p9_socket_open(client, csocket);
1362 p = (struct p9_trans_fd *) client->trans;
1363 p->conn = p9_conn_create(client);
1364 if (IS_ERR(p->conn)) {
1365 err = PTR_ERR(p->conn);
1374 sock_release(csocket);
1382 p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
1385 struct socket *csocket;
1386 struct sockaddr_un sun_server;
1387 struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */
1391 if (strlen(addr) > UNIX_PATH_MAX) {
1392 P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n",
1394 err = -ENAMETOOLONG;
1398 sun_server.sun_family = PF_UNIX;
1399 strcpy(sun_server.sun_path, addr);
1400 sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket);
1401 err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
1402 sizeof(struct sockaddr_un) - 1, 0);
1404 P9_EPRINTK(KERN_ERR,
1405 "p9_trans_unix: problem connecting socket: %s: %d\n",
1410 err = p9_socket_open(client, csocket);
1414 p = (struct p9_trans_fd *) client->trans;
1415 p->conn = p9_conn_create(client);
1416 if (IS_ERR(p->conn)) {
1417 err = PTR_ERR(p->conn);
1426 sock_release(csocket);
1433 p9_fd_create(struct p9_client *client, const char *addr, char *args)
1436 struct p9_fd_opts opts;
1437 struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */
1439 parse_opts(args, &opts);
1441 if (opts.rfd == ~0 || opts.wfd == ~0) {
1442 printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n");
1443 return -ENOPROTOOPT;
1446 err = p9_fd_open(client, opts.rfd, opts.wfd);
1450 p = (struct p9_trans_fd *) client->trans;
1451 p->conn = p9_conn_create(client);
1452 if (IS_ERR(p->conn)) {
1453 err = PTR_ERR(p->conn);
1465 static struct p9_trans_module p9_tcp_trans = {
1467 .maxsize = MAX_SOCK_BUF,
1469 .create = p9_fd_create_tcp,
1470 .close = p9_fd_close,
1472 .owner = THIS_MODULE,
1475 static struct p9_trans_module p9_unix_trans = {
1477 .maxsize = MAX_SOCK_BUF,
1479 .create = p9_fd_create_unix,
1480 .close = p9_fd_close,
1482 .owner = THIS_MODULE,
1485 static struct p9_trans_module p9_fd_trans = {
1487 .maxsize = MAX_SOCK_BUF,
1489 .create = p9_fd_create,
1490 .close = p9_fd_close,
1492 .owner = THIS_MODULE,
1495 int p9_trans_fd_init(void)
1497 p9_mux_wq = create_workqueue("v9fs");
1499 printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n");
1503 p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll");
1504 if (IS_ERR(p9_poll_task)) {
1505 destroy_workqueue(p9_mux_wq);
1506 printk(KERN_WARNING "v9fs: mux: creating poll task failed\n");
1507 return PTR_ERR(p9_poll_task);
1510 v9fs_register_trans(&p9_tcp_trans);
1511 v9fs_register_trans(&p9_unix_trans);
1512 v9fs_register_trans(&p9_fd_trans);
1517 void p9_trans_fd_exit(void)
1519 kthread_stop(p9_poll_task);
1520 v9fs_unregister_trans(&p9_tcp_trans);
1521 v9fs_unregister_trans(&p9_unix_trans);
1522 v9fs_unregister_trans(&p9_fd_trans);
1524 destroy_workqueue(p9_mux_wq);