trans_fd.c 24 KB

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