mux.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. send_sig(SIGKILL, vpt->task, 1);
  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. allow_signal(SIGKILL);
  383. while (!kthread_should_stop()) {
  384. set_current_state(TASK_INTERRUPTIBLE);
  385. if (signal_pending(current))
  386. break;
  387. list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) {
  388. v9fs_poll_mux(m);
  389. }
  390. dprintk(DEBUG_MUX, "sleeping...\n");
  391. schedule_timeout(SCHED_TIMEOUT * HZ);
  392. }
  393. __set_current_state(TASK_RUNNING);
  394. dprintk(DEBUG_MUX, "finish\n");
  395. return 0;
  396. }
  397. /**
  398. * v9fs_write_work - called when a transport can send some data
  399. */
  400. static void v9fs_write_work(struct work_struct *work)
  401. {
  402. int n, err;
  403. struct v9fs_mux_data *m;
  404. struct v9fs_req *req;
  405. m = container_of(work, struct v9fs_mux_data, wq);
  406. if (m->err < 0) {
  407. clear_bit(Wworksched, &m->wsched);
  408. return;
  409. }
  410. if (!m->wsize) {
  411. if (list_empty(&m->unsent_req_list)) {
  412. clear_bit(Wworksched, &m->wsched);
  413. return;
  414. }
  415. spin_lock(&m->lock);
  416. again:
  417. req = list_entry(m->unsent_req_list.next, struct v9fs_req,
  418. req_list);
  419. list_move_tail(&req->req_list, &m->req_list);
  420. if (req->err == ERREQFLUSH)
  421. goto again;
  422. m->wbuf = req->tcall->sdata;
  423. m->wsize = req->tcall->size;
  424. m->wpos = 0;
  425. dump_data(m->wbuf, m->wsize);
  426. spin_unlock(&m->lock);
  427. }
  428. dprintk(DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, m->wsize);
  429. clear_bit(Wpending, &m->wsched);
  430. err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos);
  431. dprintk(DEBUG_MUX, "mux %p sent %d bytes\n", m, err);
  432. if (err == -EAGAIN) {
  433. clear_bit(Wworksched, &m->wsched);
  434. return;
  435. }
  436. if (err <= 0)
  437. goto error;
  438. m->wpos += err;
  439. if (m->wpos == m->wsize)
  440. m->wpos = m->wsize = 0;
  441. if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) {
  442. if (test_and_clear_bit(Wpending, &m->wsched))
  443. n = POLLOUT;
  444. else
  445. n = m->trans->poll(m->trans, NULL);
  446. if (n & POLLOUT) {
  447. dprintk(DEBUG_MUX, "schedule write work mux %p\n", m);
  448. queue_work(v9fs_mux_wq, &m->wq);
  449. } else
  450. clear_bit(Wworksched, &m->wsched);
  451. } else
  452. clear_bit(Wworksched, &m->wsched);
  453. return;
  454. error:
  455. v9fs_mux_cancel(m, err);
  456. clear_bit(Wworksched, &m->wsched);
  457. }
  458. static void process_request(struct v9fs_mux_data *m, struct v9fs_req *req)
  459. {
  460. int ecode;
  461. struct v9fs_str *ename;
  462. if (!req->err && req->rcall->id == RERROR) {
  463. ecode = req->rcall->params.rerror.errno;
  464. ename = &req->rcall->params.rerror.error;
  465. dprintk(DEBUG_MUX, "Rerror %.*s\n", ename->len, ename->str);
  466. if (*m->extended)
  467. req->err = -ecode;
  468. if (!req->err) {
  469. req->err = v9fs_errstr2errno(ename->str, ename->len);
  470. if (!req->err) { /* string match failed */
  471. PRINT_FCALL_ERROR("unknown error", req->rcall);
  472. }
  473. if (!req->err)
  474. req->err = -ESERVERFAULT;
  475. }
  476. } else if (req->tcall && req->rcall->id != req->tcall->id + 1) {
  477. dprintk(DEBUG_ERROR, "fcall mismatch: expected %d, got %d\n",
  478. req->tcall->id + 1, req->rcall->id);
  479. if (!req->err)
  480. req->err = -EIO;
  481. }
  482. }
  483. /**
  484. * v9fs_read_work - called when there is some data to be read from a transport
  485. */
  486. static void v9fs_read_work(struct work_struct *work)
  487. {
  488. int n, err;
  489. struct v9fs_mux_data *m;
  490. struct v9fs_req *req, *rptr, *rreq;
  491. struct v9fs_fcall *rcall;
  492. char *rbuf;
  493. m = container_of(work, struct v9fs_mux_data, rq);
  494. if (m->err < 0)
  495. return;
  496. rcall = NULL;
  497. dprintk(DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos);
  498. if (!m->rcall) {
  499. m->rcall =
  500. kmalloc(sizeof(struct v9fs_fcall) + m->msize, GFP_KERNEL);
  501. if (!m->rcall) {
  502. err = -ENOMEM;
  503. goto error;
  504. }
  505. m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall);
  506. m->rpos = 0;
  507. }
  508. clear_bit(Rpending, &m->wsched);
  509. err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos);
  510. dprintk(DEBUG_MUX, "mux %p got %d bytes\n", m, err);
  511. if (err == -EAGAIN) {
  512. clear_bit(Rworksched, &m->wsched);
  513. return;
  514. }
  515. if (err <= 0)
  516. goto error;
  517. m->rpos += err;
  518. while (m->rpos > 4) {
  519. n = le32_to_cpu(*(__le32 *) m->rbuf);
  520. if (n >= m->msize) {
  521. dprintk(DEBUG_ERROR,
  522. "requested packet size too big: %d\n", n);
  523. err = -EIO;
  524. goto error;
  525. }
  526. if (m->rpos < n)
  527. break;
  528. dump_data(m->rbuf, n);
  529. err =
  530. v9fs_deserialize_fcall(m->rbuf, n, m->rcall, *m->extended);
  531. if (err < 0) {
  532. goto error;
  533. }
  534. if ((v9fs_debug_level&DEBUG_FCALL) == DEBUG_FCALL) {
  535. char buf[150];
  536. v9fs_printfcall(buf, sizeof(buf), m->rcall,
  537. *m->extended);
  538. printk(KERN_NOTICE ">>> %p %s\n", m, buf);
  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. if (req->flush != Flushing)
  565. list_del(&req->req_list);
  566. break;
  567. }
  568. }
  569. spin_unlock(&m->lock);
  570. if (req) {
  571. req->rcall = rcall;
  572. process_request(m, req);
  573. if (req->flush != Flushing) {
  574. if (req->cb)
  575. (*req->cb) (req, req->cba);
  576. else
  577. kfree(req->rcall);
  578. wake_up(&m->equeue);
  579. }
  580. } else {
  581. if (err >= 0 && rcall->id != RFLUSH)
  582. dprintk(DEBUG_ERROR,
  583. "unexpected response mux %p id %d tag %d\n",
  584. m, rcall->id, rcall->tag);
  585. kfree(rcall);
  586. }
  587. }
  588. if (!list_empty(&m->req_list)) {
  589. if (test_and_clear_bit(Rpending, &m->wsched))
  590. n = POLLIN;
  591. else
  592. n = m->trans->poll(m->trans, NULL);
  593. if (n & POLLIN) {
  594. dprintk(DEBUG_MUX, "schedule read work mux %p\n", m);
  595. queue_work(v9fs_mux_wq, &m->rq);
  596. } else
  597. clear_bit(Rworksched, &m->wsched);
  598. } else
  599. clear_bit(Rworksched, &m->wsched);
  600. return;
  601. error:
  602. v9fs_mux_cancel(m, err);
  603. clear_bit(Rworksched, &m->wsched);
  604. }
  605. /**
  606. * v9fs_send_request - send 9P request
  607. * The function can sleep until the request is scheduled for sending.
  608. * The function can be interrupted. Return from the function is not
  609. * a guarantee that the request is sent successfully. Can return errors
  610. * that can be retrieved by PTR_ERR macros.
  611. *
  612. * @m: mux data
  613. * @tc: request to be sent
  614. * @cb: callback function to call when response is received
  615. * @cba: parameter to pass to the callback function
  616. */
  617. static struct v9fs_req *v9fs_send_request(struct v9fs_mux_data *m,
  618. struct v9fs_fcall *tc,
  619. v9fs_mux_req_callback cb, void *cba)
  620. {
  621. int n;
  622. struct v9fs_req *req;
  623. dprintk(DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current,
  624. tc, tc->id);
  625. if (m->err < 0)
  626. return ERR_PTR(m->err);
  627. req = kmalloc(sizeof(struct v9fs_req), GFP_KERNEL);
  628. if (!req)
  629. return ERR_PTR(-ENOMEM);
  630. if (tc->id == TVERSION)
  631. n = V9FS_NOTAG;
  632. else
  633. n = v9fs_mux_get_tag(m);
  634. if (n < 0)
  635. return ERR_PTR(-ENOMEM);
  636. v9fs_set_tag(tc, n);
  637. if ((v9fs_debug_level&DEBUG_FCALL) == DEBUG_FCALL) {
  638. char buf[150];
  639. v9fs_printfcall(buf, sizeof(buf), tc, *m->extended);
  640. printk(KERN_NOTICE "<<< %p %s\n", m, buf);
  641. }
  642. spin_lock_init(&req->lock);
  643. req->tag = n;
  644. req->tcall = tc;
  645. req->rcall = NULL;
  646. req->err = 0;
  647. req->cb = cb;
  648. req->cba = cba;
  649. req->flush = None;
  650. spin_lock(&m->lock);
  651. list_add_tail(&req->req_list, &m->unsent_req_list);
  652. spin_unlock(&m->lock);
  653. if (test_and_clear_bit(Wpending, &m->wsched))
  654. n = POLLOUT;
  655. else
  656. n = m->trans->poll(m->trans, NULL);
  657. if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched))
  658. queue_work(v9fs_mux_wq, &m->wq);
  659. return req;
  660. }
  661. static void v9fs_mux_free_request(struct v9fs_mux_data *m, struct v9fs_req *req)
  662. {
  663. v9fs_mux_put_tag(m, req->tag);
  664. kfree(req);
  665. }
  666. static void v9fs_mux_flush_cb(struct v9fs_req *freq, void *a)
  667. {
  668. v9fs_mux_req_callback cb;
  669. int tag;
  670. struct v9fs_mux_data *m;
  671. struct v9fs_req *req, *rreq, *rptr;
  672. m = a;
  673. dprintk(DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m,
  674. freq->tcall, freq->rcall, freq->err,
  675. freq->tcall->params.tflush.oldtag);
  676. spin_lock(&m->lock);
  677. cb = NULL;
  678. tag = freq->tcall->params.tflush.oldtag;
  679. req = NULL;
  680. list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) {
  681. if (rreq->tag == tag) {
  682. req = rreq;
  683. list_del(&req->req_list);
  684. break;
  685. }
  686. }
  687. spin_unlock(&m->lock);
  688. if (req) {
  689. spin_lock(&req->lock);
  690. req->flush = Flushed;
  691. spin_unlock(&req->lock);
  692. if (req->cb)
  693. (*req->cb) (req, req->cba);
  694. else
  695. kfree(req->rcall);
  696. wake_up(&m->equeue);
  697. }
  698. kfree(freq->tcall);
  699. kfree(freq->rcall);
  700. v9fs_mux_free_request(m, freq);
  701. }
  702. static int
  703. v9fs_mux_flush_request(struct v9fs_mux_data *m, struct v9fs_req *req)
  704. {
  705. struct v9fs_fcall *fc;
  706. struct v9fs_req *rreq, *rptr;
  707. dprintk(DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag);
  708. /* if a response was received for a request, do nothing */
  709. spin_lock(&req->lock);
  710. if (req->rcall || req->err) {
  711. spin_unlock(&req->lock);
  712. dprintk(DEBUG_MUX, "mux %p req %p response already received\n", m, req);
  713. return 0;
  714. }
  715. req->flush = Flushing;
  716. spin_unlock(&req->lock);
  717. spin_lock(&m->lock);
  718. /* if the request is not sent yet, just remove it from the list */
  719. list_for_each_entry_safe(rreq, rptr, &m->unsent_req_list, req_list) {
  720. if (rreq->tag == req->tag) {
  721. dprintk(DEBUG_MUX, "mux %p req %p request is not sent yet\n", m, req);
  722. list_del(&rreq->req_list);
  723. req->flush = Flushed;
  724. spin_unlock(&m->lock);
  725. if (req->cb)
  726. (*req->cb) (req, req->cba);
  727. return 0;
  728. }
  729. }
  730. spin_unlock(&m->lock);
  731. clear_thread_flag(TIF_SIGPENDING);
  732. fc = v9fs_create_tflush(req->tag);
  733. v9fs_send_request(m, fc, v9fs_mux_flush_cb, m);
  734. return 1;
  735. }
  736. static void
  737. v9fs_mux_rpc_cb(struct v9fs_req *req, void *a)
  738. {
  739. struct v9fs_mux_rpc *r;
  740. dprintk(DEBUG_MUX, "req %p r %p\n", req, a);
  741. r = a;
  742. r->rcall = req->rcall;
  743. r->err = req->err;
  744. if (req->flush!=None && !req->err)
  745. r->err = -ERESTARTSYS;
  746. wake_up(&r->wqueue);
  747. }
  748. /**
  749. * v9fs_mux_rpc - sends 9P request and waits until a response is available.
  750. * The function can be interrupted.
  751. * @m: mux data
  752. * @tc: request to be sent
  753. * @rc: pointer where a pointer to the response is stored
  754. */
  755. int
  756. v9fs_mux_rpc(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
  757. struct v9fs_fcall **rc)
  758. {
  759. int err, sigpending;
  760. unsigned long flags;
  761. struct v9fs_req *req;
  762. struct v9fs_mux_rpc r;
  763. r.err = 0;
  764. r.tcall = tc;
  765. r.rcall = NULL;
  766. r.m = m;
  767. init_waitqueue_head(&r.wqueue);
  768. if (rc)
  769. *rc = NULL;
  770. sigpending = 0;
  771. if (signal_pending(current)) {
  772. sigpending = 1;
  773. clear_thread_flag(TIF_SIGPENDING);
  774. }
  775. req = v9fs_send_request(m, tc, v9fs_mux_rpc_cb, &r);
  776. if (IS_ERR(req)) {
  777. err = PTR_ERR(req);
  778. dprintk(DEBUG_MUX, "error %d\n", err);
  779. return err;
  780. }
  781. err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0);
  782. if (r.err < 0)
  783. err = r.err;
  784. if (err == -ERESTARTSYS && m->trans->status == Connected && m->err == 0) {
  785. if (v9fs_mux_flush_request(m, req)) {
  786. /* wait until we get response of the flush message */
  787. do {
  788. clear_thread_flag(TIF_SIGPENDING);
  789. err = wait_event_interruptible(r.wqueue,
  790. r.rcall || r.err);
  791. } while (!r.rcall && !r.err && err==-ERESTARTSYS &&
  792. m->trans->status==Connected && !m->err);
  793. err = -ERESTARTSYS;
  794. }
  795. sigpending = 1;
  796. }
  797. if (sigpending) {
  798. spin_lock_irqsave(&current->sighand->siglock, flags);
  799. recalc_sigpending();
  800. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  801. }
  802. if (rc)
  803. *rc = r.rcall;
  804. else
  805. kfree(r.rcall);
  806. v9fs_mux_free_request(m, req);
  807. if (err > 0)
  808. err = -EIO;
  809. return err;
  810. }
  811. #if 0
  812. /**
  813. * v9fs_mux_rpcnb - sends 9P request without waiting for response.
  814. * @m: mux data
  815. * @tc: request to be sent
  816. * @cb: callback function to be called when response arrives
  817. * @cba: value to pass to the callback function
  818. */
  819. int v9fs_mux_rpcnb(struct v9fs_mux_data *m, struct v9fs_fcall *tc,
  820. v9fs_mux_req_callback cb, void *a)
  821. {
  822. int err;
  823. struct v9fs_req *req;
  824. req = v9fs_send_request(m, tc, cb, a);
  825. if (IS_ERR(req)) {
  826. err = PTR_ERR(req);
  827. dprintk(DEBUG_MUX, "error %d\n", err);
  828. return PTR_ERR(req);
  829. }
  830. dprintk(DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag);
  831. return 0;
  832. }
  833. #endif /* 0 */
  834. /**
  835. * v9fs_mux_cancel - cancel all pending requests with error
  836. * @m: mux data
  837. * @err: error code
  838. */
  839. void v9fs_mux_cancel(struct v9fs_mux_data *m, int err)
  840. {
  841. struct v9fs_req *req, *rtmp;
  842. LIST_HEAD(cancel_list);
  843. dprintk(DEBUG_ERROR, "mux %p err %d\n", m, err);
  844. m->err = err;
  845. spin_lock(&m->lock);
  846. list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) {
  847. list_move(&req->req_list, &cancel_list);
  848. }
  849. list_for_each_entry_safe(req, rtmp, &m->unsent_req_list, req_list) {
  850. list_move(&req->req_list, &cancel_list);
  851. }
  852. spin_unlock(&m->lock);
  853. list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) {
  854. list_del(&req->req_list);
  855. if (!req->err)
  856. req->err = err;
  857. if (req->cb)
  858. (*req->cb) (req, req->cba);
  859. else
  860. kfree(req->rcall);
  861. }
  862. wake_up(&m->equeue);
  863. }
  864. static u16 v9fs_mux_get_tag(struct v9fs_mux_data *m)
  865. {
  866. int tag;
  867. tag = v9fs_get_idpool(&m->tagpool);
  868. if (tag < 0)
  869. return V9FS_NOTAG;
  870. else
  871. return (u16) tag;
  872. }
  873. static void v9fs_mux_put_tag(struct v9fs_mux_data *m, u16 tag)
  874. {
  875. if (tag != V9FS_NOTAG && v9fs_check_idpool(tag, &m->tagpool))
  876. v9fs_put_idpool(tag, &m->tagpool);
  877. }