rxrpc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Maintain an RxRPC server socket to do AFS communications through
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/sched/signal.h>
  9. #include <net/sock.h>
  10. #include <net/af_rxrpc.h>
  11. #include "internal.h"
  12. #include "afs_cm.h"
  13. #include "protocol_yfs.h"
  14. #define RXRPC_TRACE_ONLY_DEFINE_ENUMS
  15. #include <trace/events/rxrpc.h>
  16. struct workqueue_struct *afs_async_calls;
  17. static void afs_deferred_free_worker(struct work_struct *work);
  18. static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
  19. static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
  20. static void afs_process_async_call(struct work_struct *);
  21. static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
  22. static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
  23. static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID);
  24. static void afs_rx_notify_oob(struct sock *sk, struct sk_buff *oob);
  25. static int afs_deliver_cm_op_id(struct afs_call *);
  26. static const struct rxrpc_kernel_ops afs_rxrpc_callback_ops = {
  27. .notify_new_call = afs_rx_new_call,
  28. .discard_new_call = afs_rx_discard_new_call,
  29. .user_attach_call = afs_rx_attach,
  30. .notify_oob = afs_rx_notify_oob,
  31. };
  32. /* asynchronous incoming call initial processing */
  33. static const struct afs_call_type afs_RXCMxxxx = {
  34. .name = "CB.xxxx",
  35. .deliver = afs_deliver_cm_op_id,
  36. };
  37. /*
  38. * open an RxRPC socket and bind it to be a server for callback notifications
  39. * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
  40. */
  41. int afs_open_socket(struct afs_net *net)
  42. {
  43. struct sockaddr_rxrpc srx;
  44. struct socket *socket;
  45. int ret;
  46. _enter("");
  47. ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
  48. if (ret < 0)
  49. goto error_1;
  50. socket->sk->sk_allocation = GFP_NOFS;
  51. socket->sk->sk_user_data = net;
  52. /* bind the callback manager's address to make this a server socket */
  53. memset(&srx, 0, sizeof(srx));
  54. srx.srx_family = AF_RXRPC;
  55. srx.srx_service = CM_SERVICE;
  56. srx.transport_type = SOCK_DGRAM;
  57. srx.transport_len = sizeof(srx.transport.sin6);
  58. srx.transport.sin6.sin6_family = AF_INET6;
  59. srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
  60. ret = rxrpc_sock_set_min_security_level(socket->sk,
  61. RXRPC_SECURITY_ENCRYPT);
  62. if (ret < 0)
  63. goto error_2;
  64. ret = rxrpc_sock_set_manage_response(socket->sk, true);
  65. if (ret < 0)
  66. goto error_2;
  67. ret = afs_create_token_key(net, socket);
  68. if (ret < 0)
  69. pr_err("Couldn't create RxGK CM key: %d\n", ret);
  70. ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx));
  71. if (ret == -EADDRINUSE) {
  72. srx.transport.sin6.sin6_port = 0;
  73. ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx));
  74. }
  75. if (ret < 0)
  76. goto error_2;
  77. srx.srx_service = YFS_CM_SERVICE;
  78. ret = kernel_bind(socket, (struct sockaddr_unsized *) &srx, sizeof(srx));
  79. if (ret < 0)
  80. goto error_2;
  81. /* Ideally, we'd turn on service upgrade here, but we can't because
  82. * OpenAFS is buggy and leaks the userStatus field from packet to
  83. * packet and between FS packets and CB packets - so if we try to do an
  84. * upgrade on an FS packet, OpenAFS will leak that into the CB packet
  85. * it sends back to us.
  86. */
  87. rxrpc_kernel_set_notifications(socket, &afs_rxrpc_callback_ops);
  88. ret = kernel_listen(socket, INT_MAX);
  89. if (ret < 0)
  90. goto error_2;
  91. net->socket = socket;
  92. afs_charge_preallocation(&net->charge_preallocation_work);
  93. _leave(" = 0");
  94. return 0;
  95. error_2:
  96. sock_release(socket);
  97. error_1:
  98. _leave(" = %d", ret);
  99. return ret;
  100. }
  101. /*
  102. * close the RxRPC socket AFS was using
  103. */
  104. void afs_close_socket(struct afs_net *net)
  105. {
  106. _enter("");
  107. kernel_listen(net->socket, 0);
  108. flush_workqueue(afs_async_calls);
  109. if (net->spare_incoming_call) {
  110. afs_put_call(net->spare_incoming_call);
  111. net->spare_incoming_call = NULL;
  112. }
  113. _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
  114. wait_var_event(&net->nr_outstanding_calls,
  115. !atomic_read(&net->nr_outstanding_calls));
  116. _debug("no outstanding calls");
  117. kernel_sock_shutdown(net->socket, SHUT_RDWR);
  118. flush_workqueue(afs_async_calls);
  119. net->socket->sk->sk_user_data = NULL;
  120. sock_release(net->socket);
  121. key_put(net->fs_cm_token_key);
  122. _debug("dework");
  123. _leave("");
  124. }
  125. /*
  126. * Allocate a call.
  127. */
  128. static struct afs_call *afs_alloc_call(struct afs_net *net,
  129. const struct afs_call_type *type,
  130. gfp_t gfp)
  131. {
  132. struct afs_call *call;
  133. int o;
  134. call = kzalloc_obj(*call, gfp);
  135. if (!call)
  136. return NULL;
  137. call->type = type;
  138. call->net = net;
  139. call->debug_id = atomic_inc_return(&rxrpc_debug_id);
  140. refcount_set(&call->ref, 1);
  141. INIT_WORK(&call->async_work, type->async_rx ?: afs_process_async_call);
  142. INIT_WORK(&call->work, call->type->work);
  143. INIT_WORK(&call->free_work, afs_deferred_free_worker);
  144. init_waitqueue_head(&call->waitq);
  145. spin_lock_init(&call->state_lock);
  146. call->iter = &call->def_iter;
  147. o = atomic_inc_return(&net->nr_outstanding_calls);
  148. trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o,
  149. __builtin_return_address(0));
  150. return call;
  151. }
  152. static void afs_free_call(struct afs_call *call)
  153. {
  154. struct afs_net *net = call->net;
  155. int o;
  156. ASSERT(!work_pending(&call->async_work));
  157. rxrpc_kernel_put_peer(call->peer);
  158. if (call->rxcall) {
  159. rxrpc_kernel_shutdown_call(net->socket, call->rxcall);
  160. rxrpc_kernel_put_call(net->socket, call->rxcall);
  161. call->rxcall = NULL;
  162. }
  163. if (call->type->destructor)
  164. call->type->destructor(call);
  165. afs_unuse_server_notime(call->net, call->server, afs_server_trace_unuse_call);
  166. kfree(call->request);
  167. o = atomic_read(&net->nr_outstanding_calls);
  168. trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
  169. __builtin_return_address(0));
  170. kfree(call);
  171. o = atomic_dec_return(&net->nr_outstanding_calls);
  172. if (o == 0)
  173. wake_up_var(&net->nr_outstanding_calls);
  174. }
  175. /*
  176. * Dispose of a reference on a call.
  177. */
  178. void afs_put_call(struct afs_call *call)
  179. {
  180. struct afs_net *net = call->net;
  181. unsigned int debug_id = call->debug_id;
  182. bool zero;
  183. int r, o;
  184. zero = __refcount_dec_and_test(&call->ref, &r);
  185. o = atomic_read(&net->nr_outstanding_calls);
  186. trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
  187. __builtin_return_address(0));
  188. if (zero)
  189. afs_free_call(call);
  190. }
  191. static void afs_deferred_free_worker(struct work_struct *work)
  192. {
  193. struct afs_call *call = container_of(work, struct afs_call, free_work);
  194. afs_free_call(call);
  195. }
  196. /*
  197. * Dispose of a reference on a call, deferring the cleanup to a workqueue
  198. * to avoid lock recursion.
  199. */
  200. void afs_deferred_put_call(struct afs_call *call)
  201. {
  202. struct afs_net *net = call->net;
  203. unsigned int debug_id = call->debug_id;
  204. bool zero;
  205. int r, o;
  206. zero = __refcount_dec_and_test(&call->ref, &r);
  207. o = atomic_read(&net->nr_outstanding_calls);
  208. trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
  209. __builtin_return_address(0));
  210. if (zero)
  211. schedule_work(&call->free_work);
  212. }
  213. /*
  214. * Queue the call for actual work.
  215. */
  216. static void afs_queue_call_work(struct afs_call *call)
  217. {
  218. if (call->type->work) {
  219. afs_get_call(call, afs_call_trace_work);
  220. if (!queue_work(afs_wq, &call->work))
  221. afs_put_call(call);
  222. }
  223. }
  224. /*
  225. * allocate a call with flat request and reply buffers
  226. */
  227. struct afs_call *afs_alloc_flat_call(struct afs_net *net,
  228. const struct afs_call_type *type,
  229. size_t request_size, size_t reply_max)
  230. {
  231. struct afs_call *call;
  232. call = afs_alloc_call(net, type, GFP_NOFS);
  233. if (!call)
  234. goto nomem_call;
  235. if (request_size) {
  236. call->request_size = request_size;
  237. call->request = kmalloc(request_size, GFP_NOFS);
  238. if (!call->request)
  239. goto nomem_free;
  240. }
  241. if (reply_max) {
  242. call->reply_max = reply_max;
  243. call->buffer = kmalloc(reply_max, GFP_NOFS);
  244. if (!call->buffer)
  245. goto nomem_free;
  246. }
  247. afs_extract_to_buf(call, call->reply_max);
  248. call->operation_ID = type->op;
  249. init_waitqueue_head(&call->waitq);
  250. return call;
  251. nomem_free:
  252. afs_put_call(call);
  253. nomem_call:
  254. return NULL;
  255. }
  256. /*
  257. * clean up a call with flat buffer
  258. */
  259. void afs_flat_call_destructor(struct afs_call *call)
  260. {
  261. _enter("");
  262. kfree(call->request);
  263. call->request = NULL;
  264. kfree(call->buffer);
  265. call->buffer = NULL;
  266. }
  267. /*
  268. * Advance the AFS call state when the RxRPC call ends the transmit phase.
  269. */
  270. static void afs_notify_end_request_tx(struct sock *sock,
  271. struct rxrpc_call *rxcall,
  272. unsigned long call_user_ID)
  273. {
  274. struct afs_call *call = (struct afs_call *)call_user_ID;
  275. afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
  276. }
  277. /*
  278. * Initiate a call and synchronously queue up the parameters for dispatch. Any
  279. * error is stored into the call struct, which the caller must check for.
  280. */
  281. void afs_make_call(struct afs_call *call, gfp_t gfp)
  282. {
  283. struct rxrpc_call *rxcall;
  284. struct msghdr msg;
  285. struct kvec iov[1];
  286. size_t len;
  287. s64 tx_total_len;
  288. int ret;
  289. _enter(",{%pISp+%u},", rxrpc_kernel_remote_addr(call->peer), call->service_id);
  290. ASSERT(call->type != NULL);
  291. ASSERT(call->type->name != NULL);
  292. _debug("____MAKE %p{%s,%x} [%d]____",
  293. call, call->type->name, key_serial(call->key),
  294. atomic_read(&call->net->nr_outstanding_calls));
  295. trace_afs_make_call(call);
  296. /* Work out the length we're going to transmit. This is awkward for
  297. * calls such as FS.StoreData where there's an extra injection of data
  298. * after the initial fixed part.
  299. */
  300. tx_total_len = call->request_size;
  301. if (call->write_iter)
  302. tx_total_len += iov_iter_count(call->write_iter);
  303. /* If the call is going to be asynchronous, we need an extra ref for
  304. * the call to hold itself so the caller need not hang on to its ref.
  305. */
  306. if (call->async) {
  307. afs_get_call(call, afs_call_trace_get);
  308. call->drop_ref = true;
  309. }
  310. /* create a call */
  311. rxcall = rxrpc_kernel_begin_call(call->net->socket, call->peer, call->key,
  312. (unsigned long)call,
  313. tx_total_len,
  314. call->max_lifespan,
  315. gfp,
  316. (call->async ?
  317. afs_wake_up_async_call :
  318. afs_wake_up_call_waiter),
  319. call->service_id,
  320. call->upgrade,
  321. (call->intr ? RXRPC_PREINTERRUPTIBLE :
  322. RXRPC_UNINTERRUPTIBLE),
  323. call->debug_id);
  324. if (IS_ERR(rxcall)) {
  325. ret = PTR_ERR(rxcall);
  326. call->error = ret;
  327. goto error_kill_call;
  328. }
  329. call->rxcall = rxcall;
  330. call->issue_time = ktime_get_real();
  331. /* send the request */
  332. iov[0].iov_base = call->request;
  333. iov[0].iov_len = call->request_size;
  334. msg.msg_name = NULL;
  335. msg.msg_namelen = 0;
  336. iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, call->request_size);
  337. msg.msg_control = NULL;
  338. msg.msg_controllen = 0;
  339. msg.msg_flags = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
  340. ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
  341. &msg, call->request_size,
  342. afs_notify_end_request_tx);
  343. if (ret < 0)
  344. goto error_do_abort;
  345. if (call->write_iter) {
  346. msg.msg_iter = *call->write_iter;
  347. msg.msg_flags &= ~MSG_MORE;
  348. trace_afs_send_data(call, &msg);
  349. ret = rxrpc_kernel_send_data(call->net->socket,
  350. call->rxcall, &msg,
  351. iov_iter_count(&msg.msg_iter),
  352. afs_notify_end_request_tx);
  353. *call->write_iter = msg.msg_iter;
  354. trace_afs_sent_data(call, &msg, ret);
  355. if (ret < 0)
  356. goto error_do_abort;
  357. }
  358. /* Note that at this point, we may have received the reply or an abort
  359. * - and an asynchronous call may already have completed.
  360. *
  361. * afs_wait_for_call_to_complete(call)
  362. * must be called to synchronously clean up.
  363. */
  364. return;
  365. error_do_abort:
  366. if (ret != -ECONNABORTED)
  367. rxrpc_kernel_abort_call(call->net->socket, rxcall,
  368. RX_USER_ABORT, ret,
  369. afs_abort_send_data_error);
  370. if (call->async) {
  371. afs_see_call(call, afs_call_trace_async_abort);
  372. return;
  373. }
  374. if (ret == -ECONNABORTED) {
  375. len = 0;
  376. iov_iter_kvec(&msg.msg_iter, ITER_DEST, NULL, 0, 0);
  377. rxrpc_kernel_recv_data(call->net->socket, rxcall,
  378. &msg.msg_iter, &len, false,
  379. &call->abort_code, &call->service_id);
  380. call->responded = true;
  381. }
  382. call->error = ret;
  383. trace_afs_call_done(call);
  384. error_kill_call:
  385. if (call->async)
  386. afs_see_call(call, afs_call_trace_async_kill);
  387. if (call->type->immediate_cancel)
  388. call->type->immediate_cancel(call);
  389. /* We need to dispose of the extra ref we grabbed for an async call.
  390. * The call, however, might be queued on afs_async_calls and we need to
  391. * make sure we don't get any more notifications that might requeue it.
  392. */
  393. if (call->rxcall)
  394. rxrpc_kernel_shutdown_call(call->net->socket, call->rxcall);
  395. if (call->async) {
  396. if (cancel_work_sync(&call->async_work))
  397. afs_put_call(call);
  398. afs_set_call_complete(call, ret, 0);
  399. }
  400. call->error = ret;
  401. call->state = AFS_CALL_COMPLETE;
  402. _leave(" = %d", ret);
  403. }
  404. /*
  405. * Log remote abort codes that indicate that we have a protocol disagreement
  406. * with the server.
  407. */
  408. static void afs_log_error(struct afs_call *call, s32 remote_abort)
  409. {
  410. static int max = 0;
  411. const char *msg;
  412. int m;
  413. switch (remote_abort) {
  414. case RX_EOF: msg = "unexpected EOF"; break;
  415. case RXGEN_CC_MARSHAL: msg = "client marshalling"; break;
  416. case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling"; break;
  417. case RXGEN_SS_MARSHAL: msg = "server marshalling"; break;
  418. case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling"; break;
  419. case RXGEN_DECODE: msg = "opcode decode"; break;
  420. case RXGEN_SS_XDRFREE: msg = "server XDR cleanup"; break;
  421. case RXGEN_CC_XDRFREE: msg = "client XDR cleanup"; break;
  422. case -32: msg = "insufficient data"; break;
  423. default:
  424. return;
  425. }
  426. m = max;
  427. if (m < 3) {
  428. max = m + 1;
  429. pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
  430. msg, call->type->name,
  431. rxrpc_kernel_remote_addr(call->peer));
  432. }
  433. }
  434. /*
  435. * deliver messages to a call
  436. */
  437. void afs_deliver_to_call(struct afs_call *call)
  438. {
  439. enum afs_call_state state;
  440. size_t len;
  441. u32 abort_code, remote_abort = 0;
  442. int ret;
  443. _enter("%s", call->type->name);
  444. while (state = READ_ONCE(call->state),
  445. state == AFS_CALL_CL_AWAIT_REPLY ||
  446. state == AFS_CALL_SV_AWAIT_OP_ID ||
  447. state == AFS_CALL_SV_AWAIT_REQUEST ||
  448. state == AFS_CALL_SV_AWAIT_ACK
  449. ) {
  450. if (state == AFS_CALL_SV_AWAIT_ACK) {
  451. len = 0;
  452. iov_iter_kvec(&call->def_iter, ITER_DEST, NULL, 0, 0);
  453. ret = rxrpc_kernel_recv_data(call->net->socket,
  454. call->rxcall, &call->def_iter,
  455. &len, false, &remote_abort,
  456. &call->service_id);
  457. trace_afs_receive_data(call, &call->def_iter, false, ret);
  458. if (ret == -EINPROGRESS || ret == -EAGAIN)
  459. return;
  460. if (ret < 0 || ret == 1) {
  461. if (ret == 1)
  462. ret = 0;
  463. goto call_complete;
  464. }
  465. return;
  466. }
  467. ret = call->type->deliver(call);
  468. state = READ_ONCE(call->state);
  469. if (ret == 0 && call->unmarshalling_error)
  470. ret = -EBADMSG;
  471. switch (ret) {
  472. case 0:
  473. call->responded = true;
  474. afs_queue_call_work(call);
  475. if (state == AFS_CALL_CL_PROC_REPLY) {
  476. if (call->op)
  477. set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
  478. &call->op->server->flags);
  479. goto call_complete;
  480. }
  481. ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
  482. goto done;
  483. case -EINPROGRESS:
  484. case -EAGAIN:
  485. goto out;
  486. case -ECONNABORTED:
  487. ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
  488. call->responded = true;
  489. afs_log_error(call, call->abort_code);
  490. goto done;
  491. case -ENOTSUPP:
  492. call->responded = true;
  493. abort_code = RXGEN_OPCODE;
  494. rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  495. abort_code, ret,
  496. afs_abort_op_not_supported);
  497. goto local_abort;
  498. case -EIO:
  499. pr_err("kAFS: Call %u in bad state %u\n",
  500. call->debug_id, state);
  501. fallthrough;
  502. case -ENODATA:
  503. case -EBADMSG:
  504. case -EMSGSIZE:
  505. case -ENOMEM:
  506. case -EFAULT:
  507. abort_code = RXGEN_CC_UNMARSHAL;
  508. if (state != AFS_CALL_CL_AWAIT_REPLY)
  509. abort_code = RXGEN_SS_UNMARSHAL;
  510. rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  511. abort_code, ret,
  512. afs_abort_unmarshal_error);
  513. goto local_abort;
  514. default:
  515. abort_code = RX_CALL_DEAD;
  516. rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  517. abort_code, ret,
  518. afs_abort_general_error);
  519. goto local_abort;
  520. }
  521. }
  522. done:
  523. if (call->type->done)
  524. call->type->done(call);
  525. out:
  526. _leave("");
  527. return;
  528. local_abort:
  529. abort_code = 0;
  530. call_complete:
  531. afs_set_call_complete(call, ret, remote_abort);
  532. goto done;
  533. }
  534. /*
  535. * Wait synchronously for a call to complete.
  536. */
  537. void afs_wait_for_call_to_complete(struct afs_call *call)
  538. {
  539. bool rxrpc_complete = false;
  540. _enter("");
  541. if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
  542. DECLARE_WAITQUEUE(myself, current);
  543. add_wait_queue(&call->waitq, &myself);
  544. for (;;) {
  545. set_current_state(TASK_UNINTERRUPTIBLE);
  546. /* deliver any messages that are in the queue */
  547. if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
  548. call->need_attention) {
  549. call->need_attention = false;
  550. __set_current_state(TASK_RUNNING);
  551. afs_deliver_to_call(call);
  552. continue;
  553. }
  554. if (afs_check_call_state(call, AFS_CALL_COMPLETE))
  555. break;
  556. if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
  557. /* rxrpc terminated the call. */
  558. rxrpc_complete = true;
  559. break;
  560. }
  561. schedule();
  562. }
  563. remove_wait_queue(&call->waitq, &myself);
  564. __set_current_state(TASK_RUNNING);
  565. }
  566. if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
  567. if (rxrpc_complete) {
  568. afs_set_call_complete(call, call->error, call->abort_code);
  569. } else {
  570. /* Kill off the call if it's still live. */
  571. _debug("call interrupted");
  572. if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  573. RX_USER_ABORT, -EINTR,
  574. afs_abort_interrupted))
  575. afs_set_call_complete(call, -EINTR, 0);
  576. }
  577. }
  578. }
  579. /*
  580. * wake up a waiting call
  581. */
  582. static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
  583. unsigned long call_user_ID)
  584. {
  585. struct afs_call *call = (struct afs_call *)call_user_ID;
  586. call->need_attention = true;
  587. wake_up(&call->waitq);
  588. }
  589. /*
  590. * Wake up an asynchronous call. The caller is holding the call notify
  591. * spinlock around this, so we can't call afs_put_call().
  592. */
  593. static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
  594. unsigned long call_user_ID)
  595. {
  596. struct afs_call *call = (struct afs_call *)call_user_ID;
  597. int r;
  598. trace_afs_notify_call(rxcall, call);
  599. call->need_attention = true;
  600. if (__refcount_inc_not_zero(&call->ref, &r)) {
  601. trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
  602. atomic_read(&call->net->nr_outstanding_calls),
  603. __builtin_return_address(0));
  604. if (!queue_work(afs_async_calls, &call->async_work))
  605. afs_deferred_put_call(call);
  606. }
  607. }
  608. /*
  609. * Perform I/O processing on an asynchronous call. The work item carries a ref
  610. * to the call struct that we either need to release or to pass on.
  611. */
  612. static void afs_process_async_call(struct work_struct *work)
  613. {
  614. struct afs_call *call = container_of(work, struct afs_call, async_work);
  615. _enter("");
  616. if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
  617. call->need_attention = false;
  618. afs_deliver_to_call(call);
  619. }
  620. afs_put_call(call);
  621. _leave("");
  622. }
  623. static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
  624. {
  625. struct afs_call *call = (struct afs_call *)user_call_ID;
  626. call->rxcall = rxcall;
  627. }
  628. /*
  629. * Charge the incoming call preallocation.
  630. */
  631. void afs_charge_preallocation(struct work_struct *work)
  632. {
  633. struct afs_net *net =
  634. container_of(work, struct afs_net, charge_preallocation_work);
  635. struct afs_call *call = net->spare_incoming_call;
  636. for (;;) {
  637. if (!call) {
  638. call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
  639. if (!call)
  640. break;
  641. call->drop_ref = true;
  642. call->async = true;
  643. call->state = AFS_CALL_SV_AWAIT_OP_ID;
  644. init_waitqueue_head(&call->waitq);
  645. afs_extract_to_tmp(call);
  646. }
  647. if (rxrpc_kernel_charge_accept(net->socket,
  648. afs_wake_up_async_call,
  649. (unsigned long)call,
  650. GFP_KERNEL,
  651. call->debug_id) < 0)
  652. break;
  653. call = NULL;
  654. }
  655. net->spare_incoming_call = call;
  656. }
  657. /*
  658. * Discard a preallocated call when a socket is shut down.
  659. */
  660. static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
  661. unsigned long user_call_ID)
  662. {
  663. struct afs_call *call = (struct afs_call *)user_call_ID;
  664. call->rxcall = NULL;
  665. afs_put_call(call);
  666. }
  667. /*
  668. * Notification of an incoming call.
  669. */
  670. static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
  671. unsigned long user_call_ID)
  672. {
  673. struct afs_call *call = (struct afs_call *)user_call_ID;
  674. struct afs_net *net = afs_sock2net(sk);
  675. call->peer = rxrpc_kernel_get_call_peer(sk->sk_socket, call->rxcall);
  676. call->server = afs_find_server(call->peer);
  677. if (!call->server)
  678. trace_afs_cm_no_server(call, rxrpc_kernel_remote_srx(call->peer));
  679. queue_work(afs_wq, &net->charge_preallocation_work);
  680. }
  681. /*
  682. * Grab the operation ID from an incoming cache manager call. The socket
  683. * buffer is discarded on error or if we don't yet have sufficient data.
  684. */
  685. static int afs_deliver_cm_op_id(struct afs_call *call)
  686. {
  687. int ret;
  688. _enter("{%zu}", iov_iter_count(call->iter));
  689. /* the operation ID forms the first four bytes of the request data */
  690. ret = afs_extract_data(call, true);
  691. if (ret < 0)
  692. return ret;
  693. call->operation_ID = ntohl(call->tmp);
  694. afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
  695. /* ask the cache manager to route the call (it'll change the call type
  696. * if successful) */
  697. if (!afs_cm_incoming_call(call))
  698. return -ENOTSUPP;
  699. call->security_ix = rxrpc_kernel_query_call_security(call->rxcall,
  700. &call->service_id,
  701. &call->enctype);
  702. trace_afs_cb_call(call);
  703. call->work.func = call->type->work;
  704. /* pass responsibility for the remainder of this message off to the
  705. * cache manager op */
  706. return call->type->deliver(call);
  707. }
  708. /*
  709. * Advance the AFS call state when an RxRPC service call ends the transmit
  710. * phase.
  711. */
  712. static void afs_notify_end_reply_tx(struct sock *sock,
  713. struct rxrpc_call *rxcall,
  714. unsigned long call_user_ID)
  715. {
  716. struct afs_call *call = (struct afs_call *)call_user_ID;
  717. afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
  718. }
  719. /*
  720. * send an empty reply
  721. */
  722. void afs_send_empty_reply(struct afs_call *call)
  723. {
  724. struct afs_net *net = call->net;
  725. struct msghdr msg;
  726. _enter("");
  727. rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
  728. msg.msg_name = NULL;
  729. msg.msg_namelen = 0;
  730. iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, NULL, 0, 0);
  731. msg.msg_control = NULL;
  732. msg.msg_controllen = 0;
  733. msg.msg_flags = 0;
  734. switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
  735. afs_notify_end_reply_tx)) {
  736. case 0:
  737. _leave(" [replied]");
  738. return;
  739. case -ENOMEM:
  740. _debug("oom");
  741. rxrpc_kernel_abort_call(net->socket, call->rxcall,
  742. RXGEN_SS_MARSHAL, -ENOMEM,
  743. afs_abort_oom);
  744. fallthrough;
  745. default:
  746. _leave(" [error]");
  747. return;
  748. }
  749. }
  750. /*
  751. * send a simple reply
  752. */
  753. void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
  754. {
  755. struct afs_net *net = call->net;
  756. struct msghdr msg;
  757. struct kvec iov[1];
  758. int n;
  759. _enter("");
  760. rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
  761. iov[0].iov_base = (void *) buf;
  762. iov[0].iov_len = len;
  763. msg.msg_name = NULL;
  764. msg.msg_namelen = 0;
  765. iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, 1, len);
  766. msg.msg_control = NULL;
  767. msg.msg_controllen = 0;
  768. msg.msg_flags = 0;
  769. n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
  770. afs_notify_end_reply_tx);
  771. if (n >= 0) {
  772. /* Success */
  773. _leave(" [replied]");
  774. return;
  775. }
  776. if (n == -ENOMEM) {
  777. _debug("oom");
  778. rxrpc_kernel_abort_call(net->socket, call->rxcall,
  779. RXGEN_SS_MARSHAL, -ENOMEM,
  780. afs_abort_oom);
  781. }
  782. _leave(" [error]");
  783. }
  784. /*
  785. * Extract a piece of data from the received data socket buffers.
  786. */
  787. int afs_extract_data(struct afs_call *call, bool want_more)
  788. {
  789. struct afs_net *net = call->net;
  790. struct iov_iter *iter = call->iter;
  791. enum afs_call_state state;
  792. u32 remote_abort = 0;
  793. int ret;
  794. _enter("{%s,%zu,%zu},%d",
  795. call->type->name, call->iov_len, iov_iter_count(iter), want_more);
  796. ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
  797. &call->iov_len, want_more, &remote_abort,
  798. &call->service_id);
  799. trace_afs_receive_data(call, call->iter, want_more, ret);
  800. if (ret == 0 || ret == -EAGAIN)
  801. return ret;
  802. state = READ_ONCE(call->state);
  803. if (ret == 1) {
  804. switch (state) {
  805. case AFS_CALL_CL_AWAIT_REPLY:
  806. afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
  807. break;
  808. case AFS_CALL_SV_AWAIT_REQUEST:
  809. afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
  810. break;
  811. case AFS_CALL_COMPLETE:
  812. kdebug("prem complete %d", call->error);
  813. return afs_io_error(call, afs_io_error_extract);
  814. default:
  815. break;
  816. }
  817. return 0;
  818. }
  819. afs_set_call_complete(call, ret, remote_abort);
  820. return ret;
  821. }
  822. /*
  823. * Log protocol error production.
  824. */
  825. noinline int afs_protocol_error(struct afs_call *call,
  826. enum afs_eproto_cause cause)
  827. {
  828. trace_afs_protocol_error(call, cause);
  829. if (call)
  830. call->unmarshalling_error = true;
  831. return -EBADMSG;
  832. }
  833. /*
  834. * Wake up OOB notification processing.
  835. */
  836. static void afs_rx_notify_oob(struct sock *sk, struct sk_buff *oob)
  837. {
  838. struct afs_net *net = sk->sk_user_data;
  839. schedule_work(&net->rx_oob_work);
  840. }