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