mux.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * linux/fs/9p/mux.c
  3. *
  4. * Protocol Multiplexer
  5. *
  6. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  7. * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to:
  21. * Free Software Foundation
  22. * 51 Franklin Street, Fifth Floor
  23. * Boston, MA 02111-1301 USA
  24. *
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/errno.h>
  29. #include <linux/fs.h>
  30. #include <linux/poll.h>
  31. #include <linux/kthread.h>
  32. #include <linux/idr.h>
  33. #include "debug.h"
  34. #include "v9fs.h"
  35. #include "9p.h"
  36. #include "conv.h"
  37. #include "transport.h"
  38. #include "mux.h"
  39. #define ERREQFLUSH 1
  40. #define SCHED_TIMEOUT 10
  41. #define MAXPOLLWADDR 2
  42. enum {
  43. Rworksched = 1, /* read work scheduled or running */
  44. Rpending = 2, /* can read */
  45. Wworksched = 4, /* write work scheduled or running */
  46. Wpending = 8, /* can write */
  47. };
  48. struct v9fs_mux_poll_task;
  49. struct v9fs_req {
  50. int tag;
  51. struct v9fs_fcall *tcall;
  52. struct v9fs_fcall *rcall;
  53. int err;
  54. v9fs_mux_req_callback cb;
  55. void *cba;
  56. struct list_head req_list;
  57. };
  58. struct v9fs_mux_data {
  59. spinlock_t lock;
  60. struct list_head mux_list;
  61. struct v9fs_mux_poll_task *poll_task;
  62. int msize;
  63. unsigned char *extended;
  64. struct v9fs_transport *trans;
  65. struct v9fs_idpool tidpool;
  66. int err;
  67. wait_queue_head_t equeue;
  68. struct list_head req_list;
  69. struct list_head unsent_req_list;
  70. struct v9fs_fcall *rcall;
  71. int rpos;
  72. char *rbuf;
  73. int wpos;
  74. int wsize;
  75. char *wbuf;
  76. wait_queue_t poll_wait[MAXPOLLWADDR];
  77. wait_queue_head_t *poll_waddr[MAXPOLLWADDR];
  78. poll_table pt;
  79. struct work_struct rq;
  80. struct work_struct wq;
  81. unsigned long wsched;
  82. };
  83. struct v9fs_mux_poll_task {
  84. struct task_struct *task;
  85. struct list_head mux_list;
  86. int muxnum;
  87. };
  88. struct v9fs_mux_rpc {
  89. struct v9fs_mux_data *m;
  90. struct v9fs_req *req;
  91. int err;
  92. struct v9fs_fcall *rcall;
  93. wait_queue_head_t wqueue;
  94. };
  95. static int v9fs_poll_proc(void *);
  96. static void v9fs_read_work(void *);
  97. static void v9fs_write_work(void *);
  98. static void v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
  99. poll_table * p);
  100. static u16 v9fs_mux_get_tag(struct v9fs_mux_data *);
  101. static void v9fs_mux_put_tag(struct v9fs_mux_data *, u16);
  102. static DECLARE_MUTEX(v9fs_mux_task_lock);
  103. static struct workqueue_struct *v9fs_mux_wq;
  104. static int v9fs_mux_num;
  105. static int v9fs_mux_poll_task_num;
  106. static struct v9fs_mux_poll_task v9fs_mux_poll_tasks[100];
  107. int v9fs_mux_global_init(void)
  108. {
  109. int i;
  110. for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++)
  111. v9fs_mux_poll_tasks[i].task = NULL;
  112. v9fs_mux_wq = create_workqueue("v9fs");
  113. if (!v9fs_mux_wq)
  114. return -ENOMEM;
  115. return 0;
  116. }
  117. void v9fs_mux_global_exit(void)
  118. {
  119. destroy_workqueue(v9fs_mux_wq);
  120. }
  121. /**
  122. * v9fs_mux_calc_poll_procs - calculates the number of polling procs
  123. * based on the number of mounted v9fs filesystems.
  124. *
  125. * The current implementation returns sqrt of the number of mounts.
  126. */
  127. inline int v9fs_mux_calc_poll_procs(int muxnum)
  128. {
  129. int n;
  130. if (v9fs_mux_poll_task_num)
  131. n = muxnum / v9fs_mux_poll_task_num +
  132. (muxnum % v9fs_mux_poll_task_num ? 1 : 0);
  133. else
  134. n = 1;
  135. if (n > ARRAY_SIZE(v9fs_mux_poll_tasks))
  136. n = ARRAY_SIZE(v9fs_mux_poll_tasks);
  137. return n;
  138. }
  139. static int v9fs_mux_poll_start(struct v9fs_mux_data *m)
  140. {
  141. int i, n;
  142. struct v9fs_mux_poll_task *vpt, *vptlast;
  143. struct task_struct *pproc;
  144. dprintk(DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, v9fs_mux_num,
  145. v9fs_mux_poll_task_num);
  146. up(&v9fs_mux_task_lock);
  147. n = v9fs_mux_calc_poll_procs(v9fs_mux_num + 1);
  148. if (n > v9fs_mux_poll_task_num) {
  149. for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
  150. if (v9fs_mux_poll_tasks[i].task == NULL) {
  151. vpt = &v9fs_mux_poll_tasks[i];
  152. dprintk(DEBUG_MUX, "create proc %p\n", vpt);
  153. pproc = kthread_create(v9fs_poll_proc, vpt,
  154. "v9fs-poll");
  155. if (!IS_ERR(pproc)) {
  156. vpt->task = pproc;
  157. INIT_LIST_HEAD(&vpt->mux_list);
  158. vpt->muxnum = 0;
  159. v9fs_mux_poll_task_num++;
  160. wake_up_process(vpt->task);
  161. }
  162. break;
  163. }
  164. }
  165. if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks))
  166. dprintk(DEBUG_ERROR, "warning: no free poll slots\n");
  167. }
  168. n = (v9fs_mux_num + 1) / v9fs_mux_poll_task_num +
  169. ((v9fs_mux_num + 1) % v9fs_mux_poll_task_num ? 1 : 0);
  170. vptlast = NULL;
  171. for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) {
  172. vpt = &v9fs_mux_poll_tasks[i];
  173. if (vpt->task != NULL) {
  174. vptlast = vpt;
  175. if (vpt->muxnum < n) {
  176. dprintk(DEBUG_MUX, "put in proc %d\n", i);
  177. list_add(&m->mux_list, &vpt->mux_list);
  178. vpt->muxnum++;
  179. m->poll_task = vpt;
  180. memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
  181. init_poll_funcptr(&m->pt, v9fs_pollwait);
  182. break;
  183. }
  184. }
  185. }
  186. if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks)) {
  187. if (vptlast == NULL)
  188. return -ENOMEM;
  189. dprintk(DEBUG_MUX, "put in proc %d\n", i);
  190. list_add(&m->mux_list, &vptlast->mux_list);
  191. vptlast->muxnum++;
  192. m->poll_task = vptlast;
  193. memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
  194. init_poll_funcptr(&m->pt, v9fs_pollwait);
  195. }
  196. v9fs_mux_num++;
  197. down(&v9fs_mux_task_lock);
  198. return 0;
  199. }
  200. static void v9fs_mux_poll_stop(struct v9fs_mux_data *m)
  201. {
  202. int i;
  203. struct v9fs_mux_poll_task *vpt;
  204. up(&v9fs_mux_task_lock);
  205. vpt = m->poll_task;
  206. list_del(&m->mux_list);
  207. for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
  208. if (m->poll_waddr[i] != NULL) {
  209. remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]);
  210. m->poll_waddr[i] = NULL;
  211. }
  212. }
  213. vpt->muxnum--;
  214. if (!vpt->muxnum) {
  215. dprintk(DEBUG_MUX, "destroy proc %p\n", vpt);
  216. send_sig(SIGKILL, vpt->task, 1);
  217. vpt->task = NULL;
  218. v9fs_mux_poll_task_num--;
  219. }
  220. v9fs_mux_num--;
  221. down(&v9fs_mux_task_lock);
  222. }
  223. /**
  224. * v9fs_mux_init - allocate and initialize the per-session mux data
  225. * Creates the polling task if this is the first session.
  226. *
  227. * @trans - transport structure
  228. * @msize - maximum message size
  229. * @extended - pointer to the extended flag
  230. */
  231. struct v9fs_mux_data *v9fs_mux_init(struct v9fs_transport *trans, int msize,
  232. unsigned char *extended)
  233. {
  234. int i, n;
  235. struct v9fs_mux_data *m, *mtmp;
  236. dprintk(DEBUG_MUX, "transport %p msize %d\n", trans, msize);
  237. m = kmalloc(sizeof(struct v9fs_mux_data), GFP_KERNEL);
  238. if (!m)
  239. return ERR_PTR(-ENOMEM);
  240. spin_lock_init(&m->lock);
  241. INIT_LIST_HEAD(&m->mux_list);
  242. m->msize = msize;
  243. m->extended = extended;
  244. m->trans = trans;
  245. idr_init(&m->tidpool.pool);
  246. init_MUTEX(&m->tidpool.lock);
  247. m->err = 0;
  248. init_waitqueue_head(&m->equeue);
  249. INIT_LIST_HEAD(&m->req_list);
  250. INIT_LIST_HEAD(&m->unsent_req_list);
  251. m->rcall = NULL;
  252. m->rpos = 0;
  253. m->rbuf = NULL;
  254. m->wpos = m->wsize = 0;
  255. m->wbuf = NULL;
  256. INIT_WORK(&m->rq, v9fs_read_work, m);
  257. INIT_WORK(&m->wq, v9fs_write_work, m);
  258. m->wsched = 0;
  259. memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
  260. m->poll_task = NULL;
  261. n = v9fs_mux_poll_start(m);
  262. if (n)
  263. return ERR_PTR(n);
  264. n = trans->poll(trans, &m->pt);
  265. if (n & POLLIN) {
  266. dprintk(DEBUG_MUX, "mux %p can read\n", m);
  267. set_bit(Rpending, &m->wsched);
  268. }
  269. if (n & POLLOUT) {
  270. dprintk(DEBUG_MUX, "mux %p can write\n", m);
  271. set_bit(Wpending, &m->wsched);
  272. }
  273. for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) {
  274. if (IS_ERR(m->poll_waddr[i])) {
  275. v9fs_mux_poll_stop(m);
  276. mtmp = (void *)m->poll_waddr; /* the error code */
  277. kfree(m);
  278. m = mtmp;
  279. break;
  280. }
  281. }
  282. return m;
  283. }
  284. /**
  285. * v9fs_mux_destroy - cancels all pending requests and frees mux resources
  286. */
  287. void v9fs_mux_destroy(struct v9fs_mux_data *m)
  288. {
  289. dprintk(DEBUG_MUX, "mux %p prev %p next %p\n", m,
  290. m->mux_list.prev, m->mux_list.next);
  291. v9fs_mux_cancel(m, -ECONNRESET);
  292. if (!list_empty(&m->req_list)) {
  293. /* wait until all processes waiting on this session exit */
  294. dprintk(DEBUG_MUX, "mux %p waiting for empty request queue\n",
  295. m);
  296. wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000);
  297. dprintk(DEBUG_MUX, "mux %p request queue empty: %d\n", m,
  298. list_empty(&m->req_list));
  299. }
  300. v9fs_mux_poll_stop(m);
  301. m->trans = NULL;
  302. kfree(m);
  303. }
  304. /**
  305. * v9fs_pollwait - called by files poll operation to add v9fs-poll task
  306. * to files wait queue
  307. */
  308. static void
  309. v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address,
  310. poll_table * p)
  311. {
  312. int i;
  313. struct v9fs_mux_data *m;
  314. m = container_of(p, struct v9fs_mux_data, pt);
  315. for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++)
  316. if (m->poll_waddr[i] == NULL)
  317. break;
  318. if (i >= ARRAY_SIZE(m->poll_waddr)) {
  319. dprintk(DEBUG_ERROR, "not enough wait_address slots\n");
  320. return;
  321. }
  322. m->poll_waddr[i] = wait_address;
  323. if (!wait_address) {
  324. dprintk(DEBUG_ERROR, "no wait_address\n");
  325. m->poll_waddr[i] = ERR_PTR(-EIO);
  326. return;
  327. }
  328. init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task);
  329. add_wait_queue(wait_address, &m->poll_wait[i]);
  330. }
  331. /**
  332. * v9fs_poll_mux - polls a mux and schedules read or write works if necessary
  333. */
  334. static inline void v9fs_poll_mux(struct v9fs_mux_data *m)
  335. {
  336. int n;
  337. if (m->err < 0)
  338. return;
  339. n = m->trans->poll(m->trans, NULL);
  340. if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) {
  341. dprintk(DEBUG_MUX, "error mux %p err %d\n", m, n);
  342. if (n >= 0)
  343. n = -ECONNRESET;
  344. v9fs_mux_cancel(m, n);
  345. }
  346. if (n & POLLIN) {
  347. set_bit(Rpending, &m->wsched);
  348. dprintk(DEBUG_MUX, "mux %p can read\n", m);
  349. if (!test_and_set_bit(Rworksched, &m->wsched)) {
  350. dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
  351. queue_work(v9fs_mux_wq, &m->rq);
  352. }
  353. }
  354. if (n & POLLOUT) {
  355. set_bit(Wpending, &m->wsched);
  356. dprintk(DEBUG_MUX, "mux %p can write\n", m);
  357. if ((m->wsize || !list_empty(&m->unsent_req_list))
  358. && !test_and_set_bit(Wworksched, &m->wsched)) {
  359. dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
  360. queue_work(v9fs_mux_wq, &m->wq);
  361. }
  362. }
  363. }
  364. /**
  365. * v9fs_poll_proc - polls all v9fs transports for new events and queues
  366. * the appropriate work to the work queue
  367. */
  368. static int v9fs_poll_proc(void *a)
  369. {
  370. struct v9fs_mux_data *m, *mtmp;
  371. struct v9fs_mux_poll_task *vpt;
  372. vpt = a;
  373. dprintk(DEBUG_MUX, "start %p %p\n", current, vpt);
  374. allow_signal(SIGKILL);
  375. while (!kthread_should_stop()) {
  376. set_current_state(TASK_INTERRUPTIBLE);
  377. if (signal_pending(current))
  378. break;
  379. list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
  380. v9fs_poll_mux(m);
  381. }
  382. dprintk(DEBUG_MUX, "sleeping...\n");
  383. schedule_timeout(SCHED_TIMEOUT * HZ);
  384. }
  385. __set_current_state(TASK_RUNNING);
  386. dprintk(DEBUG_MUX, "finish\n");
  387. return 0;
  388. }
  389. /**
  390. * v9fs_write_work - called when a transport can send some data
  391. */
  392. static void v9fs_write_work(void *a)
  393. {
  394. int n, err;
  395. struct v9fs_mux_data *m;
  396. struct v9fs_req *req;
  397. m = a;
  398. if (m->err < 0) {
  399. clear_bit(Wworksched, &m->wsched);
  400. return;
  401. }
  402. if (!m->wsize) {
  403. if (list_empty(&m->unsent_req_list)) {
  404. clear_bit(Wworksched, &m->wsched);
  405. return;
  406. }
  407. spin_lock(&m->lock);
  408. req =
  409. list_entry(m->unsent_req_list.next, struct v9fs_req,
  410. req_list);
  411. list_move_tail(&req->req_list, &m->req_list);
  412. m->wbuf = req->tcall->sdata;
  413. m->wsize = req->tcall->size;
  414. m->wpos = 0;
  415. dump_data(m->wbuf, m->wsize);
  416. spin_unlock(&m->lock);
  417. }
  418. dprintk(DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, m->wsize);
  419. clear_bit(Wpending, &m->wsched);
  420. err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
  421. dprintk(DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
  422. if (err == -EAGAIN) {
  423. clear_bit(Wworksched, &m->wsched);
  424. return;
  425. }
  426. if (err <= 0)
  427. goto error;
  428. m->wpos += err;
  429. if (m->wpos == m->wsize)
  430. m->wpos = m->wsize = 0;
  431. if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
  432. if (test_and_clear_bit(Wpending, &m->wsched))
  433. n = POLLOUT;
  434. else
  435. n = m->trans->poll(m->trans, NULL);
  436. if (n & POLLOUT) {
  437. dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
  438. queue_work(v9fs_mux_wq, &m->wq);
  439. } else
  440. clear_bit(Wworksched, &m->wsched);
  441. } else
  442. clear_bit(Wworksched, &m->wsched);
  443. return;
  444. error:
  445. v9fs_mux_cancel(m, err);
  446. clear_bit(Wworksched, &m->wsched);
  447. }
  448. static void process_request(struct v9fs_mux_data *m, struct v9fs_req *req)
  449. {
  450. int ecode, tag;
  451. struct v9fs_str *ename;
  452. tag = req->tag;
  453. if (req->rcall->id == RERROR && !req->err) {
  454. ecode = req->rcall->params.rerror.errno;
  455. ename = &req->rcall->params.rerror.error;
  456. dprintk(DEBUG_MUX, "Rerror %.*s\n", ename->len, ename->str);
  457. if (*m->extended)
  458. req->err = -ecode;
  459. if (!req->err) {
  460. req->err = v9fs_errstr2errno(ename->str, ename->len);
  461. if (!req->err) { /* string match failed */
  462. PRINT_FCALL_ERROR("unknown error", req->rcall);
  463. }
  464. if (!req->err)
  465. req->err = -ESERVERFAULT;
  466. }
  467. } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
  468. dprintk(DEBUG_ERROR, "fcall mismatch: expected %d, got %d\n",
  469. req->tcall->id + 1, req->rcall->id);
  470. if (!req->err)
  471. req->err = -EIO;
  472. }
  473. if (req->cb && req->err != ERREQFLUSH) {
  474. dprintk(DEBUG_MUX, "calling callback tcall %p rcall %p\n",
  475. req->tcall, req->rcall);
  476. (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
  477. req->cb = NULL;
  478. } else
  479. kfree(req->rcall);
  480. v9fs_mux_put_tag(m, tag);
  481. wake_up(&m->equeue);
  482. kfree(req);
  483. }
  484. /**
  485. * v9fs_read_work - called when there is some data to be read from a transport
  486. */
  487. static void v9fs_read_work(void *a)
  488. {
  489. int n, err;
  490. struct v9fs_mux_data *m;
  491. struct v9fs_req *req, *rptr, *rreq;
  492. struct v9fs_fcall *rcall;
  493. char *rbuf;
  494. m = a;
  495. if (m->err < 0)
  496. return;
  497. rcall = NULL;
  498. dprintk(DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
  499. if (!m->rcall) {
  500. m->rcall =
  501. kmalloc(sizeof(struct v9fs_fcall) + m->msize, GFP_KERNEL);
  502. if (!m->rcall) {
  503. err = -ENOMEM;
  504. goto error;
  505. }
  506. m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall);
  507. m->rpos = 0;
  508. }
  509. clear_bit(Rpending, &m->wsched);
  510. err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
  511. dprintk(DEBUG_MUX, "mux %p got %d bytes\n", m, err);
  512. if (err == -EAGAIN) {
  513. clear_bit(Rworksched, &m->wsched);
  514. return;
  515. }
  516. if (err <= 0)
  517. goto error;
  518. m->rpos += err;
  519. while (m->rpos > 4) {
  520. n = le32_to_cpu(*(__le32 *) m->rbuf);
  521. if (n >= m->msize) {
  522. dprintk(DEBUG_ERROR,
  523. "requested packet size too big: %d\n", n);
  524. err = -EIO;
  525. goto error;
  526. }
  527. if (m->rpos < n)
  528. break;
  529. dump_data(m->rbuf, n);
  530. err =
  531. v9fs_deserialize_fcall(m->rbuf, n, m->rcall, *m->extended);
  532. if (err < 0) {
  533. goto error;
  534. }
  535. rcall = m->rcall;
  536. rbuf = m->rbuf;
  537. if (m->rpos > n) {
  538. m->rcall = kmalloc(sizeof(struct v9fs_fcall) + m->msize,
  539. GFP_KERNEL);
  540. if (!m->rcall) {
  541. err = -ENOMEM;
  542. goto error;
  543. }
  544. m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall);
  545. memmove(m->rbuf, rbuf + n, m->rpos - n);
  546. m->rpos -= n;
  547. } else {
  548. m->rcall = NULL;
  549. m->rbuf = NULL;
  550. m->rpos = 0;
  551. }
  552. dprintk(DEBUG_MUX, "mux %p fcall id %d tag %d\n", m, rcall->id,
  553. rcall->tag);
  554. req = NULL;
  555. spin_lock(&m->lock);
  556. list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
  557. if (rreq->tag == rcall->tag) {
  558. req = rreq;
  559. req->rcall = rcall;
  560. list_del(&req->req_list);
  561. spin_unlock(&m->lock);
  562. process_request(m, req);
  563. break;
  564. }
  565. }
  566. if (!req) {
  567. spin_unlock(&m->lock);
  568. if (err >= 0 && rcall->id != RFLUSH)
  569. dprintk(DEBUG_ERROR,
  570. "unexpected response mux %p id %d tag %d\n",
  571. m, rcall->id, rcall->tag);
  572. kfree(rcall);
  573. }
  574. }
  575. if (!list_empty(&m->req_list)) {
  576. if (test_and_clear_bit(Rpending, &m->wsched))
  577. n = POLLIN;
  578. else
  579. n = m->trans->poll(m->trans, NULL);
  580. if (n & POLLIN) {
  581. dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
  582. queue_work(v9fs_mux_wq, &m->rq);
  583. } else
  584. clear_bit(Rworksched, &m->wsched);
  585. } else
  586. clear_bit(Rworksched, &m->wsched);
  587. return;
  588. error:
  589. v9fs_mux_cancel(m, err);
  590. clear_bit(Rworksched, &m->wsched);
  591. }
  592. /**
  593. * v9fs_send_request - send 9P request
  594. * The function can sleep until the request is scheduled for sending.
  595. * The function can be interrupted. Return from the function is not
  596. * a guarantee that the request is sent succesfully. Can return errors
  597. * that can be retrieved by PTR_ERR macros.
  598. *
  599. * @m: mux data
  600. * @tc: request to be sent
  601. * @cb: callback function to call when response is received
  602. * @cba: parameter to pass to the callback function
  603. */
  604. static struct v9fs_req *v9fs_send_request(struct v9fs_mux_data *m,
  605. struct v9fs_fcall *tc,
  606. v9fs_mux_req_callback cb, void *cba)
  607. {
  608. int n;
  609. struct v9fs_req *req;
  610. dprintk(DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
  611. tc, tc->id);
  612. if (m->err < 0)
  613. return ERR_PTR(m->err);
  614. req = kmalloc(sizeof(struct v9fs_req), GFP_KERNEL);
  615. if (!req)
  616. return ERR_PTR(-ENOMEM);
  617. if (tc->id == TVERSION)
  618. n = V9FS_NOTAG;
  619. else
  620. n = v9fs_mux_get_tag(m);
  621. if (n < 0)
  622. return ERR_PTR(-ENOMEM);
  623. v9fs_set_tag(tc, n);
  624. req->tag = n;
  625. req->tcall = tc;
  626. req->rcall = NULL;
  627. req->err = 0;
  628. req->cb = cb;
  629. req->cba = cba;
  630. spin_lock(&m->lock);
  631. list_add_tail(&req->req_list, &m->unsent_req_list);
  632. spin_unlock(&m->lock);
  633. if (test_and_clear_bit(Wpending, &m->wsched))
  634. n = POLLOUT;
  635. else
  636. n = m->trans->poll(m->trans, NULL);
  637. if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
  638. queue_work(v9fs_mux_wq, &m->wq);
  639. return req;
  640. }
  641. static inline void
  642. v9fs_mux_flush_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc,
  643. int err)
  644. {
  645. v9fs_mux_req_callback cb;
  646. int tag;
  647. struct v9fs_mux_data *m;
  648. struct v9fs_req *req, *rptr;
  649. m = a;
  650. dprintk(DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m, tc,
  651. rc, err, tc->params.tflush.oldtag);
  652. spin_lock(&m->lock);
  653. cb = NULL;
  654. tag = tc->params.tflush.oldtag;
  655. list_for_each_entry_safe(req, rptr, &m->req_list, req_list) {
  656. if (req->tag == tag) {
  657. list_del(&req->req_list);
  658. if (req->cb) {
  659. cb = req->cb;
  660. req->cb = NULL;
  661. spin_unlock(&m->lock);
  662. (*cb) (req->cba, req->tcall, req->rcall,
  663. req->err);
  664. }
  665. kfree(req);
  666. wake_up(&m->equeue);
  667. break;
  668. }
  669. }
  670. if (!cb)
  671. spin_unlock(&m->lock);
  672. v9fs_mux_put_tag(m, tag);
  673. kfree(tc);
  674. kfree(rc);
  675. }
  676. static void
  677. v9fs_mux_flush_request(struct v9fs_mux_data *m, struct v9fs_req *req)
  678. {
  679. struct v9fs_fcall *fc;
  680. dprintk(DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
  681. fc = v9fs_create_tflush(req->tag);
  682. v9fs_send_request(m, fc, v9fs_mux_flush_cb, m);
  683. }
  684. static void
  685. v9fs_mux_rpc_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc, int err)
  686. {
  687. struct v9fs_mux_rpc *r;
  688. if (err == ERREQFLUSH) {
  689. dprintk(DEBUG_MUX, "err req flush\n");
  690. return;
  691. }
  692. r = a;
  693. dprintk(DEBUG_MUX, "mux %p req %p tc %p rc %p err %d\n", r->m, r->req,
  694. tc, rc, err);
  695. r->rcall = rc;
  696. r->err = err;
  697. wake_up(&r->wqueue);
  698. }
  699. /**
  700. * v9fs_mux_rpc - sends 9P request and waits until a response is available.
  701. * The function can be interrupted.
  702. * @m: mux data
  703. * @tc: request to be sent
  704. * @rc: pointer where a pointer to the response is stored
  705. */
  706. int
  707. v9fs_mux_rpc(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
  708. struct v9fs_fcall **rc)
  709. {
  710. int err;
  711. unsigned long flags;
  712. struct v9fs_req *req;
  713. struct v9fs_mux_rpc r;
  714. r.err = 0;
  715. r.rcall = NULL;
  716. r.m = m;
  717. init_waitqueue_head(&r.wqueue);
  718. if (rc)
  719. *rc = NULL;
  720. req = v9fs_send_request(m, tc, v9fs_mux_rpc_cb, &r);
  721. if (IS_ERR(req)) {
  722. err = PTR_ERR(req);
  723. dprintk(DEBUG_MUX, "error %d\n", err);
  724. return PTR_ERR(req);
  725. }
  726. r.req = req;
  727. dprintk(DEBUG_MUX, "mux %p tc %p tag %d rpc %p req %p\n", m, tc,
  728. req->tag, &r, req);
  729. err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
  730. if (r.err < 0)
  731. err = r.err;
  732. if (err == -ERESTARTSYS && m->trans->status == Connected && m->err == 0) {
  733. spin_lock(&m->lock);
  734. req->tcall = NULL;
  735. req->err = ERREQFLUSH;
  736. spin_unlock(&m->lock);
  737. clear_thread_flag(TIF_SIGPENDING);
  738. v9fs_mux_flush_request(m, req);
  739. spin_lock_irqsave(&current->sighand->siglock, flags);
  740. recalc_sigpending();
  741. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  742. }
  743. if (!err) {
  744. if (r.rcall)
  745. dprintk(DEBUG_MUX, "got response id %d tag %d\n",
  746. r.rcall->id, r.rcall->tag);
  747. if (rc)
  748. *rc = r.rcall;
  749. else
  750. kfree(r.rcall);
  751. } else {
  752. kfree(r.rcall);
  753. dprintk(DEBUG_MUX, "got error %d\n", err);
  754. if (err > 0)
  755. err = -EIO;
  756. }
  757. return err;
  758. }
  759. /**
  760. * v9fs_mux_rpcnb - sends 9P request without waiting for response.
  761. * @m: mux data
  762. * @tc: request to be sent
  763. * @cb: callback function to be called when response arrives
  764. * @cba: value to pass to the callback function
  765. */
  766. int v9fs_mux_rpcnb(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
  767. v9fs_mux_req_callback cb, void *a)
  768. {
  769. int err;
  770. struct v9fs_req *req;
  771. req = v9fs_send_request(m, tc, cb, a);
  772. if (IS_ERR(req)) {
  773. err = PTR_ERR(req);
  774. dprintk(DEBUG_MUX, "error %d\n", err);
  775. return PTR_ERR(req);
  776. }
  777. dprintk(DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
  778. return 0;
  779. }
  780. /**
  781. * v9fs_mux_cancel - cancel all pending requests with error
  782. * @m: mux data
  783. * @err: error code
  784. */
  785. void v9fs_mux_cancel(struct v9fs_mux_data *m, int err)
  786. {
  787. struct v9fs_req *req, *rtmp;
  788. LIST_HEAD(cancel_list);
  789. dprintk(DEBUG_MUX, "mux %p err %d\n", m, err);
  790. m->err = err;
  791. spin_lock(&m->lock);
  792. list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
  793. list_move(&req->req_list, &cancel_list);
  794. }
  795. spin_unlock(&m->lock);
  796. list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
  797. list_del(&req->req_list);
  798. if (!req->err)
  799. req->err = err;
  800. if (req->cb)
  801. (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
  802. else
  803. kfree(req->rcall);
  804. kfree(req);
  805. }
  806. wake_up(&m->equeue);
  807. }
  808. static u16 v9fs_mux_get_tag(struct v9fs_mux_data *m)
  809. {
  810. int tag;
  811. tag = v9fs_get_idpool(&m->tidpool);
  812. if (tag < 0)
  813. return V9FS_NOTAG;
  814. else
  815. return (u16) tag;
  816. }
  817. static void v9fs_mux_put_tag(struct v9fs_mux_data *m, u16 tag)
  818. {
  819. if (tag != V9FS_NOTAG && v9fs_check_idpool(tag, &m->tidpool))
  820. v9fs_put_idpool(tag, &m->tidpool);
  821. }