rxrpc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /* Maintain an RxRPC server socket to do AFS communications through
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/slab.h>
  12. #include <net/sock.h>
  13. #include <net/af_rxrpc.h>
  14. #include <rxrpc/packet.h>
  15. #include "internal.h"
  16. #include "afs_cm.h"
  17. static struct socket *afs_socket; /* my RxRPC socket */
  18. static struct workqueue_struct *afs_async_calls;
  19. static atomic_t afs_outstanding_calls;
  20. static atomic_t afs_outstanding_skbs;
  21. static void afs_wake_up_call_waiter(struct afs_call *);
  22. static int afs_wait_for_call_to_complete(struct afs_call *);
  23. static void afs_wake_up_async_call(struct afs_call *);
  24. static int afs_dont_wait_for_call_to_complete(struct afs_call *);
  25. static void afs_process_async_call(struct afs_call *);
  26. static void afs_rx_interceptor(struct sock *, unsigned long, struct sk_buff *);
  27. static int afs_deliver_cm_op_id(struct afs_call *, struct sk_buff *, bool);
  28. /* synchronous call management */
  29. const struct afs_wait_mode afs_sync_call = {
  30. .rx_wakeup = afs_wake_up_call_waiter,
  31. .wait = afs_wait_for_call_to_complete,
  32. };
  33. /* asynchronous call management */
  34. const struct afs_wait_mode afs_async_call = {
  35. .rx_wakeup = afs_wake_up_async_call,
  36. .wait = afs_dont_wait_for_call_to_complete,
  37. };
  38. /* asynchronous incoming call management */
  39. static const struct afs_wait_mode afs_async_incoming_call = {
  40. .rx_wakeup = afs_wake_up_async_call,
  41. };
  42. /* asynchronous incoming call initial processing */
  43. static const struct afs_call_type afs_RXCMxxxx = {
  44. .name = "CB.xxxx",
  45. .deliver = afs_deliver_cm_op_id,
  46. .abort_to_error = afs_abort_to_error,
  47. };
  48. static void afs_collect_incoming_call(struct work_struct *);
  49. static struct sk_buff_head afs_incoming_calls;
  50. static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
  51. static void afs_async_workfn(struct work_struct *work)
  52. {
  53. struct afs_call *call = container_of(work, struct afs_call, async_work);
  54. call->async_workfn(call);
  55. }
  56. static int afs_wait_atomic_t(atomic_t *p)
  57. {
  58. schedule();
  59. return 0;
  60. }
  61. /*
  62. * open an RxRPC socket and bind it to be a server for callback notifications
  63. * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
  64. */
  65. int afs_open_socket(void)
  66. {
  67. struct sockaddr_rxrpc srx;
  68. struct socket *socket;
  69. int ret;
  70. _enter("");
  71. skb_queue_head_init(&afs_incoming_calls);
  72. ret = -ENOMEM;
  73. afs_async_calls = create_singlethread_workqueue("kafsd");
  74. if (!afs_async_calls)
  75. goto error_0;
  76. ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
  77. if (ret < 0)
  78. goto error_1;
  79. socket->sk->sk_allocation = GFP_NOFS;
  80. /* bind the callback manager's address to make this a server socket */
  81. srx.srx_family = AF_RXRPC;
  82. srx.srx_service = CM_SERVICE;
  83. srx.transport_type = SOCK_DGRAM;
  84. srx.transport_len = sizeof(srx.transport.sin);
  85. srx.transport.sin.sin_family = AF_INET;
  86. srx.transport.sin.sin_port = htons(AFS_CM_PORT);
  87. memset(&srx.transport.sin.sin_addr, 0,
  88. sizeof(srx.transport.sin.sin_addr));
  89. ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
  90. if (ret < 0)
  91. goto error_2;
  92. ret = kernel_listen(socket, INT_MAX);
  93. if (ret < 0)
  94. goto error_2;
  95. rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
  96. afs_socket = socket;
  97. _leave(" = 0");
  98. return 0;
  99. error_2:
  100. sock_release(socket);
  101. error_1:
  102. destroy_workqueue(afs_async_calls);
  103. error_0:
  104. _leave(" = %d", ret);
  105. return ret;
  106. }
  107. /*
  108. * close the RxRPC socket AFS was using
  109. */
  110. void afs_close_socket(void)
  111. {
  112. _enter("");
  113. wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
  114. TASK_UNINTERRUPTIBLE);
  115. _debug("no outstanding calls");
  116. sock_release(afs_socket);
  117. _debug("dework");
  118. destroy_workqueue(afs_async_calls);
  119. ASSERTCMP(atomic_read(&afs_outstanding_skbs), ==, 0);
  120. _leave("");
  121. }
  122. /*
  123. * Note that the data in a socket buffer is now consumed.
  124. */
  125. void afs_data_consumed(struct afs_call *call, struct sk_buff *skb)
  126. {
  127. if (!skb) {
  128. _debug("DLVR NULL [%d]", atomic_read(&afs_outstanding_skbs));
  129. dump_stack();
  130. } else {
  131. _debug("DLVR %p{%u} [%d]",
  132. skb, skb->mark, atomic_read(&afs_outstanding_skbs));
  133. rxrpc_kernel_data_consumed(call->rxcall, skb);
  134. }
  135. }
  136. /*
  137. * free a socket buffer
  138. */
  139. static void afs_free_skb(struct sk_buff *skb)
  140. {
  141. if (!skb) {
  142. _debug("FREE NULL [%d]", atomic_read(&afs_outstanding_skbs));
  143. dump_stack();
  144. } else {
  145. _debug("FREE %p{%u} [%d]",
  146. skb, skb->mark, atomic_read(&afs_outstanding_skbs));
  147. if (atomic_dec_return(&afs_outstanding_skbs) == -1)
  148. BUG();
  149. rxrpc_kernel_free_skb(skb);
  150. }
  151. }
  152. /*
  153. * free a call
  154. */
  155. static void afs_free_call(struct afs_call *call)
  156. {
  157. _debug("DONE %p{%s} [%d]",
  158. call, call->type->name, atomic_read(&afs_outstanding_calls));
  159. ASSERTCMP(call->rxcall, ==, NULL);
  160. ASSERT(!work_pending(&call->async_work));
  161. ASSERT(skb_queue_empty(&call->rx_queue));
  162. ASSERT(call->type->name != NULL);
  163. kfree(call->request);
  164. kfree(call);
  165. if (atomic_dec_and_test(&afs_outstanding_calls))
  166. wake_up_atomic_t(&afs_outstanding_calls);
  167. }
  168. /*
  169. * End a call but do not free it
  170. */
  171. static void afs_end_call_nofree(struct afs_call *call)
  172. {
  173. if (call->rxcall) {
  174. rxrpc_kernel_end_call(call->rxcall);
  175. call->rxcall = NULL;
  176. }
  177. if (call->type->destructor)
  178. call->type->destructor(call);
  179. }
  180. /*
  181. * End a call and free it
  182. */
  183. static void afs_end_call(struct afs_call *call)
  184. {
  185. afs_end_call_nofree(call);
  186. afs_free_call(call);
  187. }
  188. /*
  189. * allocate a call with flat request and reply buffers
  190. */
  191. struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
  192. size_t request_size, size_t reply_size)
  193. {
  194. struct afs_call *call;
  195. call = kzalloc(sizeof(*call), GFP_NOFS);
  196. if (!call)
  197. goto nomem_call;
  198. _debug("CALL %p{%s} [%d]",
  199. call, type->name, atomic_read(&afs_outstanding_calls));
  200. atomic_inc(&afs_outstanding_calls);
  201. call->type = type;
  202. call->request_size = request_size;
  203. call->reply_max = reply_size;
  204. if (request_size) {
  205. call->request = kmalloc(request_size, GFP_NOFS);
  206. if (!call->request)
  207. goto nomem_free;
  208. }
  209. if (reply_size) {
  210. call->buffer = kmalloc(reply_size, GFP_NOFS);
  211. if (!call->buffer)
  212. goto nomem_free;
  213. }
  214. init_waitqueue_head(&call->waitq);
  215. skb_queue_head_init(&call->rx_queue);
  216. return call;
  217. nomem_free:
  218. afs_free_call(call);
  219. nomem_call:
  220. return NULL;
  221. }
  222. /*
  223. * clean up a call with flat buffer
  224. */
  225. void afs_flat_call_destructor(struct afs_call *call)
  226. {
  227. _enter("");
  228. kfree(call->request);
  229. call->request = NULL;
  230. kfree(call->buffer);
  231. call->buffer = NULL;
  232. }
  233. /*
  234. * attach the data from a bunch of pages on an inode to a call
  235. */
  236. static int afs_send_pages(struct afs_call *call, struct msghdr *msg,
  237. struct kvec *iov)
  238. {
  239. struct page *pages[8];
  240. unsigned count, n, loop, offset, to;
  241. pgoff_t first = call->first, last = call->last;
  242. int ret;
  243. _enter("");
  244. offset = call->first_offset;
  245. call->first_offset = 0;
  246. do {
  247. _debug("attach %lx-%lx", first, last);
  248. count = last - first + 1;
  249. if (count > ARRAY_SIZE(pages))
  250. count = ARRAY_SIZE(pages);
  251. n = find_get_pages_contig(call->mapping, first, count, pages);
  252. ASSERTCMP(n, ==, count);
  253. loop = 0;
  254. do {
  255. msg->msg_flags = 0;
  256. to = PAGE_SIZE;
  257. if (first + loop >= last)
  258. to = call->last_to;
  259. else
  260. msg->msg_flags = MSG_MORE;
  261. iov->iov_base = kmap(pages[loop]) + offset;
  262. iov->iov_len = to - offset;
  263. offset = 0;
  264. _debug("- range %u-%u%s",
  265. offset, to, msg->msg_flags ? " [more]" : "");
  266. iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC,
  267. iov, 1, to - offset);
  268. /* have to change the state *before* sending the last
  269. * packet as RxRPC might give us the reply before it
  270. * returns from sending the request */
  271. if (first + loop >= last)
  272. call->state = AFS_CALL_AWAIT_REPLY;
  273. ret = rxrpc_kernel_send_data(call->rxcall, msg,
  274. to - offset);
  275. kunmap(pages[loop]);
  276. if (ret < 0)
  277. break;
  278. } while (++loop < count);
  279. first += count;
  280. for (loop = 0; loop < count; loop++)
  281. put_page(pages[loop]);
  282. if (ret < 0)
  283. break;
  284. } while (first <= last);
  285. _leave(" = %d", ret);
  286. return ret;
  287. }
  288. /*
  289. * initiate a call
  290. */
  291. int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
  292. const struct afs_wait_mode *wait_mode)
  293. {
  294. struct sockaddr_rxrpc srx;
  295. struct rxrpc_call *rxcall;
  296. struct msghdr msg;
  297. struct kvec iov[1];
  298. int ret;
  299. struct sk_buff *skb;
  300. _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
  301. ASSERT(call->type != NULL);
  302. ASSERT(call->type->name != NULL);
  303. _debug("____MAKE %p{%s,%x} [%d]____",
  304. call, call->type->name, key_serial(call->key),
  305. atomic_read(&afs_outstanding_calls));
  306. call->wait_mode = wait_mode;
  307. call->async_workfn = afs_process_async_call;
  308. INIT_WORK(&call->async_work, afs_async_workfn);
  309. memset(&srx, 0, sizeof(srx));
  310. srx.srx_family = AF_RXRPC;
  311. srx.srx_service = call->service_id;
  312. srx.transport_type = SOCK_DGRAM;
  313. srx.transport_len = sizeof(srx.transport.sin);
  314. srx.transport.sin.sin_family = AF_INET;
  315. srx.transport.sin.sin_port = call->port;
  316. memcpy(&srx.transport.sin.sin_addr, addr, 4);
  317. /* create a call */
  318. rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
  319. (unsigned long) call, gfp);
  320. call->key = NULL;
  321. if (IS_ERR(rxcall)) {
  322. ret = PTR_ERR(rxcall);
  323. goto error_kill_call;
  324. }
  325. call->rxcall = rxcall;
  326. /* send the request */
  327. iov[0].iov_base = call->request;
  328. iov[0].iov_len = call->request_size;
  329. msg.msg_name = NULL;
  330. msg.msg_namelen = 0;
  331. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
  332. call->request_size);
  333. msg.msg_control = NULL;
  334. msg.msg_controllen = 0;
  335. msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
  336. /* have to change the state *before* sending the last packet as RxRPC
  337. * might give us the reply before it returns from sending the
  338. * request */
  339. if (!call->send_pages)
  340. call->state = AFS_CALL_AWAIT_REPLY;
  341. ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
  342. if (ret < 0)
  343. goto error_do_abort;
  344. if (call->send_pages) {
  345. ret = afs_send_pages(call, &msg, iov);
  346. if (ret < 0)
  347. goto error_do_abort;
  348. }
  349. /* at this point, an async call may no longer exist as it may have
  350. * already completed */
  351. return wait_mode->wait(call);
  352. error_do_abort:
  353. rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
  354. while ((skb = skb_dequeue(&call->rx_queue)))
  355. afs_free_skb(skb);
  356. error_kill_call:
  357. afs_end_call(call);
  358. _leave(" = %d", ret);
  359. return ret;
  360. }
  361. /*
  362. * Handles intercepted messages that were arriving in the socket's Rx queue.
  363. *
  364. * Called from the AF_RXRPC call processor in waitqueue process context. For
  365. * each call, it is guaranteed this will be called in order of packet to be
  366. * delivered.
  367. */
  368. static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
  369. struct sk_buff *skb)
  370. {
  371. struct afs_call *call = (struct afs_call *) user_call_ID;
  372. _enter("%p,,%u", call, skb->mark);
  373. _debug("ICPT %p{%u} [%d]",
  374. skb, skb->mark, atomic_read(&afs_outstanding_skbs));
  375. ASSERTCMP(sk, ==, afs_socket->sk);
  376. atomic_inc(&afs_outstanding_skbs);
  377. if (!call) {
  378. /* its an incoming call for our callback service */
  379. skb_queue_tail(&afs_incoming_calls, skb);
  380. queue_work(afs_wq, &afs_collect_incoming_call_work);
  381. } else {
  382. /* route the messages directly to the appropriate call */
  383. skb_queue_tail(&call->rx_queue, skb);
  384. call->wait_mode->rx_wakeup(call);
  385. }
  386. _leave("");
  387. }
  388. /*
  389. * deliver messages to a call
  390. */
  391. static void afs_deliver_to_call(struct afs_call *call)
  392. {
  393. struct sk_buff *skb;
  394. bool last;
  395. u32 abort_code;
  396. int ret;
  397. _enter("");
  398. while ((call->state == AFS_CALL_AWAIT_REPLY ||
  399. call->state == AFS_CALL_AWAIT_OP_ID ||
  400. call->state == AFS_CALL_AWAIT_REQUEST ||
  401. call->state == AFS_CALL_AWAIT_ACK) &&
  402. (skb = skb_dequeue(&call->rx_queue))) {
  403. switch (skb->mark) {
  404. case RXRPC_SKB_MARK_DATA:
  405. _debug("Rcv DATA");
  406. last = rxrpc_kernel_is_data_last(skb);
  407. ret = call->type->deliver(call, skb, last);
  408. switch (ret) {
  409. case -EAGAIN:
  410. if (last) {
  411. _debug("short data");
  412. goto unmarshal_error;
  413. }
  414. break;
  415. case 0:
  416. ASSERT(last);
  417. if (call->state == AFS_CALL_AWAIT_REPLY)
  418. call->state = AFS_CALL_COMPLETE;
  419. break;
  420. case -ENOTCONN:
  421. abort_code = RX_CALL_DEAD;
  422. goto do_abort;
  423. case -ENOTSUPP:
  424. abort_code = RX_INVALID_OPERATION;
  425. goto do_abort;
  426. default:
  427. unmarshal_error:
  428. abort_code = RXGEN_CC_UNMARSHAL;
  429. if (call->state != AFS_CALL_AWAIT_REPLY)
  430. abort_code = RXGEN_SS_UNMARSHAL;
  431. do_abort:
  432. rxrpc_kernel_abort_call(call->rxcall,
  433. abort_code);
  434. call->error = ret;
  435. call->state = AFS_CALL_ERROR;
  436. break;
  437. }
  438. break;
  439. case RXRPC_SKB_MARK_FINAL_ACK:
  440. _debug("Rcv ACK");
  441. call->state = AFS_CALL_COMPLETE;
  442. break;
  443. case RXRPC_SKB_MARK_BUSY:
  444. _debug("Rcv BUSY");
  445. call->error = -EBUSY;
  446. call->state = AFS_CALL_BUSY;
  447. break;
  448. case RXRPC_SKB_MARK_REMOTE_ABORT:
  449. abort_code = rxrpc_kernel_get_abort_code(skb);
  450. call->error = call->type->abort_to_error(abort_code);
  451. call->state = AFS_CALL_ABORTED;
  452. _debug("Rcv ABORT %u -> %d", abort_code, call->error);
  453. break;
  454. case RXRPC_SKB_MARK_LOCAL_ABORT:
  455. abort_code = rxrpc_kernel_get_abort_code(skb);
  456. call->error = call->type->abort_to_error(abort_code);
  457. call->state = AFS_CALL_ABORTED;
  458. _debug("Loc ABORT %u -> %d", abort_code, call->error);
  459. break;
  460. case RXRPC_SKB_MARK_NET_ERROR:
  461. call->error = -rxrpc_kernel_get_error_number(skb);
  462. call->state = AFS_CALL_ERROR;
  463. _debug("Rcv NET ERROR %d", call->error);
  464. break;
  465. case RXRPC_SKB_MARK_LOCAL_ERROR:
  466. call->error = -rxrpc_kernel_get_error_number(skb);
  467. call->state = AFS_CALL_ERROR;
  468. _debug("Rcv LOCAL ERROR %d", call->error);
  469. break;
  470. default:
  471. BUG();
  472. break;
  473. }
  474. afs_free_skb(skb);
  475. }
  476. /* make sure the queue is empty if the call is done with (we might have
  477. * aborted the call early because of an unmarshalling error) */
  478. if (call->state >= AFS_CALL_COMPLETE) {
  479. while ((skb = skb_dequeue(&call->rx_queue)))
  480. afs_free_skb(skb);
  481. if (call->incoming)
  482. afs_end_call(call);
  483. }
  484. _leave("");
  485. }
  486. /*
  487. * wait synchronously for a call to complete
  488. */
  489. static int afs_wait_for_call_to_complete(struct afs_call *call)
  490. {
  491. struct sk_buff *skb;
  492. int ret;
  493. DECLARE_WAITQUEUE(myself, current);
  494. _enter("");
  495. add_wait_queue(&call->waitq, &myself);
  496. for (;;) {
  497. set_current_state(TASK_INTERRUPTIBLE);
  498. /* deliver any messages that are in the queue */
  499. if (!skb_queue_empty(&call->rx_queue)) {
  500. __set_current_state(TASK_RUNNING);
  501. afs_deliver_to_call(call);
  502. continue;
  503. }
  504. ret = call->error;
  505. if (call->state >= AFS_CALL_COMPLETE)
  506. break;
  507. ret = -EINTR;
  508. if (signal_pending(current))
  509. break;
  510. schedule();
  511. }
  512. remove_wait_queue(&call->waitq, &myself);
  513. __set_current_state(TASK_RUNNING);
  514. /* kill the call */
  515. if (call->state < AFS_CALL_COMPLETE) {
  516. _debug("call incomplete");
  517. rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
  518. while ((skb = skb_dequeue(&call->rx_queue)))
  519. afs_free_skb(skb);
  520. }
  521. _debug("call complete");
  522. afs_end_call(call);
  523. _leave(" = %d", ret);
  524. return ret;
  525. }
  526. /*
  527. * wake up a waiting call
  528. */
  529. static void afs_wake_up_call_waiter(struct afs_call *call)
  530. {
  531. wake_up(&call->waitq);
  532. }
  533. /*
  534. * wake up an asynchronous call
  535. */
  536. static void afs_wake_up_async_call(struct afs_call *call)
  537. {
  538. _enter("");
  539. queue_work(afs_async_calls, &call->async_work);
  540. }
  541. /*
  542. * put a call into asynchronous mode
  543. * - mustn't touch the call descriptor as the call my have completed by the
  544. * time we get here
  545. */
  546. static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
  547. {
  548. _enter("");
  549. return -EINPROGRESS;
  550. }
  551. /*
  552. * delete an asynchronous call
  553. */
  554. static void afs_delete_async_call(struct afs_call *call)
  555. {
  556. _enter("");
  557. afs_free_call(call);
  558. _leave("");
  559. }
  560. /*
  561. * perform processing on an asynchronous call
  562. * - on a multiple-thread workqueue this work item may try to run on several
  563. * CPUs at the same time
  564. */
  565. static void afs_process_async_call(struct afs_call *call)
  566. {
  567. _enter("");
  568. if (!skb_queue_empty(&call->rx_queue))
  569. afs_deliver_to_call(call);
  570. if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
  571. if (call->wait_mode->async_complete)
  572. call->wait_mode->async_complete(call->reply,
  573. call->error);
  574. call->reply = NULL;
  575. /* kill the call */
  576. afs_end_call_nofree(call);
  577. /* we can't just delete the call because the work item may be
  578. * queued */
  579. call->async_workfn = afs_delete_async_call;
  580. queue_work(afs_async_calls, &call->async_work);
  581. }
  582. _leave("");
  583. }
  584. /*
  585. * Empty a socket buffer into a flat reply buffer.
  586. */
  587. int afs_transfer_reply(struct afs_call *call, struct sk_buff *skb, bool last)
  588. {
  589. size_t len = skb->len;
  590. if (len > call->reply_max - call->reply_size) {
  591. _leave(" = -EBADMSG [%zu > %u]",
  592. len, call->reply_max - call->reply_size);
  593. return -EBADMSG;
  594. }
  595. if (len > 0) {
  596. if (skb_copy_bits(skb, 0, call->buffer + call->reply_size,
  597. len) < 0)
  598. BUG();
  599. call->reply_size += len;
  600. }
  601. afs_data_consumed(call, skb);
  602. if (!last)
  603. return -EAGAIN;
  604. if (call->reply_size != call->reply_max) {
  605. _leave(" = -EBADMSG [%u != %u]",
  606. call->reply_size, call->reply_max);
  607. return -EBADMSG;
  608. }
  609. return 0;
  610. }
  611. /*
  612. * accept the backlog of incoming calls
  613. */
  614. static void afs_collect_incoming_call(struct work_struct *work)
  615. {
  616. struct rxrpc_call *rxcall;
  617. struct afs_call *call = NULL;
  618. struct sk_buff *skb;
  619. while ((skb = skb_dequeue(&afs_incoming_calls))) {
  620. _debug("new call");
  621. /* don't need the notification */
  622. afs_free_skb(skb);
  623. if (!call) {
  624. call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
  625. if (!call) {
  626. rxrpc_kernel_reject_call(afs_socket);
  627. return;
  628. }
  629. call->async_workfn = afs_process_async_call;
  630. INIT_WORK(&call->async_work, afs_async_workfn);
  631. call->wait_mode = &afs_async_incoming_call;
  632. call->type = &afs_RXCMxxxx;
  633. init_waitqueue_head(&call->waitq);
  634. skb_queue_head_init(&call->rx_queue);
  635. call->state = AFS_CALL_AWAIT_OP_ID;
  636. _debug("CALL %p{%s} [%d]",
  637. call, call->type->name,
  638. atomic_read(&afs_outstanding_calls));
  639. atomic_inc(&afs_outstanding_calls);
  640. }
  641. rxcall = rxrpc_kernel_accept_call(afs_socket,
  642. (unsigned long) call);
  643. if (!IS_ERR(rxcall)) {
  644. call->rxcall = rxcall;
  645. call = NULL;
  646. }
  647. }
  648. if (call)
  649. afs_free_call(call);
  650. }
  651. /*
  652. * Grab the operation ID from an incoming cache manager call. The socket
  653. * buffer is discarded on error or if we don't yet have sufficient data.
  654. */
  655. static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
  656. bool last)
  657. {
  658. size_t len = skb->len;
  659. void *oibuf = (void *) &call->operation_ID;
  660. _enter("{%u},{%zu},%d", call->offset, len, last);
  661. ASSERTCMP(call->offset, <, 4);
  662. /* the operation ID forms the first four bytes of the request data */
  663. len = min_t(size_t, len, 4 - call->offset);
  664. if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
  665. BUG();
  666. if (!pskb_pull(skb, len))
  667. BUG();
  668. call->offset += len;
  669. if (call->offset < 4) {
  670. afs_data_consumed(call, skb);
  671. _leave(" = -EAGAIN");
  672. return -EAGAIN;
  673. }
  674. call->state = AFS_CALL_AWAIT_REQUEST;
  675. /* ask the cache manager to route the call (it'll change the call type
  676. * if successful) */
  677. if (!afs_cm_incoming_call(call))
  678. return -ENOTSUPP;
  679. /* pass responsibility for the remainer of this message off to the
  680. * cache manager op */
  681. return call->type->deliver(call, skb, last);
  682. }
  683. /*
  684. * send an empty reply
  685. */
  686. void afs_send_empty_reply(struct afs_call *call)
  687. {
  688. struct msghdr msg;
  689. _enter("");
  690. msg.msg_name = NULL;
  691. msg.msg_namelen = 0;
  692. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
  693. msg.msg_control = NULL;
  694. msg.msg_controllen = 0;
  695. msg.msg_flags = 0;
  696. call->state = AFS_CALL_AWAIT_ACK;
  697. switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
  698. case 0:
  699. _leave(" [replied]");
  700. return;
  701. case -ENOMEM:
  702. _debug("oom");
  703. rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
  704. default:
  705. afs_end_call(call);
  706. _leave(" [error]");
  707. return;
  708. }
  709. }
  710. /*
  711. * send a simple reply
  712. */
  713. void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
  714. {
  715. struct msghdr msg;
  716. struct kvec iov[1];
  717. int n;
  718. _enter("");
  719. iov[0].iov_base = (void *) buf;
  720. iov[0].iov_len = len;
  721. msg.msg_name = NULL;
  722. msg.msg_namelen = 0;
  723. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
  724. msg.msg_control = NULL;
  725. msg.msg_controllen = 0;
  726. msg.msg_flags = 0;
  727. call->state = AFS_CALL_AWAIT_ACK;
  728. n = rxrpc_kernel_send_data(call->rxcall, &msg, len);
  729. if (n >= 0) {
  730. /* Success */
  731. _leave(" [replied]");
  732. return;
  733. }
  734. if (n == -ENOMEM) {
  735. _debug("oom");
  736. rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
  737. }
  738. afs_end_call(call);
  739. _leave(" [error]");
  740. }
  741. /*
  742. * Extract a piece of data from the received data socket buffers.
  743. */
  744. int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
  745. bool last, void *buf, size_t count)
  746. {
  747. size_t len = skb->len;
  748. _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
  749. ASSERTCMP(call->offset, <, count);
  750. len = min_t(size_t, len, count - call->offset);
  751. if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
  752. !pskb_pull(skb, len))
  753. BUG();
  754. call->offset += len;
  755. if (call->offset < count) {
  756. afs_data_consumed(call, skb);
  757. _leave(" = -EAGAIN");
  758. return -EAGAIN;
  759. }
  760. return 0;
  761. }