mux.c 22 KB

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