mux.c 23 KB

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