mux.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  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 version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to:
  20. * Free Software Foundation
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02111-1301 USA
  23. *
  24. */
  25. #include <linux/config.h>
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/poll.h>
  30. #include <linux/kthread.h>
  31. #include <linux/idr.h>
  32. #include <linux/mutex.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 tagpool;
  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 DEFINE_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. static 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. mutex_lock(&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. mutex_unlock(&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. mutex_lock(&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. mutex_unlock(&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->tagpool.pool);
  246. init_MUTEX(&m->tagpool.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 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. again:
  409. req = list_entry(m->unsent_req_list.next, struct v9fs_req,
  410. req_list);
  411. list_move_tail(&req->req_list, &m->req_list);
  412. if (req->err == ERREQFLUSH)
  413. goto again;
  414. m->wbuf = req->tcall->sdata;
  415. m->wsize = req->tcall->size;
  416. m->wpos = 0;
  417. dump_data(m->wbuf, m->wsize);
  418. spin_unlock(&m->lock);
  419. }
  420. dprintk(DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, m->wsize);
  421. clear_bit(Wpending, &m->wsched);
  422. err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
  423. dprintk(DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
  424. if (err == -EAGAIN) {
  425. clear_bit(Wworksched, &m->wsched);
  426. return;
  427. }
  428. if (err <= 0)
  429. goto error;
  430. m->wpos += err;
  431. if (m->wpos == m->wsize)
  432. m->wpos = m->wsize = 0;
  433. if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
  434. if (test_and_clear_bit(Wpending, &m->wsched))
  435. n = POLLOUT;
  436. else
  437. n = m->trans->poll(m->trans, NULL);
  438. if (n & POLLOUT) {
  439. dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
  440. queue_work(v9fs_mux_wq, &m->wq);
  441. } else
  442. clear_bit(Wworksched, &m->wsched);
  443. } else
  444. clear_bit(Wworksched, &m->wsched);
  445. return;
  446. error:
  447. v9fs_mux_cancel(m, err);
  448. clear_bit(Wworksched, &m->wsched);
  449. }
  450. static void process_request(struct v9fs_mux_data *m, struct v9fs_req *req)
  451. {
  452. int ecode, tag;
  453. struct v9fs_str *ename;
  454. tag = req->tag;
  455. if (!req->err && req->rcall->id == RERROR) {
  456. ecode = req->rcall->params.rerror.errno;
  457. ename = &req->rcall->params.rerror.error;
  458. dprintk(DEBUG_MUX, "Rerror %.*s\n", ename->len, ename->str);
  459. if (*m->extended)
  460. req->err = -ecode;
  461. if (!req->err) {
  462. req->err = v9fs_errstr2errno(ename->str, ename->len);
  463. if (!req->err) { /* string match failed */
  464. PRINT_FCALL_ERROR("unknown error", req->rcall);
  465. }
  466. if (!req->err)
  467. req->err = -ESERVERFAULT;
  468. }
  469. } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
  470. dprintk(DEBUG_ERROR, "fcall mismatch: expected %d, got %d\n",
  471. req->tcall->id + 1, req->rcall->id);
  472. if (!req->err)
  473. req->err = -EIO;
  474. }
  475. if (req->err == ERREQFLUSH)
  476. return;
  477. if (req->cb) {
  478. dprintk(DEBUG_MUX, "calling callback tcall %p rcall %p\n",
  479. req->tcall, req->rcall);
  480. (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
  481. req->cb = NULL;
  482. } else
  483. kfree(req->rcall);
  484. v9fs_mux_put_tag(m, tag);
  485. wake_up(&m->equeue);
  486. kfree(req);
  487. }
  488. /**
  489. * v9fs_read_work - called when there is some data to be read from a transport
  490. */
  491. static void v9fs_read_work(void *a)
  492. {
  493. int n, err;
  494. struct v9fs_mux_data *m;
  495. struct v9fs_req *req, *rptr, *rreq;
  496. struct v9fs_fcall *rcall;
  497. char *rbuf;
  498. m = a;
  499. if (m->err < 0)
  500. return;
  501. rcall = NULL;
  502. dprintk(DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
  503. if (!m->rcall) {
  504. m->rcall =
  505. kmalloc(sizeof(struct v9fs_fcall) + m->msize, GFP_KERNEL);
  506. if (!m->rcall) {
  507. err = -ENOMEM;
  508. goto error;
  509. }
  510. m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall);
  511. m->rpos = 0;
  512. }
  513. clear_bit(Rpending, &m->wsched);
  514. err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
  515. dprintk(DEBUG_MUX, "mux %p got %d bytes\n", m, err);
  516. if (err == -EAGAIN) {
  517. clear_bit(Rworksched, &m->wsched);
  518. return;
  519. }
  520. if (err <= 0)
  521. goto error;
  522. m->rpos += err;
  523. while (m->rpos > 4) {
  524. n = le32_to_cpu(*(__le32 *) m->rbuf);
  525. if (n >= m->msize) {
  526. dprintk(DEBUG_ERROR,
  527. "requested packet size too big: %d\n", n);
  528. err = -EIO;
  529. goto error;
  530. }
  531. if (m->rpos < n)
  532. break;
  533. dump_data(m->rbuf, n);
  534. err =
  535. v9fs_deserialize_fcall(m->rbuf, n, m->rcall, *m->extended);
  536. if (err < 0) {
  537. goto error;
  538. }
  539. if ((v9fs_debug_level&DEBUG_FCALL) == DEBUG_FCALL) {
  540. char buf[150];
  541. v9fs_printfcall(buf, sizeof(buf), m->rcall,
  542. *m->extended);
  543. printk(KERN_NOTICE ">>> %p %s\n", m, buf);
  544. }
  545. rcall = m->rcall;
  546. rbuf = m->rbuf;
  547. if (m->rpos > n) {
  548. m->rcall = kmalloc(sizeof(struct v9fs_fcall) + m->msize,
  549. GFP_KERNEL);
  550. if (!m->rcall) {
  551. err = -ENOMEM;
  552. goto error;
  553. }
  554. m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall);
  555. memmove(m->rbuf, rbuf + n, m->rpos - n);
  556. m->rpos -= n;
  557. } else {
  558. m->rcall = NULL;
  559. m->rbuf = NULL;
  560. m->rpos = 0;
  561. }
  562. dprintk(DEBUG_MUX, "mux %p fcall id %d tag %d\n", m, rcall->id,
  563. rcall->tag);
  564. req = NULL;
  565. spin_lock(&m->lock);
  566. list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
  567. if (rreq->tag == rcall->tag) {
  568. req = rreq;
  569. req->rcall = rcall;
  570. list_del(&req->req_list);
  571. spin_unlock(&m->lock);
  572. process_request(m, req);
  573. break;
  574. }
  575. }
  576. if (!req) {
  577. spin_unlock(&m->lock);
  578. if (err >= 0 && rcall->id != RFLUSH)
  579. dprintk(DEBUG_ERROR,
  580. "unexpected response mux %p id %d tag %d\n",
  581. m, rcall->id, rcall->tag);
  582. kfree(rcall);
  583. }
  584. }
  585. if (!list_empty(&m->req_list)) {
  586. if (test_and_clear_bit(Rpending, &m->wsched))
  587. n = POLLIN;
  588. else
  589. n = m->trans->poll(m->trans, NULL);
  590. if (n & POLLIN) {
  591. dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
  592. queue_work(v9fs_mux_wq, &m->rq);
  593. } else
  594. clear_bit(Rworksched, &m->wsched);
  595. } else
  596. clear_bit(Rworksched, &m->wsched);
  597. return;
  598. error:
  599. v9fs_mux_cancel(m, err);
  600. clear_bit(Rworksched, &m->wsched);
  601. }
  602. /**
  603. * v9fs_send_request - send 9P request
  604. * The function can sleep until the request is scheduled for sending.
  605. * The function can be interrupted. Return from the function is not
  606. * a guarantee that the request is sent succesfully. Can return errors
  607. * that can be retrieved by PTR_ERR macros.
  608. *
  609. * @m: mux data
  610. * @tc: request to be sent
  611. * @cb: callback function to call when response is received
  612. * @cba: parameter to pass to the callback function
  613. */
  614. static struct v9fs_req *v9fs_send_request(struct v9fs_mux_data *m,
  615. struct v9fs_fcall *tc,
  616. v9fs_mux_req_callback cb, void *cba)
  617. {
  618. int n;
  619. struct v9fs_req *req;
  620. dprintk(DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
  621. tc, tc->id);
  622. if (m->err < 0)
  623. return ERR_PTR(m->err);
  624. req = kmalloc(sizeof(struct v9fs_req), GFP_KERNEL);
  625. if (!req)
  626. return ERR_PTR(-ENOMEM);
  627. if (tc->id == TVERSION)
  628. n = V9FS_NOTAG;
  629. else
  630. n = v9fs_mux_get_tag(m);
  631. if (n < 0)
  632. return ERR_PTR(-ENOMEM);
  633. v9fs_set_tag(tc, n);
  634. if ((v9fs_debug_level&DEBUG_FCALL) == DEBUG_FCALL) {
  635. char buf[150];
  636. v9fs_printfcall(buf, sizeof(buf), tc, *m->extended);
  637. printk(KERN_NOTICE "<<< %p %s\n", m, buf);
  638. }
  639. req->tag = n;
  640. req->tcall = tc;
  641. req->rcall = NULL;
  642. req->err = 0;
  643. req->cb = cb;
  644. req->cba = cba;
  645. spin_lock(&m->lock);
  646. list_add_tail(&req->req_list, &m->unsent_req_list);
  647. spin_unlock(&m->lock);
  648. if (test_and_clear_bit(Wpending, &m->wsched))
  649. n = POLLOUT;
  650. else
  651. n = m->trans->poll(m->trans, NULL);
  652. if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
  653. queue_work(v9fs_mux_wq, &m->wq);
  654. return req;
  655. }
  656. static void v9fs_mux_flush_cb(void *a, struct v9fs_fcall *tc,
  657. struct v9fs_fcall *rc, int err)
  658. {
  659. v9fs_mux_req_callback cb;
  660. int tag;
  661. struct v9fs_mux_data *m;
  662. struct v9fs_req *req, *rptr;
  663. m = a;
  664. dprintk(DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m, tc,
  665. rc, err, tc->params.tflush.oldtag);
  666. spin_lock(&m->lock);
  667. cb = NULL;
  668. tag = tc->params.tflush.oldtag;
  669. list_for_each_entry_safe(req, rptr, &m->req_list, req_list) {
  670. if (req->tag == tag) {
  671. list_del(&req->req_list);
  672. if (req->cb) {
  673. cb = req->cb;
  674. req->cb = NULL;
  675. spin_unlock(&m->lock);
  676. (*cb) (req->cba, req->tcall, req->rcall,
  677. req->err);
  678. }
  679. kfree(req);
  680. wake_up(&m->equeue);
  681. break;
  682. }
  683. }
  684. if (!cb)
  685. spin_unlock(&m->lock);
  686. v9fs_mux_put_tag(m, tag);
  687. kfree(tc);
  688. kfree(rc);
  689. }
  690. static void
  691. v9fs_mux_flush_request(struct v9fs_mux_data *m, struct v9fs_req *req)
  692. {
  693. struct v9fs_fcall *fc;
  694. dprintk(DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
  695. fc = v9fs_create_tflush(req->tag);
  696. v9fs_send_request(m, fc, v9fs_mux_flush_cb, m);
  697. }
  698. static void
  699. v9fs_mux_rpc_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc, int err)
  700. {
  701. struct v9fs_mux_rpc *r;
  702. if (err == ERREQFLUSH) {
  703. kfree(rc);
  704. dprintk(DEBUG_MUX, "err req flush\n");
  705. return;
  706. }
  707. r = a;
  708. dprintk(DEBUG_MUX, "mux %p req %p tc %p rc %p err %d\n", r->m, r->req,
  709. tc, rc, err);
  710. r->rcall = rc;
  711. r->err = err;
  712. wake_up(&r->wqueue);
  713. }
  714. /**
  715. * v9fs_mux_rpc - sends 9P request and waits until a response is available.
  716. * The function can be interrupted.
  717. * @m: mux data
  718. * @tc: request to be sent
  719. * @rc: pointer where a pointer to the response is stored
  720. */
  721. int
  722. v9fs_mux_rpc(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
  723. struct v9fs_fcall **rc)
  724. {
  725. int err;
  726. unsigned long flags;
  727. struct v9fs_req *req;
  728. struct v9fs_mux_rpc r;
  729. r.err = 0;
  730. r.rcall = NULL;
  731. r.m = m;
  732. init_waitqueue_head(&r.wqueue);
  733. if (rc)
  734. *rc = NULL;
  735. req = v9fs_send_request(m, tc, v9fs_mux_rpc_cb, &r);
  736. if (IS_ERR(req)) {
  737. err = PTR_ERR(req);
  738. dprintk(DEBUG_MUX, "error %d\n", err);
  739. return PTR_ERR(req);
  740. }
  741. r.req = req;
  742. dprintk(DEBUG_MUX, "mux %p tc %p tag %d rpc %p req %p\n", m, tc,
  743. req->tag, &r, req);
  744. err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
  745. if (r.err < 0)
  746. err = r.err;
  747. if (err == -ERESTARTSYS && m->trans->status == Connected && m->err == 0) {
  748. spin_lock(&m->lock);
  749. req->tcall = NULL;
  750. req->err = ERREQFLUSH;
  751. spin_unlock(&m->lock);
  752. clear_thread_flag(TIF_SIGPENDING);
  753. v9fs_mux_flush_request(m, req);
  754. spin_lock_irqsave(&current->sighand->siglock, flags);
  755. recalc_sigpending();
  756. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  757. }
  758. if (!err) {
  759. if (r.rcall)
  760. dprintk(DEBUG_MUX, "got response id %d tag %d\n",
  761. r.rcall->id, r.rcall->tag);
  762. if (rc)
  763. *rc = r.rcall;
  764. else
  765. kfree(r.rcall);
  766. } else {
  767. kfree(r.rcall);
  768. dprintk(DEBUG_MUX, "got error %d\n", err);
  769. if (err > 0)
  770. err = -EIO;
  771. }
  772. return err;
  773. }
  774. #if 0
  775. /**
  776. * v9fs_mux_rpcnb - sends 9P request without waiting for response.
  777. * @m: mux data
  778. * @tc: request to be sent
  779. * @cb: callback function to be called when response arrives
  780. * @cba: value to pass to the callback function
  781. */
  782. int v9fs_mux_rpcnb(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
  783. v9fs_mux_req_callback cb, void *a)
  784. {
  785. int err;
  786. struct v9fs_req *req;
  787. req = v9fs_send_request(m, tc, cb, a);
  788. if (IS_ERR(req)) {
  789. err = PTR_ERR(req);
  790. dprintk(DEBUG_MUX, "error %d\n", err);
  791. return PTR_ERR(req);
  792. }
  793. dprintk(DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
  794. return 0;
  795. }
  796. #endif /* 0 */
  797. /**
  798. * v9fs_mux_cancel - cancel all pending requests with error
  799. * @m: mux data
  800. * @err: error code
  801. */
  802. void v9fs_mux_cancel(struct v9fs_mux_data *m, int err)
  803. {
  804. struct v9fs_req *req, *rtmp;
  805. LIST_HEAD(cancel_list);
  806. dprintk(DEBUG_MUX, "mux %p err %d\n", m, err);
  807. m->err = err;
  808. spin_lock(&m->lock);
  809. list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
  810. list_move(&req->req_list, &cancel_list);
  811. }
  812. spin_unlock(&m->lock);
  813. list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
  814. list_del(&req->req_list);
  815. if (!req->err)
  816. req->err = err;
  817. if (req->cb)
  818. (*req->cb) (req->cba, req->tcall, req->rcall, req->err);
  819. else
  820. kfree(req->rcall);
  821. kfree(req);
  822. }
  823. wake_up(&m->equeue);
  824. }
  825. static u16 v9fs_mux_get_tag(struct v9fs_mux_data *m)
  826. {
  827. int tag;
  828. tag = v9fs_get_idpool(&m->tagpool);
  829. if (tag < 0)
  830. return V9FS_NOTAG;
  831. else
  832. return (u16) tag;
  833. }
  834. static void v9fs_mux_put_tag(struct v9fs_mux_data *m, u16 tag)
  835. {
  836. if (tag != V9FS_NOTAG && v9fs_check_idpool(tag, &m->tagpool))
  837. v9fs_put_idpool(tag, &m->tagpool);
  838. }