rxrpc.c 20 KB

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