rxrpc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 <linux/sched/signal.h>
  13. #include <net/sock.h>
  14. #include <net/af_rxrpc.h>
  15. #include "internal.h"
  16. #include "afs_cm.h"
  17. struct socket *afs_socket; /* my RxRPC socket */
  18. static struct workqueue_struct *afs_async_calls;
  19. static struct afs_call *afs_spare_incoming_call;
  20. atomic_t afs_outstanding_calls;
  21. static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
  22. static int afs_wait_for_call_to_complete(struct afs_call *);
  23. static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
  24. static void afs_process_async_call(struct work_struct *);
  25. static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
  26. static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
  27. static int afs_deliver_cm_op_id(struct afs_call *);
  28. /* asynchronous incoming call initial processing */
  29. static const struct afs_call_type afs_RXCMxxxx = {
  30. .name = "CB.xxxx",
  31. .deliver = afs_deliver_cm_op_id,
  32. .abort_to_error = afs_abort_to_error,
  33. };
  34. static void afs_charge_preallocation(struct work_struct *);
  35. static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
  36. static int afs_wait_atomic_t(atomic_t *p)
  37. {
  38. schedule();
  39. return 0;
  40. }
  41. /*
  42. * open an RxRPC socket and bind it to be a server for callback notifications
  43. * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
  44. */
  45. int afs_open_socket(void)
  46. {
  47. struct sockaddr_rxrpc srx;
  48. struct socket *socket;
  49. int ret;
  50. _enter("");
  51. ret = -ENOMEM;
  52. afs_async_calls = alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0);
  53. if (!afs_async_calls)
  54. goto error_0;
  55. ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
  56. if (ret < 0)
  57. goto error_1;
  58. socket->sk->sk_allocation = GFP_NOFS;
  59. /* bind the callback manager's address to make this a server socket */
  60. srx.srx_family = AF_RXRPC;
  61. srx.srx_service = CM_SERVICE;
  62. srx.transport_type = SOCK_DGRAM;
  63. srx.transport_len = sizeof(srx.transport.sin);
  64. srx.transport.sin.sin_family = AF_INET;
  65. srx.transport.sin.sin_port = htons(AFS_CM_PORT);
  66. memset(&srx.transport.sin.sin_addr, 0,
  67. sizeof(srx.transport.sin.sin_addr));
  68. ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
  69. if (ret < 0)
  70. goto error_2;
  71. rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
  72. afs_rx_discard_new_call);
  73. ret = kernel_listen(socket, INT_MAX);
  74. if (ret < 0)
  75. goto error_2;
  76. afs_socket = socket;
  77. afs_charge_preallocation(NULL);
  78. _leave(" = 0");
  79. return 0;
  80. error_2:
  81. sock_release(socket);
  82. error_1:
  83. destroy_workqueue(afs_async_calls);
  84. error_0:
  85. _leave(" = %d", ret);
  86. return ret;
  87. }
  88. /*
  89. * close the RxRPC socket AFS was using
  90. */
  91. void afs_close_socket(void)
  92. {
  93. _enter("");
  94. kernel_listen(afs_socket, 0);
  95. flush_workqueue(afs_async_calls);
  96. if (afs_spare_incoming_call) {
  97. afs_put_call(afs_spare_incoming_call);
  98. afs_spare_incoming_call = NULL;
  99. }
  100. _debug("outstanding %u", atomic_read(&afs_outstanding_calls));
  101. wait_on_atomic_t(&afs_outstanding_calls, afs_wait_atomic_t,
  102. TASK_UNINTERRUPTIBLE);
  103. _debug("no outstanding calls");
  104. kernel_sock_shutdown(afs_socket, SHUT_RDWR);
  105. flush_workqueue(afs_async_calls);
  106. sock_release(afs_socket);
  107. _debug("dework");
  108. destroy_workqueue(afs_async_calls);
  109. _leave("");
  110. }
  111. /*
  112. * Allocate a call.
  113. */
  114. static struct afs_call *afs_alloc_call(const struct afs_call_type *type,
  115. gfp_t gfp)
  116. {
  117. struct afs_call *call;
  118. int o;
  119. call = kzalloc(sizeof(*call), gfp);
  120. if (!call)
  121. return NULL;
  122. call->type = type;
  123. atomic_set(&call->usage, 1);
  124. INIT_WORK(&call->async_work, afs_process_async_call);
  125. init_waitqueue_head(&call->waitq);
  126. o = atomic_inc_return(&afs_outstanding_calls);
  127. trace_afs_call(call, afs_call_trace_alloc, 1, o,
  128. __builtin_return_address(0));
  129. return call;
  130. }
  131. /*
  132. * Dispose of a reference on a call.
  133. */
  134. void afs_put_call(struct afs_call *call)
  135. {
  136. int n = atomic_dec_return(&call->usage);
  137. int o = atomic_read(&afs_outstanding_calls);
  138. trace_afs_call(call, afs_call_trace_put, n + 1, o,
  139. __builtin_return_address(0));
  140. ASSERTCMP(n, >=, 0);
  141. if (n == 0) {
  142. ASSERT(!work_pending(&call->async_work));
  143. ASSERT(call->type->name != NULL);
  144. if (call->rxcall) {
  145. rxrpc_kernel_end_call(afs_socket, call->rxcall);
  146. call->rxcall = NULL;
  147. }
  148. if (call->type->destructor)
  149. call->type->destructor(call);
  150. kfree(call->request);
  151. kfree(call);
  152. o = atomic_dec_return(&afs_outstanding_calls);
  153. trace_afs_call(call, afs_call_trace_free, 0, o,
  154. __builtin_return_address(0));
  155. if (o == 0)
  156. wake_up_atomic_t(&afs_outstanding_calls);
  157. }
  158. }
  159. /*
  160. * Queue the call for actual work. Returns 0 unconditionally for convenience.
  161. */
  162. int afs_queue_call_work(struct afs_call *call)
  163. {
  164. int u = atomic_inc_return(&call->usage);
  165. trace_afs_call(call, afs_call_trace_work, u,
  166. atomic_read(&afs_outstanding_calls),
  167. __builtin_return_address(0));
  168. INIT_WORK(&call->work, call->type->work);
  169. if (!queue_work(afs_wq, &call->work))
  170. afs_put_call(call);
  171. return 0;
  172. }
  173. /*
  174. * allocate a call with flat request and reply buffers
  175. */
  176. struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
  177. size_t request_size, size_t reply_max)
  178. {
  179. struct afs_call *call;
  180. call = afs_alloc_call(type, GFP_NOFS);
  181. if (!call)
  182. goto nomem_call;
  183. if (request_size) {
  184. call->request_size = request_size;
  185. call->request = kmalloc(request_size, GFP_NOFS);
  186. if (!call->request)
  187. goto nomem_free;
  188. }
  189. if (reply_max) {
  190. call->reply_max = reply_max;
  191. call->buffer = kmalloc(reply_max, GFP_NOFS);
  192. if (!call->buffer)
  193. goto nomem_free;
  194. }
  195. init_waitqueue_head(&call->waitq);
  196. return call;
  197. nomem_free:
  198. afs_put_call(call);
  199. nomem_call:
  200. return NULL;
  201. }
  202. /*
  203. * clean up a call with flat buffer
  204. */
  205. void afs_flat_call_destructor(struct afs_call *call)
  206. {
  207. _enter("");
  208. kfree(call->request);
  209. call->request = NULL;
  210. kfree(call->buffer);
  211. call->buffer = NULL;
  212. }
  213. #define AFS_BVEC_MAX 8
  214. /*
  215. * Load the given bvec with the next few pages.
  216. */
  217. static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
  218. struct bio_vec *bv, pgoff_t first, pgoff_t last,
  219. unsigned offset)
  220. {
  221. struct page *pages[AFS_BVEC_MAX];
  222. unsigned int nr, n, i, to, bytes = 0;
  223. nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
  224. n = find_get_pages_contig(call->mapping, first, nr, pages);
  225. ASSERTCMP(n, ==, nr);
  226. msg->msg_flags |= MSG_MORE;
  227. for (i = 0; i < nr; i++) {
  228. to = PAGE_SIZE;
  229. if (first + i >= last) {
  230. to = call->last_to;
  231. msg->msg_flags &= ~MSG_MORE;
  232. }
  233. bv[i].bv_page = pages[i];
  234. bv[i].bv_len = to - offset;
  235. bv[i].bv_offset = offset;
  236. bytes += to - offset;
  237. offset = 0;
  238. }
  239. iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes);
  240. }
  241. /*
  242. * Advance the AFS call state when the RxRPC call ends the transmit phase.
  243. */
  244. static void afs_notify_end_request_tx(struct sock *sock,
  245. struct rxrpc_call *rxcall,
  246. unsigned long call_user_ID)
  247. {
  248. struct afs_call *call = (struct afs_call *)call_user_ID;
  249. if (call->state == AFS_CALL_REQUESTING)
  250. call->state = AFS_CALL_AWAIT_REPLY;
  251. }
  252. /*
  253. * attach the data from a bunch of pages on an inode to a call
  254. */
  255. static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
  256. {
  257. struct bio_vec bv[AFS_BVEC_MAX];
  258. unsigned int bytes, nr, loop, offset;
  259. pgoff_t first = call->first, last = call->last;
  260. int ret;
  261. offset = call->first_offset;
  262. call->first_offset = 0;
  263. do {
  264. afs_load_bvec(call, msg, bv, first, last, offset);
  265. offset = 0;
  266. bytes = msg->msg_iter.count;
  267. nr = msg->msg_iter.nr_segs;
  268. ret = rxrpc_kernel_send_data(afs_socket, call->rxcall, msg,
  269. bytes, afs_notify_end_request_tx);
  270. for (loop = 0; loop < nr; loop++)
  271. put_page(bv[loop].bv_page);
  272. if (ret < 0)
  273. break;
  274. first += nr;
  275. } while (first <= last);
  276. return ret;
  277. }
  278. /*
  279. * initiate a call
  280. */
  281. int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
  282. bool async)
  283. {
  284. struct sockaddr_rxrpc srx;
  285. struct rxrpc_call *rxcall;
  286. struct msghdr msg;
  287. struct kvec iov[1];
  288. size_t offset;
  289. s64 tx_total_len;
  290. u32 abort_code;
  291. int ret;
  292. _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
  293. ASSERT(call->type != NULL);
  294. ASSERT(call->type->name != NULL);
  295. _debug("____MAKE %p{%s,%x} [%d]____",
  296. call, call->type->name, key_serial(call->key),
  297. atomic_read(&afs_outstanding_calls));
  298. call->async = async;
  299. memset(&srx, 0, sizeof(srx));
  300. srx.srx_family = AF_RXRPC;
  301. srx.srx_service = call->service_id;
  302. srx.transport_type = SOCK_DGRAM;
  303. srx.transport_len = sizeof(srx.transport.sin);
  304. srx.transport.sin.sin_family = AF_INET;
  305. srx.transport.sin.sin_port = call->port;
  306. memcpy(&srx.transport.sin.sin_addr, addr, 4);
  307. /* Work out the length we're going to transmit. This is awkward for
  308. * calls such as FS.StoreData where there's an extra injection of data
  309. * after the initial fixed part.
  310. */
  311. tx_total_len = call->request_size;
  312. if (call->send_pages) {
  313. tx_total_len += call->last_to - call->first_offset;
  314. tx_total_len += (call->last - call->first) * PAGE_SIZE;
  315. }
  316. /* create a call */
  317. rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
  318. (unsigned long)call,
  319. tx_total_len, gfp,
  320. (async ?
  321. afs_wake_up_async_call :
  322. afs_wake_up_call_waiter));
  323. call->key = NULL;
  324. if (IS_ERR(rxcall)) {
  325. ret = PTR_ERR(rxcall);
  326. goto error_kill_call;
  327. }
  328. call->rxcall = rxcall;
  329. /* send the request */
  330. iov[0].iov_base = call->request;
  331. iov[0].iov_len = call->request_size;
  332. msg.msg_name = NULL;
  333. msg.msg_namelen = 0;
  334. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
  335. call->request_size);
  336. msg.msg_control = NULL;
  337. msg.msg_controllen = 0;
  338. msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
  339. /* We have to change the state *before* sending the last packet as
  340. * rxrpc might give us the reply before it returns from sending the
  341. * request. Further, if the send fails, we may already have been given
  342. * a notification and may have collected it.
  343. */
  344. if (!call->send_pages)
  345. call->state = AFS_CALL_AWAIT_REPLY;
  346. ret = rxrpc_kernel_send_data(afs_socket, rxcall,
  347. &msg, call->request_size,
  348. afs_notify_end_request_tx);
  349. if (ret < 0)
  350. goto error_do_abort;
  351. if (call->send_pages) {
  352. ret = afs_send_pages(call, &msg);
  353. if (ret < 0)
  354. goto error_do_abort;
  355. }
  356. /* at this point, an async call may no longer exist as it may have
  357. * already completed */
  358. if (call->async)
  359. return -EINPROGRESS;
  360. return afs_wait_for_call_to_complete(call);
  361. error_do_abort:
  362. call->state = AFS_CALL_COMPLETE;
  363. if (ret != -ECONNABORTED) {
  364. rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT,
  365. ret, "KSD");
  366. } else {
  367. abort_code = 0;
  368. offset = 0;
  369. rxrpc_kernel_recv_data(afs_socket, rxcall, NULL, 0, &offset,
  370. false, &abort_code);
  371. ret = call->type->abort_to_error(abort_code);
  372. }
  373. error_kill_call:
  374. afs_put_call(call);
  375. _leave(" = %d", ret);
  376. return ret;
  377. }
  378. /*
  379. * deliver messages to a call
  380. */
  381. static void afs_deliver_to_call(struct afs_call *call)
  382. {
  383. u32 abort_code;
  384. int ret;
  385. _enter("%s", call->type->name);
  386. while (call->state == AFS_CALL_AWAIT_REPLY ||
  387. call->state == AFS_CALL_AWAIT_OP_ID ||
  388. call->state == AFS_CALL_AWAIT_REQUEST ||
  389. call->state == AFS_CALL_AWAIT_ACK
  390. ) {
  391. if (call->state == AFS_CALL_AWAIT_ACK) {
  392. size_t offset = 0;
  393. ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
  394. NULL, 0, &offset, false,
  395. &call->abort_code);
  396. trace_afs_recv_data(call, 0, offset, false, ret);
  397. if (ret == -EINPROGRESS || ret == -EAGAIN)
  398. return;
  399. if (ret == 1 || ret < 0) {
  400. call->state = AFS_CALL_COMPLETE;
  401. goto done;
  402. }
  403. return;
  404. }
  405. ret = call->type->deliver(call);
  406. switch (ret) {
  407. case 0:
  408. if (call->state == AFS_CALL_AWAIT_REPLY)
  409. call->state = AFS_CALL_COMPLETE;
  410. goto done;
  411. case -EINPROGRESS:
  412. case -EAGAIN:
  413. goto out;
  414. case -ECONNABORTED:
  415. goto call_complete;
  416. case -ENOTCONN:
  417. abort_code = RX_CALL_DEAD;
  418. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  419. abort_code, ret, "KNC");
  420. goto save_error;
  421. case -ENOTSUPP:
  422. abort_code = RXGEN_OPCODE;
  423. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  424. abort_code, ret, "KIV");
  425. goto save_error;
  426. case -ENODATA:
  427. case -EBADMSG:
  428. case -EMSGSIZE:
  429. default:
  430. abort_code = RXGEN_CC_UNMARSHAL;
  431. if (call->state != AFS_CALL_AWAIT_REPLY)
  432. abort_code = RXGEN_SS_UNMARSHAL;
  433. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  434. abort_code, -EBADMSG, "KUM");
  435. goto save_error;
  436. }
  437. }
  438. done:
  439. if (call->state == AFS_CALL_COMPLETE && call->incoming)
  440. afs_put_call(call);
  441. out:
  442. _leave("");
  443. return;
  444. save_error:
  445. call->error = ret;
  446. call_complete:
  447. call->state = AFS_CALL_COMPLETE;
  448. goto done;
  449. }
  450. /*
  451. * wait synchronously for a call to complete
  452. */
  453. static int afs_wait_for_call_to_complete(struct afs_call *call)
  454. {
  455. int ret;
  456. DECLARE_WAITQUEUE(myself, current);
  457. _enter("");
  458. add_wait_queue(&call->waitq, &myself);
  459. for (;;) {
  460. set_current_state(TASK_INTERRUPTIBLE);
  461. /* deliver any messages that are in the queue */
  462. if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
  463. call->need_attention = false;
  464. __set_current_state(TASK_RUNNING);
  465. afs_deliver_to_call(call);
  466. continue;
  467. }
  468. if (call->state == AFS_CALL_COMPLETE ||
  469. signal_pending(current))
  470. break;
  471. schedule();
  472. }
  473. remove_wait_queue(&call->waitq, &myself);
  474. __set_current_state(TASK_RUNNING);
  475. /* Kill off the call if it's still live. */
  476. if (call->state < AFS_CALL_COMPLETE) {
  477. _debug("call interrupted");
  478. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  479. RX_USER_ABORT, -EINTR, "KWI");
  480. }
  481. ret = call->error;
  482. _debug("call complete");
  483. afs_put_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 sock *sk, struct rxrpc_call *rxcall,
  491. unsigned long call_user_ID)
  492. {
  493. struct afs_call *call = (struct afs_call *)call_user_ID;
  494. call->need_attention = true;
  495. wake_up(&call->waitq);
  496. }
  497. /*
  498. * wake up an asynchronous call
  499. */
  500. static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
  501. unsigned long call_user_ID)
  502. {
  503. struct afs_call *call = (struct afs_call *)call_user_ID;
  504. int u;
  505. trace_afs_notify_call(rxcall, call);
  506. call->need_attention = true;
  507. u = __atomic_add_unless(&call->usage, 1, 0);
  508. if (u != 0) {
  509. trace_afs_call(call, afs_call_trace_wake, u,
  510. atomic_read(&afs_outstanding_calls),
  511. __builtin_return_address(0));
  512. if (!queue_work(afs_async_calls, &call->async_work))
  513. afs_put_call(call);
  514. }
  515. }
  516. /*
  517. * Delete an asynchronous call. The work item carries a ref to the call struct
  518. * that we need to release.
  519. */
  520. static void afs_delete_async_call(struct work_struct *work)
  521. {
  522. struct afs_call *call = container_of(work, struct afs_call, async_work);
  523. _enter("");
  524. afs_put_call(call);
  525. _leave("");
  526. }
  527. /*
  528. * Perform I/O processing on an asynchronous call. The work item carries a ref
  529. * to the call struct that we either need to release or to pass on.
  530. */
  531. static void afs_process_async_call(struct work_struct *work)
  532. {
  533. struct afs_call *call = container_of(work, struct afs_call, async_work);
  534. _enter("");
  535. if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
  536. call->need_attention = false;
  537. afs_deliver_to_call(call);
  538. }
  539. if (call->state == AFS_CALL_COMPLETE) {
  540. call->reply = NULL;
  541. /* We have two refs to release - one from the alloc and one
  542. * queued with the work item - and we can't just deallocate the
  543. * call because the work item may be queued again.
  544. */
  545. call->async_work.func = afs_delete_async_call;
  546. if (!queue_work(afs_async_calls, &call->async_work))
  547. afs_put_call(call);
  548. }
  549. afs_put_call(call);
  550. _leave("");
  551. }
  552. static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
  553. {
  554. struct afs_call *call = (struct afs_call *)user_call_ID;
  555. call->rxcall = rxcall;
  556. }
  557. /*
  558. * Charge the incoming call preallocation.
  559. */
  560. static void afs_charge_preallocation(struct work_struct *work)
  561. {
  562. struct afs_call *call = afs_spare_incoming_call;
  563. for (;;) {
  564. if (!call) {
  565. call = afs_alloc_call(&afs_RXCMxxxx, GFP_KERNEL);
  566. if (!call)
  567. break;
  568. call->async = true;
  569. call->state = AFS_CALL_AWAIT_OP_ID;
  570. init_waitqueue_head(&call->waitq);
  571. }
  572. if (rxrpc_kernel_charge_accept(afs_socket,
  573. afs_wake_up_async_call,
  574. afs_rx_attach,
  575. (unsigned long)call,
  576. GFP_KERNEL) < 0)
  577. break;
  578. call = NULL;
  579. }
  580. afs_spare_incoming_call = call;
  581. }
  582. /*
  583. * Discard a preallocated call when a socket is shut down.
  584. */
  585. static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
  586. unsigned long user_call_ID)
  587. {
  588. struct afs_call *call = (struct afs_call *)user_call_ID;
  589. call->rxcall = NULL;
  590. afs_put_call(call);
  591. }
  592. /*
  593. * Notification of an incoming call.
  594. */
  595. static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
  596. unsigned long user_call_ID)
  597. {
  598. queue_work(afs_wq, &afs_charge_preallocation_work);
  599. }
  600. /*
  601. * Grab the operation ID from an incoming cache manager call. The socket
  602. * buffer is discarded on error or if we don't yet have sufficient data.
  603. */
  604. static int afs_deliver_cm_op_id(struct afs_call *call)
  605. {
  606. int ret;
  607. _enter("{%zu}", call->offset);
  608. ASSERTCMP(call->offset, <, 4);
  609. /* the operation ID forms the first four bytes of the request data */
  610. ret = afs_extract_data(call, &call->tmp, 4, true);
  611. if (ret < 0)
  612. return ret;
  613. call->operation_ID = ntohl(call->tmp);
  614. call->state = AFS_CALL_AWAIT_REQUEST;
  615. call->offset = 0;
  616. /* ask the cache manager to route the call (it'll change the call type
  617. * if successful) */
  618. if (!afs_cm_incoming_call(call))
  619. return -ENOTSUPP;
  620. trace_afs_cb_call(call);
  621. /* pass responsibility for the remainer of this message off to the
  622. * cache manager op */
  623. return call->type->deliver(call);
  624. }
  625. /*
  626. * Advance the AFS call state when an RxRPC service call ends the transmit
  627. * phase.
  628. */
  629. static void afs_notify_end_reply_tx(struct sock *sock,
  630. struct rxrpc_call *rxcall,
  631. unsigned long call_user_ID)
  632. {
  633. struct afs_call *call = (struct afs_call *)call_user_ID;
  634. if (call->state == AFS_CALL_REPLYING)
  635. call->state = AFS_CALL_AWAIT_ACK;
  636. }
  637. /*
  638. * send an empty reply
  639. */
  640. void afs_send_empty_reply(struct afs_call *call)
  641. {
  642. struct msghdr msg;
  643. _enter("");
  644. rxrpc_kernel_set_tx_length(afs_socket, call->rxcall, 0);
  645. msg.msg_name = NULL;
  646. msg.msg_namelen = 0;
  647. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 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(afs_socket, call->rxcall, &msg, 0,
  653. afs_notify_end_reply_tx)) {
  654. case 0:
  655. _leave(" [replied]");
  656. return;
  657. case -ENOMEM:
  658. _debug("oom");
  659. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  660. RX_USER_ABORT, -ENOMEM, "KOO");
  661. default:
  662. _leave(" [error]");
  663. return;
  664. }
  665. }
  666. /*
  667. * send a simple reply
  668. */
  669. void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
  670. {
  671. struct msghdr msg;
  672. struct kvec iov[1];
  673. int n;
  674. _enter("");
  675. rxrpc_kernel_set_tx_length(afs_socket, call->rxcall, len);
  676. iov[0].iov_base = (void *) buf;
  677. iov[0].iov_len = len;
  678. msg.msg_name = NULL;
  679. msg.msg_namelen = 0;
  680. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
  681. msg.msg_control = NULL;
  682. msg.msg_controllen = 0;
  683. msg.msg_flags = 0;
  684. call->state = AFS_CALL_AWAIT_ACK;
  685. n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len,
  686. afs_notify_end_reply_tx);
  687. if (n >= 0) {
  688. /* Success */
  689. _leave(" [replied]");
  690. return;
  691. }
  692. if (n == -ENOMEM) {
  693. _debug("oom");
  694. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  695. RX_USER_ABORT, -ENOMEM, "KOO");
  696. }
  697. _leave(" [error]");
  698. }
  699. /*
  700. * Extract a piece of data from the received data socket buffers.
  701. */
  702. int afs_extract_data(struct afs_call *call, void *buf, size_t count,
  703. bool want_more)
  704. {
  705. int ret;
  706. _enter("{%s,%zu},,%zu,%d",
  707. call->type->name, call->offset, count, want_more);
  708. ASSERTCMP(call->offset, <=, count);
  709. ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
  710. buf, count, &call->offset,
  711. want_more, &call->abort_code);
  712. trace_afs_recv_data(call, count, call->offset, want_more, ret);
  713. if (ret == 0 || ret == -EAGAIN)
  714. return ret;
  715. if (ret == 1) {
  716. switch (call->state) {
  717. case AFS_CALL_AWAIT_REPLY:
  718. call->state = AFS_CALL_COMPLETE;
  719. break;
  720. case AFS_CALL_AWAIT_REQUEST:
  721. call->state = AFS_CALL_REPLYING;
  722. break;
  723. default:
  724. break;
  725. }
  726. return 0;
  727. }
  728. if (ret == -ECONNABORTED)
  729. call->error = call->type->abort_to_error(call->abort_code);
  730. else
  731. call->error = ret;
  732. call->state = AFS_CALL_COMPLETE;
  733. return ret;
  734. }