rxrpc.c 20 KB

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