rxrpc.c 20 KB

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