trans_fd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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>
  8. * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/in.h>
  29. #include <linux/module.h>
  30. #include <linux/net.h>
  31. #include <linux/ipv6.h>
  32. #include <linux/kthread.h>
  33. #include <linux/errno.h>
  34. #include <linux/kernel.h>
  35. #include <linux/un.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/inet.h>
  38. #include <linux/idr.h>
  39. #include <linux/file.h>
  40. #include <linux/parser.h>
  41. #include <linux/slab.h>
  42. #include <net/9p/9p.h>
  43. #include <net/9p/client.h>
  44. #include <net/9p/transport.h>
  45. #include <linux/syscalls.h> /* killme */
  46. #define P9_PORT 564
  47. #define MAX_SOCK_BUF (64*1024)
  48. #define MAXPOLLWADDR 2
  49. /**
  50. * struct p9_fd_opts - per-transport options
  51. * @rfd: file descriptor for reading (trans=fd)
  52. * @wfd: file descriptor for writing (trans=fd)
  53. * @port: port to connect to (trans=tcp)
  54. *
  55. */
  56. struct p9_fd_opts {
  57. int rfd;
  58. int wfd;
  59. u16 port;
  60. int privport;
  61. };
  62. /*
  63. * Option Parsing (code inspired by NFS code)
  64. * - a little lazy - parse all fd-transport options
  65. */
  66. enum {
  67. /* Options that take integer arguments */
  68. Opt_port, Opt_rfdno, Opt_wfdno, Opt_err,
  69. /* Options that take no arguments */
  70. Opt_privport,
  71. };
  72. static const match_table_t tokens = {
  73. {Opt_port, "port=%u"},
  74. {Opt_rfdno, "rfdno=%u"},
  75. {Opt_wfdno, "wfdno=%u"},
  76. {Opt_privport, "privport"},
  77. {Opt_err, NULL},
  78. };
  79. enum {
  80. Rworksched = 1, /* read work scheduled or running */
  81. Rpending = 2, /* can read */
  82. Wworksched = 4, /* write work scheduled or running */
  83. Wpending = 8, /* can write */
  84. };
  85. struct p9_poll_wait {
  86. struct p9_conn *conn;
  87. wait_queue_t wait;
  88. wait_queue_head_t *wait_addr;
  89. };
  90. /**
  91. * struct p9_conn - fd mux connection state information
  92. * @mux_list: list link for mux to manage multiple connections (?)
  93. * @client: reference to client instance for this connection
  94. * @err: error state
  95. * @req_list: accounting for requests which have been sent
  96. * @unsent_req_list: accounting for requests that haven't been sent
  97. * @req: current request being processed (if any)
  98. * @tmp_buf: temporary buffer to read in header
  99. * @rsize: amount to read for current frame
  100. * @rpos: read position in current frame
  101. * @rbuf: current read buffer
  102. * @wpos: write position for current frame
  103. * @wsize: amount of data to write for current frame
  104. * @wbuf: current write buffer
  105. * @poll_pending_link: pending links to be polled per conn
  106. * @poll_wait: array of wait_q's for various worker threads
  107. * @pt: poll state
  108. * @rq: current read work
  109. * @wq: current write work
  110. * @wsched: ????
  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 *req;
  120. char tmp_buf[7];
  121. int rsize;
  122. int rpos;
  123. char *rbuf;
  124. int wpos;
  125. int wsize;
  126. char *wbuf;
  127. struct list_head poll_pending_link;
  128. struct p9_poll_wait poll_wait[MAXPOLLWADDR];
  129. poll_table pt;
  130. struct work_struct rq;
  131. struct work_struct wq;
  132. unsigned long wsched;
  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. struct p9_trans_fd {
  142. struct file *rd;
  143. struct file *wr;
  144. struct p9_conn conn;
  145. };
  146. static void p9_poll_workfn(struct work_struct *work);
  147. static DEFINE_SPINLOCK(p9_poll_lock);
  148. static LIST_HEAD(p9_poll_pending_list);
  149. static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
  150. static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
  151. static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
  152. static void p9_mux_poll_stop(struct p9_conn *m)
  153. {
  154. unsigned long flags;
  155. int i;
  156. for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
  157. struct p9_poll_wait *pwait = &m->poll_wait[i];
  158. if (pwait->wait_addr) {
  159. remove_wait_queue(pwait->wait_addr, &pwait->wait);
  160. pwait->wait_addr = NULL;
  161. }
  162. }
  163. spin_lock_irqsave(&p9_poll_lock, flags);
  164. list_del_init(&m->poll_pending_link);
  165. spin_unlock_irqrestore(&p9_poll_lock, flags);
  166. flush_work(&p9_poll_work);
  167. }
  168. /**
  169. * p9_conn_cancel - cancel all pending requests with error
  170. * @m: mux data
  171. * @err: error code
  172. *
  173. */
  174. static void p9_conn_cancel(struct p9_conn *m, int err)
  175. {
  176. struct p9_req_t *req, *rtmp;
  177. unsigned long flags;
  178. LIST_HEAD(cancel_list);
  179. p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
  180. spin_lock_irqsave(&m->client->lock, flags);
  181. if (m->err) {
  182. spin_unlock_irqrestore(&m->client->lock, flags);
  183. return;
  184. }
  185. m->err = err;
  186. list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
  187. list_move(&req->req_list, &cancel_list);
  188. }
  189. list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
  190. list_move(&req->req_list, &cancel_list);
  191. }
  192. spin_unlock_irqrestore(&m->client->lock, flags);
  193. list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
  194. p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
  195. list_del(&req->req_list);
  196. if (!req->t_err)
  197. req->t_err = err;
  198. p9_client_cb(m->client, req, REQ_STATUS_ERROR);
  199. }
  200. }
  201. static int
  202. p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
  203. {
  204. int ret, n;
  205. struct p9_trans_fd *ts = NULL;
  206. if (client && client->status == Connected)
  207. ts = client->trans;
  208. if (!ts)
  209. return -EREMOTEIO;
  210. if (!ts->rd->f_op->poll)
  211. return -EIO;
  212. if (!ts->wr->f_op->poll)
  213. return -EIO;
  214. ret = ts->rd->f_op->poll(ts->rd, pt);
  215. if (ret < 0)
  216. return ret;
  217. if (ts->rd != ts->wr) {
  218. n = ts->wr->f_op->poll(ts->wr, pt);
  219. if (n < 0)
  220. return n;
  221. ret = (ret & ~POLLOUT) | (n & ~POLLIN);
  222. }
  223. return ret;
  224. }
  225. /**
  226. * p9_fd_read- read from a fd
  227. * @client: client instance
  228. * @v: buffer to receive data into
  229. * @len: size of receive buffer
  230. *
  231. */
  232. static int p9_fd_read(struct p9_client *client, void *v, int len)
  233. {
  234. int ret;
  235. struct p9_trans_fd *ts = NULL;
  236. if (client && client->status != Disconnected)
  237. ts = client->trans;
  238. if (!ts)
  239. return -EREMOTEIO;
  240. if (!(ts->rd->f_flags & O_NONBLOCK))
  241. p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
  242. ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
  243. if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
  244. client->status = Disconnected;
  245. return ret;
  246. }
  247. /**
  248. * p9_read_work - called when there is some data to be read from a transport
  249. * @work: container of work to be done
  250. *
  251. */
  252. static void p9_read_work(struct work_struct *work)
  253. {
  254. int n, err;
  255. struct p9_conn *m;
  256. int status = REQ_STATUS_ERROR;
  257. m = container_of(work, struct p9_conn, rq);
  258. if (m->err < 0)
  259. return;
  260. p9_debug(P9_DEBUG_TRANS, "start mux %p pos %d\n", m, m->rpos);
  261. if (!m->rbuf) {
  262. m->rbuf = m->tmp_buf;
  263. m->rpos = 0;
  264. m->rsize = 7; /* start by reading header */
  265. }
  266. clear_bit(Rpending, &m->wsched);
  267. p9_debug(P9_DEBUG_TRANS, "read mux %p pos %d size: %d = %d\n",
  268. m, m->rpos, m->rsize, m->rsize-m->rpos);
  269. err = p9_fd_read(m->client, m->rbuf + m->rpos,
  270. m->rsize - m->rpos);
  271. p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
  272. if (err == -EAGAIN) {
  273. goto end_clear;
  274. }
  275. if (err <= 0)
  276. goto error;
  277. m->rpos += err;
  278. if ((!m->req) && (m->rpos == m->rsize)) { /* header read in */
  279. u16 tag;
  280. p9_debug(P9_DEBUG_TRANS, "got new header\n");
  281. n = le32_to_cpu(*(__le32 *) m->rbuf); /* read packet size */
  282. if (n >= m->client->msize) {
  283. p9_debug(P9_DEBUG_ERROR,
  284. "requested packet size too big: %d\n", n);
  285. err = -EIO;
  286. goto error;
  287. }
  288. tag = le16_to_cpu(*(__le16 *) (m->rbuf+5)); /* read tag */
  289. p9_debug(P9_DEBUG_TRANS,
  290. "mux %p pkt: size: %d bytes tag: %d\n", m, n, tag);
  291. m->req = p9_tag_lookup(m->client, tag);
  292. if (!m->req || (m->req->status != REQ_STATUS_SENT)) {
  293. p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
  294. tag);
  295. err = -EIO;
  296. goto error;
  297. }
  298. if (m->req->rc == NULL) {
  299. m->req->rc = kmalloc(sizeof(struct p9_fcall) +
  300. m->client->msize, GFP_NOFS);
  301. if (!m->req->rc) {
  302. m->req = NULL;
  303. err = -ENOMEM;
  304. goto error;
  305. }
  306. }
  307. m->rbuf = (char *)m->req->rc + sizeof(struct p9_fcall);
  308. memcpy(m->rbuf, m->tmp_buf, m->rsize);
  309. m->rsize = n;
  310. }
  311. /* not an else because some packets (like clunk) have no payload */
  312. if ((m->req) && (m->rpos == m->rsize)) { /* packet is read in */
  313. p9_debug(P9_DEBUG_TRANS, "got new packet\n");
  314. spin_lock(&m->client->lock);
  315. if (m->req->status != REQ_STATUS_ERROR)
  316. status = REQ_STATUS_RCVD;
  317. list_del(&m->req->req_list);
  318. spin_unlock(&m->client->lock);
  319. p9_client_cb(m->client, m->req, status);
  320. m->rbuf = NULL;
  321. m->rpos = 0;
  322. m->rsize = 0;
  323. m->req = NULL;
  324. }
  325. end_clear:
  326. clear_bit(Rworksched, &m->wsched);
  327. if (!list_empty(&m->req_list)) {
  328. if (test_and_clear_bit(Rpending, &m->wsched))
  329. n = POLLIN;
  330. else
  331. n = p9_fd_poll(m->client, NULL);
  332. if ((n & POLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
  333. p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
  334. schedule_work(&m->rq);
  335. }
  336. }
  337. return;
  338. error:
  339. p9_conn_cancel(m, err);
  340. clear_bit(Rworksched, &m->wsched);
  341. }
  342. /**
  343. * p9_fd_write - write to a socket
  344. * @client: client instance
  345. * @v: buffer to send data from
  346. * @len: size of send buffer
  347. *
  348. */
  349. static int p9_fd_write(struct p9_client *client, void *v, int len)
  350. {
  351. int ret;
  352. mm_segment_t oldfs;
  353. struct p9_trans_fd *ts = NULL;
  354. if (client && client->status != Disconnected)
  355. ts = client->trans;
  356. if (!ts)
  357. return -EREMOTEIO;
  358. if (!(ts->wr->f_flags & O_NONBLOCK))
  359. p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
  360. oldfs = get_fs();
  361. set_fs(get_ds());
  362. /* The cast to a user pointer is valid due to the set_fs() */
  363. ret = vfs_write(ts->wr, (__force void __user *)v, len, &ts->wr->f_pos);
  364. set_fs(oldfs);
  365. if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
  366. client->status = Disconnected;
  367. return ret;
  368. }
  369. /**
  370. * p9_write_work - called when a transport can send some data
  371. * @work: container for work to be done
  372. *
  373. */
  374. static void p9_write_work(struct work_struct *work)
  375. {
  376. int n, err;
  377. struct p9_conn *m;
  378. struct p9_req_t *req;
  379. m = container_of(work, struct p9_conn, wq);
  380. if (m->err < 0) {
  381. clear_bit(Wworksched, &m->wsched);
  382. return;
  383. }
  384. if (!m->wsize) {
  385. spin_lock(&m->client->lock);
  386. if (list_empty(&m->unsent_req_list)) {
  387. clear_bit(Wworksched, &m->wsched);
  388. spin_unlock(&m->client->lock);
  389. return;
  390. }
  391. req = list_entry(m->unsent_req_list.next, struct p9_req_t,
  392. req_list);
  393. req->status = REQ_STATUS_SENT;
  394. p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
  395. list_move_tail(&req->req_list, &m->req_list);
  396. m->wbuf = req->tc->sdata;
  397. m->wsize = req->tc->size;
  398. m->wpos = 0;
  399. spin_unlock(&m->client->lock);
  400. }
  401. p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
  402. m, m->wpos, m->wsize);
  403. clear_bit(Wpending, &m->wsched);
  404. err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
  405. p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
  406. if (err == -EAGAIN)
  407. goto end_clear;
  408. if (err < 0)
  409. goto error;
  410. else if (err == 0) {
  411. err = -EREMOTEIO;
  412. goto error;
  413. }
  414. m->wpos += err;
  415. if (m->wpos == m->wsize)
  416. m->wpos = m->wsize = 0;
  417. end_clear:
  418. clear_bit(Wworksched, &m->wsched);
  419. if (m->wsize || !list_empty(&m->unsent_req_list)) {
  420. if (test_and_clear_bit(Wpending, &m->wsched))
  421. n = POLLOUT;
  422. else
  423. n = p9_fd_poll(m->client, NULL);
  424. if ((n & POLLOUT) &&
  425. !test_and_set_bit(Wworksched, &m->wsched)) {
  426. p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
  427. schedule_work(&m->wq);
  428. }
  429. }
  430. return;
  431. error:
  432. p9_conn_cancel(m, err);
  433. clear_bit(Wworksched, &m->wsched);
  434. }
  435. static int p9_pollwake(wait_queue_t *wait, unsigned int mode, int sync, void *key)
  436. {
  437. struct p9_poll_wait *pwait =
  438. container_of(wait, struct p9_poll_wait, wait);
  439. struct p9_conn *m = pwait->conn;
  440. unsigned long flags;
  441. spin_lock_irqsave(&p9_poll_lock, flags);
  442. if (list_empty(&m->poll_pending_link))
  443. list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
  444. spin_unlock_irqrestore(&p9_poll_lock, flags);
  445. schedule_work(&p9_poll_work);
  446. return 1;
  447. }
  448. /**
  449. * p9_pollwait - add poll task to the wait queue
  450. * @filp: file pointer being polled
  451. * @wait_address: wait_q to block on
  452. * @p: poll state
  453. *
  454. * called by files poll operation to add v9fs-poll task to files wait queue
  455. */
  456. static void
  457. p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
  458. {
  459. struct p9_conn *m = container_of(p, struct p9_conn, pt);
  460. struct p9_poll_wait *pwait = NULL;
  461. int i;
  462. for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
  463. if (m->poll_wait[i].wait_addr == NULL) {
  464. pwait = &m->poll_wait[i];
  465. break;
  466. }
  467. }
  468. if (!pwait) {
  469. p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
  470. return;
  471. }
  472. pwait->conn = m;
  473. pwait->wait_addr = wait_address;
  474. init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
  475. add_wait_queue(wait_address, &pwait->wait);
  476. }
  477. /**
  478. * p9_conn_create - initialize the per-session mux data
  479. * @client: client instance
  480. *
  481. * Note: Creates the polling task if this is the first session.
  482. */
  483. static void p9_conn_create(struct p9_client *client)
  484. {
  485. int n;
  486. struct p9_trans_fd *ts = client->trans;
  487. struct p9_conn *m = &ts->conn;
  488. p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
  489. INIT_LIST_HEAD(&m->mux_list);
  490. m->client = client;
  491. INIT_LIST_HEAD(&m->req_list);
  492. INIT_LIST_HEAD(&m->unsent_req_list);
  493. INIT_WORK(&m->rq, p9_read_work);
  494. INIT_WORK(&m->wq, p9_write_work);
  495. INIT_LIST_HEAD(&m->poll_pending_link);
  496. init_poll_funcptr(&m->pt, p9_pollwait);
  497. n = p9_fd_poll(client, &m->pt);
  498. if (n & POLLIN) {
  499. p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
  500. set_bit(Rpending, &m->wsched);
  501. }
  502. if (n & POLLOUT) {
  503. p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
  504. set_bit(Wpending, &m->wsched);
  505. }
  506. }
  507. /**
  508. * p9_poll_mux - polls a mux and schedules read or write works if necessary
  509. * @m: connection to poll
  510. *
  511. */
  512. static void p9_poll_mux(struct p9_conn *m)
  513. {
  514. int n;
  515. if (m->err < 0)
  516. return;
  517. n = p9_fd_poll(m->client, NULL);
  518. if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
  519. p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
  520. if (n >= 0)
  521. n = -ECONNRESET;
  522. p9_conn_cancel(m, n);
  523. }
  524. if (n & POLLIN) {
  525. set_bit(Rpending, &m->wsched);
  526. p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
  527. if (!test_and_set_bit(Rworksched, &m->wsched)) {
  528. p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
  529. schedule_work(&m->rq);
  530. }
  531. }
  532. if (n & POLLOUT) {
  533. set_bit(Wpending, &m->wsched);
  534. p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
  535. if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
  536. !test_and_set_bit(Wworksched, &m->wsched)) {
  537. p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
  538. schedule_work(&m->wq);
  539. }
  540. }
  541. }
  542. /**
  543. * p9_fd_request - send 9P request
  544. * The function can sleep until the request is scheduled for sending.
  545. * The function can be interrupted. Return from the function is not
  546. * a guarantee that the request is sent successfully.
  547. *
  548. * @client: client instance
  549. * @req: request to be sent
  550. *
  551. */
  552. static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
  553. {
  554. int n;
  555. struct p9_trans_fd *ts = client->trans;
  556. struct p9_conn *m = &ts->conn;
  557. p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
  558. m, current, req->tc, req->tc->id);
  559. if (m->err < 0)
  560. return m->err;
  561. spin_lock(&client->lock);
  562. req->status = REQ_STATUS_UNSENT;
  563. list_add_tail(&req->req_list, &m->unsent_req_list);
  564. spin_unlock(&client->lock);
  565. if (test_and_clear_bit(Wpending, &m->wsched))
  566. n = POLLOUT;
  567. else
  568. n = p9_fd_poll(m->client, NULL);
  569. if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
  570. schedule_work(&m->wq);
  571. return 0;
  572. }
  573. static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
  574. {
  575. int ret = 1;
  576. p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
  577. spin_lock(&client->lock);
  578. if (req->status == REQ_STATUS_UNSENT) {
  579. list_del(&req->req_list);
  580. req->status = REQ_STATUS_FLSHD;
  581. ret = 0;
  582. }
  583. spin_unlock(&client->lock);
  584. return ret;
  585. }
  586. static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
  587. {
  588. p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
  589. /* we haven't received a response for oldreq,
  590. * remove it from the list.
  591. */
  592. spin_lock(&client->lock);
  593. list_del(&req->req_list);
  594. spin_unlock(&client->lock);
  595. return 0;
  596. }
  597. /**
  598. * parse_opts - parse mount options into p9_fd_opts structure
  599. * @params: options string passed from mount
  600. * @opts: fd transport-specific structure to parse options into
  601. *
  602. * Returns 0 upon success, -ERRNO upon failure
  603. */
  604. static int parse_opts(char *params, struct p9_fd_opts *opts)
  605. {
  606. char *p;
  607. substring_t args[MAX_OPT_ARGS];
  608. int option;
  609. char *options, *tmp_options;
  610. opts->port = P9_PORT;
  611. opts->rfd = ~0;
  612. opts->wfd = ~0;
  613. opts->privport = 0;
  614. if (!params)
  615. return 0;
  616. tmp_options = kstrdup(params, GFP_KERNEL);
  617. if (!tmp_options) {
  618. p9_debug(P9_DEBUG_ERROR,
  619. "failed to allocate copy of option string\n");
  620. return -ENOMEM;
  621. }
  622. options = tmp_options;
  623. while ((p = strsep(&options, ",")) != NULL) {
  624. int token;
  625. int r;
  626. if (!*p)
  627. continue;
  628. token = match_token(p, tokens, args);
  629. if ((token != Opt_err) && (token != Opt_privport)) {
  630. r = match_int(&args[0], &option);
  631. if (r < 0) {
  632. p9_debug(P9_DEBUG_ERROR,
  633. "integer field, but no integer?\n");
  634. continue;
  635. }
  636. }
  637. switch (token) {
  638. case Opt_port:
  639. opts->port = option;
  640. break;
  641. case Opt_rfdno:
  642. opts->rfd = option;
  643. break;
  644. case Opt_wfdno:
  645. opts->wfd = option;
  646. break;
  647. case Opt_privport:
  648. opts->privport = 1;
  649. break;
  650. default:
  651. continue;
  652. }
  653. }
  654. kfree(tmp_options);
  655. return 0;
  656. }
  657. static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
  658. {
  659. struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
  660. GFP_KERNEL);
  661. if (!ts)
  662. return -ENOMEM;
  663. ts->rd = fget(rfd);
  664. ts->wr = fget(wfd);
  665. if (!ts->rd || !ts->wr) {
  666. if (ts->rd)
  667. fput(ts->rd);
  668. if (ts->wr)
  669. fput(ts->wr);
  670. kfree(ts);
  671. return -EIO;
  672. }
  673. client->trans = ts;
  674. client->status = Connected;
  675. return 0;
  676. }
  677. static int p9_socket_open(struct p9_client *client, struct socket *csocket)
  678. {
  679. struct p9_trans_fd *p;
  680. struct file *file;
  681. p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
  682. if (!p)
  683. return -ENOMEM;
  684. csocket->sk->sk_allocation = GFP_NOIO;
  685. file = sock_alloc_file(csocket, 0, NULL);
  686. if (IS_ERR(file)) {
  687. pr_err("%s (%d): failed to map fd\n",
  688. __func__, task_pid_nr(current));
  689. sock_release(csocket);
  690. kfree(p);
  691. return PTR_ERR(file);
  692. }
  693. get_file(file);
  694. p->wr = p->rd = file;
  695. client->trans = p;
  696. client->status = Connected;
  697. p->rd->f_flags |= O_NONBLOCK;
  698. p9_conn_create(client);
  699. return 0;
  700. }
  701. /**
  702. * p9_mux_destroy - cancels all pending requests of mux
  703. * @m: mux to destroy
  704. *
  705. */
  706. static void p9_conn_destroy(struct p9_conn *m)
  707. {
  708. p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
  709. m, m->mux_list.prev, m->mux_list.next);
  710. p9_mux_poll_stop(m);
  711. cancel_work_sync(&m->rq);
  712. cancel_work_sync(&m->wq);
  713. p9_conn_cancel(m, -ECONNRESET);
  714. m->client = NULL;
  715. }
  716. /**
  717. * p9_fd_close - shutdown file descriptor transport
  718. * @client: client instance
  719. *
  720. */
  721. static void p9_fd_close(struct p9_client *client)
  722. {
  723. struct p9_trans_fd *ts;
  724. if (!client)
  725. return;
  726. ts = client->trans;
  727. if (!ts)
  728. return;
  729. client->status = Disconnected;
  730. p9_conn_destroy(&ts->conn);
  731. if (ts->rd)
  732. fput(ts->rd);
  733. if (ts->wr)
  734. fput(ts->wr);
  735. kfree(ts);
  736. }
  737. /*
  738. * stolen from NFS - maybe should be made a generic function?
  739. */
  740. static inline int valid_ipaddr4(const char *buf)
  741. {
  742. int rc, count, in[4];
  743. rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
  744. if (rc != 4)
  745. return -EINVAL;
  746. for (count = 0; count < 4; count++) {
  747. if (in[count] > 255)
  748. return -EINVAL;
  749. }
  750. return 0;
  751. }
  752. static int p9_bind_privport(struct socket *sock)
  753. {
  754. struct sockaddr_in cl;
  755. int port, err = -EINVAL;
  756. memset(&cl, 0, sizeof(cl));
  757. cl.sin_family = AF_INET;
  758. cl.sin_addr.s_addr = INADDR_ANY;
  759. for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
  760. cl.sin_port = htons((ushort)port);
  761. err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
  762. if (err != -EADDRINUSE)
  763. break;
  764. }
  765. return err;
  766. }
  767. static int
  768. p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
  769. {
  770. int err;
  771. struct socket *csocket;
  772. struct sockaddr_in sin_server;
  773. struct p9_fd_opts opts;
  774. err = parse_opts(args, &opts);
  775. if (err < 0)
  776. return err;
  777. if (addr == NULL || valid_ipaddr4(addr) < 0)
  778. return -EINVAL;
  779. csocket = NULL;
  780. sin_server.sin_family = AF_INET;
  781. sin_server.sin_addr.s_addr = in_aton(addr);
  782. sin_server.sin_port = htons(opts.port);
  783. err = __sock_create(current->nsproxy->net_ns, PF_INET,
  784. SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
  785. if (err) {
  786. pr_err("%s (%d): problem creating socket\n",
  787. __func__, task_pid_nr(current));
  788. return err;
  789. }
  790. if (opts.privport) {
  791. err = p9_bind_privport(csocket);
  792. if (err < 0) {
  793. pr_err("%s (%d): problem binding to privport\n",
  794. __func__, task_pid_nr(current));
  795. sock_release(csocket);
  796. return err;
  797. }
  798. }
  799. err = csocket->ops->connect(csocket,
  800. (struct sockaddr *)&sin_server,
  801. sizeof(struct sockaddr_in), 0);
  802. if (err < 0) {
  803. pr_err("%s (%d): problem connecting socket to %s\n",
  804. __func__, task_pid_nr(current), addr);
  805. sock_release(csocket);
  806. return err;
  807. }
  808. return p9_socket_open(client, csocket);
  809. }
  810. static int
  811. p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
  812. {
  813. int err;
  814. struct socket *csocket;
  815. struct sockaddr_un sun_server;
  816. csocket = NULL;
  817. if (addr == NULL)
  818. return -EINVAL;
  819. if (strlen(addr) >= UNIX_PATH_MAX) {
  820. pr_err("%s (%d): address too long: %s\n",
  821. __func__, task_pid_nr(current), addr);
  822. return -ENAMETOOLONG;
  823. }
  824. sun_server.sun_family = PF_UNIX;
  825. strcpy(sun_server.sun_path, addr);
  826. err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
  827. SOCK_STREAM, 0, &csocket, 1);
  828. if (err < 0) {
  829. pr_err("%s (%d): problem creating socket\n",
  830. __func__, task_pid_nr(current));
  831. return err;
  832. }
  833. err = csocket->ops->connect(csocket, (struct sockaddr *)&sun_server,
  834. sizeof(struct sockaddr_un) - 1, 0);
  835. if (err < 0) {
  836. pr_err("%s (%d): problem connecting socket: %s: %d\n",
  837. __func__, task_pid_nr(current), addr, err);
  838. sock_release(csocket);
  839. return err;
  840. }
  841. return p9_socket_open(client, csocket);
  842. }
  843. static int
  844. p9_fd_create(struct p9_client *client, const char *addr, char *args)
  845. {
  846. int err;
  847. struct p9_fd_opts opts;
  848. parse_opts(args, &opts);
  849. if (opts.rfd == ~0 || opts.wfd == ~0) {
  850. pr_err("Insufficient options for proto=fd\n");
  851. return -ENOPROTOOPT;
  852. }
  853. err = p9_fd_open(client, opts.rfd, opts.wfd);
  854. if (err < 0)
  855. return err;
  856. p9_conn_create(client);
  857. return 0;
  858. }
  859. static struct p9_trans_module p9_tcp_trans = {
  860. .name = "tcp",
  861. .maxsize = MAX_SOCK_BUF,
  862. .def = 0,
  863. .create = p9_fd_create_tcp,
  864. .close = p9_fd_close,
  865. .request = p9_fd_request,
  866. .cancel = p9_fd_cancel,
  867. .cancelled = p9_fd_cancelled,
  868. .owner = THIS_MODULE,
  869. };
  870. static struct p9_trans_module p9_unix_trans = {
  871. .name = "unix",
  872. .maxsize = MAX_SOCK_BUF,
  873. .def = 0,
  874. .create = p9_fd_create_unix,
  875. .close = p9_fd_close,
  876. .request = p9_fd_request,
  877. .cancel = p9_fd_cancel,
  878. .cancelled = p9_fd_cancelled,
  879. .owner = THIS_MODULE,
  880. };
  881. static struct p9_trans_module p9_fd_trans = {
  882. .name = "fd",
  883. .maxsize = MAX_SOCK_BUF,
  884. .def = 0,
  885. .create = p9_fd_create,
  886. .close = p9_fd_close,
  887. .request = p9_fd_request,
  888. .cancel = p9_fd_cancel,
  889. .cancelled = p9_fd_cancelled,
  890. .owner = THIS_MODULE,
  891. };
  892. /**
  893. * p9_poll_proc - poll worker thread
  894. * @a: thread state and arguments
  895. *
  896. * polls all v9fs transports for new events and queues the appropriate
  897. * work to the work queue
  898. *
  899. */
  900. static void p9_poll_workfn(struct work_struct *work)
  901. {
  902. unsigned long flags;
  903. p9_debug(P9_DEBUG_TRANS, "start %p\n", current);
  904. spin_lock_irqsave(&p9_poll_lock, flags);
  905. while (!list_empty(&p9_poll_pending_list)) {
  906. struct p9_conn *conn = list_first_entry(&p9_poll_pending_list,
  907. struct p9_conn,
  908. poll_pending_link);
  909. list_del_init(&conn->poll_pending_link);
  910. spin_unlock_irqrestore(&p9_poll_lock, flags);
  911. p9_poll_mux(conn);
  912. spin_lock_irqsave(&p9_poll_lock, flags);
  913. }
  914. spin_unlock_irqrestore(&p9_poll_lock, flags);
  915. p9_debug(P9_DEBUG_TRANS, "finish\n");
  916. }
  917. int p9_trans_fd_init(void)
  918. {
  919. v9fs_register_trans(&p9_tcp_trans);
  920. v9fs_register_trans(&p9_unix_trans);
  921. v9fs_register_trans(&p9_fd_trans);
  922. return 0;
  923. }
  924. void p9_trans_fd_exit(void)
  925. {
  926. flush_work(&p9_poll_work);
  927. v9fs_unregister_trans(&p9_tcp_trans);
  928. v9fs_unregister_trans(&p9_unix_trans);
  929. v9fs_unregister_trans(&p9_fd_trans);
  930. }