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