rxrpc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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 workqueue_struct *afs_async_calls;
  18. static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
  19. static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
  20. static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
  21. static void afs_process_async_call(struct work_struct *);
  22. static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
  23. static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
  24. static int afs_deliver_cm_op_id(struct afs_call *);
  25. /* asynchronous incoming call initial processing */
  26. static const struct afs_call_type afs_RXCMxxxx = {
  27. .name = "CB.xxxx",
  28. .deliver = afs_deliver_cm_op_id,
  29. };
  30. /*
  31. * open an RxRPC socket and bind it to be a server for callback notifications
  32. * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
  33. */
  34. int afs_open_socket(struct afs_net *net)
  35. {
  36. struct sockaddr_rxrpc srx;
  37. struct socket *socket;
  38. unsigned int min_level;
  39. int ret;
  40. _enter("");
  41. ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
  42. if (ret < 0)
  43. goto error_1;
  44. socket->sk->sk_allocation = GFP_NOFS;
  45. /* bind the callback manager's address to make this a server socket */
  46. memset(&srx, 0, sizeof(srx));
  47. srx.srx_family = AF_RXRPC;
  48. srx.srx_service = CM_SERVICE;
  49. srx.transport_type = SOCK_DGRAM;
  50. srx.transport_len = sizeof(srx.transport.sin6);
  51. srx.transport.sin6.sin6_family = AF_INET6;
  52. srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
  53. min_level = RXRPC_SECURITY_ENCRYPT;
  54. ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
  55. (void *)&min_level, sizeof(min_level));
  56. if (ret < 0)
  57. goto error_2;
  58. ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
  59. if (ret == -EADDRINUSE) {
  60. srx.transport.sin6.sin6_port = 0;
  61. ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
  62. }
  63. if (ret < 0)
  64. goto error_2;
  65. rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
  66. afs_rx_discard_new_call);
  67. ret = kernel_listen(socket, INT_MAX);
  68. if (ret < 0)
  69. goto error_2;
  70. net->socket = socket;
  71. afs_charge_preallocation(&net->charge_preallocation_work);
  72. _leave(" = 0");
  73. return 0;
  74. error_2:
  75. sock_release(socket);
  76. error_1:
  77. _leave(" = %d", ret);
  78. return ret;
  79. }
  80. /*
  81. * close the RxRPC socket AFS was using
  82. */
  83. void afs_close_socket(struct afs_net *net)
  84. {
  85. _enter("");
  86. kernel_listen(net->socket, 0);
  87. flush_workqueue(afs_async_calls);
  88. if (net->spare_incoming_call) {
  89. afs_put_call(net->spare_incoming_call);
  90. net->spare_incoming_call = NULL;
  91. }
  92. _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
  93. wait_var_event(&net->nr_outstanding_calls,
  94. !atomic_read(&net->nr_outstanding_calls));
  95. _debug("no outstanding calls");
  96. kernel_sock_shutdown(net->socket, SHUT_RDWR);
  97. flush_workqueue(afs_async_calls);
  98. sock_release(net->socket);
  99. _debug("dework");
  100. _leave("");
  101. }
  102. /*
  103. * Allocate a call.
  104. */
  105. static struct afs_call *afs_alloc_call(struct afs_net *net,
  106. const struct afs_call_type *type,
  107. gfp_t gfp)
  108. {
  109. struct afs_call *call;
  110. int o;
  111. call = kzalloc(sizeof(*call), gfp);
  112. if (!call)
  113. return NULL;
  114. call->type = type;
  115. call->net = net;
  116. call->debug_id = atomic_inc_return(&rxrpc_debug_id);
  117. atomic_set(&call->usage, 1);
  118. INIT_WORK(&call->async_work, afs_process_async_call);
  119. init_waitqueue_head(&call->waitq);
  120. spin_lock_init(&call->state_lock);
  121. o = atomic_inc_return(&net->nr_outstanding_calls);
  122. trace_afs_call(call, afs_call_trace_alloc, 1, o,
  123. __builtin_return_address(0));
  124. return call;
  125. }
  126. /*
  127. * Dispose of a reference on a call.
  128. */
  129. void afs_put_call(struct afs_call *call)
  130. {
  131. struct afs_net *net = call->net;
  132. int n = atomic_dec_return(&call->usage);
  133. int o = atomic_read(&net->nr_outstanding_calls);
  134. trace_afs_call(call, afs_call_trace_put, n + 1, o,
  135. __builtin_return_address(0));
  136. ASSERTCMP(n, >=, 0);
  137. if (n == 0) {
  138. ASSERT(!work_pending(&call->async_work));
  139. ASSERT(call->type->name != NULL);
  140. if (call->rxcall) {
  141. rxrpc_kernel_end_call(net->socket, call->rxcall);
  142. call->rxcall = NULL;
  143. }
  144. if (call->type->destructor)
  145. call->type->destructor(call);
  146. afs_put_server(call->net, call->cm_server);
  147. afs_put_cb_interest(call->net, call->cbi);
  148. kfree(call->request);
  149. trace_afs_call(call, afs_call_trace_free, 0, o,
  150. __builtin_return_address(0));
  151. kfree(call);
  152. o = atomic_dec_return(&net->nr_outstanding_calls);
  153. if (o == 0)
  154. wake_up_var(&net->nr_outstanding_calls);
  155. }
  156. }
  157. /*
  158. * Queue the call for actual work. Returns 0 unconditionally for convenience.
  159. */
  160. int afs_queue_call_work(struct afs_call *call)
  161. {
  162. int u = atomic_inc_return(&call->usage);
  163. trace_afs_call(call, afs_call_trace_work, u,
  164. atomic_read(&call->net->nr_outstanding_calls),
  165. __builtin_return_address(0));
  166. INIT_WORK(&call->work, call->type->work);
  167. if (!queue_work(afs_wq, &call->work))
  168. afs_put_call(call);
  169. return 0;
  170. }
  171. /*
  172. * allocate a call with flat request and reply buffers
  173. */
  174. struct afs_call *afs_alloc_flat_call(struct afs_net *net,
  175. const struct afs_call_type *type,
  176. size_t request_size, size_t reply_max)
  177. {
  178. struct afs_call *call;
  179. call = afs_alloc_call(net, type, GFP_NOFS);
  180. if (!call)
  181. goto nomem_call;
  182. if (request_size) {
  183. call->request_size = request_size;
  184. call->request = kmalloc(request_size, GFP_NOFS);
  185. if (!call->request)
  186. goto nomem_free;
  187. }
  188. if (reply_max) {
  189. call->reply_max = reply_max;
  190. call->buffer = kmalloc(reply_max, GFP_NOFS);
  191. if (!call->buffer)
  192. goto nomem_free;
  193. }
  194. call->operation_ID = type->op;
  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, 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. afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
  250. }
  251. /*
  252. * attach the data from a bunch of pages on an inode to a call
  253. */
  254. static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
  255. {
  256. struct bio_vec bv[AFS_BVEC_MAX];
  257. unsigned int bytes, nr, loop, offset;
  258. pgoff_t first = call->first, last = call->last;
  259. int ret;
  260. offset = call->first_offset;
  261. call->first_offset = 0;
  262. do {
  263. afs_load_bvec(call, msg, bv, first, last, offset);
  264. trace_afs_send_pages(call, msg, 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(call->net->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. trace_afs_sent_pages(call, call->first, last, first, ret);
  277. return ret;
  278. }
  279. /*
  280. * initiate a call
  281. */
  282. long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
  283. gfp_t gfp, bool async)
  284. {
  285. struct sockaddr_rxrpc *srx = ac->addr;
  286. struct rxrpc_call *rxcall;
  287. struct msghdr msg;
  288. struct kvec iov[1];
  289. s64 tx_total_len;
  290. int ret;
  291. _enter(",{%pISp},", &srx->transport);
  292. ASSERT(call->type != NULL);
  293. ASSERT(call->type->name != NULL);
  294. _debug("____MAKE %p{%s,%x} [%d]____",
  295. call, call->type->name, key_serial(call->key),
  296. atomic_read(&call->net->nr_outstanding_calls));
  297. call->async = async;
  298. /* Work out the length we're going to transmit. This is awkward for
  299. * calls such as FS.StoreData where there's an extra injection of data
  300. * after the initial fixed part.
  301. */
  302. tx_total_len = call->request_size;
  303. if (call->send_pages) {
  304. if (call->last == call->first) {
  305. tx_total_len += call->last_to - call->first_offset;
  306. } else {
  307. /* It looks mathematically like you should be able to
  308. * combine the following lines with the ones above, but
  309. * unsigned arithmetic is fun when it wraps...
  310. */
  311. tx_total_len += PAGE_SIZE - call->first_offset;
  312. tx_total_len += call->last_to;
  313. tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
  314. }
  315. }
  316. /* create a call */
  317. rxcall = rxrpc_kernel_begin_call(call->net->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->upgrade,
  324. call->debug_id);
  325. if (IS_ERR(rxcall)) {
  326. ret = PTR_ERR(rxcall);
  327. goto error_kill_call;
  328. }
  329. call->rxcall = rxcall;
  330. /* send the request */
  331. iov[0].iov_base = call->request;
  332. iov[0].iov_len = call->request_size;
  333. msg.msg_name = NULL;
  334. msg.msg_namelen = 0;
  335. iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
  336. msg.msg_control = NULL;
  337. msg.msg_controllen = 0;
  338. msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
  339. ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
  340. &msg, call->request_size,
  341. afs_notify_end_request_tx);
  342. if (ret < 0)
  343. goto error_do_abort;
  344. if (call->send_pages) {
  345. ret = afs_send_pages(call, &msg);
  346. if (ret < 0)
  347. goto error_do_abort;
  348. }
  349. /* at this point, an async call may no longer exist as it may have
  350. * already completed */
  351. if (call->async)
  352. return -EINPROGRESS;
  353. return afs_wait_for_call_to_complete(call, ac);
  354. error_do_abort:
  355. call->state = AFS_CALL_COMPLETE;
  356. if (ret != -ECONNABORTED) {
  357. rxrpc_kernel_abort_call(call->net->socket, rxcall,
  358. RX_USER_ABORT, ret, "KSD");
  359. } else {
  360. iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
  361. rxrpc_kernel_recv_data(call->net->socket, rxcall,
  362. &msg.msg_iter, false,
  363. &call->abort_code, &call->service_id);
  364. ac->abort_code = call->abort_code;
  365. ac->responded = true;
  366. }
  367. call->error = ret;
  368. trace_afs_call_done(call);
  369. error_kill_call:
  370. afs_put_call(call);
  371. ac->error = ret;
  372. _leave(" = %d", ret);
  373. return ret;
  374. }
  375. /*
  376. * deliver messages to a call
  377. */
  378. static void afs_deliver_to_call(struct afs_call *call)
  379. {
  380. enum afs_call_state state;
  381. u32 abort_code, remote_abort = 0;
  382. int ret;
  383. _enter("%s", call->type->name);
  384. while (state = READ_ONCE(call->state),
  385. state == AFS_CALL_CL_AWAIT_REPLY ||
  386. state == AFS_CALL_SV_AWAIT_OP_ID ||
  387. state == AFS_CALL_SV_AWAIT_REQUEST ||
  388. state == AFS_CALL_SV_AWAIT_ACK
  389. ) {
  390. if (state == AFS_CALL_SV_AWAIT_ACK) {
  391. struct iov_iter iter;
  392. iov_iter_kvec(&iter, READ, NULL, 0, 0);
  393. ret = rxrpc_kernel_recv_data(call->net->socket,
  394. call->rxcall, &iter, false,
  395. &remote_abort,
  396. &call->service_id);
  397. trace_afs_recv_data(call, 0, 0, false, ret);
  398. if (ret == -EINPROGRESS || ret == -EAGAIN)
  399. return;
  400. if (ret < 0 || ret == 1) {
  401. if (ret == 1)
  402. ret = 0;
  403. goto call_complete;
  404. }
  405. return;
  406. }
  407. ret = call->type->deliver(call);
  408. state = READ_ONCE(call->state);
  409. switch (ret) {
  410. case 0:
  411. if (state == AFS_CALL_CL_PROC_REPLY) {
  412. if (call->cbi)
  413. set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
  414. &call->cbi->server->flags);
  415. goto call_complete;
  416. }
  417. ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
  418. goto done;
  419. case -EINPROGRESS:
  420. case -EAGAIN:
  421. goto out;
  422. case -EIO:
  423. case -ECONNABORTED:
  424. ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
  425. goto done;
  426. case -ENOTSUPP:
  427. abort_code = RXGEN_OPCODE;
  428. rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  429. abort_code, ret, "KIV");
  430. goto local_abort;
  431. case -ENODATA:
  432. case -EBADMSG:
  433. case -EMSGSIZE:
  434. default:
  435. abort_code = RXGEN_CC_UNMARSHAL;
  436. if (state != AFS_CALL_CL_AWAIT_REPLY)
  437. abort_code = RXGEN_SS_UNMARSHAL;
  438. rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  439. abort_code, -EBADMSG, "KUM");
  440. goto local_abort;
  441. }
  442. }
  443. done:
  444. if (state == AFS_CALL_COMPLETE && call->incoming)
  445. afs_put_call(call);
  446. out:
  447. _leave("");
  448. return;
  449. local_abort:
  450. abort_code = 0;
  451. call_complete:
  452. afs_set_call_complete(call, ret, remote_abort);
  453. state = AFS_CALL_COMPLETE;
  454. goto done;
  455. }
  456. /*
  457. * wait synchronously for a call to complete
  458. */
  459. static long afs_wait_for_call_to_complete(struct afs_call *call,
  460. struct afs_addr_cursor *ac)
  461. {
  462. signed long rtt2, timeout;
  463. long ret;
  464. u64 rtt;
  465. u32 life, last_life;
  466. DECLARE_WAITQUEUE(myself, current);
  467. _enter("");
  468. rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
  469. rtt2 = nsecs_to_jiffies64(rtt) * 2;
  470. if (rtt2 < 2)
  471. rtt2 = 2;
  472. timeout = rtt2;
  473. last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
  474. add_wait_queue(&call->waitq, &myself);
  475. for (;;) {
  476. set_current_state(TASK_UNINTERRUPTIBLE);
  477. /* deliver any messages that are in the queue */
  478. if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
  479. call->need_attention) {
  480. call->need_attention = false;
  481. __set_current_state(TASK_RUNNING);
  482. afs_deliver_to_call(call);
  483. continue;
  484. }
  485. if (afs_check_call_state(call, AFS_CALL_COMPLETE))
  486. break;
  487. life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
  488. if (timeout == 0 &&
  489. life == last_life && signal_pending(current))
  490. break;
  491. if (life != last_life) {
  492. timeout = rtt2;
  493. last_life = life;
  494. }
  495. timeout = schedule_timeout(timeout);
  496. }
  497. remove_wait_queue(&call->waitq, &myself);
  498. __set_current_state(TASK_RUNNING);
  499. /* Kill off the call if it's still live. */
  500. if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
  501. _debug("call interrupted");
  502. if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  503. RX_USER_ABORT, -EINTR, "KWI"))
  504. afs_set_call_complete(call, -EINTR, 0);
  505. }
  506. spin_lock_bh(&call->state_lock);
  507. ac->abort_code = call->abort_code;
  508. ac->error = call->error;
  509. spin_unlock_bh(&call->state_lock);
  510. ret = ac->error;
  511. switch (ret) {
  512. case 0:
  513. if (call->ret_reply0) {
  514. ret = (long)call->reply[0];
  515. call->reply[0] = NULL;
  516. }
  517. /* Fall through */
  518. case -ECONNABORTED:
  519. ac->responded = true;
  520. break;
  521. }
  522. _debug("call complete");
  523. afs_put_call(call);
  524. _leave(" = %p", (void *)ret);
  525. return ret;
  526. }
  527. /*
  528. * wake up a waiting call
  529. */
  530. static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
  531. unsigned long call_user_ID)
  532. {
  533. struct afs_call *call = (struct afs_call *)call_user_ID;
  534. call->need_attention = true;
  535. wake_up(&call->waitq);
  536. }
  537. /*
  538. * wake up an asynchronous call
  539. */
  540. static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
  541. unsigned long call_user_ID)
  542. {
  543. struct afs_call *call = (struct afs_call *)call_user_ID;
  544. int u;
  545. trace_afs_notify_call(rxcall, call);
  546. call->need_attention = true;
  547. u = atomic_fetch_add_unless(&call->usage, 1, 0);
  548. if (u != 0) {
  549. trace_afs_call(call, afs_call_trace_wake, u,
  550. atomic_read(&call->net->nr_outstanding_calls),
  551. __builtin_return_address(0));
  552. if (!queue_work(afs_async_calls, &call->async_work))
  553. afs_put_call(call);
  554. }
  555. }
  556. /*
  557. * Delete an asynchronous call. The work item carries a ref to the call struct
  558. * that we need to release.
  559. */
  560. static void afs_delete_async_call(struct work_struct *work)
  561. {
  562. struct afs_call *call = container_of(work, struct afs_call, async_work);
  563. _enter("");
  564. afs_put_call(call);
  565. _leave("");
  566. }
  567. /*
  568. * Perform I/O processing on an asynchronous call. The work item carries a ref
  569. * to the call struct that we either need to release or to pass on.
  570. */
  571. static void afs_process_async_call(struct work_struct *work)
  572. {
  573. struct afs_call *call = container_of(work, struct afs_call, async_work);
  574. _enter("");
  575. if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
  576. call->need_attention = false;
  577. afs_deliver_to_call(call);
  578. }
  579. if (call->state == AFS_CALL_COMPLETE) {
  580. /* We have two refs to release - one from the alloc and one
  581. * queued with the work item - and we can't just deallocate the
  582. * call because the work item may be queued again.
  583. */
  584. call->async_work.func = afs_delete_async_call;
  585. if (!queue_work(afs_async_calls, &call->async_work))
  586. afs_put_call(call);
  587. }
  588. afs_put_call(call);
  589. _leave("");
  590. }
  591. static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
  592. {
  593. struct afs_call *call = (struct afs_call *)user_call_ID;
  594. call->rxcall = rxcall;
  595. }
  596. /*
  597. * Charge the incoming call preallocation.
  598. */
  599. void afs_charge_preallocation(struct work_struct *work)
  600. {
  601. struct afs_net *net =
  602. container_of(work, struct afs_net, charge_preallocation_work);
  603. struct afs_call *call = net->spare_incoming_call;
  604. for (;;) {
  605. if (!call) {
  606. call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
  607. if (!call)
  608. break;
  609. call->async = true;
  610. call->state = AFS_CALL_SV_AWAIT_OP_ID;
  611. init_waitqueue_head(&call->waitq);
  612. }
  613. if (rxrpc_kernel_charge_accept(net->socket,
  614. afs_wake_up_async_call,
  615. afs_rx_attach,
  616. (unsigned long)call,
  617. GFP_KERNEL,
  618. call->debug_id) < 0)
  619. break;
  620. call = NULL;
  621. }
  622. net->spare_incoming_call = call;
  623. }
  624. /*
  625. * Discard a preallocated call when a socket is shut down.
  626. */
  627. static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
  628. unsigned long user_call_ID)
  629. {
  630. struct afs_call *call = (struct afs_call *)user_call_ID;
  631. call->rxcall = NULL;
  632. afs_put_call(call);
  633. }
  634. /*
  635. * Notification of an incoming call.
  636. */
  637. static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
  638. unsigned long user_call_ID)
  639. {
  640. struct afs_net *net = afs_sock2net(sk);
  641. queue_work(afs_wq, &net->charge_preallocation_work);
  642. }
  643. /*
  644. * Grab the operation ID from an incoming cache manager call. The socket
  645. * buffer is discarded on error or if we don't yet have sufficient data.
  646. */
  647. static int afs_deliver_cm_op_id(struct afs_call *call)
  648. {
  649. int ret;
  650. _enter("{%zu}", call->offset);
  651. ASSERTCMP(call->offset, <, 4);
  652. /* the operation ID forms the first four bytes of the request data */
  653. ret = afs_extract_data(call, &call->tmp, 4, true);
  654. if (ret < 0)
  655. return ret;
  656. call->operation_ID = ntohl(call->tmp);
  657. afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
  658. call->offset = 0;
  659. /* ask the cache manager to route the call (it'll change the call type
  660. * if successful) */
  661. if (!afs_cm_incoming_call(call))
  662. return -ENOTSUPP;
  663. trace_afs_cb_call(call);
  664. /* pass responsibility for the remainer of this message off to the
  665. * cache manager op */
  666. return call->type->deliver(call);
  667. }
  668. /*
  669. * Advance the AFS call state when an RxRPC service call ends the transmit
  670. * phase.
  671. */
  672. static void afs_notify_end_reply_tx(struct sock *sock,
  673. struct rxrpc_call *rxcall,
  674. unsigned long call_user_ID)
  675. {
  676. struct afs_call *call = (struct afs_call *)call_user_ID;
  677. afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
  678. }
  679. /*
  680. * send an empty reply
  681. */
  682. void afs_send_empty_reply(struct afs_call *call)
  683. {
  684. struct afs_net *net = call->net;
  685. struct msghdr msg;
  686. _enter("");
  687. rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
  688. msg.msg_name = NULL;
  689. msg.msg_namelen = 0;
  690. iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
  691. msg.msg_control = NULL;
  692. msg.msg_controllen = 0;
  693. msg.msg_flags = 0;
  694. switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
  695. afs_notify_end_reply_tx)) {
  696. case 0:
  697. _leave(" [replied]");
  698. return;
  699. case -ENOMEM:
  700. _debug("oom");
  701. rxrpc_kernel_abort_call(net->socket, call->rxcall,
  702. RX_USER_ABORT, -ENOMEM, "KOO");
  703. default:
  704. _leave(" [error]");
  705. return;
  706. }
  707. }
  708. /*
  709. * send a simple reply
  710. */
  711. void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
  712. {
  713. struct afs_net *net = call->net;
  714. struct msghdr msg;
  715. struct kvec iov[1];
  716. int n;
  717. _enter("");
  718. rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
  719. iov[0].iov_base = (void *) buf;
  720. iov[0].iov_len = len;
  721. msg.msg_name = NULL;
  722. msg.msg_namelen = 0;
  723. iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
  724. msg.msg_control = NULL;
  725. msg.msg_controllen = 0;
  726. msg.msg_flags = 0;
  727. n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
  728. afs_notify_end_reply_tx);
  729. if (n >= 0) {
  730. /* Success */
  731. _leave(" [replied]");
  732. return;
  733. }
  734. if (n == -ENOMEM) {
  735. _debug("oom");
  736. rxrpc_kernel_abort_call(net->socket, call->rxcall,
  737. RX_USER_ABORT, -ENOMEM, "KOO");
  738. }
  739. _leave(" [error]");
  740. }
  741. /*
  742. * Extract a piece of data from the received data socket buffers.
  743. */
  744. int afs_extract_data(struct afs_call *call, void *buf, size_t count,
  745. bool want_more)
  746. {
  747. struct afs_net *net = call->net;
  748. struct iov_iter iter;
  749. struct kvec iov;
  750. enum afs_call_state state;
  751. u32 remote_abort = 0;
  752. int ret;
  753. _enter("{%s,%zu},,%zu,%d",
  754. call->type->name, call->offset, count, want_more);
  755. ASSERTCMP(call->offset, <=, count);
  756. iov.iov_base = buf + call->offset;
  757. iov.iov_len = count - call->offset;
  758. iov_iter_kvec(&iter, READ, &iov, 1, count - call->offset);
  759. ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, &iter,
  760. want_more, &remote_abort,
  761. &call->service_id);
  762. call->offset += (count - call->offset) - iov_iter_count(&iter);
  763. trace_afs_recv_data(call, count, call->offset, want_more, ret);
  764. if (ret == 0 || ret == -EAGAIN)
  765. return ret;
  766. state = READ_ONCE(call->state);
  767. if (ret == 1) {
  768. switch (state) {
  769. case AFS_CALL_CL_AWAIT_REPLY:
  770. afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
  771. break;
  772. case AFS_CALL_SV_AWAIT_REQUEST:
  773. afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
  774. break;
  775. case AFS_CALL_COMPLETE:
  776. kdebug("prem complete %d", call->error);
  777. return -EIO;
  778. default:
  779. break;
  780. }
  781. return 0;
  782. }
  783. afs_set_call_complete(call, ret, remote_abort);
  784. return ret;
  785. }
  786. /*
  787. * Log protocol error production.
  788. */
  789. noinline int afs_protocol_error(struct afs_call *call, int error)
  790. {
  791. trace_afs_protocol_error(call, error, __builtin_return_address(0));
  792. return error;
  793. }