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