rxrpc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. int ret;
  272. _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
  273. ASSERT(call->type != NULL);
  274. ASSERT(call->type->name != NULL);
  275. _debug("____MAKE %p{%s,%x} [%d]____",
  276. call, call->type->name, key_serial(call->key),
  277. atomic_read(&afs_outstanding_calls));
  278. call->wait_mode = wait_mode;
  279. INIT_WORK(&call->async_work, afs_process_async_call);
  280. memset(&srx, 0, sizeof(srx));
  281. srx.srx_family = AF_RXRPC;
  282. srx.srx_service = call->service_id;
  283. srx.transport_type = SOCK_DGRAM;
  284. srx.transport_len = sizeof(srx.transport.sin);
  285. srx.transport.sin.sin_family = AF_INET;
  286. srx.transport.sin.sin_port = call->port;
  287. memcpy(&srx.transport.sin.sin_addr, addr, 4);
  288. /* create a call */
  289. rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
  290. (unsigned long) call, gfp,
  291. wait_mode->notify_rx);
  292. call->key = NULL;
  293. if (IS_ERR(rxcall)) {
  294. ret = PTR_ERR(rxcall);
  295. goto error_kill_call;
  296. }
  297. call->rxcall = rxcall;
  298. /* send the request */
  299. iov[0].iov_base = call->request;
  300. iov[0].iov_len = call->request_size;
  301. msg.msg_name = NULL;
  302. msg.msg_namelen = 0;
  303. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1,
  304. call->request_size);
  305. msg.msg_control = NULL;
  306. msg.msg_controllen = 0;
  307. msg.msg_flags = (call->send_pages ? MSG_MORE : 0);
  308. /* have to change the state *before* sending the last packet as RxRPC
  309. * might give us the reply before it returns from sending the
  310. * request */
  311. if (!call->send_pages)
  312. call->state = AFS_CALL_AWAIT_REPLY;
  313. ret = rxrpc_kernel_send_data(afs_socket, rxcall,
  314. &msg, call->request_size);
  315. if (ret < 0)
  316. goto error_do_abort;
  317. if (call->send_pages) {
  318. ret = afs_send_pages(call, &msg, iov);
  319. if (ret < 0)
  320. goto error_do_abort;
  321. }
  322. /* at this point, an async call may no longer exist as it may have
  323. * already completed */
  324. return wait_mode->wait(call);
  325. error_do_abort:
  326. rxrpc_kernel_abort_call(afs_socket, rxcall, RX_USER_ABORT, -ret, "KSD");
  327. error_kill_call:
  328. afs_end_call(call);
  329. _leave(" = %d", ret);
  330. return ret;
  331. }
  332. /*
  333. * deliver messages to a call
  334. */
  335. static void afs_deliver_to_call(struct afs_call *call)
  336. {
  337. u32 abort_code;
  338. int ret;
  339. _enter("%s", call->type->name);
  340. while (call->state == AFS_CALL_AWAIT_REPLY ||
  341. call->state == AFS_CALL_AWAIT_OP_ID ||
  342. call->state == AFS_CALL_AWAIT_REQUEST ||
  343. call->state == AFS_CALL_AWAIT_ACK
  344. ) {
  345. if (call->state == AFS_CALL_AWAIT_ACK) {
  346. size_t offset = 0;
  347. ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
  348. NULL, 0, &offset, false,
  349. &call->abort_code);
  350. if (ret == -EINPROGRESS || ret == -EAGAIN)
  351. return;
  352. if (ret == 1 || ret < 0) {
  353. call->state = AFS_CALL_COMPLETE;
  354. goto done;
  355. }
  356. return;
  357. }
  358. ret = call->type->deliver(call);
  359. switch (ret) {
  360. case 0:
  361. if (call->state == AFS_CALL_AWAIT_REPLY)
  362. call->state = AFS_CALL_COMPLETE;
  363. goto done;
  364. case -EINPROGRESS:
  365. case -EAGAIN:
  366. goto out;
  367. case -ENOTCONN:
  368. abort_code = RX_CALL_DEAD;
  369. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  370. abort_code, -ret, "KNC");
  371. goto do_abort;
  372. case -ENOTSUPP:
  373. abort_code = RX_INVALID_OPERATION;
  374. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  375. abort_code, -ret, "KIV");
  376. goto do_abort;
  377. case -ENODATA:
  378. case -EBADMSG:
  379. case -EMSGSIZE:
  380. default:
  381. abort_code = RXGEN_CC_UNMARSHAL;
  382. if (call->state != AFS_CALL_AWAIT_REPLY)
  383. abort_code = RXGEN_SS_UNMARSHAL;
  384. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  385. abort_code, EBADMSG, "KUM");
  386. goto do_abort;
  387. }
  388. }
  389. done:
  390. if (call->state == AFS_CALL_COMPLETE && call->incoming)
  391. afs_end_call(call);
  392. out:
  393. _leave("");
  394. return;
  395. do_abort:
  396. call->error = ret;
  397. call->state = AFS_CALL_COMPLETE;
  398. goto done;
  399. }
  400. /*
  401. * wait synchronously for a call to complete
  402. */
  403. static int afs_wait_for_call_to_complete(struct afs_call *call)
  404. {
  405. const char *abort_why;
  406. int ret;
  407. DECLARE_WAITQUEUE(myself, current);
  408. _enter("");
  409. add_wait_queue(&call->waitq, &myself);
  410. for (;;) {
  411. set_current_state(TASK_INTERRUPTIBLE);
  412. /* deliver any messages that are in the queue */
  413. if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
  414. call->need_attention = false;
  415. __set_current_state(TASK_RUNNING);
  416. afs_deliver_to_call(call);
  417. continue;
  418. }
  419. abort_why = "KWC";
  420. ret = call->error;
  421. if (call->state == AFS_CALL_COMPLETE)
  422. break;
  423. abort_why = "KWI";
  424. ret = -EINTR;
  425. if (signal_pending(current))
  426. break;
  427. schedule();
  428. }
  429. remove_wait_queue(&call->waitq, &myself);
  430. __set_current_state(TASK_RUNNING);
  431. /* kill the call */
  432. if (call->state < AFS_CALL_COMPLETE) {
  433. _debug("call incomplete");
  434. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  435. RX_CALL_DEAD, -ret, abort_why);
  436. }
  437. _debug("call complete");
  438. afs_end_call(call);
  439. _leave(" = %d", ret);
  440. return ret;
  441. }
  442. /*
  443. * wake up a waiting call
  444. */
  445. static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
  446. unsigned long call_user_ID)
  447. {
  448. struct afs_call *call = (struct afs_call *)call_user_ID;
  449. call->need_attention = true;
  450. wake_up(&call->waitq);
  451. }
  452. /*
  453. * wake up an asynchronous call
  454. */
  455. static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
  456. unsigned long call_user_ID)
  457. {
  458. struct afs_call *call = (struct afs_call *)call_user_ID;
  459. call->need_attention = true;
  460. queue_work(afs_async_calls, &call->async_work);
  461. }
  462. /*
  463. * put a call into asynchronous mode
  464. * - mustn't touch the call descriptor as the call my have completed by the
  465. * time we get here
  466. */
  467. static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
  468. {
  469. _enter("");
  470. return -EINPROGRESS;
  471. }
  472. /*
  473. * delete an asynchronous call
  474. */
  475. static void afs_delete_async_call(struct work_struct *work)
  476. {
  477. struct afs_call *call = container_of(work, struct afs_call, async_work);
  478. _enter("");
  479. afs_free_call(call);
  480. _leave("");
  481. }
  482. /*
  483. * perform processing on an asynchronous call
  484. */
  485. static void afs_process_async_call(struct work_struct *work)
  486. {
  487. struct afs_call *call = container_of(work, struct afs_call, async_work);
  488. _enter("");
  489. if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
  490. call->need_attention = false;
  491. afs_deliver_to_call(call);
  492. }
  493. if (call->state == AFS_CALL_COMPLETE && call->wait_mode) {
  494. if (call->wait_mode->async_complete)
  495. call->wait_mode->async_complete(call->reply,
  496. call->error);
  497. call->reply = NULL;
  498. /* kill the call */
  499. afs_end_call_nofree(call);
  500. /* we can't just delete the call because the work item may be
  501. * queued */
  502. call->async_work.func = afs_delete_async_call;
  503. queue_work(afs_async_calls, &call->async_work);
  504. }
  505. _leave("");
  506. }
  507. static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
  508. {
  509. struct afs_call *call = (struct afs_call *)user_call_ID;
  510. call->rxcall = rxcall;
  511. }
  512. /*
  513. * Charge the incoming call preallocation.
  514. */
  515. static void afs_charge_preallocation(struct work_struct *work)
  516. {
  517. struct afs_call *call = afs_spare_incoming_call;
  518. for (;;) {
  519. if (!call) {
  520. call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
  521. if (!call)
  522. break;
  523. INIT_WORK(&call->async_work, afs_process_async_call);
  524. call->wait_mode = &afs_async_incoming_call;
  525. call->type = &afs_RXCMxxxx;
  526. init_waitqueue_head(&call->waitq);
  527. call->state = AFS_CALL_AWAIT_OP_ID;
  528. }
  529. if (rxrpc_kernel_charge_accept(afs_socket,
  530. afs_wake_up_async_call,
  531. afs_rx_attach,
  532. (unsigned long)call,
  533. GFP_KERNEL) < 0)
  534. break;
  535. call = NULL;
  536. }
  537. afs_spare_incoming_call = call;
  538. }
  539. /*
  540. * Discard a preallocated call when a socket is shut down.
  541. */
  542. static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
  543. unsigned long user_call_ID)
  544. {
  545. struct afs_call *call = (struct afs_call *)user_call_ID;
  546. atomic_inc(&afs_outstanding_calls);
  547. call->rxcall = NULL;
  548. afs_free_call(call);
  549. }
  550. /*
  551. * Notification of an incoming call.
  552. */
  553. static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
  554. unsigned long user_call_ID)
  555. {
  556. atomic_inc(&afs_outstanding_calls);
  557. queue_work(afs_wq, &afs_charge_preallocation_work);
  558. }
  559. /*
  560. * Grab the operation ID from an incoming cache manager call. The socket
  561. * buffer is discarded on error or if we don't yet have sufficient data.
  562. */
  563. static int afs_deliver_cm_op_id(struct afs_call *call)
  564. {
  565. int ret;
  566. _enter("{%zu}", call->offset);
  567. ASSERTCMP(call->offset, <, 4);
  568. /* the operation ID forms the first four bytes of the request data */
  569. ret = afs_extract_data(call, &call->tmp, 4, true);
  570. if (ret < 0)
  571. return ret;
  572. call->operation_ID = ntohl(call->tmp);
  573. call->state = AFS_CALL_AWAIT_REQUEST;
  574. call->offset = 0;
  575. /* ask the cache manager to route the call (it'll change the call type
  576. * if successful) */
  577. if (!afs_cm_incoming_call(call))
  578. return -ENOTSUPP;
  579. /* pass responsibility for the remainer of this message off to the
  580. * cache manager op */
  581. return call->type->deliver(call);
  582. }
  583. /*
  584. * send an empty reply
  585. */
  586. void afs_send_empty_reply(struct afs_call *call)
  587. {
  588. struct msghdr msg;
  589. _enter("");
  590. msg.msg_name = NULL;
  591. msg.msg_namelen = 0;
  592. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0);
  593. msg.msg_control = NULL;
  594. msg.msg_controllen = 0;
  595. msg.msg_flags = 0;
  596. call->state = AFS_CALL_AWAIT_ACK;
  597. switch (rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, 0)) {
  598. case 0:
  599. _leave(" [replied]");
  600. return;
  601. case -ENOMEM:
  602. _debug("oom");
  603. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  604. RX_USER_ABORT, ENOMEM, "KOO");
  605. default:
  606. afs_end_call(call);
  607. _leave(" [error]");
  608. return;
  609. }
  610. }
  611. /*
  612. * send a simple reply
  613. */
  614. void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
  615. {
  616. struct msghdr msg;
  617. struct kvec iov[1];
  618. int n;
  619. _enter("");
  620. iov[0].iov_base = (void *) buf;
  621. iov[0].iov_len = len;
  622. msg.msg_name = NULL;
  623. msg.msg_namelen = 0;
  624. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len);
  625. msg.msg_control = NULL;
  626. msg.msg_controllen = 0;
  627. msg.msg_flags = 0;
  628. call->state = AFS_CALL_AWAIT_ACK;
  629. n = rxrpc_kernel_send_data(afs_socket, call->rxcall, &msg, len);
  630. if (n >= 0) {
  631. /* Success */
  632. _leave(" [replied]");
  633. return;
  634. }
  635. if (n == -ENOMEM) {
  636. _debug("oom");
  637. rxrpc_kernel_abort_call(afs_socket, call->rxcall,
  638. RX_USER_ABORT, ENOMEM, "KOO");
  639. }
  640. afs_end_call(call);
  641. _leave(" [error]");
  642. }
  643. /*
  644. * Extract a piece of data from the received data socket buffers.
  645. */
  646. int afs_extract_data(struct afs_call *call, void *buf, size_t count,
  647. bool want_more)
  648. {
  649. int ret;
  650. _enter("{%s,%zu},,%zu,%d",
  651. call->type->name, call->offset, count, want_more);
  652. ASSERTCMP(call->offset, <=, count);
  653. ret = rxrpc_kernel_recv_data(afs_socket, call->rxcall,
  654. buf, count, &call->offset,
  655. want_more, &call->abort_code);
  656. if (ret == 0 || ret == -EAGAIN)
  657. return ret;
  658. if (ret == 1) {
  659. switch (call->state) {
  660. case AFS_CALL_AWAIT_REPLY:
  661. call->state = AFS_CALL_COMPLETE;
  662. break;
  663. case AFS_CALL_AWAIT_REQUEST:
  664. call->state = AFS_CALL_REPLYING;
  665. break;
  666. default:
  667. break;
  668. }
  669. return 0;
  670. }
  671. if (ret == -ECONNABORTED)
  672. call->error = call->type->abort_to_error(call->abort_code);
  673. else
  674. call->error = ret;
  675. call->state = AFS_CALL_COMPLETE;
  676. return ret;
  677. }