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 TB |
345 | m->rc.tag, m->rreq); |
346 | m->rreq = NULL; | |
3053600e DM |
347 | err = -EIO; |
348 | goto error; | |
1b0a763b | 349 | } |
6d35190f | 350 | m->rc.sdata = m->rreq->rc.sdata; |
947867aa DM |
351 | memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity); |
352 | m->rc.capacity = m->rc.size; | |
1b0a763b | 353 | } |
5503ac56 | 354 | |
947867aa DM |
355 | /* packet is read in |
356 | * not an else because some packets (like clunk) have no payload | |
357 | */ | |
6d35190f | 358 | if ((m->rreq) && (m->rc.offset == m->rc.capacity)) { |
5d385153 | 359 | p9_debug(P9_DEBUG_TRANS, "got new packet\n"); |
6d35190f | 360 | m->rreq->rc.size = m->rc.offset; |
7eb923b8 | 361 | spin_lock(&m->client->lock); |
e4ca13f7 DM |
362 | if (m->rreq->status == REQ_STATUS_SENT) { |
363 | list_del(&m->rreq->req_list); | |
364 | p9_client_cb(m->client, m->rreq, REQ_STATUS_RCVD); | |
74d6a5d5 WH |
365 | } else if (m->rreq->status == REQ_STATUS_FLSHD) { |
366 | /* Ignore replies associated with a cancelled request. */ | |
367 | p9_debug(P9_DEBUG_TRANS, | |
368 | "Ignore replies associated with a cancelled request\n"); | |
e4ca13f7 DM |
369 | } else { |
370 | spin_unlock(&m->client->lock); | |
371 | p9_debug(P9_DEBUG_ERROR, | |
372 | "Request tag %d errored out while we were reading the reply\n", | |
373 | m->rc.tag); | |
374 | err = -EIO; | |
375 | goto error; | |
376 | } | |
9f476d7c | 377 | spin_unlock(&m->client->lock); |
947867aa DM |
378 | m->rc.sdata = NULL; |
379 | m->rc.offset = 0; | |
380 | m->rc.capacity = 0; | |
6d35190f TB |
381 | p9_req_put(m->rreq); |
382 | m->rreq = NULL; | |
5503ac56 EVH |
383 | } |
384 | ||
0462194d SD |
385 | end_clear: |
386 | clear_bit(Rworksched, &m->wsched); | |
387 | ||
5503ac56 EVH |
388 | if (!list_empty(&m->req_list)) { |
389 | if (test_and_clear_bit(Rpending, &m->wsched)) | |
a9a08845 | 390 | n = EPOLLIN; |
5503ac56 | 391 | else |
7594bf37 | 392 | n = p9_fd_poll(m->client, NULL, NULL); |
5503ac56 | 393 | |
a9a08845 | 394 | if ((n & EPOLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) { |
5d385153 | 395 | p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m); |
61edeeed | 396 | schedule_work(&m->rq); |
0462194d SD |
397 | } |
398 | } | |
5503ac56 EVH |
399 | |
400 | return; | |
5503ac56 EVH |
401 | error: |
402 | p9_conn_cancel(m, err); | |
403 | clear_bit(Rworksched, &m->wsched); | |
404 | } | |
405 | ||
406 | /** | |
407 | * p9_fd_write - write to a socket | |
408 | * @client: client instance | |
409 | * @v: buffer to send data from | |
410 | * @len: size of send buffer | |
ee443996 | 411 | * |
8a0dc95f | 412 | */ |
ee443996 | 413 | |
5503ac56 | 414 | static int p9_fd_write(struct p9_client *client, void *v, int len) |
8a0dc95f | 415 | { |
670986ec | 416 | ssize_t ret; |
5503ac56 | 417 | struct p9_trans_fd *ts = NULL; |
8a0dc95f | 418 | |
5503ac56 EVH |
419 | if (client && client->status != Disconnected) |
420 | ts = client->trans; | |
8a0dc95f | 421 | |
5503ac56 EVH |
422 | if (!ts) |
423 | return -EREMOTEIO; | |
8a0dc95f | 424 | |
5503ac56 | 425 | if (!(ts->wr->f_flags & O_NONBLOCK)) |
5d385153 | 426 | p9_debug(P9_DEBUG_ERROR, "blocking write ...\n"); |
992b3f1d | 427 | |
670986ec | 428 | ret = kernel_write(ts->wr, v, len, &ts->wr->f_pos); |
5503ac56 EVH |
429 | if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN) |
430 | client->status = Disconnected; | |
431 | return ret; | |
8a0dc95f EVH |
432 | } |
433 | ||
434 | /** | |
435 | * p9_write_work - called when a transport can send some data | |
ee443996 EVH |
436 | * @work: container for work to be done |
437 | * | |
8a0dc95f | 438 | */ |
ee443996 | 439 | |
8a0dc95f EVH |
440 | static void p9_write_work(struct work_struct *work) |
441 | { | |
7594bf37 AV |
442 | __poll_t n; |
443 | int err; | |
8a0dc95f | 444 | struct p9_conn *m; |
673d62cd | 445 | struct p9_req_t *req; |
8a0dc95f EVH |
446 | |
447 | m = container_of(work, struct p9_conn, wq); | |
448 | ||
449 | if (m->err < 0) { | |
450 | clear_bit(Wworksched, &m->wsched); | |
451 | return; | |
452 | } | |
453 | ||
454 | if (!m->wsize) { | |
759f4298 | 455 | spin_lock(&m->client->lock); |
8a0dc95f EVH |
456 | if (list_empty(&m->unsent_req_list)) { |
457 | clear_bit(Wworksched, &m->wsched); | |
759f4298 | 458 | spin_unlock(&m->client->lock); |
8a0dc95f EVH |
459 | return; |
460 | } | |
461 | ||
673d62cd | 462 | req = list_entry(m->unsent_req_list.next, struct p9_req_t, |
8a0dc95f | 463 | req_list); |
673d62cd | 464 | req->status = REQ_STATUS_SENT; |
5d385153 | 465 | p9_debug(P9_DEBUG_TRANS, "move req %p\n", req); |
8a0dc95f | 466 | list_move_tail(&req->req_list, &m->req_list); |
8a0dc95f | 467 | |
523adb6c DM |
468 | m->wbuf = req->tc.sdata; |
469 | m->wsize = req->tc.size; | |
8a0dc95f | 470 | m->wpos = 0; |
728356de TB |
471 | p9_req_get(req); |
472 | m->wreq = req; | |
673d62cd | 473 | spin_unlock(&m->client->lock); |
8a0dc95f EVH |
474 | } |
475 | ||
5d385153 JP |
476 | p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n", |
477 | m, m->wpos, m->wsize); | |
8a0dc95f | 478 | clear_bit(Wpending, &m->wsched); |
8b81ef58 | 479 | err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos); |
5d385153 | 480 | p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err); |
584a8c13 SD |
481 | if (err == -EAGAIN) |
482 | goto end_clear; | |
483 | ||
8a0dc95f EVH |
484 | |
485 | if (err < 0) | |
486 | goto error; | |
487 | else if (err == 0) { | |
488 | err = -EREMOTEIO; | |
489 | goto error; | |
490 | } | |
491 | ||
492 | m->wpos += err; | |
728356de | 493 | if (m->wpos == m->wsize) { |
8a0dc95f | 494 | m->wpos = m->wsize = 0; |
728356de TB |
495 | p9_req_put(m->wreq); |
496 | m->wreq = NULL; | |
497 | } | |
8a0dc95f | 498 | |
584a8c13 SD |
499 | end_clear: |
500 | clear_bit(Wworksched, &m->wsched); | |
501 | ||
1957b3a8 | 502 | if (m->wsize || !list_empty(&m->unsent_req_list)) { |
8a0dc95f | 503 | if (test_and_clear_bit(Wpending, &m->wsched)) |
a9a08845 | 504 | n = EPOLLOUT; |
8a0dc95f | 505 | else |
7594bf37 | 506 | n = p9_fd_poll(m->client, NULL, NULL); |
8a0dc95f | 507 | |
a9a08845 | 508 | if ((n & EPOLLOUT) && |
584a8c13 | 509 | !test_and_set_bit(Wworksched, &m->wsched)) { |
5d385153 | 510 | p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m); |
61edeeed | 511 | schedule_work(&m->wq); |
584a8c13 SD |
512 | } |
513 | } | |
8a0dc95f EVH |
514 | |
515 | return; | |
516 | ||
517 | error: | |
518 | p9_conn_cancel(m, err); | |
519 | clear_bit(Wworksched, &m->wsched); | |
520 | } | |
521 | ||
ac6424b9 | 522 | static int p9_pollwake(wait_queue_entry_t *wait, unsigned int mode, int sync, void *key) |
8a0dc95f | 523 | { |
5503ac56 EVH |
524 | struct p9_poll_wait *pwait = |
525 | container_of(wait, struct p9_poll_wait, wait); | |
526 | struct p9_conn *m = pwait->conn; | |
527 | unsigned long flags; | |
8a0dc95f | 528 | |
5503ac56 EVH |
529 | spin_lock_irqsave(&p9_poll_lock, flags); |
530 | if (list_empty(&m->poll_pending_link)) | |
531 | list_add_tail(&m->poll_pending_link, &p9_poll_pending_list); | |
532 | spin_unlock_irqrestore(&p9_poll_lock, flags); | |
8a0dc95f | 533 | |
aa70c585 TH |
534 | schedule_work(&p9_poll_work); |
535 | return 1; | |
8a0dc95f EVH |
536 | } |
537 | ||
538 | /** | |
5503ac56 EVH |
539 | * p9_pollwait - add poll task to the wait queue |
540 | * @filp: file pointer being polled | |
541 | * @wait_address: wait_q to block on | |
542 | * @p: poll state | |
ee443996 | 543 | * |
5503ac56 | 544 | * called by files poll operation to add v9fs-poll task to files wait queue |
8a0dc95f | 545 | */ |
ee443996 | 546 | |
5503ac56 EVH |
547 | static void |
548 | p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p) | |
8a0dc95f | 549 | { |
5503ac56 EVH |
550 | struct p9_conn *m = container_of(p, struct p9_conn, pt); |
551 | struct p9_poll_wait *pwait = NULL; | |
552 | int i; | |
8a0dc95f | 553 | |
5503ac56 EVH |
554 | for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) { |
555 | if (m->poll_wait[i].wait_addr == NULL) { | |
556 | pwait = &m->poll_wait[i]; | |
557 | break; | |
8a0dc95f | 558 | } |
8a0dc95f EVH |
559 | } |
560 | ||
5503ac56 | 561 | if (!pwait) { |
5d385153 | 562 | p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n"); |
8a0dc95f EVH |
563 | return; |
564 | } | |
565 | ||
5503ac56 EVH |
566 | pwait->conn = m; |
567 | pwait->wait_addr = wait_address; | |
568 | init_waitqueue_func_entry(&pwait->wait, p9_pollwake); | |
569 | add_wait_queue(wait_address, &pwait->wait); | |
570 | } | |
8a0dc95f | 571 | |
5503ac56 | 572 | /** |
263c5828 | 573 | * p9_conn_create - initialize the per-session mux data |
5503ac56 EVH |
574 | * @client: client instance |
575 | * | |
576 | * Note: Creates the polling task if this is the first session. | |
577 | */ | |
8a0dc95f | 578 | |
263c5828 | 579 | static void p9_conn_create(struct p9_client *client) |
5503ac56 | 580 | { |
7594bf37 | 581 | __poll_t n; |
263c5828 SD |
582 | struct p9_trans_fd *ts = client->trans; |
583 | struct p9_conn *m = &ts->conn; | |
8a0dc95f | 584 | |
5d385153 | 585 | p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize); |
8a0dc95f | 586 | |
5503ac56 EVH |
587 | INIT_LIST_HEAD(&m->mux_list); |
588 | m->client = client; | |
8a0dc95f | 589 | |
5503ac56 EVH |
590 | INIT_LIST_HEAD(&m->req_list); |
591 | INIT_LIST_HEAD(&m->unsent_req_list); | |
592 | INIT_WORK(&m->rq, p9_read_work); | |
593 | INIT_WORK(&m->wq, p9_write_work); | |
594 | INIT_LIST_HEAD(&m->poll_pending_link); | |
595 | init_poll_funcptr(&m->pt, p9_pollwait); | |
8a0dc95f | 596 | |
7594bf37 | 597 | n = p9_fd_poll(client, &m->pt, NULL); |
a9a08845 | 598 | if (n & EPOLLIN) { |
5d385153 | 599 | p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m); |
5503ac56 EVH |
600 | set_bit(Rpending, &m->wsched); |
601 | } | |
8a0dc95f | 602 | |
a9a08845 | 603 | if (n & EPOLLOUT) { |
5d385153 | 604 | p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m); |
5503ac56 EVH |
605 | set_bit(Wpending, &m->wsched); |
606 | } | |
5503ac56 | 607 | } |
8a0dc95f | 608 | |
5503ac56 EVH |
609 | /** |
610 | * p9_poll_mux - polls a mux and schedules read or write works if necessary | |
611 | * @m: connection to poll | |
612 | * | |
613 | */ | |
614 | ||
615 | static void p9_poll_mux(struct p9_conn *m) | |
616 | { | |
7594bf37 AV |
617 | __poll_t n; |
618 | int err = -ECONNRESET; | |
5503ac56 EVH |
619 | |
620 | if (m->err < 0) | |
621 | return; | |
622 | ||
7594bf37 | 623 | n = p9_fd_poll(m->client, NULL, &err); |
a9a08845 | 624 | if (n & (EPOLLERR | EPOLLHUP | EPOLLNVAL)) { |
5d385153 | 625 | p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n); |
7594bf37 | 626 | p9_conn_cancel(m, err); |
5503ac56 EVH |
627 | } |
628 | ||
a9a08845 | 629 | if (n & EPOLLIN) { |
5503ac56 | 630 | set_bit(Rpending, &m->wsched); |
5d385153 | 631 | p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m); |
5503ac56 | 632 | if (!test_and_set_bit(Rworksched, &m->wsched)) { |
5d385153 | 633 | p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m); |
61edeeed | 634 | schedule_work(&m->rq); |
5503ac56 EVH |
635 | } |
636 | } | |
8a0dc95f | 637 | |
a9a08845 | 638 | if (n & EPOLLOUT) { |
5503ac56 | 639 | set_bit(Wpending, &m->wsched); |
5d385153 | 640 | p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m); |
f64f9e71 JP |
641 | if ((m->wsize || !list_empty(&m->unsent_req_list)) && |
642 | !test_and_set_bit(Wworksched, &m->wsched)) { | |
5d385153 | 643 | p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m); |
61edeeed | 644 | schedule_work(&m->wq); |
5503ac56 EVH |
645 | } |
646 | } | |
8a0dc95f EVH |
647 | } |
648 | ||
649 | /** | |
91b8534f | 650 | * p9_fd_request - send 9P request |
8a0dc95f EVH |
651 | * The function can sleep until the request is scheduled for sending. |
652 | * The function can be interrupted. Return from the function is not | |
91b8534f | 653 | * a guarantee that the request is sent successfully. |
8a0dc95f | 654 | * |
91b8534f EVH |
655 | * @client: client instance |
656 | * @req: request to be sent | |
ee443996 | 657 | * |
8a0dc95f | 658 | */ |
ee443996 | 659 | |
91b8534f | 660 | static int p9_fd_request(struct p9_client *client, struct p9_req_t *req) |
8a0dc95f | 661 | { |
7594bf37 | 662 | __poll_t n; |
91b8534f | 663 | struct p9_trans_fd *ts = client->trans; |
263c5828 | 664 | struct p9_conn *m = &ts->conn; |
8a0dc95f | 665 | |
5d385153 | 666 | p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n", |
523adb6c | 667 | m, current, &req->tc, req->tc.id); |
8a0dc95f | 668 | if (m->err < 0) |
91b8534f | 669 | return m->err; |
8a0dc95f | 670 | |
91b8534f | 671 | spin_lock(&client->lock); |
7eb923b8 | 672 | req->status = REQ_STATUS_UNSENT; |
8a0dc95f | 673 | list_add_tail(&req->req_list, &m->unsent_req_list); |
91b8534f | 674 | spin_unlock(&client->lock); |
8a0dc95f EVH |
675 | |
676 | if (test_and_clear_bit(Wpending, &m->wsched)) | |
a9a08845 | 677 | n = EPOLLOUT; |
8a0dc95f | 678 | else |
7594bf37 | 679 | n = p9_fd_poll(m->client, NULL, NULL); |
8a0dc95f | 680 | |
a9a08845 | 681 | if (n & EPOLLOUT && !test_and_set_bit(Wworksched, &m->wsched)) |
61edeeed | 682 | schedule_work(&m->wq); |
8a0dc95f | 683 | |
91b8534f | 684 | return 0; |
8a0dc95f EVH |
685 | } |
686 | ||
91b8534f | 687 | static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req) |
8a0dc95f | 688 | { |
7eb923b8 | 689 | int ret = 1; |
8a0dc95f | 690 | |
5d385153 | 691 | p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req); |
8a0dc95f | 692 | |
91b8534f | 693 | spin_lock(&client->lock); |
91b8534f | 694 | |
91b8534f | 695 | if (req->status == REQ_STATUS_UNSENT) { |
1bab88b2 | 696 | list_del(&req->req_list); |
91b8534f | 697 | req->status = REQ_STATUS_FLSHD; |
728356de | 698 | p9_req_put(req); |
7eb923b8 | 699 | ret = 0; |
0bfd6845 | 700 | } |
7eb923b8 EVH |
701 | spin_unlock(&client->lock); |
702 | ||
703 | return ret; | |
8a0dc95f EVH |
704 | } |
705 | ||
afd8d654 SD |
706 | static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req) |
707 | { | |
708 | p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req); | |
709 | ||
74d6a5d5 WH |
710 | spin_lock(&client->lock); |
711 | /* Ignore cancelled request if message has been received | |
712 | * before lock. | |
713 | */ | |
714 | if (req->status == REQ_STATUS_RCVD) { | |
715 | spin_unlock(&client->lock); | |
716 | return 0; | |
717 | } | |
718 | ||
afd8d654 SD |
719 | /* we haven't received a response for oldreq, |
720 | * remove it from the list. | |
721 | */ | |
afd8d654 | 722 | list_del(&req->req_list); |
74d6a5d5 | 723 | req->status = REQ_STATUS_FLSHD; |
afd8d654 | 724 | spin_unlock(&client->lock); |
728356de | 725 | p9_req_put(req); |
afd8d654 SD |
726 | |
727 | return 0; | |
728 | } | |
729 | ||
c4fac910 DH |
730 | static int p9_fd_show_options(struct seq_file *m, struct p9_client *clnt) |
731 | { | |
732 | if (clnt->trans_mod == &p9_tcp_trans) { | |
733 | if (clnt->trans_opts.tcp.port != P9_PORT) | |
61b272c3 | 734 | seq_printf(m, ",port=%u", clnt->trans_opts.tcp.port); |
c4fac910 DH |
735 | } else if (clnt->trans_mod == &p9_fd_trans) { |
736 | if (clnt->trans_opts.fd.rfd != ~0) | |
61b272c3 | 737 | seq_printf(m, ",rfd=%u", clnt->trans_opts.fd.rfd); |
c4fac910 | 738 | if (clnt->trans_opts.fd.wfd != ~0) |
61b272c3 | 739 | seq_printf(m, ",wfd=%u", clnt->trans_opts.fd.wfd); |
c4fac910 DH |
740 | } |
741 | return 0; | |
742 | } | |
743 | ||
a80d923e | 744 | /** |
0e15597e AK |
745 | * parse_opts - parse mount options into p9_fd_opts structure |
746 | * @params: options string passed from mount | |
747 | * @opts: fd transport-specific structure to parse options into | |
a80d923e | 748 | * |
bb8ffdfc | 749 | * Returns 0 upon success, -ERRNO upon failure |
a80d923e | 750 | */ |
bd238fb4 | 751 | |
bb8ffdfc | 752 | static int parse_opts(char *params, struct p9_fd_opts *opts) |
bd238fb4 | 753 | { |
a80d923e EVH |
754 | char *p; |
755 | substring_t args[MAX_OPT_ARGS]; | |
756 | int option; | |
d8c8a9e3 | 757 | char *options, *tmp_options; |
bd238fb4 | 758 | |
a80d923e EVH |
759 | opts->port = P9_PORT; |
760 | opts->rfd = ~0; | |
761 | opts->wfd = ~0; | |
c4fac910 | 762 | opts->privport = false; |
bd238fb4 | 763 | |
bb8ffdfc EVH |
764 | if (!params) |
765 | return 0; | |
766 | ||
d8c8a9e3 EVH |
767 | tmp_options = kstrdup(params, GFP_KERNEL); |
768 | if (!tmp_options) { | |
5d385153 JP |
769 | p9_debug(P9_DEBUG_ERROR, |
770 | "failed to allocate copy of option string\n"); | |
bb8ffdfc EVH |
771 | return -ENOMEM; |
772 | } | |
d8c8a9e3 | 773 | options = tmp_options; |
bd238fb4 | 774 | |
a80d923e EVH |
775 | while ((p = strsep(&options, ",")) != NULL) { |
776 | int token; | |
bb8ffdfc | 777 | int r; |
a80d923e EVH |
778 | if (!*p) |
779 | continue; | |
780 | token = match_token(p, tokens, args); | |
2f28c8b3 | 781 | if ((token != Opt_err) && (token != Opt_privport)) { |
15da4b16 AK |
782 | r = match_int(&args[0], &option); |
783 | if (r < 0) { | |
5d385153 JP |
784 | p9_debug(P9_DEBUG_ERROR, |
785 | "integer field, but no integer?\n"); | |
15da4b16 AK |
786 | continue; |
787 | } | |
a80d923e EVH |
788 | } |
789 | switch (token) { | |
790 | case Opt_port: | |
791 | opts->port = option; | |
792 | break; | |
793 | case Opt_rfdno: | |
794 | opts->rfd = option; | |
795 | break; | |
796 | case Opt_wfdno: | |
797 | opts->wfd = option; | |
798 | break; | |
2f28c8b3 | 799 | case Opt_privport: |
c4fac910 | 800 | opts->privport = true; |
2f28c8b3 | 801 | break; |
a80d923e EVH |
802 | default: |
803 | continue; | |
804 | } | |
bd238fb4 | 805 | } |
d8c8a9e3 EVH |
806 | |
807 | kfree(tmp_options); | |
bb8ffdfc | 808 | return 0; |
bd238fb4 | 809 | } |
bd238fb4 | 810 | |
8b81ef58 | 811 | static int p9_fd_open(struct p9_client *client, int rfd, int wfd) |
bd238fb4 | 812 | { |
263c5828 | 813 | struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd), |
a80d923e EVH |
814 | GFP_KERNEL); |
815 | if (!ts) | |
816 | return -ENOMEM; | |
bd238fb4 | 817 | |
a80d923e | 818 | ts->rd = fget(rfd); |
a39c4606 CH |
819 | if (!ts->rd) |
820 | goto out_free_ts; | |
821 | if (!(ts->rd->f_mode & FMODE_READ)) | |
822 | goto out_put_rd; | |
a80d923e | 823 | ts->wr = fget(wfd); |
a39c4606 CH |
824 | if (!ts->wr) |
825 | goto out_put_rd; | |
826 | if (!(ts->wr->f_mode & FMODE_WRITE)) | |
827 | goto out_put_wr; | |
bd238fb4 | 828 | |
8b81ef58 EVH |
829 | client->trans = ts; |
830 | client->status = Connected; | |
bd238fb4 | 831 | |
a80d923e | 832 | return 0; |
a39c4606 CH |
833 | |
834 | out_put_wr: | |
835 | fput(ts->wr); | |
836 | out_put_rd: | |
837 | fput(ts->rd); | |
838 | out_free_ts: | |
839 | kfree(ts); | |
840 | return -EIO; | |
bd238fb4 | 841 | } |
bd238fb4 | 842 | |
8b81ef58 | 843 | static int p9_socket_open(struct p9_client *client, struct socket *csocket) |
bd238fb4 | 844 | { |
6b18662e | 845 | struct p9_trans_fd *p; |
56b31d1c | 846 | struct file *file; |
6b18662e | 847 | |
263c5828 | 848 | p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL); |
6b18662e AV |
849 | if (!p) |
850 | return -ENOMEM; | |
bd238fb4 LI |
851 | |
852 | csocket->sk->sk_allocation = GFP_NOIO; | |
aab174f0 | 853 | file = sock_alloc_file(csocket, 0, NULL); |
56b31d1c | 854 | if (IS_ERR(file)) { |
5d385153 JP |
855 | pr_err("%s (%d): failed to map fd\n", |
856 | __func__, task_pid_nr(current)); | |
6b18662e | 857 | kfree(p); |
56b31d1c | 858 | return PTR_ERR(file); |
bd238fb4 LI |
859 | } |
860 | ||
56b31d1c AV |
861 | get_file(file); |
862 | p->wr = p->rd = file; | |
6b18662e AV |
863 | client->trans = p; |
864 | client->status = Connected; | |
865 | ||
6b18662e AV |
866 | p->rd->f_flags |= O_NONBLOCK; |
867 | ||
263c5828 | 868 | p9_conn_create(client); |
bd238fb4 LI |
869 | return 0; |
870 | } | |
871 | ||
bd238fb4 | 872 | /** |
54e625e3 | 873 | * p9_conn_destroy - cancels all pending requests of mux |
5503ac56 | 874 | * @m: mux to destroy |
bd238fb4 LI |
875 | * |
876 | */ | |
ee443996 | 877 | |
5503ac56 | 878 | static void p9_conn_destroy(struct p9_conn *m) |
bd238fb4 | 879 | { |
5d385153 JP |
880 | p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n", |
881 | m, m->mux_list.prev, m->mux_list.next); | |
bd238fb4 | 882 | |
5503ac56 EVH |
883 | p9_mux_poll_stop(m); |
884 | cancel_work_sync(&m->rq); | |
fb488fc1 DM |
885 | if (m->rreq) { |
886 | p9_req_put(m->rreq); | |
887 | m->rreq = NULL; | |
888 | } | |
5503ac56 | 889 | cancel_work_sync(&m->wq); |
fb488fc1 DM |
890 | if (m->wreq) { |
891 | p9_req_put(m->wreq); | |
892 | m->wreq = NULL; | |
893 | } | |
bd238fb4 | 894 | |
5503ac56 | 895 | p9_conn_cancel(m, -ECONNRESET); |
bd238fb4 | 896 | |
5503ac56 | 897 | m->client = NULL; |
bd238fb4 LI |
898 | } |
899 | ||
900 | /** | |
8b81ef58 EVH |
901 | * p9_fd_close - shutdown file descriptor transport |
902 | * @client: client instance | |
bd238fb4 LI |
903 | * |
904 | */ | |
ee443996 | 905 | |
8b81ef58 | 906 | static void p9_fd_close(struct p9_client *client) |
bd238fb4 LI |
907 | { |
908 | struct p9_trans_fd *ts; | |
909 | ||
8b81ef58 | 910 | if (!client) |
bd238fb4 LI |
911 | return; |
912 | ||
8b81ef58 | 913 | ts = client->trans; |
bd238fb4 LI |
914 | if (!ts) |
915 | return; | |
916 | ||
8b81ef58 EVH |
917 | client->status = Disconnected; |
918 | ||
263c5828 | 919 | p9_conn_destroy(&ts->conn); |
8a0dc95f | 920 | |
bd238fb4 LI |
921 | if (ts->rd) |
922 | fput(ts->rd); | |
923 | if (ts->wr) | |
924 | fput(ts->wr); | |
8b81ef58 | 925 | |
bd238fb4 LI |
926 | kfree(ts); |
927 | } | |
928 | ||
887b3ece EVH |
929 | /* |
930 | * stolen from NFS - maybe should be made a generic function? | |
931 | */ | |
932 | static inline int valid_ipaddr4(const char *buf) | |
933 | { | |
934 | int rc, count, in[4]; | |
935 | ||
936 | rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]); | |
937 | if (rc != 4) | |
938 | return -EINVAL; | |
939 | for (count = 0; count < 4; count++) { | |
940 | if (in[count] > 255) | |
941 | return -EINVAL; | |
942 | } | |
943 | return 0; | |
944 | } | |
945 | ||
2f28c8b3 JG |
946 | static int p9_bind_privport(struct socket *sock) |
947 | { | |
948 | struct sockaddr_in cl; | |
949 | int port, err = -EINVAL; | |
950 | ||
951 | memset(&cl, 0, sizeof(cl)); | |
952 | cl.sin_family = AF_INET; | |
6db6ea79 | 953 | cl.sin_addr.s_addr = htonl(INADDR_ANY); |
2f28c8b3 JG |
954 | for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) { |
955 | cl.sin_port = htons((ushort)port); | |
956 | err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl)); | |
957 | if (err != -EADDRINUSE) | |
958 | break; | |
959 | } | |
960 | return err; | |
961 | } | |
962 | ||
963 | ||
8b81ef58 EVH |
964 | static int |
965 | p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args) | |
a80d923e EVH |
966 | { |
967 | int err; | |
a80d923e EVH |
968 | struct socket *csocket; |
969 | struct sockaddr_in sin_server; | |
970 | struct p9_fd_opts opts; | |
971 | ||
bb8ffdfc EVH |
972 | err = parse_opts(args, &opts); |
973 | if (err < 0) | |
8b81ef58 | 974 | return err; |
a80d923e | 975 | |
10aa1452 | 976 | if (addr == NULL || valid_ipaddr4(addr) < 0) |
8b81ef58 | 977 | return -EINVAL; |
887b3ece | 978 | |
a80d923e | 979 | csocket = NULL; |
a80d923e | 980 | |
c4fac910 DH |
981 | client->trans_opts.tcp.port = opts.port; |
982 | client->trans_opts.tcp.privport = opts.privport; | |
a80d923e EVH |
983 | sin_server.sin_family = AF_INET; |
984 | sin_server.sin_addr.s_addr = in_aton(addr); | |
985 | sin_server.sin_port = htons(opts.port); | |
0c5c9fb5 | 986 | err = __sock_create(current->nsproxy->net_ns, PF_INET, |
e75762fd | 987 | SOCK_STREAM, IPPROTO_TCP, &csocket, 1); |
6b18662e | 988 | if (err) { |
5d385153 JP |
989 | pr_err("%s (%d): problem creating socket\n", |
990 | __func__, task_pid_nr(current)); | |
6b18662e | 991 | return err; |
a80d923e EVH |
992 | } |
993 | ||
2f28c8b3 JG |
994 | if (opts.privport) { |
995 | err = p9_bind_privport(csocket); | |
996 | if (err < 0) { | |
997 | pr_err("%s (%d): problem binding to privport\n", | |
998 | __func__, task_pid_nr(current)); | |
999 | sock_release(csocket); | |
1000 | return err; | |
1001 | } | |
1002 | } | |
1003 | ||
a80d923e EVH |
1004 | err = csocket->ops->connect(csocket, |
1005 | (struct sockaddr *)&sin_server, | |
1006 | sizeof(struct sockaddr_in), 0); | |
1007 | if (err < 0) { | |
5d385153 JP |
1008 | pr_err("%s (%d): problem connecting socket to %s\n", |
1009 | __func__, task_pid_nr(current), addr); | |
a80d923e | 1010 | sock_release(csocket); |
6b18662e AV |
1011 | return err; |
1012 | } | |
a80d923e | 1013 | |
6b18662e | 1014 | return p9_socket_open(client, csocket); |
a80d923e EVH |
1015 | } |
1016 | ||
8b81ef58 EVH |
1017 | static int |
1018 | p9_fd_create_unix(struct p9_client *client, const char *addr, char *args) | |
a80d923e EVH |
1019 | { |
1020 | int err; | |
1021 | struct socket *csocket; | |
1022 | struct sockaddr_un sun_server; | |
a80d923e EVH |
1023 | |
1024 | csocket = NULL; | |
a80d923e | 1025 | |
7ca1db21 | 1026 | if (!addr || !strlen(addr)) |
10aa1452 TB |
1027 | return -EINVAL; |
1028 | ||
cff6b8a9 | 1029 | if (strlen(addr) >= UNIX_PATH_MAX) { |
5d385153 JP |
1030 | pr_err("%s (%d): address too long: %s\n", |
1031 | __func__, task_pid_nr(current), addr); | |
6b18662e | 1032 | return -ENAMETOOLONG; |
a80d923e EVH |
1033 | } |
1034 | ||
1035 | sun_server.sun_family = PF_UNIX; | |
1036 | strcpy(sun_server.sun_path, addr); | |
0c5c9fb5 | 1037 | err = __sock_create(current->nsproxy->net_ns, PF_UNIX, |
e75762fd | 1038 | SOCK_STREAM, 0, &csocket, 1); |
6b18662e | 1039 | if (err < 0) { |
5d385153 JP |
1040 | pr_err("%s (%d): problem creating socket\n", |
1041 | __func__, task_pid_nr(current)); | |
1042 | ||
6b18662e AV |
1043 | return err; |
1044 | } | |
a80d923e EVH |
1045 | err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server, |
1046 | sizeof(struct sockaddr_un) - 1, 0); | |
1047 | if (err < 0) { | |
5d385153 JP |
1048 | pr_err("%s (%d): problem connecting socket: %s: %d\n", |
1049 | __func__, task_pid_nr(current), addr, err); | |
a80d923e | 1050 | sock_release(csocket); |
6b18662e AV |
1051 | return err; |
1052 | } | |
a80d923e | 1053 | |
6b18662e | 1054 | return p9_socket_open(client, csocket); |
a80d923e EVH |
1055 | } |
1056 | ||
8b81ef58 EVH |
1057 | static int |
1058 | p9_fd_create(struct p9_client *client, const char *addr, char *args) | |
a80d923e EVH |
1059 | { |
1060 | int err; | |
a80d923e EVH |
1061 | struct p9_fd_opts opts; |
1062 | ||
1063 | parse_opts(args, &opts); | |
c4fac910 DH |
1064 | client->trans_opts.fd.rfd = opts.rfd; |
1065 | client->trans_opts.fd.wfd = opts.wfd; | |
a80d923e EVH |
1066 | |
1067 | if (opts.rfd == ~0 || opts.wfd == ~0) { | |
5d385153 | 1068 | pr_err("Insufficient options for proto=fd\n"); |
8b81ef58 | 1069 | return -ENOPROTOOPT; |
a80d923e EVH |
1070 | } |
1071 | ||
8b81ef58 | 1072 | err = p9_fd_open(client, opts.rfd, opts.wfd); |
a80d923e | 1073 | if (err < 0) |
6b18662e | 1074 | return err; |
a80d923e | 1075 | |
263c5828 | 1076 | p9_conn_create(client); |
8a0dc95f | 1077 | |
8b81ef58 | 1078 | return 0; |
a80d923e EVH |
1079 | } |
1080 | ||
1081 | static struct p9_trans_module p9_tcp_trans = { | |
1082 | .name = "tcp", | |
1083 | .maxsize = MAX_SOCK_BUF, | |
f94741fd | 1084 | .def = 0, |
8b81ef58 EVH |
1085 | .create = p9_fd_create_tcp, |
1086 | .close = p9_fd_close, | |
91b8534f EVH |
1087 | .request = p9_fd_request, |
1088 | .cancel = p9_fd_cancel, | |
afd8d654 | 1089 | .cancelled = p9_fd_cancelled, |
c4fac910 | 1090 | .show_options = p9_fd_show_options, |
72029fe8 | 1091 | .owner = THIS_MODULE, |
a80d923e | 1092 | }; |
1c582c6d | 1093 | MODULE_ALIAS_9P("tcp"); |
a80d923e EVH |
1094 | |
1095 | static struct p9_trans_module p9_unix_trans = { | |
1096 | .name = "unix", | |
1097 | .maxsize = MAX_SOCK_BUF, | |
1098 | .def = 0, | |
8b81ef58 EVH |
1099 | .create = p9_fd_create_unix, |
1100 | .close = p9_fd_close, | |
91b8534f EVH |
1101 | .request = p9_fd_request, |
1102 | .cancel = p9_fd_cancel, | |
afd8d654 | 1103 | .cancelled = p9_fd_cancelled, |
c4fac910 | 1104 | .show_options = p9_fd_show_options, |
72029fe8 | 1105 | .owner = THIS_MODULE, |
a80d923e | 1106 | }; |
1c582c6d | 1107 | MODULE_ALIAS_9P("unix"); |
a80d923e EVH |
1108 | |
1109 | static struct p9_trans_module p9_fd_trans = { | |
1110 | .name = "fd", | |
1111 | .maxsize = MAX_SOCK_BUF, | |
1112 | .def = 0, | |
8b81ef58 EVH |
1113 | .create = p9_fd_create, |
1114 | .close = p9_fd_close, | |
91b8534f EVH |
1115 | .request = p9_fd_request, |
1116 | .cancel = p9_fd_cancel, | |
afd8d654 | 1117 | .cancelled = p9_fd_cancelled, |
c4fac910 | 1118 | .show_options = p9_fd_show_options, |
72029fe8 | 1119 | .owner = THIS_MODULE, |
a80d923e | 1120 | }; |
1c582c6d | 1121 | MODULE_ALIAS_9P("fd"); |
a80d923e | 1122 | |
5503ac56 | 1123 | /** |
4a026da9 SL |
1124 | * p9_poll_workfn - poll worker thread |
1125 | * @work: work queue | |
5503ac56 EVH |
1126 | * |
1127 | * polls all v9fs transports for new events and queues the appropriate | |
1128 | * work to the work queue | |
1129 | * | |
1130 | */ | |
1131 | ||
aa70c585 | 1132 | static void p9_poll_workfn(struct work_struct *work) |
5503ac56 EVH |
1133 | { |
1134 | unsigned long flags; | |
1135 | ||
5d385153 | 1136 | p9_debug(P9_DEBUG_TRANS, "start %p\n", current); |
aa70c585 | 1137 | |
5503ac56 EVH |
1138 | spin_lock_irqsave(&p9_poll_lock, flags); |
1139 | while (!list_empty(&p9_poll_pending_list)) { | |
1140 | struct p9_conn *conn = list_first_entry(&p9_poll_pending_list, | |
1141 | struct p9_conn, | |
1142 | poll_pending_link); | |
1143 | list_del_init(&conn->poll_pending_link); | |
1144 | spin_unlock_irqrestore(&p9_poll_lock, flags); | |
1145 | ||
1146 | p9_poll_mux(conn); | |
1147 | ||
1148 | spin_lock_irqsave(&p9_poll_lock, flags); | |
1149 | } | |
1150 | spin_unlock_irqrestore(&p9_poll_lock, flags); | |
1151 | ||
5d385153 | 1152 | p9_debug(P9_DEBUG_TRANS, "finish\n"); |
5503ac56 EVH |
1153 | } |
1154 | ||
1c582c6d | 1155 | static int __init p9_trans_fd_init(void) |
a80d923e EVH |
1156 | { |
1157 | v9fs_register_trans(&p9_tcp_trans); | |
1158 | v9fs_register_trans(&p9_unix_trans); | |
1159 | v9fs_register_trans(&p9_fd_trans); | |
1160 | ||
3387b804 | 1161 | return 0; |
a80d923e | 1162 | } |
72029fe8 | 1163 | |
1c582c6d | 1164 | static void __exit p9_trans_fd_exit(void) |
72029fe8 | 1165 | { |
43829731 | 1166 | flush_work(&p9_poll_work); |
72029fe8 TH |
1167 | v9fs_unregister_trans(&p9_tcp_trans); |
1168 | v9fs_unregister_trans(&p9_unix_trans); | |
1169 | v9fs_unregister_trans(&p9_fd_trans); | |
1170 | } | |
1c582c6d TW |
1171 | |
1172 | module_init(p9_trans_fd_init); | |
1173 | module_exit(p9_trans_fd_exit); | |
1174 | ||
1175 | MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>"); | |
1176 | MODULE_DESCRIPTION("Filedescriptor Transport for 9P"); | |
1177 | MODULE_LICENSE("GPL"); |