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