rxrpc.c 19 KB

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