rxrpc.c 24 KB

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