]>
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 <[email protected]> | |
7 | * Copyright (C) 2004-2005 by Latchesar Ionkov <[email protected]> | |
8a0dc95f | 8 | * Copyright (C) 2004-2008 by Eric Van Hensbergen <[email protected]> |
bd238fb4 LI |
9 | * Copyright (C) 1997-2002 by Ron Minnich <[email protected]> |
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 MAXPOLLWADDR 2 |
a80d923e | 48 | |
ee443996 EVH |
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 | ||
a80d923e EVH |
57 | struct p9_fd_opts { |
58 | int rfd; | |
59 | int wfd; | |
60 | u16 port; | |
61 | }; | |
bd238fb4 | 62 | |
ee443996 EVH |
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 | ||
bd238fb4 LI |
71 | struct p9_trans_fd { |
72 | struct file *rd; | |
73 | struct file *wr; | |
8a0dc95f | 74 | struct p9_conn *conn; |
bd238fb4 LI |
75 | }; |
76 | ||
a80d923e EVH |
77 | /* |
78 | * Option Parsing (code inspired by NFS code) | |
79 | * - a little lazy - parse all fd-transport options | |
80 | */ | |
bd238fb4 | 81 | |
a80d923e EVH |
82 | enum { |
83 | /* Options that take integer arguments */ | |
55762690 | 84 | Opt_port, Opt_rfdno, Opt_wfdno, Opt_err, |
a80d923e | 85 | }; |
bd238fb4 | 86 | |
a447c093 | 87 | static const match_table_t tokens = { |
a80d923e EVH |
88 | {Opt_port, "port=%u"}, |
89 | {Opt_rfdno, "rfdno=%u"}, | |
90 | {Opt_wfdno, "wfdno=%u"}, | |
55762690 | 91 | {Opt_err, NULL}, |
a80d923e | 92 | }; |
bd238fb4 | 93 | |
8a0dc95f EVH |
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 | ||
992b3f1d TH |
101 | struct p9_poll_wait { |
102 | struct p9_conn *conn; | |
103 | wait_queue_t wait; | |
104 | wait_queue_head_t *wait_addr; | |
ee443996 EVH |
105 | }; |
106 | ||
107 | /** | |
108 | * struct p9_conn - fd mux connection state information | |
ee443996 | 109 | * @mux_list: list link for mux to manage multiple connections (?) |
8b81ef58 | 110 | * @client: reference to client instance for this connection |
ee443996 | 111 | * @err: error state |
ee443996 EVH |
112 | * @req_list: accounting for requests which have been sent |
113 | * @unsent_req_list: accounting for requests that haven't been sent | |
1b0a763b EVH |
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 | |
ee443996 EVH |
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 | */ | |
8a0dc95f EVH |
130 | |
131 | struct p9_conn { | |
8a0dc95f | 132 | struct list_head mux_list; |
8b81ef58 | 133 | struct p9_client *client; |
8a0dc95f | 134 | int err; |
8a0dc95f EVH |
135 | struct list_head req_list; |
136 | struct list_head unsent_req_list; | |
1b0a763b EVH |
137 | struct p9_req_t *req; |
138 | char tmp_buf[7]; | |
139 | int rsize; | |
8a0dc95f EVH |
140 | int rpos; |
141 | char *rbuf; | |
142 | int wpos; | |
143 | int wsize; | |
144 | char *wbuf; | |
992b3f1d TH |
145 | struct list_head poll_pending_link; |
146 | struct p9_poll_wait poll_wait[MAXPOLLWADDR]; | |
8a0dc95f EVH |
147 | poll_table pt; |
148 | struct work_struct rq; | |
149 | struct work_struct wq; | |
150 | unsigned long wsched; | |
151 | }; | |
152 | ||
992b3f1d TH |
153 | static DEFINE_SPINLOCK(p9_poll_lock); |
154 | static LIST_HEAD(p9_poll_pending_list); | |
8a0dc95f | 155 | static struct workqueue_struct *p9_mux_wq; |
992b3f1d | 156 | static struct task_struct *p9_poll_task; |
8a0dc95f | 157 | |
992b3f1d | 158 | static void p9_mux_poll_stop(struct p9_conn *m) |
8a0dc95f | 159 | { |
992b3f1d TH |
160 | unsigned long flags; |
161 | int i; | |
8a0dc95f | 162 | |
992b3f1d TH |
163 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
164 | struct p9_poll_wait *pwait = &m->poll_wait[i]; | |
8a0dc95f | 165 | |
992b3f1d TH |
166 | if (pwait->wait_addr) { |
167 | remove_wait_queue(pwait->wait_addr, &pwait->wait); | |
168 | pwait->wait_addr = NULL; | |
8a0dc95f | 169 | } |
8a0dc95f EVH |
170 | } |
171 | ||
992b3f1d TH |
172 | spin_lock_irqsave(&p9_poll_lock, flags); |
173 | list_del_init(&m->poll_pending_link); | |
174 | spin_unlock_irqrestore(&p9_poll_lock, flags); | |
8a0dc95f EVH |
175 | } |
176 | ||
177 | /** | |
5503ac56 EVH |
178 | * p9_conn_cancel - cancel all pending requests with error |
179 | * @m: mux data | |
180 | * @err: error code | |
8a0dc95f | 181 | * |
8a0dc95f | 182 | */ |
ee443996 | 183 | |
5503ac56 | 184 | void p9_conn_cancel(struct p9_conn *m, int err) |
8a0dc95f | 185 | { |
673d62cd | 186 | struct p9_req_t *req, *rtmp; |
91b8534f | 187 | unsigned long flags; |
5503ac56 | 188 | LIST_HEAD(cancel_list); |
8a0dc95f | 189 | |
5503ac56 EVH |
190 | P9_DPRINTK(P9_DEBUG_ERROR, "mux %p err %d\n", m, err); |
191 | m->err = err; | |
91b8534f | 192 | spin_lock_irqsave(&m->client->lock, flags); |
5503ac56 | 193 | list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) { |
673d62cd EVH |
194 | req->status = REQ_STATUS_ERROR; |
195 | if (!req->t_err) | |
196 | req->t_err = err; | |
5503ac56 EVH |
197 | list_move(&req->req_list, &cancel_list); |
198 | } | |
199 | list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) { | |
673d62cd EVH |
200 | req->status = REQ_STATUS_ERROR; |
201 | if (!req->t_err) | |
202 | req->t_err = err; | |
5503ac56 | 203 | list_move(&req->req_list, &cancel_list); |
8a0dc95f | 204 | } |
91b8534f | 205 | spin_unlock_irqrestore(&m->client->lock, flags); |
8a0dc95f | 206 | |
5503ac56 EVH |
207 | list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) { |
208 | list_del(&req->req_list); | |
91b8534f EVH |
209 | P9_DPRINTK(P9_DEBUG_ERROR, "call back req %p\n", req); |
210 | p9_client_cb(m->client, req); | |
8a0dc95f | 211 | } |
8a0dc95f EVH |
212 | } |
213 | ||
5503ac56 EVH |
214 | static unsigned int |
215 | p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt) | |
8a0dc95f | 216 | { |
5503ac56 EVH |
217 | int ret, n; |
218 | struct p9_trans_fd *ts = NULL; | |
8a0dc95f | 219 | |
5503ac56 EVH |
220 | if (client && client->status == Connected) |
221 | ts = client->trans; | |
7dc5d24b | 222 | |
5503ac56 EVH |
223 | if (!ts) |
224 | return -EREMOTEIO; | |
7dc5d24b | 225 | |
5503ac56 EVH |
226 | if (!ts->rd->f_op || !ts->rd->f_op->poll) |
227 | return -EIO; | |
8a0dc95f | 228 | |
5503ac56 EVH |
229 | if (!ts->wr->f_op || !ts->wr->f_op->poll) |
230 | return -EIO; | |
992b3f1d | 231 | |
5503ac56 EVH |
232 | ret = ts->rd->f_op->poll(ts->rd, pt); |
233 | if (ret < 0) | |
234 | return ret; | |
992b3f1d | 235 | |
5503ac56 EVH |
236 | if (ts->rd != ts->wr) { |
237 | n = ts->wr->f_op->poll(ts->wr, pt); | |
238 | if (n < 0) | |
239 | return n; | |
240 | ret = (ret & ~POLLOUT) | (n & ~POLLIN); | |
241 | } | |
242 | ||
243 | return ret; | |
992b3f1d TH |
244 | } |
245 | ||
8a0dc95f | 246 | /** |
5503ac56 EVH |
247 | * p9_fd_read- read from a fd |
248 | * @client: client instance | |
249 | * @v: buffer to receive data into | |
250 | * @len: size of receive buffer | |
ee443996 | 251 | * |
8a0dc95f | 252 | */ |
ee443996 | 253 | |
5503ac56 | 254 | static int p9_fd_read(struct p9_client *client, void *v, int len) |
8a0dc95f | 255 | { |
5503ac56 EVH |
256 | int ret; |
257 | struct p9_trans_fd *ts = NULL; | |
8a0dc95f | 258 | |
5503ac56 EVH |
259 | if (client && client->status != Disconnected) |
260 | ts = client->trans; | |
8a0dc95f | 261 | |
5503ac56 EVH |
262 | if (!ts) |
263 | return -EREMOTEIO; | |
8a0dc95f | 264 | |
5503ac56 EVH |
265 | if (!(ts->rd->f_flags & O_NONBLOCK)) |
266 | P9_DPRINTK(P9_DEBUG_ERROR, "blocking read ...\n"); | |
8a0dc95f | 267 | |
5503ac56 EVH |
268 | ret = kernel_read(ts->rd, ts->rd->f_pos, v, len); |
269 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) | |
270 | client->status = Disconnected; | |
271 | return ret; | |
8a0dc95f EVH |
272 | } |
273 | ||
274 | /** | |
5503ac56 EVH |
275 | * p9_read_work - called when there is some data to be read from a transport |
276 | * @work: container of work to be done | |
ee443996 | 277 | * |
8a0dc95f | 278 | */ |
ee443996 | 279 | |
5503ac56 | 280 | static void p9_read_work(struct work_struct *work) |
8a0dc95f | 281 | { |
5503ac56 EVH |
282 | int n, err; |
283 | struct p9_conn *m; | |
5503ac56 EVH |
284 | |
285 | m = container_of(work, struct p9_conn, rq); | |
8a0dc95f EVH |
286 | |
287 | if (m->err < 0) | |
288 | return; | |
289 | ||
5503ac56 | 290 | P9_DPRINTK(P9_DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos); |
8a0dc95f | 291 | |
1b0a763b EVH |
292 | if (!m->rbuf) { |
293 | m->rbuf = m->tmp_buf; | |
5503ac56 | 294 | m->rpos = 0; |
1b0a763b | 295 | m->rsize = 7; /* start by reading header */ |
8a0dc95f EVH |
296 | } |
297 | ||
5503ac56 | 298 | clear_bit(Rpending, &m->wsched); |
1b0a763b EVH |
299 | P9_DPRINTK(P9_DEBUG_MUX, "read mux %p pos %d size: %d = %d\n", m, |
300 | m->rpos, m->rsize, m->rsize-m->rpos); | |
5503ac56 | 301 | err = p9_fd_read(m->client, m->rbuf + m->rpos, |
1b0a763b | 302 | m->rsize - m->rpos); |
5503ac56 EVH |
303 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p got %d bytes\n", m, err); |
304 | if (err == -EAGAIN) { | |
305 | clear_bit(Rworksched, &m->wsched); | |
306 | return; | |
8a0dc95f | 307 | } |
8a0dc95f | 308 | |
5503ac56 EVH |
309 | if (err <= 0) |
310 | goto error; | |
311 | ||
312 | m->rpos += err; | |
1b0a763b EVH |
313 | |
314 | if ((!m->req) && (m->rpos == m->rsize)) { /* header read in */ | |
315 | u16 tag; | |
316 | P9_DPRINTK(P9_DEBUG_MUX, "got new header\n"); | |
317 | ||
318 | n = le32_to_cpu(*(__le32 *) m->rbuf); /* read packet size */ | |
5503ac56 EVH |
319 | if (n >= m->client->msize) { |
320 | P9_DPRINTK(P9_DEBUG_ERROR, | |
321 | "requested packet size too big: %d\n", n); | |
322 | err = -EIO; | |
323 | goto error; | |
324 | } | |
325 | ||
1b0a763b EVH |
326 | tag = le16_to_cpu(*(__le16 *) (m->rbuf+5)); /* read tag */ |
327 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p pkt: size: %d bytes tag: %d\n", | |
328 | m, n, tag); | |
329 | ||
330 | m->req = p9_tag_lookup(m->client, tag); | |
331 | if (!m->req) { | |
332 | P9_DPRINTK(P9_DEBUG_ERROR, "Unexpected packet tag %d\n", | |
333 | tag); | |
334 | err = -EIO; | |
335 | goto error; | |
336 | } | |
337 | ||
338 | if (m->req->rc == NULL) { | |
339 | m->req->rc = kmalloc(sizeof(struct p9_fcall) + | |
340 | m->client->msize, GFP_KERNEL); | |
341 | if (!m->req->rc) { | |
342 | m->req = NULL; | |
343 | err = -ENOMEM; | |
344 | goto error; | |
345 | } | |
346 | } | |
347 | m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall); | |
348 | memcpy(m->rbuf, m->tmp_buf, m->rsize); | |
349 | m->rsize = n; | |
350 | } | |
5503ac56 | 351 | |
1b0a763b EVH |
352 | /* not an else because some packets (like clunk) have no payload */ |
353 | if ((m->req) && (m->rpos == m->rsize)) { /* packet is read in */ | |
354 | P9_DPRINTK(P9_DEBUG_MUX, "got new packet\n"); | |
5503ac56 | 355 | |
91b8534f EVH |
356 | list_del(&m->req->req_list); |
357 | p9_client_cb(m->client, m->req); | |
5503ac56 | 358 | |
1b0a763b EVH |
359 | m->rbuf = NULL; |
360 | m->rpos = 0; | |
361 | m->rsize = 0; | |
1b0a763b | 362 | m->req = NULL; |
5503ac56 EVH |
363 | } |
364 | ||
365 | if (!list_empty(&m->req_list)) { | |
366 | if (test_and_clear_bit(Rpending, &m->wsched)) | |
367 | n = POLLIN; | |
368 | else | |
369 | n = p9_fd_poll(m->client, NULL); | |
370 | ||
371 | if (n & POLLIN) { | |
372 | P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m); | |
373 | queue_work(p9_mux_wq, &m->rq); | |
374 | } else | |
375 | clear_bit(Rworksched, &m->wsched); | |
376 | } else | |
377 | clear_bit(Rworksched, &m->wsched); | |
378 | ||
379 | return; | |
5503ac56 EVH |
380 | error: |
381 | p9_conn_cancel(m, err); | |
382 | clear_bit(Rworksched, &m->wsched); | |
383 | } | |
384 | ||
385 | /** | |
386 | * p9_fd_write - write to a socket | |
387 | * @client: client instance | |
388 | * @v: buffer to send data from | |
389 | * @len: size of send buffer | |
ee443996 | 390 | * |
8a0dc95f | 391 | */ |
ee443996 | 392 | |
5503ac56 | 393 | static int p9_fd_write(struct p9_client *client, void *v, int len) |
8a0dc95f | 394 | { |
5503ac56 EVH |
395 | int ret; |
396 | mm_segment_t oldfs; | |
397 | struct p9_trans_fd *ts = NULL; | |
8a0dc95f | 398 | |
5503ac56 EVH |
399 | if (client && client->status != Disconnected) |
400 | ts = client->trans; | |
8a0dc95f | 401 | |
5503ac56 EVH |
402 | if (!ts) |
403 | return -EREMOTEIO; | |
8a0dc95f | 404 | |
5503ac56 EVH |
405 | if (!(ts->wr->f_flags & O_NONBLOCK)) |
406 | P9_DPRINTK(P9_DEBUG_ERROR, "blocking write ...\n"); | |
992b3f1d | 407 | |
5503ac56 EVH |
408 | oldfs = get_fs(); |
409 | set_fs(get_ds()); | |
410 | /* The cast to a user pointer is valid due to the set_fs() */ | |
411 | ret = vfs_write(ts->wr, (void __user *)v, len, &ts->wr->f_pos); | |
412 | set_fs(oldfs); | |
992b3f1d | 413 | |
5503ac56 EVH |
414 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
415 | client->status = Disconnected; | |
416 | return ret; | |
8a0dc95f EVH |
417 | } |
418 | ||
419 | /** | |
420 | * p9_write_work - called when a transport can send some data | |
ee443996 EVH |
421 | * @work: container for work to be done |
422 | * | |
8a0dc95f | 423 | */ |
ee443996 | 424 | |
8a0dc95f EVH |
425 | static void p9_write_work(struct work_struct *work) |
426 | { | |
427 | int n, err; | |
428 | struct p9_conn *m; | |
673d62cd | 429 | struct p9_req_t *req; |
8a0dc95f EVH |
430 | |
431 | m = container_of(work, struct p9_conn, wq); | |
432 | ||
433 | if (m->err < 0) { | |
434 | clear_bit(Wworksched, &m->wsched); | |
435 | return; | |
436 | } | |
437 | ||
438 | if (!m->wsize) { | |
439 | if (list_empty(&m->unsent_req_list)) { | |
440 | clear_bit(Wworksched, &m->wsched); | |
441 | return; | |
442 | } | |
443 | ||
673d62cd EVH |
444 | spin_lock(&m->client->lock); |
445 | req = list_entry(m->unsent_req_list.next, struct p9_req_t, | |
8a0dc95f | 446 | req_list); |
673d62cd | 447 | req->status = REQ_STATUS_SENT; |
8a0dc95f | 448 | list_move_tail(&req->req_list, &m->req_list); |
8a0dc95f | 449 | |
673d62cd EVH |
450 | m->wbuf = req->tc->sdata; |
451 | m->wsize = req->tc->size; | |
8a0dc95f | 452 | m->wpos = 0; |
673d62cd | 453 | spin_unlock(&m->client->lock); |
8a0dc95f EVH |
454 | } |
455 | ||
456 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, | |
457 | m->wsize); | |
458 | clear_bit(Wpending, &m->wsched); | |
8b81ef58 | 459 | err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos); |
8a0dc95f EVH |
460 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p sent %d bytes\n", m, err); |
461 | if (err == -EAGAIN) { | |
462 | clear_bit(Wworksched, &m->wsched); | |
463 | return; | |
464 | } | |
465 | ||
466 | if (err < 0) | |
467 | goto error; | |
468 | else if (err == 0) { | |
469 | err = -EREMOTEIO; | |
470 | goto error; | |
471 | } | |
472 | ||
473 | m->wpos += err; | |
474 | if (m->wpos == m->wsize) | |
475 | m->wpos = m->wsize = 0; | |
476 | ||
477 | if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) { | |
478 | if (test_and_clear_bit(Wpending, &m->wsched)) | |
479 | n = POLLOUT; | |
480 | else | |
8b81ef58 | 481 | n = p9_fd_poll(m->client, NULL); |
8a0dc95f EVH |
482 | |
483 | if (n & POLLOUT) { | |
484 | P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m); | |
485 | queue_work(p9_mux_wq, &m->wq); | |
486 | } else | |
487 | clear_bit(Wworksched, &m->wsched); | |
488 | } else | |
489 | clear_bit(Wworksched, &m->wsched); | |
490 | ||
491 | return; | |
492 | ||
493 | error: | |
494 | p9_conn_cancel(m, err); | |
495 | clear_bit(Wworksched, &m->wsched); | |
496 | } | |
497 | ||
5503ac56 | 498 | static int p9_pollwake(wait_queue_t *wait, unsigned mode, int sync, void *key) |
8a0dc95f | 499 | { |
5503ac56 EVH |
500 | struct p9_poll_wait *pwait = |
501 | container_of(wait, struct p9_poll_wait, wait); | |
502 | struct p9_conn *m = pwait->conn; | |
503 | unsigned long flags; | |
504 | DECLARE_WAITQUEUE(dummy_wait, p9_poll_task); | |
8a0dc95f | 505 | |
5503ac56 EVH |
506 | spin_lock_irqsave(&p9_poll_lock, flags); |
507 | if (list_empty(&m->poll_pending_link)) | |
508 | list_add_tail(&m->poll_pending_link, &p9_poll_pending_list); | |
509 | spin_unlock_irqrestore(&p9_poll_lock, flags); | |
8a0dc95f | 510 | |
5503ac56 EVH |
511 | /* perform the default wake up operation */ |
512 | return default_wake_function(&dummy_wait, mode, sync, key); | |
8a0dc95f EVH |
513 | } |
514 | ||
515 | /** | |
5503ac56 EVH |
516 | * p9_pollwait - add poll task to the wait queue |
517 | * @filp: file pointer being polled | |
518 | * @wait_address: wait_q to block on | |
519 | * @p: poll state | |
ee443996 | 520 | * |
5503ac56 | 521 | * called by files poll operation to add v9fs-poll task to files wait queue |
8a0dc95f | 522 | */ |
ee443996 | 523 | |
5503ac56 EVH |
524 | static void |
525 | p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p) | |
8a0dc95f | 526 | { |
5503ac56 EVH |
527 | struct p9_conn *m = container_of(p, struct p9_conn, pt); |
528 | struct p9_poll_wait *pwait = NULL; | |
529 | int i; | |
8a0dc95f | 530 | |
5503ac56 EVH |
531 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
532 | if (m->poll_wait[i].wait_addr == NULL) { | |
533 | pwait = &m->poll_wait[i]; | |
534 | break; | |
8a0dc95f | 535 | } |
8a0dc95f EVH |
536 | } |
537 | ||
5503ac56 EVH |
538 | if (!pwait) { |
539 | P9_DPRINTK(P9_DEBUG_ERROR, "not enough wait_address slots\n"); | |
8a0dc95f EVH |
540 | return; |
541 | } | |
542 | ||
5503ac56 EVH |
543 | pwait->conn = m; |
544 | pwait->wait_addr = wait_address; | |
545 | init_waitqueue_func_entry(&pwait->wait, p9_pollwake); | |
546 | add_wait_queue(wait_address, &pwait->wait); | |
547 | } | |
8a0dc95f | 548 | |
5503ac56 EVH |
549 | /** |
550 | * p9_conn_create - allocate and initialize the per-session mux data | |
551 | * @client: client instance | |
552 | * | |
553 | * Note: Creates the polling task if this is the first session. | |
554 | */ | |
8a0dc95f | 555 | |
5503ac56 EVH |
556 | static struct p9_conn *p9_conn_create(struct p9_client *client) |
557 | { | |
95820a36 | 558 | int n; |
5503ac56 | 559 | struct p9_conn *m; |
8a0dc95f | 560 | |
5503ac56 EVH |
561 | P9_DPRINTK(P9_DEBUG_MUX, "client %p msize %d\n", client, client->msize); |
562 | m = kzalloc(sizeof(struct p9_conn), GFP_KERNEL); | |
563 | if (!m) | |
564 | return ERR_PTR(-ENOMEM); | |
8a0dc95f | 565 | |
5503ac56 EVH |
566 | INIT_LIST_HEAD(&m->mux_list); |
567 | m->client = client; | |
8a0dc95f | 568 | |
5503ac56 EVH |
569 | INIT_LIST_HEAD(&m->req_list); |
570 | INIT_LIST_HEAD(&m->unsent_req_list); | |
571 | INIT_WORK(&m->rq, p9_read_work); | |
572 | INIT_WORK(&m->wq, p9_write_work); | |
573 | INIT_LIST_HEAD(&m->poll_pending_link); | |
574 | init_poll_funcptr(&m->pt, p9_pollwait); | |
8a0dc95f | 575 | |
5503ac56 EVH |
576 | n = p9_fd_poll(client, &m->pt); |
577 | if (n & POLLIN) { | |
578 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m); | |
579 | set_bit(Rpending, &m->wsched); | |
580 | } | |
8a0dc95f | 581 | |
5503ac56 EVH |
582 | if (n & POLLOUT) { |
583 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m); | |
584 | set_bit(Wpending, &m->wsched); | |
585 | } | |
586 | ||
5503ac56 EVH |
587 | return m; |
588 | } | |
8a0dc95f | 589 | |
5503ac56 EVH |
590 | /** |
591 | * p9_poll_mux - polls a mux and schedules read or write works if necessary | |
592 | * @m: connection to poll | |
593 | * | |
594 | */ | |
595 | ||
596 | static void p9_poll_mux(struct p9_conn *m) | |
597 | { | |
598 | int n; | |
599 | ||
600 | if (m->err < 0) | |
601 | return; | |
602 | ||
603 | n = p9_fd_poll(m->client, NULL); | |
604 | if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) { | |
605 | P9_DPRINTK(P9_DEBUG_MUX, "error mux %p err %d\n", m, n); | |
606 | if (n >= 0) | |
607 | n = -ECONNRESET; | |
608 | p9_conn_cancel(m, n); | |
609 | } | |
610 | ||
611 | if (n & POLLIN) { | |
612 | set_bit(Rpending, &m->wsched); | |
613 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can read\n", m); | |
614 | if (!test_and_set_bit(Rworksched, &m->wsched)) { | |
8a0dc95f EVH |
615 | P9_DPRINTK(P9_DEBUG_MUX, "schedule read work %p\n", m); |
616 | queue_work(p9_mux_wq, &m->rq); | |
5503ac56 EVH |
617 | } |
618 | } | |
8a0dc95f | 619 | |
5503ac56 EVH |
620 | if (n & POLLOUT) { |
621 | set_bit(Wpending, &m->wsched); | |
622 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p can write\n", m); | |
623 | if ((m->wsize || !list_empty(&m->unsent_req_list)) | |
624 | && !test_and_set_bit(Wworksched, &m->wsched)) { | |
625 | P9_DPRINTK(P9_DEBUG_MUX, "schedule write work %p\n", m); | |
626 | queue_work(p9_mux_wq, &m->wq); | |
627 | } | |
628 | } | |
8a0dc95f EVH |
629 | } |
630 | ||
631 | /** | |
91b8534f | 632 | * p9_fd_request - send 9P request |
8a0dc95f EVH |
633 | * The function can sleep until the request is scheduled for sending. |
634 | * The function can be interrupted. Return from the function is not | |
91b8534f | 635 | * a guarantee that the request is sent successfully. |
8a0dc95f | 636 | * |
91b8534f EVH |
637 | * @client: client instance |
638 | * @req: request to be sent | |
ee443996 | 639 | * |
8a0dc95f | 640 | */ |
ee443996 | 641 | |
91b8534f | 642 | static int p9_fd_request(struct p9_client *client, struct p9_req_t *req) |
8a0dc95f EVH |
643 | { |
644 | int n; | |
91b8534f EVH |
645 | struct p9_trans_fd *ts = client->trans; |
646 | struct p9_conn *m = ts->conn; | |
8a0dc95f EVH |
647 | |
648 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current, | |
91b8534f | 649 | req->tc, req->tc->id); |
8a0dc95f | 650 | if (m->err < 0) |
91b8534f | 651 | return m->err; |
8a0dc95f | 652 | |
673d62cd | 653 | req->status = REQ_STATUS_UNSENT; |
8a0dc95f | 654 | |
91b8534f | 655 | spin_lock(&client->lock); |
8a0dc95f | 656 | list_add_tail(&req->req_list, &m->unsent_req_list); |
91b8534f | 657 | spin_unlock(&client->lock); |
8a0dc95f EVH |
658 | |
659 | if (test_and_clear_bit(Wpending, &m->wsched)) | |
660 | n = POLLOUT; | |
661 | else | |
8b81ef58 | 662 | n = p9_fd_poll(m->client, NULL); |
8a0dc95f EVH |
663 | |
664 | if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched)) | |
665 | queue_work(p9_mux_wq, &m->wq); | |
666 | ||
91b8534f | 667 | return 0; |
8a0dc95f EVH |
668 | } |
669 | ||
91b8534f | 670 | static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req) |
8a0dc95f | 671 | { |
91b8534f EVH |
672 | struct p9_trans_fd *ts = client->trans; |
673 | struct p9_conn *m = ts->conn; | |
8a0dc95f | 674 | |
cb198131 | 675 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p req %p\n", m, req); |
8a0dc95f | 676 | |
91b8534f EVH |
677 | spin_lock(&client->lock); |
678 | list_del(&req->req_list); | |
679 | spin_unlock(&client->lock); | |
680 | ||
8a0dc95f | 681 | /* if a response was received for a request, do nothing */ |
673d62cd | 682 | if (req->rc || req->t_err) { |
8a0dc95f EVH |
683 | P9_DPRINTK(P9_DEBUG_MUX, |
684 | "mux %p req %p response already received\n", m, req); | |
685 | return 0; | |
686 | } | |
687 | ||
91b8534f EVH |
688 | if (req->status == REQ_STATUS_UNSENT) { |
689 | req->status = REQ_STATUS_FLSHD; | |
690 | return 0; | |
8a0dc95f | 691 | } |
8a0dc95f | 692 | |
8a0dc95f EVH |
693 | return 1; |
694 | } | |
695 | ||
a80d923e | 696 | /** |
bb8ffdfc | 697 | * parse_options - parse mount options into session structure |
a80d923e | 698 | * @options: options string passed from mount |
ee443996 | 699 | * @opts: transport-specific structure to parse options into |
a80d923e | 700 | * |
bb8ffdfc | 701 | * Returns 0 upon success, -ERRNO upon failure |
a80d923e | 702 | */ |
bd238fb4 | 703 | |
bb8ffdfc | 704 | static int parse_opts(char *params, struct p9_fd_opts *opts) |
bd238fb4 | 705 | { |
a80d923e EVH |
706 | char *p; |
707 | substring_t args[MAX_OPT_ARGS]; | |
708 | int option; | |
bb8ffdfc | 709 | char *options; |
a80d923e | 710 | int ret; |
bd238fb4 | 711 | |
a80d923e EVH |
712 | opts->port = P9_PORT; |
713 | opts->rfd = ~0; | |
714 | opts->wfd = ~0; | |
bd238fb4 | 715 | |
bb8ffdfc EVH |
716 | if (!params) |
717 | return 0; | |
718 | ||
719 | options = kstrdup(params, GFP_KERNEL); | |
720 | if (!options) { | |
721 | P9_DPRINTK(P9_DEBUG_ERROR, | |
722 | "failed to allocate copy of option string\n"); | |
723 | return -ENOMEM; | |
724 | } | |
bd238fb4 | 725 | |
a80d923e EVH |
726 | while ((p = strsep(&options, ",")) != NULL) { |
727 | int token; | |
bb8ffdfc | 728 | int r; |
a80d923e EVH |
729 | if (!*p) |
730 | continue; | |
731 | token = match_token(p, tokens, args); | |
bb8ffdfc EVH |
732 | r = match_int(&args[0], &option); |
733 | if (r < 0) { | |
a80d923e EVH |
734 | P9_DPRINTK(P9_DEBUG_ERROR, |
735 | "integer field, but no integer?\n"); | |
bb8ffdfc | 736 | ret = r; |
a80d923e EVH |
737 | continue; |
738 | } | |
739 | switch (token) { | |
740 | case Opt_port: | |
741 | opts->port = option; | |
742 | break; | |
743 | case Opt_rfdno: | |
744 | opts->rfd = option; | |
745 | break; | |
746 | case Opt_wfdno: | |
747 | opts->wfd = option; | |
748 | break; | |
749 | default: | |
750 | continue; | |
751 | } | |
bd238fb4 | 752 | } |
bb8ffdfc EVH |
753 | kfree(options); |
754 | return 0; | |
bd238fb4 | 755 | } |
bd238fb4 | 756 | |
8b81ef58 | 757 | static int p9_fd_open(struct p9_client *client, int rfd, int wfd) |
bd238fb4 | 758 | { |
a80d923e EVH |
759 | struct p9_trans_fd *ts = kmalloc(sizeof(struct p9_trans_fd), |
760 | GFP_KERNEL); | |
761 | if (!ts) | |
762 | return -ENOMEM; | |
bd238fb4 | 763 | |
a80d923e EVH |
764 | ts->rd = fget(rfd); |
765 | ts->wr = fget(wfd); | |
766 | if (!ts->rd || !ts->wr) { | |
767 | if (ts->rd) | |
768 | fput(ts->rd); | |
769 | if (ts->wr) | |
770 | fput(ts->wr); | |
771 | kfree(ts); | |
772 | return -EIO; | |
bd238fb4 LI |
773 | } |
774 | ||
8b81ef58 EVH |
775 | client->trans = ts; |
776 | client->status = Connected; | |
bd238fb4 | 777 | |
a80d923e | 778 | return 0; |
bd238fb4 | 779 | } |
bd238fb4 | 780 | |
8b81ef58 | 781 | static int p9_socket_open(struct p9_client *client, struct socket *csocket) |
bd238fb4 LI |
782 | { |
783 | int fd, ret; | |
784 | ||
785 | csocket->sk->sk_allocation = GFP_NOIO; | |
a677a039 | 786 | fd = sock_map_fd(csocket, 0); |
bd238fb4 LI |
787 | if (fd < 0) { |
788 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to map fd\n"); | |
789 | return fd; | |
790 | } | |
791 | ||
8b81ef58 | 792 | ret = p9_fd_open(client, fd, fd); |
bd238fb4 LI |
793 | if (ret < 0) { |
794 | P9_EPRINTK(KERN_ERR, "p9_socket_open: failed to open fd\n"); | |
795 | sockfd_put(csocket); | |
796 | return ret; | |
797 | } | |
798 | ||
8b81ef58 | 799 | ((struct p9_trans_fd *)client->trans)->rd->f_flags |= O_NONBLOCK; |
bd238fb4 LI |
800 | |
801 | return 0; | |
802 | } | |
803 | ||
bd238fb4 | 804 | /** |
5503ac56 EVH |
805 | * p9_mux_destroy - cancels all pending requests and frees mux resources |
806 | * @m: mux to destroy | |
bd238fb4 LI |
807 | * |
808 | */ | |
ee443996 | 809 | |
5503ac56 | 810 | static void p9_conn_destroy(struct p9_conn *m) |
bd238fb4 | 811 | { |
5503ac56 EVH |
812 | P9_DPRINTK(P9_DEBUG_MUX, "mux %p prev %p next %p\n", m, |
813 | m->mux_list.prev, m->mux_list.next); | |
bd238fb4 | 814 | |
5503ac56 EVH |
815 | p9_mux_poll_stop(m); |
816 | cancel_work_sync(&m->rq); | |
817 | cancel_work_sync(&m->wq); | |
bd238fb4 | 818 | |
5503ac56 | 819 | p9_conn_cancel(m, -ECONNRESET); |
bd238fb4 | 820 | |
5503ac56 | 821 | m->client = NULL; |
5503ac56 | 822 | kfree(m); |
bd238fb4 LI |
823 | } |
824 | ||
825 | /** | |
8b81ef58 EVH |
826 | * p9_fd_close - shutdown file descriptor transport |
827 | * @client: client instance | |
bd238fb4 LI |
828 | * |
829 | */ | |
ee443996 | 830 | |
8b81ef58 | 831 | static void p9_fd_close(struct p9_client *client) |
bd238fb4 LI |
832 | { |
833 | struct p9_trans_fd *ts; | |
834 | ||
8b81ef58 | 835 | if (!client) |
bd238fb4 LI |
836 | return; |
837 | ||
8b81ef58 | 838 | ts = client->trans; |
bd238fb4 LI |
839 | if (!ts) |
840 | return; | |
841 | ||
8b81ef58 EVH |
842 | client->status = Disconnected; |
843 | ||
8a0dc95f EVH |
844 | p9_conn_destroy(ts->conn); |
845 | ||
bd238fb4 LI |
846 | if (ts->rd) |
847 | fput(ts->rd); | |
848 | if (ts->wr) | |
849 | fput(ts->wr); | |
8b81ef58 | 850 | |
bd238fb4 LI |
851 | kfree(ts); |
852 | } | |
853 | ||
887b3ece EVH |
854 | /* |
855 | * stolen from NFS - maybe should be made a generic function? | |
856 | */ | |
857 | static inline int valid_ipaddr4(const char *buf) | |
858 | { | |
859 | int rc, count, in[4]; | |
860 | ||
861 | rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]); | |
862 | if (rc != 4) | |
863 | return -EINVAL; | |
864 | for (count = 0; count < 4; count++) { | |
865 | if (in[count] > 255) | |
866 | return -EINVAL; | |
867 | } | |
868 | return 0; | |
869 | } | |
870 | ||
8b81ef58 EVH |
871 | static int |
872 | p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) | |
a80d923e EVH |
873 | { |
874 | int err; | |
a80d923e EVH |
875 | struct socket *csocket; |
876 | struct sockaddr_in sin_server; | |
877 | struct p9_fd_opts opts; | |
8b81ef58 | 878 | struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */ |
a80d923e | 879 | |
bb8ffdfc EVH |
880 | err = parse_opts(args, &opts); |
881 | if (err < 0) | |
8b81ef58 | 882 | return err; |
a80d923e | 883 | |
887b3ece | 884 | if (valid_ipaddr4(addr) < 0) |
8b81ef58 | 885 | return -EINVAL; |
887b3ece | 886 | |
a80d923e | 887 | csocket = NULL; |
a80d923e EVH |
888 | |
889 | sin_server.sin_family = AF_INET; | |
890 | sin_server.sin_addr.s_addr = in_aton(addr); | |
891 | sin_server.sin_port = htons(opts.port); | |
892 | sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &csocket); | |
893 | ||
894 | if (!csocket) { | |
895 | P9_EPRINTK(KERN_ERR, "p9_trans_tcp: problem creating socket\n"); | |
896 | err = -EIO; | |
897 | goto error; | |
898 | } | |
899 | ||
900 | err = csocket->ops->connect(csocket, | |
901 | (struct sockaddr *)&sin_server, | |
902 | sizeof(struct sockaddr_in), 0); | |
903 | if (err < 0) { | |
904 | P9_EPRINTK(KERN_ERR, | |
905 | "p9_trans_tcp: problem connecting socket to %s\n", | |
906 | addr); | |
907 | goto error; | |
908 | } | |
909 | ||
8b81ef58 | 910 | err = p9_socket_open(client, csocket); |
a80d923e EVH |
911 | if (err < 0) |
912 | goto error; | |
913 | ||
8b81ef58 EVH |
914 | p = (struct p9_trans_fd *) client->trans; |
915 | p->conn = p9_conn_create(client); | |
8a0dc95f EVH |
916 | if (IS_ERR(p->conn)) { |
917 | err = PTR_ERR(p->conn); | |
918 | p->conn = NULL; | |
919 | goto error; | |
920 | } | |
921 | ||
8b81ef58 | 922 | return 0; |
a80d923e EVH |
923 | |
924 | error: | |
925 | if (csocket) | |
926 | sock_release(csocket); | |
927 | ||
8b81ef58 EVH |
928 | kfree(p); |
929 | ||
930 | return err; | |
a80d923e EVH |
931 | } |
932 | ||
8b81ef58 EVH |
933 | static int |
934 | p9_fd_create_unix(struct p9_client *client, const char *addr, char *args) | |
a80d923e EVH |
935 | { |
936 | int err; | |
937 | struct socket *csocket; | |
938 | struct sockaddr_un sun_server; | |
8b81ef58 | 939 | struct p9_trans_fd *p = NULL; /* this gets allocated in p9_fd_open */ |
a80d923e EVH |
940 | |
941 | csocket = NULL; | |
a80d923e EVH |
942 | |
943 | if (strlen(addr) > UNIX_PATH_MAX) { | |
944 | P9_EPRINTK(KERN_ERR, "p9_trans_unix: address too long: %s\n", | |
945 | addr); | |
946 | err = -ENAMETOOLONG; | |
947 | goto error; | |
948 | } | |
949 | ||
950 | sun_server.sun_family = PF_UNIX; | |
951 | strcpy(sun_server.sun_path, addr); | |
952 | sock_create_kern(PF_UNIX, SOCK_STREAM, 0, &csocket); | |
953 | err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, | |
954 | sizeof(struct sockaddr_un) - 1, 0); | |
955 | if (err < 0) { | |
956 | P9_EPRINTK(KERN_ERR, | |
957 | "p9_trans_unix: problem connecting socket: %s: %d\n", | |
958 | addr, err); | |
959 | goto error; | |
960 | } | |
961 | ||
8b81ef58 | 962 | err = p9_socket_open(client, csocket); |
a80d923e EVH |
963 | if (err < 0) |
964 | goto error; | |
965 | ||
8b81ef58 EVH |
966 | p = (struct p9_trans_fd *) client->trans; |
967 | p->conn = p9_conn_create(client); | |
8a0dc95f EVH |
968 | if (IS_ERR(p->conn)) { |
969 | err = PTR_ERR(p->conn); | |
970 | p->conn = NULL; | |
971 | goto error; | |
972 | } | |
973 | ||
8b81ef58 | 974 | return 0; |
a80d923e EVH |
975 | |
976 | error: | |
977 | if (csocket) | |
978 | sock_release(csocket); | |
979 | ||
8b81ef58 EVH |
980 | kfree(p); |
981 | return err; | |
a80d923e EVH |
982 | } |
983 | ||
8b81ef58 EVH |
984 | static int |
985 | p9_fd_create(struct p9_client *client, const char *addr, char *args) | |
a80d923e EVH |
986 | { |
987 | int err; | |
a80d923e | 988 | struct p9_fd_opts opts; |
8b81ef58 | 989 | struct p9_trans_fd *p = NULL; /* this get allocated in p9_fd_open */ |
a80d923e EVH |
990 | |
991 | parse_opts(args, &opts); | |
992 | ||
993 | if (opts.rfd == ~0 || opts.wfd == ~0) { | |
994 | printk(KERN_ERR "v9fs: Insufficient options for proto=fd\n"); | |
8b81ef58 | 995 | return -ENOPROTOOPT; |
a80d923e EVH |
996 | } |
997 | ||
8b81ef58 | 998 | err = p9_fd_open(client, opts.rfd, opts.wfd); |
a80d923e EVH |
999 | if (err < 0) |
1000 | goto error; | |
1001 | ||
8b81ef58 EVH |
1002 | p = (struct p9_trans_fd *) client->trans; |
1003 | p->conn = p9_conn_create(client); | |
8a0dc95f EVH |
1004 | if (IS_ERR(p->conn)) { |
1005 | err = PTR_ERR(p->conn); | |
1006 | p->conn = NULL; | |
1007 | goto error; | |
1008 | } | |
1009 | ||
8b81ef58 | 1010 | return 0; |
a80d923e EVH |
1011 | |
1012 | error: | |
8b81ef58 EVH |
1013 | kfree(p); |
1014 | return err; | |
a80d923e EVH |
1015 | } |
1016 | ||
1017 | static struct p9_trans_module p9_tcp_trans = { | |
1018 | .name = "tcp", | |
1019 | .maxsize = MAX_SOCK_BUF, | |
1020 | .def = 1, | |
8b81ef58 EVH |
1021 | .create = p9_fd_create_tcp, |
1022 | .close = p9_fd_close, | |
91b8534f EVH |
1023 | .request = p9_fd_request, |
1024 | .cancel = p9_fd_cancel, | |
72029fe8 | 1025 | .owner = THIS_MODULE, |
a80d923e EVH |
1026 | }; |
1027 | ||
1028 | static struct p9_trans_module p9_unix_trans = { | |
1029 | .name = "unix", | |
1030 | .maxsize = MAX_SOCK_BUF, | |
1031 | .def = 0, | |
8b81ef58 EVH |
1032 | .create = p9_fd_create_unix, |
1033 | .close = p9_fd_close, | |
91b8534f EVH |
1034 | .request = p9_fd_request, |
1035 | .cancel = p9_fd_cancel, | |
72029fe8 | 1036 | .owner = THIS_MODULE, |
a80d923e EVH |
1037 | }; |
1038 | ||
1039 | static struct p9_trans_module p9_fd_trans = { | |
1040 | .name = "fd", | |
1041 | .maxsize = MAX_SOCK_BUF, | |
1042 | .def = 0, | |
8b81ef58 EVH |
1043 | .create = p9_fd_create, |
1044 | .close = p9_fd_close, | |
91b8534f EVH |
1045 | .request = p9_fd_request, |
1046 | .cancel = p9_fd_cancel, | |
72029fe8 | 1047 | .owner = THIS_MODULE, |
a80d923e EVH |
1048 | }; |
1049 | ||
5503ac56 EVH |
1050 | /** |
1051 | * p9_poll_proc - poll worker thread | |
1052 | * @a: thread state and arguments | |
1053 | * | |
1054 | * polls all v9fs transports for new events and queues the appropriate | |
1055 | * work to the work queue | |
1056 | * | |
1057 | */ | |
1058 | ||
1059 | static int p9_poll_proc(void *a) | |
1060 | { | |
1061 | unsigned long flags; | |
1062 | ||
1063 | P9_DPRINTK(P9_DEBUG_MUX, "start %p\n", current); | |
1064 | repeat: | |
1065 | spin_lock_irqsave(&p9_poll_lock, flags); | |
1066 | while (!list_empty(&p9_poll_pending_list)) { | |
1067 | struct p9_conn *conn = list_first_entry(&p9_poll_pending_list, | |
1068 | struct p9_conn, | |
1069 | poll_pending_link); | |
1070 | list_del_init(&conn->poll_pending_link); | |
1071 | spin_unlock_irqrestore(&p9_poll_lock, flags); | |
1072 | ||
1073 | p9_poll_mux(conn); | |
1074 | ||
1075 | spin_lock_irqsave(&p9_poll_lock, flags); | |
1076 | } | |
1077 | spin_unlock_irqrestore(&p9_poll_lock, flags); | |
1078 | ||
1079 | set_current_state(TASK_INTERRUPTIBLE); | |
1080 | if (list_empty(&p9_poll_pending_list)) { | |
1081 | P9_DPRINTK(P9_DEBUG_MUX, "sleeping...\n"); | |
1082 | schedule(); | |
1083 | } | |
1084 | __set_current_state(TASK_RUNNING); | |
1085 | ||
1086 | if (!kthread_should_stop()) | |
1087 | goto repeat; | |
1088 | ||
1089 | P9_DPRINTK(P9_DEBUG_MUX, "finish\n"); | |
1090 | return 0; | |
1091 | } | |
1092 | ||
887b3ece | 1093 | int p9_trans_fd_init(void) |
a80d923e | 1094 | { |
206ca50d TH |
1095 | p9_mux_wq = create_workqueue("v9fs"); |
1096 | if (!p9_mux_wq) { | |
1097 | printk(KERN_WARNING "v9fs: mux: creating workqueue failed\n"); | |
1098 | return -ENOMEM; | |
8a0dc95f EVH |
1099 | } |
1100 | ||
992b3f1d TH |
1101 | p9_poll_task = kthread_run(p9_poll_proc, NULL, "v9fs-poll"); |
1102 | if (IS_ERR(p9_poll_task)) { | |
1103 | destroy_workqueue(p9_mux_wq); | |
1104 | printk(KERN_WARNING "v9fs: mux: creating poll task failed\n"); | |
1105 | return PTR_ERR(p9_poll_task); | |
1106 | } | |
1107 | ||
a80d923e EVH |
1108 | v9fs_register_trans(&p9_tcp_trans); |
1109 | v9fs_register_trans(&p9_unix_trans); | |
1110 | v9fs_register_trans(&p9_fd_trans); | |
1111 | ||
3387b804 | 1112 | return 0; |
a80d923e | 1113 | } |
72029fe8 TH |
1114 | |
1115 | void p9_trans_fd_exit(void) | |
1116 | { | |
992b3f1d | 1117 | kthread_stop(p9_poll_task); |
72029fe8 TH |
1118 | v9fs_unregister_trans(&p9_tcp_trans); |
1119 | v9fs_unregister_trans(&p9_unix_trans); | |
1120 | v9fs_unregister_trans(&p9_fd_trans); | |
206ca50d TH |
1121 | |
1122 | destroy_workqueue(p9_mux_wq); | |
72029fe8 | 1123 | } |