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