rxrpc.c 20 KB

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