mux.c 23 KB

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