rxrpc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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, 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 | ITER_BVEC, bv, nr, bytes);
  240. }
  241. /*
  242. * Advance the AFS call state when the RxRPC call ends the transmit phase.
  243. */
  244. static void afs_notify_end_request_tx(struct sock *sock,
  245. struct rxrpc_call *rxcall,
  246. unsigned long call_user_ID)
  247. {
  248. struct afs_call *call = (struct afs_call *)call_user_ID;
  249. 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 | ITER_KVEC, iov, 1,
  336. call->request_size);
  337. msg.msg_control = NULL;
  338. msg.msg_controllen = 0;
  339. msg.msg_flags = MSG_WAITALL | (call->send_pages ? 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->send_pages) {
  346. ret = afs_send_pages(call, &msg);
  347. if (ret < 0)
  348. goto error_do_abort;
  349. }
  350. /* at this point, an async call may no longer exist as it may have
  351. * already completed */
  352. if (call->async)
  353. return -EINPROGRESS;
  354. return afs_wait_for_call_to_complete(call, ac);
  355. error_do_abort:
  356. call->state = AFS_CALL_COMPLETE;
  357. if (ret != -ECONNABORTED) {
  358. rxrpc_kernel_abort_call(call->net->socket, rxcall,
  359. RX_USER_ABORT, ret, "KSD");
  360. } else {
  361. iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, NULL, 0, 0);
  362. rxrpc_kernel_recv_data(call->net->socket, rxcall,
  363. &msg.msg_iter, false,
  364. &call->abort_code, &call->service_id);
  365. ac->abort_code = call->abort_code;
  366. ac->responded = true;
  367. }
  368. call->error = ret;
  369. trace_afs_call_done(call);
  370. error_kill_call:
  371. afs_put_call(call);
  372. ac->error = ret;
  373. _leave(" = %d", ret);
  374. return ret;
  375. }
  376. /*
  377. * deliver messages to a call
  378. */
  379. static void afs_deliver_to_call(struct afs_call *call)
  380. {
  381. enum afs_call_state state;
  382. u32 abort_code, remote_abort = 0;
  383. int ret;
  384. _enter("%s", call->type->name);
  385. while (state = READ_ONCE(call->state),
  386. state == AFS_CALL_CL_AWAIT_REPLY ||
  387. state == AFS_CALL_SV_AWAIT_OP_ID ||
  388. state == AFS_CALL_SV_AWAIT_REQUEST ||
  389. state == AFS_CALL_SV_AWAIT_ACK
  390. ) {
  391. if (state == AFS_CALL_SV_AWAIT_ACK) {
  392. struct iov_iter iter;
  393. iov_iter_kvec(&iter, READ | ITER_KVEC, NULL, 0, 0);
  394. ret = rxrpc_kernel_recv_data(call->net->socket,
  395. call->rxcall, &iter, false,
  396. &remote_abort,
  397. &call->service_id);
  398. trace_afs_recv_data(call, 0, 0, false, ret);
  399. if (ret == -EINPROGRESS || ret == -EAGAIN)
  400. return;
  401. if (ret < 0 || ret == 1) {
  402. if (ret == 1)
  403. ret = 0;
  404. goto call_complete;
  405. }
  406. return;
  407. }
  408. ret = call->type->deliver(call);
  409. state = READ_ONCE(call->state);
  410. switch (ret) {
  411. case 0:
  412. if (state == AFS_CALL_CL_PROC_REPLY) {
  413. if (call->cbi)
  414. set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
  415. &call->cbi->server->flags);
  416. goto call_complete;
  417. }
  418. ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
  419. goto done;
  420. case -EINPROGRESS:
  421. case -EAGAIN:
  422. goto out;
  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 -EIO:
  432. pr_err("kAFS: Call %u in bad state %u\n",
  433. call->debug_id, state);
  434. /* Fall through */
  435. case -ENODATA:
  436. case -EBADMSG:
  437. case -EMSGSIZE:
  438. default:
  439. abort_code = RXGEN_CC_UNMARSHAL;
  440. if (state != AFS_CALL_CL_AWAIT_REPLY)
  441. abort_code = RXGEN_SS_UNMARSHAL;
  442. rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  443. abort_code, -EBADMSG, "KUM");
  444. goto local_abort;
  445. }
  446. }
  447. done:
  448. if (state == AFS_CALL_COMPLETE && call->incoming)
  449. afs_put_call(call);
  450. out:
  451. _leave("");
  452. return;
  453. local_abort:
  454. abort_code = 0;
  455. call_complete:
  456. afs_set_call_complete(call, ret, remote_abort);
  457. state = AFS_CALL_COMPLETE;
  458. goto done;
  459. }
  460. /*
  461. * wait synchronously for a call to complete
  462. */
  463. static long afs_wait_for_call_to_complete(struct afs_call *call,
  464. struct afs_addr_cursor *ac)
  465. {
  466. signed long rtt2, timeout;
  467. long ret;
  468. u64 rtt;
  469. u32 life, last_life;
  470. DECLARE_WAITQUEUE(myself, current);
  471. _enter("");
  472. rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
  473. rtt2 = nsecs_to_jiffies64(rtt) * 2;
  474. if (rtt2 < 2)
  475. rtt2 = 2;
  476. timeout = rtt2;
  477. last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
  478. add_wait_queue(&call->waitq, &myself);
  479. for (;;) {
  480. set_current_state(TASK_UNINTERRUPTIBLE);
  481. /* deliver any messages that are in the queue */
  482. if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
  483. call->need_attention) {
  484. call->need_attention = false;
  485. __set_current_state(TASK_RUNNING);
  486. afs_deliver_to_call(call);
  487. timeout = rtt2;
  488. continue;
  489. }
  490. if (afs_check_call_state(call, AFS_CALL_COMPLETE))
  491. break;
  492. life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
  493. if (timeout == 0 &&
  494. life == last_life && signal_pending(current))
  495. break;
  496. if (life != last_life) {
  497. timeout = rtt2;
  498. last_life = life;
  499. }
  500. timeout = schedule_timeout(timeout);
  501. }
  502. remove_wait_queue(&call->waitq, &myself);
  503. __set_current_state(TASK_RUNNING);
  504. /* Kill off the call if it's still live. */
  505. if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
  506. _debug("call interrupted");
  507. if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  508. RX_USER_ABORT, -EINTR, "KWI"))
  509. afs_set_call_complete(call, -EINTR, 0);
  510. }
  511. spin_lock_bh(&call->state_lock);
  512. ac->abort_code = call->abort_code;
  513. ac->error = call->error;
  514. spin_unlock_bh(&call->state_lock);
  515. ret = ac->error;
  516. switch (ret) {
  517. case 0:
  518. if (call->ret_reply0) {
  519. ret = (long)call->reply[0];
  520. call->reply[0] = NULL;
  521. }
  522. /* Fall through */
  523. case -ECONNABORTED:
  524. ac->responded = true;
  525. break;
  526. }
  527. _debug("call complete");
  528. afs_put_call(call);
  529. _leave(" = %p", (void *)ret);
  530. return ret;
  531. }
  532. /*
  533. * wake up a waiting call
  534. */
  535. static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
  536. unsigned long call_user_ID)
  537. {
  538. struct afs_call *call = (struct afs_call *)call_user_ID;
  539. call->need_attention = true;
  540. wake_up(&call->waitq);
  541. }
  542. /*
  543. * wake up an asynchronous call
  544. */
  545. static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
  546. unsigned long call_user_ID)
  547. {
  548. struct afs_call *call = (struct afs_call *)call_user_ID;
  549. int u;
  550. trace_afs_notify_call(rxcall, call);
  551. call->need_attention = true;
  552. u = atomic_fetch_add_unless(&call->usage, 1, 0);
  553. if (u != 0) {
  554. trace_afs_call(call, afs_call_trace_wake, u + 1,
  555. atomic_read(&call->net->nr_outstanding_calls),
  556. __builtin_return_address(0));
  557. if (!queue_work(afs_async_calls, &call->async_work))
  558. afs_put_call(call);
  559. }
  560. }
  561. /*
  562. * Delete an asynchronous call. The work item carries a ref to the call struct
  563. * that we need to release.
  564. */
  565. static void afs_delete_async_call(struct work_struct *work)
  566. {
  567. struct afs_call *call = container_of(work, struct afs_call, async_work);
  568. _enter("");
  569. afs_put_call(call);
  570. _leave("");
  571. }
  572. /*
  573. * Perform I/O processing on an asynchronous call. The work item carries a ref
  574. * to the call struct that we either need to release or to pass on.
  575. */
  576. static void afs_process_async_call(struct work_struct *work)
  577. {
  578. struct afs_call *call = container_of(work, struct afs_call, async_work);
  579. _enter("");
  580. if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
  581. call->need_attention = false;
  582. afs_deliver_to_call(call);
  583. }
  584. if (call->state == AFS_CALL_COMPLETE) {
  585. /* We have two refs to release - one from the alloc and one
  586. * queued with the work item - and we can't just deallocate the
  587. * call because the work item may be queued again.
  588. */
  589. call->async_work.func = afs_delete_async_call;
  590. if (!queue_work(afs_async_calls, &call->async_work))
  591. afs_put_call(call);
  592. }
  593. afs_put_call(call);
  594. _leave("");
  595. }
  596. static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
  597. {
  598. struct afs_call *call = (struct afs_call *)user_call_ID;
  599. call->rxcall = rxcall;
  600. }
  601. /*
  602. * Charge the incoming call preallocation.
  603. */
  604. void afs_charge_preallocation(struct work_struct *work)
  605. {
  606. struct afs_net *net =
  607. container_of(work, struct afs_net, charge_preallocation_work);
  608. struct afs_call *call = net->spare_incoming_call;
  609. for (;;) {
  610. if (!call) {
  611. call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
  612. if (!call)
  613. break;
  614. call->async = true;
  615. call->state = AFS_CALL_SV_AWAIT_OP_ID;
  616. init_waitqueue_head(&call->waitq);
  617. }
  618. if (rxrpc_kernel_charge_accept(net->socket,
  619. afs_wake_up_async_call,
  620. afs_rx_attach,
  621. (unsigned long)call,
  622. GFP_KERNEL,
  623. call->debug_id) < 0)
  624. break;
  625. call = NULL;
  626. }
  627. net->spare_incoming_call = call;
  628. }
  629. /*
  630. * Discard a preallocated call when a socket is shut down.
  631. */
  632. static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
  633. unsigned long user_call_ID)
  634. {
  635. struct afs_call *call = (struct afs_call *)user_call_ID;
  636. call->rxcall = NULL;
  637. afs_put_call(call);
  638. }
  639. /*
  640. * Notification of an incoming call.
  641. */
  642. static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
  643. unsigned long user_call_ID)
  644. {
  645. struct afs_net *net = afs_sock2net(sk);
  646. queue_work(afs_wq, &net->charge_preallocation_work);
  647. }
  648. /*
  649. * Grab the operation ID from an incoming cache manager call. The socket
  650. * buffer is discarded on error or if we don't yet have sufficient data.
  651. */
  652. static int afs_deliver_cm_op_id(struct afs_call *call)
  653. {
  654. int ret;
  655. _enter("{%zu}", call->offset);
  656. ASSERTCMP(call->offset, <, 4);
  657. /* the operation ID forms the first four bytes of the request data */
  658. ret = afs_extract_data(call, &call->tmp, 4, true);
  659. if (ret < 0)
  660. return ret;
  661. call->operation_ID = ntohl(call->tmp);
  662. afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
  663. call->offset = 0;
  664. /* ask the cache manager to route the call (it'll change the call type
  665. * if successful) */
  666. if (!afs_cm_incoming_call(call))
  667. return -ENOTSUPP;
  668. trace_afs_cb_call(call);
  669. /* pass responsibility for the remainer of this message off to the
  670. * cache manager op */
  671. return call->type->deliver(call);
  672. }
  673. /*
  674. * Advance the AFS call state when an RxRPC service call ends the transmit
  675. * phase.
  676. */
  677. static void afs_notify_end_reply_tx(struct sock *sock,
  678. struct rxrpc_call *rxcall,
  679. unsigned long call_user_ID)
  680. {
  681. struct afs_call *call = (struct afs_call *)call_user_ID;
  682. afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
  683. }
  684. /*
  685. * send an empty reply
  686. */
  687. void afs_send_empty_reply(struct afs_call *call)
  688. {
  689. struct afs_net *net = call->net;
  690. struct msghdr msg;
  691. _enter("");
  692. rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
  693. msg.msg_name = NULL;
  694. msg.msg_namelen = 0;
  695. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
  696. msg.msg_control = NULL;
  697. msg.msg_controllen = 0;
  698. msg.msg_flags = 0;
  699. switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
  700. afs_notify_end_reply_tx)) {
  701. case 0:
  702. _leave(" [replied]");
  703. return;
  704. case -ENOMEM:
  705. _debug("oom");
  706. rxrpc_kernel_abort_call(net->socket, call->rxcall,
  707. RX_USER_ABORT, -ENOMEM, "KOO");
  708. default:
  709. _leave(" [error]");
  710. return;
  711. }
  712. }
  713. /*
  714. * send a simple reply
  715. */
  716. void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
  717. {
  718. struct afs_net *net = call->net;
  719. struct msghdr msg;
  720. struct kvec iov[1];
  721. int n;
  722. _enter("");
  723. rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
  724. iov[0].iov_base = (void *) buf;
  725. iov[0].iov_len = len;
  726. msg.msg_name = NULL;
  727. msg.msg_namelen = 0;
  728. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
  729. msg.msg_control = NULL;
  730. msg.msg_controllen = 0;
  731. msg.msg_flags = 0;
  732. n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
  733. afs_notify_end_reply_tx);
  734. if (n >= 0) {
  735. /* Success */
  736. _leave(" [replied]");
  737. return;
  738. }
  739. if (n == -ENOMEM) {
  740. _debug("oom");
  741. rxrpc_kernel_abort_call(net->socket, call->rxcall,
  742. RX_USER_ABORT, -ENOMEM, "KOO");
  743. }
  744. _leave(" [error]");
  745. }
  746. /*
  747. * Extract a piece of data from the received data socket buffers.
  748. */
  749. int afs_extract_data(struct afs_call *call, void *buf, size_t count,
  750. bool want_more)
  751. {
  752. struct afs_net *net = call->net;
  753. struct iov_iter iter;
  754. struct kvec iov;
  755. enum afs_call_state state;
  756. u32 remote_abort = 0;
  757. int ret;
  758. _enter("{%s,%zu},,%zu,%d",
  759. call->type->name, call->offset, count, want_more);
  760. ASSERTCMP(call->offset, <=, count);
  761. iov.iov_base = buf + call->offset;
  762. iov.iov_len = count - call->offset;
  763. iov_iter_kvec(&iter, ITER_KVEC | READ, &iov, 1, count - call->offset);
  764. ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, &iter,
  765. want_more, &remote_abort,
  766. &call->service_id);
  767. call->offset += (count - call->offset) - iov_iter_count(&iter);
  768. trace_afs_recv_data(call, count, call->offset, want_more, ret);
  769. if (ret == 0 || ret == -EAGAIN)
  770. return ret;
  771. state = READ_ONCE(call->state);
  772. if (ret == 1) {
  773. switch (state) {
  774. case AFS_CALL_CL_AWAIT_REPLY:
  775. afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
  776. break;
  777. case AFS_CALL_SV_AWAIT_REQUEST:
  778. afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
  779. break;
  780. case AFS_CALL_COMPLETE:
  781. kdebug("prem complete %d", call->error);
  782. return -EIO;
  783. default:
  784. break;
  785. }
  786. return 0;
  787. }
  788. afs_set_call_complete(call, ret, remote_abort);
  789. return ret;
  790. }
  791. /*
  792. * Log protocol error production.
  793. */
  794. noinline int afs_protocol_error(struct afs_call *call, int error)
  795. {
  796. trace_afs_protocol_error(call, error, __builtin_return_address(0));
  797. return error;
  798. }