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. * @rc: temporary fcall for reading current frame
  100. * @wpos: write position for current frame
  101. * @wsize: amount of data to write for current frame
  102. * @wbuf: current write buffer
  103. * @poll_pending_link: pending links to be polled per conn
  104. * @poll_wait: array of wait_q's for various worker threads
  105. * @pt: poll state
  106. * @rq: current read work
  107. * @wq: current write work
  108. * @wsched: ????
  109. *
  110. */
  111. struct p9_conn {
  112. struct list_head mux_list;
  113. struct p9_client *client;
  114. int err;
  115. struct list_head req_list;
  116. struct list_head unsent_req_list;
  117. struct p9_req_t *req;
  118. char tmp_buf[7];
  119. struct p9_fcall rc;
  120. int wpos;
  121. int wsize;
  122. char *wbuf;
  123. struct list_head poll_pending_link;
  124. struct p9_poll_wait poll_wait[MAXPOLLWADDR];
  125. poll_table pt;
  126. struct work_struct rq;
  127. struct work_struct wq;
  128. unsigned long wsched;
  129. };
  130. /**
  131. * struct p9_trans_fd - transport state
  132. * @rd: reference to file to read from
  133. * @wr: reference of file to write to
  134. * @conn: connection state reference
  135. *
  136. */
  137. struct p9_trans_fd {
  138. struct file *rd;
  139. struct file *wr;
  140. struct p9_conn conn;
  141. };
  142. static void p9_poll_workfn(struct work_struct *work);
  143. static DEFINE_SPINLOCK(p9_poll_lock);
  144. static LIST_HEAD(p9_poll_pending_list);
  145. static DECLARE_WORK(p9_poll_work, p9_poll_workfn);
  146. static unsigned int p9_ipport_resv_min = P9_DEF_MIN_RESVPORT;
  147. static unsigned int p9_ipport_resv_max = P9_DEF_MAX_RESVPORT;
  148. static void p9_mux_poll_stop(struct p9_conn *m)
  149. {
  150. unsigned long flags;
  151. int i;
  152. for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
  153. struct p9_poll_wait *pwait = &m->poll_wait[i];
  154. if (pwait->wait_addr) {
  155. remove_wait_queue(pwait->wait_addr, &pwait->wait);
  156. pwait->wait_addr = NULL;
  157. }
  158. }
  159. spin_lock_irqsave(&p9_poll_lock, flags);
  160. list_del_init(&m->poll_pending_link);
  161. spin_unlock_irqrestore(&p9_poll_lock, flags);
  162. }
  163. /**
  164. * p9_conn_cancel - cancel all pending requests with error
  165. * @m: mux data
  166. * @err: error code
  167. *
  168. */
  169. static void p9_conn_cancel(struct p9_conn *m, int err)
  170. {
  171. struct p9_req_t *req, *rtmp;
  172. unsigned long flags;
  173. LIST_HEAD(cancel_list);
  174. p9_debug(P9_DEBUG_ERROR, "mux %p err %d\n", m, err);
  175. spin_lock_irqsave(&m->client->lock, flags);
  176. if (m->err) {
  177. spin_unlock_irqrestore(&m->client->lock, flags);
  178. return;
  179. }
  180. m->err = err;
  181. list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
  182. list_move(&req->req_list, &cancel_list);
  183. }
  184. list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
  185. list_move(&req->req_list, &cancel_list);
  186. }
  187. spin_unlock_irqrestore(&m->client->lock, flags);
  188. list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
  189. p9_debug(P9_DEBUG_ERROR, "call back req %p\n", req);
  190. list_del(&req->req_list);
  191. if (!req->t_err)
  192. req->t_err = err;
  193. p9_client_cb(m->client, req, REQ_STATUS_ERROR);
  194. }
  195. }
  196. static int
  197. p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
  198. {
  199. int ret, n;
  200. struct p9_trans_fd *ts = NULL;
  201. if (client && client->status == Connected)
  202. ts = client->trans;
  203. if (!ts)
  204. return -EREMOTEIO;
  205. if (!ts->rd->f_op->poll)
  206. return -EIO;
  207. if (!ts->wr->f_op->poll)
  208. return -EIO;
  209. ret = ts->rd->f_op->poll(ts->rd, pt);
  210. if (ret < 0)
  211. return ret;
  212. if (ts->rd != ts->wr) {
  213. n = ts->wr->f_op->poll(ts->wr, pt);
  214. if (n < 0)
  215. return n;
  216. ret = (ret & ~POLLOUT) | (n & ~POLLIN);
  217. }
  218. return ret;
  219. }
  220. /**
  221. * p9_fd_read- read from a fd
  222. * @client: client instance
  223. * @v: buffer to receive data into
  224. * @len: size of receive buffer
  225. *
  226. */
  227. static int p9_fd_read(struct p9_client *client, void *v, int len)
  228. {
  229. int ret;
  230. struct p9_trans_fd *ts = NULL;
  231. if (client && client->status != Disconnected)
  232. ts = client->trans;
  233. if (!ts)
  234. return -EREMOTEIO;
  235. if (!(ts->rd->f_flags & O_NONBLOCK))
  236. p9_debug(P9_DEBUG_ERROR, "blocking read ...\n");
  237. ret = kernel_read(ts->rd, ts->rd->f_pos, v, len);
  238. if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
  239. client->status = Disconnected;
  240. return ret;
  241. }
  242. /**
  243. * p9_read_work - called when there is some data to be read from a transport
  244. * @work: container of work to be done
  245. *
  246. */
  247. static void p9_read_work(struct work_struct *work)
  248. {
  249. int n, err;
  250. struct p9_conn *m;
  251. int status = REQ_STATUS_ERROR;
  252. m = container_of(work, struct p9_conn, rq);
  253. if (m->err < 0)
  254. return;
  255. p9_debug(P9_DEBUG_TRANS, "start mux %p pos %zd\n", m, m->rc.offset);
  256. if (!m->rc.sdata) {
  257. m->rc.sdata = m->tmp_buf;
  258. m->rc.offset = 0;
  259. m->rc.capacity = 7; /* start by reading header */
  260. }
  261. clear_bit(Rpending, &m->wsched);
  262. p9_debug(P9_DEBUG_TRANS, "read mux %p pos %zd size: %zd = %zd\n",
  263. m, m->rc.offset, m->rc.capacity,
  264. m->rc.capacity - m->rc.offset);
  265. err = p9_fd_read(m->client, m->rc.sdata + m->rc.offset,
  266. m->rc.capacity - m->rc.offset);
  267. p9_debug(P9_DEBUG_TRANS, "mux %p got %d bytes\n", m, err);
  268. if (err == -EAGAIN)
  269. goto end_clear;
  270. if (err <= 0)
  271. goto error;
  272. m->rc.offset += err;
  273. /* header read in */
  274. if ((!m->req) && (m->rc.offset == m->rc.capacity)) {
  275. p9_debug(P9_DEBUG_TRANS, "got new header\n");
  276. err = p9_parse_header(&m->rc, NULL, NULL, NULL, 0);
  277. if (err) {
  278. p9_debug(P9_DEBUG_ERROR,
  279. "error parsing header: %d\n", err);
  280. goto error;
  281. }
  282. if (m->rc.size >= m->client->msize) {
  283. p9_debug(P9_DEBUG_ERROR,
  284. "requested packet size too big: %d\n",
  285. m->rc.size);
  286. err = -EIO;
  287. goto error;
  288. }
  289. p9_debug(P9_DEBUG_TRANS,
  290. "mux %p pkt: size: %d bytes tag: %d\n",
  291. m, m->rc.size, m->rc.tag);
  292. m->req = p9_tag_lookup(m->client, m->rc.tag);
  293. if (!m->req || (m->req->status != REQ_STATUS_SENT)) {
  294. p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n",
  295. m->rc.tag);
  296. err = -EIO;
  297. goto error;
  298. }
  299. if (m->req->rc == NULL) {
  300. p9_debug(P9_DEBUG_ERROR,
  301. "No recv fcall for tag %d (req %p), disconnecting!\n",
  302. m->rc.tag, m->req);
  303. m->req = NULL;
  304. err = -EIO;
  305. goto error;
  306. }
  307. m->rc.sdata = (char *)m->req->rc + sizeof(struct p9_fcall);
  308. memcpy(m->rc.sdata, m->tmp_buf, m->rc.capacity);
  309. m->rc.capacity = m->rc.size;
  310. }
  311. /* packet is read in
  312. * not an else because some packets (like clunk) have no payload
  313. */
  314. if ((m->req) && (m->rc.offset == m->rc.capacity)) {
  315. p9_debug(P9_DEBUG_TRANS, "got new packet\n");
  316. spin_lock(&m->client->lock);
  317. if (m->req->status != REQ_STATUS_ERROR)
  318. status = REQ_STATUS_RCVD;
  319. list_del(&m->req->req_list);
  320. spin_unlock(&m->client->lock);
  321. p9_client_cb(m->client, m->req, status);
  322. m->rc.sdata = NULL;
  323. m->rc.offset = 0;
  324. m->rc.capacity = 0;
  325. m->req = NULL;
  326. }
  327. end_clear:
  328. clear_bit(Rworksched, &m->wsched);
  329. if (!list_empty(&m->req_list)) {
  330. if (test_and_clear_bit(Rpending, &m->wsched))
  331. n = POLLIN;
  332. else
  333. n = p9_fd_poll(m->client, NULL);
  334. if ((n & POLLIN) && !test_and_set_bit(Rworksched, &m->wsched)) {
  335. p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
  336. schedule_work(&m->rq);
  337. }
  338. }
  339. return;
  340. error:
  341. p9_conn_cancel(m, err);
  342. clear_bit(Rworksched, &m->wsched);
  343. }
  344. /**
  345. * p9_fd_write - write to a socket
  346. * @client: client instance
  347. * @v: buffer to send data from
  348. * @len: size of send buffer
  349. *
  350. */
  351. static int p9_fd_write(struct p9_client *client, void *v, int len)
  352. {
  353. int ret;
  354. mm_segment_t oldfs;
  355. struct p9_trans_fd *ts = NULL;
  356. if (client && client->status != Disconnected)
  357. ts = client->trans;
  358. if (!ts)
  359. return -EREMOTEIO;
  360. if (!(ts->wr->f_flags & O_NONBLOCK))
  361. p9_debug(P9_DEBUG_ERROR, "blocking write ...\n");
  362. oldfs = get_fs();
  363. set_fs(get_ds());
  364. /* The cast to a user pointer is valid due to the set_fs() */
  365. ret = vfs_write(ts->wr, (__force void __user *)v, len, &ts->wr->f_pos);
  366. set_fs(oldfs);
  367. if (ret <= 0 && ret != -ERESTARTSYS && ret != -EAGAIN)
  368. client->status = Disconnected;
  369. return ret;
  370. }
  371. /**
  372. * p9_write_work - called when a transport can send some data
  373. * @work: container for work to be done
  374. *
  375. */
  376. static void p9_write_work(struct work_struct *work)
  377. {
  378. int n, err;
  379. struct p9_conn *m;
  380. struct p9_req_t *req;
  381. m = container_of(work, struct p9_conn, wq);
  382. if (m->err < 0) {
  383. clear_bit(Wworksched, &m->wsched);
  384. return;
  385. }
  386. if (!m->wsize) {
  387. spin_lock(&m->client->lock);
  388. if (list_empty(&m->unsent_req_list)) {
  389. clear_bit(Wworksched, &m->wsched);
  390. spin_unlock(&m->client->lock);
  391. return;
  392. }
  393. req = list_entry(m->unsent_req_list.next, struct p9_req_t,
  394. req_list);
  395. req->status = REQ_STATUS_SENT;
  396. p9_debug(P9_DEBUG_TRANS, "move req %p\n", req);
  397. list_move_tail(&req->req_list, &m->req_list);
  398. m->wbuf = req->tc->sdata;
  399. m->wsize = req->tc->size;
  400. m->wpos = 0;
  401. spin_unlock(&m->client->lock);
  402. }
  403. p9_debug(P9_DEBUG_TRANS, "mux %p pos %d size %d\n",
  404. m, m->wpos, m->wsize);
  405. clear_bit(Wpending, &m->wsched);
  406. err = p9_fd_write(m->client, m->wbuf + m->wpos, m->wsize - m->wpos);
  407. p9_debug(P9_DEBUG_TRANS, "mux %p sent %d bytes\n", m, err);
  408. if (err == -EAGAIN)
  409. goto end_clear;
  410. if (err < 0)
  411. goto error;
  412. else if (err == 0) {
  413. err = -EREMOTEIO;
  414. goto error;
  415. }
  416. m->wpos += err;
  417. if (m->wpos == m->wsize)
  418. m->wpos = m->wsize = 0;
  419. end_clear:
  420. clear_bit(Wworksched, &m->wsched);
  421. if (m->wsize || !list_empty(&m->unsent_req_list)) {
  422. if (test_and_clear_bit(Wpending, &m->wsched))
  423. n = POLLOUT;
  424. else
  425. n = p9_fd_poll(m->client, NULL);
  426. if ((n & POLLOUT) &&
  427. !test_and_set_bit(Wworksched, &m->wsched)) {
  428. p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
  429. schedule_work(&m->wq);
  430. }
  431. }
  432. return;
  433. error:
  434. p9_conn_cancel(m, err);
  435. clear_bit(Wworksched, &m->wsched);
  436. }
  437. static int p9_pollwake(wait_queue_t *wait, unsigned int mode, int sync, void *key)
  438. {
  439. struct p9_poll_wait *pwait =
  440. container_of(wait, struct p9_poll_wait, wait);
  441. struct p9_conn *m = pwait->conn;
  442. unsigned long flags;
  443. spin_lock_irqsave(&p9_poll_lock, flags);
  444. if (list_empty(&m->poll_pending_link))
  445. list_add_tail(&m->poll_pending_link, &p9_poll_pending_list);
  446. spin_unlock_irqrestore(&p9_poll_lock, flags);
  447. schedule_work(&p9_poll_work);
  448. return 1;
  449. }
  450. /**
  451. * p9_pollwait - add poll task to the wait queue
  452. * @filp: file pointer being polled
  453. * @wait_address: wait_q to block on
  454. * @p: poll state
  455. *
  456. * called by files poll operation to add v9fs-poll task to files wait queue
  457. */
  458. static void
  459. p9_pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
  460. {
  461. struct p9_conn *m = container_of(p, struct p9_conn, pt);
  462. struct p9_poll_wait *pwait = NULL;
  463. int i;
  464. for (i = 0; i < ARRAY_SIZE(m->poll_wait); i++) {
  465. if (m->poll_wait[i].wait_addr == NULL) {
  466. pwait = &m->poll_wait[i];
  467. break;
  468. }
  469. }
  470. if (!pwait) {
  471. p9_debug(P9_DEBUG_ERROR, "not enough wait_address slots\n");
  472. return;
  473. }
  474. pwait->conn = m;
  475. pwait->wait_addr = wait_address;
  476. init_waitqueue_func_entry(&pwait->wait, p9_pollwake);
  477. add_wait_queue(wait_address, &pwait->wait);
  478. }
  479. /**
  480. * p9_conn_create - initialize the per-session mux data
  481. * @client: client instance
  482. *
  483. * Note: Creates the polling task if this is the first session.
  484. */
  485. static void p9_conn_create(struct p9_client *client)
  486. {
  487. int n;
  488. struct p9_trans_fd *ts = client->trans;
  489. struct p9_conn *m = &ts->conn;
  490. p9_debug(P9_DEBUG_TRANS, "client %p msize %d\n", client, client->msize);
  491. INIT_LIST_HEAD(&m->mux_list);
  492. m->client = client;
  493. INIT_LIST_HEAD(&m->req_list);
  494. INIT_LIST_HEAD(&m->unsent_req_list);
  495. INIT_WORK(&m->rq, p9_read_work);
  496. INIT_WORK(&m->wq, p9_write_work);
  497. INIT_LIST_HEAD(&m->poll_pending_link);
  498. init_poll_funcptr(&m->pt, p9_pollwait);
  499. n = p9_fd_poll(client, &m->pt);
  500. if (n & POLLIN) {
  501. p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
  502. set_bit(Rpending, &m->wsched);
  503. }
  504. if (n & POLLOUT) {
  505. p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
  506. set_bit(Wpending, &m->wsched);
  507. }
  508. }
  509. /**
  510. * p9_poll_mux - polls a mux and schedules read or write works if necessary
  511. * @m: connection to poll
  512. *
  513. */
  514. static void p9_poll_mux(struct p9_conn *m)
  515. {
  516. int n;
  517. if (m->err < 0)
  518. return;
  519. n = p9_fd_poll(m->client, NULL);
  520. if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
  521. p9_debug(P9_DEBUG_TRANS, "error mux %p err %d\n", m, n);
  522. if (n >= 0)
  523. n = -ECONNRESET;
  524. p9_conn_cancel(m, n);
  525. }
  526. if (n & POLLIN) {
  527. set_bit(Rpending, &m->wsched);
  528. p9_debug(P9_DEBUG_TRANS, "mux %p can read\n", m);
  529. if (!test_and_set_bit(Rworksched, &m->wsched)) {
  530. p9_debug(P9_DEBUG_TRANS, "sched read work %p\n", m);
  531. schedule_work(&m->rq);
  532. }
  533. }
  534. if (n & POLLOUT) {
  535. set_bit(Wpending, &m->wsched);
  536. p9_debug(P9_DEBUG_TRANS, "mux %p can write\n", m);
  537. if ((m->wsize || !list_empty(&m->unsent_req_list)) &&
  538. !test_and_set_bit(Wworksched, &m->wsched)) {
  539. p9_debug(P9_DEBUG_TRANS, "sched write work %p\n", m);
  540. schedule_work(&m->wq);
  541. }
  542. }
  543. }
  544. /**
  545. * p9_fd_request - send 9P request
  546. * The function can sleep until the request is scheduled for sending.
  547. * The function can be interrupted. Return from the function is not
  548. * a guarantee that the request is sent successfully.
  549. *
  550. * @client: client instance
  551. * @req: request to be sent
  552. *
  553. */
  554. static int p9_fd_request(struct p9_client *client, struct p9_req_t *req)
  555. {
  556. int n;
  557. struct p9_trans_fd *ts = client->trans;
  558. struct p9_conn *m = &ts->conn;
  559. p9_debug(P9_DEBUG_TRANS, "mux %p task %p tcall %p id %d\n",
  560. m, current, req->tc, req->tc->id);
  561. if (m->err < 0)
  562. return m->err;
  563. spin_lock(&client->lock);
  564. req->status = REQ_STATUS_UNSENT;
  565. list_add_tail(&req->req_list, &m->unsent_req_list);
  566. spin_unlock(&client->lock);
  567. if (test_and_clear_bit(Wpending, &m->wsched))
  568. n = POLLOUT;
  569. else
  570. n = p9_fd_poll(m->client, NULL);
  571. if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
  572. schedule_work(&m->wq);
  573. return 0;
  574. }
  575. static int p9_fd_cancel(struct p9_client *client, struct p9_req_t *req)
  576. {
  577. int ret = 1;
  578. p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
  579. spin_lock(&client->lock);
  580. if (req->status == REQ_STATUS_UNSENT) {
  581. list_del(&req->req_list);
  582. req->status = REQ_STATUS_FLSHD;
  583. ret = 0;
  584. }
  585. spin_unlock(&client->lock);
  586. return ret;
  587. }
  588. static int p9_fd_cancelled(struct p9_client *client, struct p9_req_t *req)
  589. {
  590. p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
  591. /* we haven't received a response for oldreq,
  592. * remove it from the list.
  593. */
  594. spin_lock(&client->lock);
  595. list_del(&req->req_list);
  596. spin_unlock(&client->lock);
  597. return 0;
  598. }
  599. /**
  600. * parse_opts - parse mount options into p9_fd_opts structure
  601. * @params: options string passed from mount
  602. * @opts: fd transport-specific structure to parse options into
  603. *
  604. * Returns 0 upon success, -ERRNO upon failure
  605. */
  606. static int parse_opts(char *params, struct p9_fd_opts *opts)
  607. {
  608. char *p;
  609. substring_t args[MAX_OPT_ARGS];
  610. int option;
  611. char *options, *tmp_options;
  612. opts->port = P9_PORT;
  613. opts->rfd = ~0;
  614. opts->wfd = ~0;
  615. opts->privport = 0;
  616. if (!params)
  617. return 0;
  618. tmp_options = kstrdup(params, GFP_KERNEL);
  619. if (!tmp_options) {
  620. p9_debug(P9_DEBUG_ERROR,
  621. "failed to allocate copy of option string\n");
  622. return -ENOMEM;
  623. }
  624. options = tmp_options;
  625. while ((p = strsep(&options, ",")) != NULL) {
  626. int token;
  627. int r;
  628. if (!*p)
  629. continue;
  630. token = match_token(p, tokens, args);
  631. if ((token != Opt_err) && (token != Opt_privport)) {
  632. r = match_int(&args[0], &option);
  633. if (r < 0) {
  634. p9_debug(P9_DEBUG_ERROR,
  635. "integer field, but no integer?\n");
  636. continue;
  637. }
  638. }
  639. switch (token) {
  640. case Opt_port:
  641. opts->port = option;
  642. break;
  643. case Opt_rfdno:
  644. opts->rfd = option;
  645. break;
  646. case Opt_wfdno:
  647. opts->wfd = option;
  648. break;
  649. case Opt_privport:
  650. opts->privport = 1;
  651. break;
  652. default:
  653. continue;
  654. }
  655. }
  656. kfree(tmp_options);
  657. return 0;
  658. }
  659. static int p9_fd_open(struct p9_client *client, int rfd, int wfd)
  660. {
  661. struct p9_trans_fd *ts = kzalloc(sizeof(struct p9_trans_fd),
  662. GFP_KERNEL);
  663. if (!ts)
  664. return -ENOMEM;
  665. ts->rd = fget(rfd);
  666. ts->wr = fget(wfd);
  667. if (!ts->rd || !ts->wr) {
  668. if (ts->rd)
  669. fput(ts->rd);
  670. if (ts->wr)
  671. fput(ts->wr);
  672. kfree(ts);
  673. return -EIO;
  674. }
  675. client->trans = ts;
  676. client->status = Connected;
  677. return 0;
  678. }
  679. static int p9_socket_open(struct p9_client *client, struct socket *csocket)
  680. {
  681. struct p9_trans_fd *p;
  682. struct file *file;
  683. p = kzalloc(sizeof(struct p9_trans_fd), GFP_KERNEL);
  684. if (!p)
  685. return -ENOMEM;
  686. csocket->sk->sk_allocation = GFP_NOIO;
  687. file = sock_alloc_file(csocket, 0, NULL);
  688. if (IS_ERR(file)) {
  689. pr_err("%s (%d): failed to map fd\n",
  690. __func__, task_pid_nr(current));
  691. sock_release(csocket);
  692. kfree(p);
  693. return PTR_ERR(file);
  694. }
  695. get_file(file);
  696. p->wr = p->rd = file;
  697. client->trans = p;
  698. client->status = Connected;
  699. p->rd->f_flags |= O_NONBLOCK;
  700. p9_conn_create(client);
  701. return 0;
  702. }
  703. /**
  704. * p9_mux_destroy - cancels all pending requests of mux
  705. * @m: mux to destroy
  706. *
  707. */
  708. static void p9_conn_destroy(struct p9_conn *m)
  709. {
  710. p9_debug(P9_DEBUG_TRANS, "mux %p prev %p next %p\n",
  711. m, m->mux_list.prev, m->mux_list.next);
  712. p9_mux_poll_stop(m);
  713. cancel_work_sync(&m->rq);
  714. cancel_work_sync(&m->wq);
  715. p9_conn_cancel(m, -ECONNRESET);
  716. m->client = NULL;
  717. }
  718. /**
  719. * p9_fd_close - shutdown file descriptor transport
  720. * @client: client instance
  721. *
  722. */
  723. static void p9_fd_close(struct p9_client *client)
  724. {
  725. struct p9_trans_fd *ts;
  726. if (!client)
  727. return;
  728. ts = client->trans;
  729. if (!ts)
  730. return;
  731. client->status = Disconnected;
  732. p9_conn_destroy(&ts->conn);
  733. if (ts->rd)
  734. fput(ts->rd);
  735. if (ts->wr)
  736. fput(ts->wr);
  737. kfree(ts);
  738. }
  739. /*
  740. * stolen from NFS - maybe should be made a generic function?
  741. */
  742. static inline int valid_ipaddr4(const char *buf)
  743. {
  744. int rc, count, in[4];
  745. rc = sscanf(buf, "%d.%d.%d.%d", &in[0], &in[1], &in[2], &in[3]);
  746. if (rc != 4)
  747. return -EINVAL;
  748. for (count = 0; count < 4; count++) {
  749. if (in[count] > 255)
  750. return -EINVAL;
  751. }
  752. return 0;
  753. }
  754. static int p9_bind_privport(struct socket *sock)
  755. {
  756. struct sockaddr_in cl;
  757. int port, err = -EINVAL;
  758. memset(&cl, 0, sizeof(cl));
  759. cl.sin_family = AF_INET;
  760. cl.sin_addr.s_addr = INADDR_ANY;
  761. for (port = p9_ipport_resv_max; port >= p9_ipport_resv_min; port--) {
  762. cl.sin_port = htons((ushort)port);
  763. err = kernel_bind(sock, (struct sockaddr *)&cl, sizeof(cl));
  764. if (err != -EADDRINUSE)
  765. break;
  766. }
  767. return err;
  768. }
  769. static int
  770. p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
  771. {
  772. int err;
  773. struct socket *csocket;
  774. struct sockaddr_in sin_server;
  775. struct p9_fd_opts opts;
  776. err = parse_opts(args, &opts);
  777. if (err < 0)
  778. return err;
  779. if (valid_ipaddr4(addr) < 0)
  780. return -EINVAL;
  781. csocket = NULL;
  782. sin_server.sin_family = AF_INET;
  783. sin_server.sin_addr.s_addr = in_aton(addr);
  784. sin_server.sin_port = htons(opts.port);
  785. err = __sock_create(current->nsproxy->net_ns, PF_INET,
  786. SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
  787. if (err) {
  788. pr_err("%s (%d): problem creating socket\n",
  789. __func__, task_pid_nr(current));
  790. return err;
  791. }
  792. if (opts.privport) {
  793. err = p9_bind_privport(csocket);
  794. if (err < 0) {
  795. pr_err("%s (%d): problem binding to privport\n",
  796. __func__, task_pid_nr(current));
  797. sock_release(csocket);
  798. return err;
  799. }
  800. }
  801. err = csocket->ops->connect(csocket,
  802. (struct sockaddr *)&sin_server,
  803. sizeof(struct sockaddr_in), 0);
  804. if (err < 0) {
  805. pr_err("%s (%d): problem connecting socket to %s\n",
  806. __func__, task_pid_nr(current), addr);
  807. sock_release(csocket);
  808. return err;
  809. }
  810. return p9_socket_open(client, csocket);
  811. }
  812. static int
  813. p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
  814. {
  815. int err;
  816. struct socket *csocket;
  817. struct sockaddr_un sun_server;
  818. csocket = NULL;
  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. }