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