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