blob: ce98e133caa6d11531c5c41e5b03b4f2faeeceea [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
14
David Howells08e0e7c2007-04-26 15:55:03 -070015#include <net/sock.h>
16#include <net/af_rxrpc.h>
David Howells08e0e7c2007-04-26 15:55:03 -070017#include "internal.h"
18#include "afs_cm.h"
David Howells35dbfba2018-10-20 00:57:58 +010019#include "protocol_yfs.h"
David Howells08e0e7c2007-04-26 15:55:03 -070020
David Howellsf044c882017-11-02 15:27:45 +000021struct workqueue_struct *afs_async_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070022
David Howellsd0016482016-08-30 20:42:14 +010023static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd2ddc772017-11-02 15:27:50 +000024static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
David Howellsd0016482016-08-30 20:42:14 +010025static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010026static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010027static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
28static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010029static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070030
David Howells08e0e7c2007-04-26 15:55:03 -070031/* asynchronous incoming call initial processing */
32static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070033 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070034 .deliver = afs_deliver_cm_op_id,
David Howells08e0e7c2007-04-26 15:55:03 -070035};
36
David Howells08e0e7c2007-04-26 15:55:03 -070037/*
38 * open an RxRPC socket and bind it to be a server for callback notifications
39 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
40 */
David Howellsf044c882017-11-02 15:27:45 +000041int afs_open_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070042{
43 struct sockaddr_rxrpc srx;
44 struct socket *socket;
David Howells4776cab2018-05-10 23:10:40 +010045 unsigned int min_level;
David Howells35dbfba2018-10-20 00:57:58 +010046 u16 service_upgrade[2];
David Howells08e0e7c2007-04-26 15:55:03 -070047 int ret;
48
49 _enter("");
50
David Howells5b86d4f2018-05-18 11:46:15 +010051 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010052 if (ret < 0)
53 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070054
55 socket->sk->sk_allocation = GFP_NOFS;
56
57 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000058 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070059 srx.srx_family = AF_RXRPC;
60 srx.srx_service = CM_SERVICE;
61 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000062 srx.transport_len = sizeof(srx.transport.sin6);
63 srx.transport.sin6.sin6_family = AF_INET6;
64 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
David Howells08e0e7c2007-04-26 15:55:03 -070065
David Howells4776cab2018-05-10 23:10:40 +010066 min_level = RXRPC_SECURITY_ENCRYPT;
67 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
68 (void *)&min_level, sizeof(min_level));
69 if (ret < 0)
70 goto error_2;
71
David Howells08e0e7c2007-04-26 15:55:03 -070072 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
Marc Dionne83732ec2017-11-02 15:27:52 +000073 if (ret == -EADDRINUSE) {
74 srx.transport.sin6.sin6_port = 0;
75 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
76 }
David Howells0e119b42016-06-10 22:30:37 +010077 if (ret < 0)
78 goto error_2;
79
David Howells35dbfba2018-10-20 00:57:58 +010080 srx.srx_service = YFS_CM_SERVICE;
81 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
82 if (ret < 0)
83 goto error_2;
84
85 service_upgrade[0] = CM_SERVICE;
86 service_upgrade[1] = YFS_CM_SERVICE;
87 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_UPGRADEABLE_SERVICE,
88 (void *)service_upgrade, sizeof(service_upgrade));
89 if (ret < 0)
90 goto error_2;
91
92
David Howells00e90712016-09-08 11:10:12 +010093 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
94 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010095
David Howells0e119b42016-06-10 22:30:37 +010096 ret = kernel_listen(socket, INT_MAX);
97 if (ret < 0)
98 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070099
David Howellsf044c882017-11-02 15:27:45 +0000100 net->socket = socket;
101 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700102 _leave(" = 0");
103 return 0;
David Howells0e119b42016-06-10 22:30:37 +0100104
105error_2:
106 sock_release(socket);
107error_1:
David Howells0e119b42016-06-10 22:30:37 +0100108 _leave(" = %d", ret);
109 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700110}
111
112/*
113 * close the RxRPC socket AFS was using
114 */
David Howellsf044c882017-11-02 15:27:45 +0000115void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -0700116{
117 _enter("");
118
David Howellsf044c882017-11-02 15:27:45 +0000119 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +0000120 flush_workqueue(afs_async_calls);
121
David Howellsf044c882017-11-02 15:27:45 +0000122 if (net->spare_incoming_call) {
123 afs_put_call(net->spare_incoming_call);
124 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +0100125 }
126
David Howellsf044c882017-11-02 15:27:45 +0000127 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100128 wait_var_event(&net->nr_outstanding_calls,
129 !atomic_read(&net->nr_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100130 _debug("no outstanding calls");
131
David Howellsf044c882017-11-02 15:27:45 +0000132 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100133 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000134 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700135
136 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700137 _leave("");
138}
139
140/*
David Howells341f7412017-01-05 10:38:36 +0000141 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700142 */
David Howellsf044c882017-11-02 15:27:45 +0000143static struct afs_call *afs_alloc_call(struct afs_net *net,
144 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000145 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700146{
David Howells341f7412017-01-05 10:38:36 +0000147 struct afs_call *call;
148 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700149
David Howells341f7412017-01-05 10:38:36 +0000150 call = kzalloc(sizeof(*call), gfp);
151 if (!call)
152 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700153
David Howells341f7412017-01-05 10:38:36 +0000154 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000155 call->net = net;
David Howellsa25e21f2018-03-27 23:03:00 +0100156 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howells341f7412017-01-05 10:38:36 +0000157 atomic_set(&call->usage, 1);
158 INIT_WORK(&call->async_work, afs_process_async_call);
159 init_waitqueue_head(&call->waitq);
David Howells98bf40c2017-11-02 15:27:53 +0000160 spin_lock_init(&call->state_lock);
David Howells12bdcf32018-10-20 00:57:56 +0100161 call->_iter = &call->iter;
David Howells2f02f7a2016-04-07 17:23:03 +0100162
David Howellsf044c882017-11-02 15:27:45 +0000163 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000164 trace_afs_call(call, afs_call_trace_alloc, 1, o,
165 __builtin_return_address(0));
166 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700167}
168
169/*
David Howells341f7412017-01-05 10:38:36 +0000170 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100171 */
David Howells341f7412017-01-05 10:38:36 +0000172void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100173{
David Howellsf044c882017-11-02 15:27:45 +0000174 struct afs_net *net = call->net;
David Howells341f7412017-01-05 10:38:36 +0000175 int n = atomic_dec_return(&call->usage);
David Howellsf044c882017-11-02 15:27:45 +0000176 int o = atomic_read(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000177
178 trace_afs_call(call, afs_call_trace_put, n + 1, o,
179 __builtin_return_address(0));
180
181 ASSERTCMP(n, >=, 0);
182 if (n == 0) {
183 ASSERT(!work_pending(&call->async_work));
184 ASSERT(call->type->name != NULL);
185
186 if (call->rxcall) {
David Howellsf044c882017-11-02 15:27:45 +0000187 rxrpc_kernel_end_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000188 call->rxcall = NULL;
189 }
190 if (call->type->destructor)
191 call->type->destructor(call);
192
David Howellsd0676a12017-11-02 15:27:49 +0000193 afs_put_server(call->net, call->cm_server);
David Howellsd2ddc772017-11-02 15:27:50 +0000194 afs_put_cb_interest(call->net, call->cbi);
David Howells341f7412017-01-05 10:38:36 +0000195 kfree(call->request);
David Howellsa25e21f2018-03-27 23:03:00 +0100196
197 trace_afs_call(call, afs_call_trace_free, 0, o,
198 __builtin_return_address(0));
David Howells341f7412017-01-05 10:38:36 +0000199 kfree(call);
200
David Howellsf044c882017-11-02 15:27:45 +0000201 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000202 if (o == 0)
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100203 wake_up_var(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100204 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100205}
206
207/*
David Howells341f7412017-01-05 10:38:36 +0000208 * Queue the call for actual work. Returns 0 unconditionally for convenience.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100209 */
David Howells341f7412017-01-05 10:38:36 +0000210int afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100211{
David Howells341f7412017-01-05 10:38:36 +0000212 int u = atomic_inc_return(&call->usage);
213
214 trace_afs_call(call, afs_call_trace_work, u,
David Howellsf044c882017-11-02 15:27:45 +0000215 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000216 __builtin_return_address(0));
217
218 INIT_WORK(&call->work, call->type->work);
219
220 if (!queue_work(afs_wq, &call->work))
221 afs_put_call(call);
222 return 0;
David Howells6c67c7c2014-05-21 14:48:05 +0100223}
224
225/*
David Howells08e0e7c2007-04-26 15:55:03 -0700226 * allocate a call with flat request and reply buffers
227 */
David Howellsf044c882017-11-02 15:27:45 +0000228struct afs_call *afs_alloc_flat_call(struct afs_net *net,
229 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100230 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700231{
232 struct afs_call *call;
233
David Howellsf044c882017-11-02 15:27:45 +0000234 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700235 if (!call)
236 goto nomem_call;
237
David Howells00d3b7a2007-04-26 15:57:07 -0700238 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000239 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700240 call->request = kmalloc(request_size, GFP_NOFS);
241 if (!call->request)
242 goto nomem_free;
243 }
244
David Howellsd0016482016-08-30 20:42:14 +0100245 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000246 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100247 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700248 if (!call->buffer)
249 goto nomem_free;
250 }
251
David Howells12bdcf32018-10-20 00:57:56 +0100252 afs_extract_to_buf(call, call->reply_max);
David Howells025db802017-11-02 15:27:51 +0000253 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700254 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700255 return call;
256
David Howells00d3b7a2007-04-26 15:57:07 -0700257nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000258 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700259nomem_call:
260 return NULL;
261}
262
263/*
264 * clean up a call with flat buffer
265 */
266void afs_flat_call_destructor(struct afs_call *call)
267{
268 _enter("");
269
270 kfree(call->request);
271 call->request = NULL;
272 kfree(call->buffer);
273 call->buffer = NULL;
274}
275
David Howells2f5705a2017-03-16 16:27:46 +0000276#define AFS_BVEC_MAX 8
277
278/*
279 * Load the given bvec with the next few pages.
280 */
281static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
282 struct bio_vec *bv, pgoff_t first, pgoff_t last,
283 unsigned offset)
284{
285 struct page *pages[AFS_BVEC_MAX];
286 unsigned int nr, n, i, to, bytes = 0;
287
288 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
289 n = find_get_pages_contig(call->mapping, first, nr, pages);
290 ASSERTCMP(n, ==, nr);
291
292 msg->msg_flags |= MSG_MORE;
293 for (i = 0; i < nr; i++) {
294 to = PAGE_SIZE;
295 if (first + i >= last) {
296 to = call->last_to;
297 msg->msg_flags &= ~MSG_MORE;
298 }
299 bv[i].bv_page = pages[i];
300 bv[i].bv_len = to - offset;
301 bv[i].bv_offset = offset;
302 bytes += to - offset;
303 offset = 0;
304 }
305
David Howellsaa563d72018-10-20 00:57:56 +0100306 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
David Howells2f5705a2017-03-16 16:27:46 +0000307}
308
David Howells08e0e7c2007-04-26 15:55:03 -0700309/*
David Howellse8332512017-08-29 10:18:56 +0100310 * Advance the AFS call state when the RxRPC call ends the transmit phase.
311 */
312static void afs_notify_end_request_tx(struct sock *sock,
313 struct rxrpc_call *rxcall,
314 unsigned long call_user_ID)
315{
316 struct afs_call *call = (struct afs_call *)call_user_ID;
317
David Howells98bf40c2017-11-02 15:27:53 +0000318 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100319}
320
321/*
David Howells31143d52007-05-09 02:33:46 -0700322 * attach the data from a bunch of pages on an inode to a call
323 */
Al Viro39c6ace2016-01-09 20:36:51 -0500324static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700325{
David Howells2f5705a2017-03-16 16:27:46 +0000326 struct bio_vec bv[AFS_BVEC_MAX];
327 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700328 pgoff_t first = call->first, last = call->last;
329 int ret;
330
David Howells31143d52007-05-09 02:33:46 -0700331 offset = call->first_offset;
332 call->first_offset = 0;
333
334 do {
David Howells2f5705a2017-03-16 16:27:46 +0000335 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000336 trace_afs_send_pages(call, msg, first, last, offset);
337
David Howells2f5705a2017-03-16 16:27:46 +0000338 offset = 0;
339 bytes = msg->msg_iter.count;
340 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700341
David Howellsf044c882017-11-02 15:27:45 +0000342 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100343 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000344 for (loop = 0; loop < nr; loop++)
345 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700346 if (ret < 0)
347 break;
David Howells2f5705a2017-03-16 16:27:46 +0000348
349 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700350 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700351
David Howells2c099012017-11-02 15:27:51 +0000352 trace_afs_sent_pages(call, call->first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700353 return ret;
354}
355
356/*
David Howells08e0e7c2007-04-26 15:55:03 -0700357 * initiate a call
358 */
David Howells8b2a4642017-11-02 15:27:50 +0000359long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
David Howells33cd7f22017-11-02 15:27:48 +0000360 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700361{
David Howells8b2a4642017-11-02 15:27:50 +0000362 struct sockaddr_rxrpc *srx = ac->addr;
David Howells08e0e7c2007-04-26 15:55:03 -0700363 struct rxrpc_call *rxcall;
364 struct msghdr msg;
365 struct kvec iov[1];
David Howellse754eba2017-06-07 12:40:03 +0100366 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700367 int ret;
368
David Howells4d9df982017-11-02 15:27:47 +0000369 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700370
David Howells00d3b7a2007-04-26 15:57:07 -0700371 ASSERT(call->type != NULL);
372 ASSERT(call->type->name != NULL);
373
David Howells31143d52007-05-09 02:33:46 -0700374 _debug("____MAKE %p{%s,%x} [%d]____",
375 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000376 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700377
David Howells56ff9c82017-01-05 10:38:36 +0000378 call->async = async;
David Howells08e0e7c2007-04-26 15:55:03 -0700379
David Howellse754eba2017-06-07 12:40:03 +0100380 /* Work out the length we're going to transmit. This is awkward for
381 * calls such as FS.StoreData where there's an extra injection of data
382 * after the initial fixed part.
383 */
384 tx_total_len = call->request_size;
385 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000386 if (call->last == call->first) {
387 tx_total_len += call->last_to - call->first_offset;
388 } else {
389 /* It looks mathematically like you should be able to
390 * combine the following lines with the ones above, but
391 * unsigned arithmetic is fun when it wraps...
392 */
393 tx_total_len += PAGE_SIZE - call->first_offset;
394 tx_total_len += call->last_to;
395 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
396 }
David Howellse754eba2017-06-07 12:40:03 +0100397 }
398
David Howells08e0e7c2007-04-26 15:55:03 -0700399 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000400 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100401 (unsigned long)call,
402 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000403 (async ?
404 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100405 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100406 call->upgrade,
407 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700408 if (IS_ERR(rxcall)) {
409 ret = PTR_ERR(rxcall);
410 goto error_kill_call;
411 }
412
413 call->rxcall = rxcall;
414
415 /* send the request */
416 iov[0].iov_base = call->request;
417 iov[0].iov_len = call->request_size;
418
419 msg.msg_name = NULL;
420 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100421 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700422 msg.msg_control = NULL;
423 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100424 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700425
David Howellsf044c882017-11-02 15:27:45 +0000426 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100427 &msg, call->request_size,
428 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700429 if (ret < 0)
430 goto error_do_abort;
431
David Howells31143d52007-05-09 02:33:46 -0700432 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500433 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700434 if (ret < 0)
435 goto error_do_abort;
436 }
437
David Howells08e0e7c2007-04-26 15:55:03 -0700438 /* at this point, an async call may no longer exist as it may have
439 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000440 if (call->async)
441 return -EINPROGRESS;
442
David Howellsd2ddc772017-11-02 15:27:50 +0000443 return afs_wait_for_call_to_complete(call, ac);
David Howells08e0e7c2007-04-26 15:55:03 -0700444
445error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000446 call->state = AFS_CALL_COMPLETE;
447 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000448 rxrpc_kernel_abort_call(call->net->socket, rxcall,
449 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000450 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100451 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
David Howellseb9950e2018-08-03 17:06:56 +0100452 rxrpc_kernel_recv_data(call->net->socket, rxcall,
453 &msg.msg_iter, false,
454 &call->abort_code, &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000455 ac->abort_code = call->abort_code;
456 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000457 }
David Howells025db802017-11-02 15:27:51 +0000458 call->error = ret;
459 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700460error_kill_call:
David Howells341f7412017-01-05 10:38:36 +0000461 afs_put_call(call);
David Howellsd2ddc772017-11-02 15:27:50 +0000462 ac->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700463 _leave(" = %d", ret);
464 return ret;
465}
466
467/*
David Howells08e0e7c2007-04-26 15:55:03 -0700468 * deliver messages to a call
469 */
470static void afs_deliver_to_call(struct afs_call *call)
471{
David Howells98bf40c2017-11-02 15:27:53 +0000472 enum afs_call_state state;
473 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700474 int ret;
475
David Howellsd0016482016-08-30 20:42:14 +0100476 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700477
David Howells98bf40c2017-11-02 15:27:53 +0000478 while (state = READ_ONCE(call->state),
479 state == AFS_CALL_CL_AWAIT_REPLY ||
480 state == AFS_CALL_SV_AWAIT_OP_ID ||
481 state == AFS_CALL_SV_AWAIT_REQUEST ||
482 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100483 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000484 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howells12bdcf32018-10-20 00:57:56 +0100485 iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
David Howellsf044c882017-11-02 15:27:45 +0000486 ret = rxrpc_kernel_recv_data(call->net->socket,
David Howells12bdcf32018-10-20 00:57:56 +0100487 call->rxcall, &call->iter,
488 false, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100489 &call->service_id);
David Howells12bdcf32018-10-20 00:57:56 +0100490 trace_afs_receive_data(call, &call->iter, false, ret);
David Howells8e8d7f12017-01-05 10:38:34 +0000491
David Howellsd0016482016-08-30 20:42:14 +0100492 if (ret == -EINPROGRESS || ret == -EAGAIN)
493 return;
David Howells98bf40c2017-11-02 15:27:53 +0000494 if (ret < 0 || ret == 1) {
495 if (ret == 1)
496 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000497 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000498 }
David Howellsd0016482016-08-30 20:42:14 +0100499 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700500 }
501
David Howellsd0016482016-08-30 20:42:14 +0100502 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000503 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100504 switch (ret) {
505 case 0:
David Howellsf2686b02018-05-10 14:12:50 +0100506 if (state == AFS_CALL_CL_PROC_REPLY) {
507 if (call->cbi)
508 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
509 &call->cbi->server->flags);
David Howells025db802017-11-02 15:27:51 +0000510 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100511 }
David Howells98bf40c2017-11-02 15:27:53 +0000512 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100513 goto done;
514 case -EINPROGRESS:
515 case -EAGAIN:
516 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000517 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000518 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
519 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100520 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000521 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000522 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100523 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000524 goto local_abort;
David Howells4ac15ea2018-10-20 00:57:57 +0100525 case -EIO:
526 pr_err("kAFS: Call %u in bad state %u\n",
527 call->debug_id, state);
528 /* Fall through */
David Howellsd0016482016-08-30 20:42:14 +0100529 case -ENODATA:
530 case -EBADMSG:
531 case -EMSGSIZE:
532 default:
533 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000534 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100535 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000536 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells12bdcf32018-10-20 00:57:56 +0100537 abort_code, ret, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000538 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100539 }
David Howells08e0e7c2007-04-26 15:55:03 -0700540 }
541
David Howellsd0016482016-08-30 20:42:14 +0100542done:
David Howells98bf40c2017-11-02 15:27:53 +0000543 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000544 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100545out:
David Howells08e0e7c2007-04-26 15:55:03 -0700546 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100547 return;
548
David Howells98bf40c2017-11-02 15:27:53 +0000549local_abort:
550 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000551call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000552 afs_set_call_complete(call, ret, remote_abort);
553 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100554 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700555}
556
557/*
558 * wait synchronously for a call to complete
559 */
David Howellsd2ddc772017-11-02 15:27:50 +0000560static long afs_wait_for_call_to_complete(struct afs_call *call,
561 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700562{
David Howellsbc5e3a52017-10-18 11:07:31 +0100563 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000564 long ret;
David Howellsbc5e3a52017-10-18 11:07:31 +0100565 u64 rtt;
566 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700567
568 DECLARE_WAITQUEUE(myself, current);
569
570 _enter("");
571
David Howellsf044c882017-11-02 15:27:45 +0000572 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100573 rtt2 = nsecs_to_jiffies64(rtt) * 2;
574 if (rtt2 < 2)
575 rtt2 = 2;
576
577 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000578 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100579
David Howells08e0e7c2007-04-26 15:55:03 -0700580 add_wait_queue(&call->waitq, &myself);
581 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100582 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700583
584 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000585 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
586 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100587 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700588 __set_current_state(TASK_RUNNING);
589 afs_deliver_to_call(call);
590 continue;
591 }
592
David Howells98bf40c2017-11-02 15:27:53 +0000593 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700594 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100595
David Howellsf044c882017-11-02 15:27:45 +0000596 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100597 if (timeout == 0 &&
598 life == last_life && signal_pending(current))
599 break;
600
601 if (life != last_life) {
602 timeout = rtt2;
603 last_life = life;
604 }
605
606 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700607 }
608
609 remove_wait_queue(&call->waitq, &myself);
610 __set_current_state(TASK_RUNNING);
611
David Howells954cd6d2017-03-16 16:27:49 +0000612 /* Kill off the call if it's still live. */
David Howells98bf40c2017-11-02 15:27:53 +0000613 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
David Howells954cd6d2017-03-16 16:27:49 +0000614 _debug("call interrupted");
David Howellsd2ddc772017-11-02 15:27:50 +0000615 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells98bf40c2017-11-02 15:27:53 +0000616 RX_USER_ABORT, -EINTR, "KWI"))
617 afs_set_call_complete(call, -EINTR, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700618 }
619
David Howells98bf40c2017-11-02 15:27:53 +0000620 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000621 ac->abort_code = call->abort_code;
622 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000623 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000624
625 ret = ac->error;
626 switch (ret) {
627 case 0:
628 if (call->ret_reply0) {
629 ret = (long)call->reply[0];
630 call->reply[0] = NULL;
631 }
632 /* Fall through */
633 case -ECONNABORTED:
634 ac->responded = true;
635 break;
David Howells33cd7f22017-11-02 15:27:48 +0000636 }
637
David Howells08e0e7c2007-04-26 15:55:03 -0700638 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000639 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000640 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700641 return ret;
642}
643
644/*
645 * wake up a waiting call
646 */
David Howellsd0016482016-08-30 20:42:14 +0100647static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
648 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700649{
David Howellsd0016482016-08-30 20:42:14 +0100650 struct afs_call *call = (struct afs_call *)call_user_ID;
651
652 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700653 wake_up(&call->waitq);
654}
655
656/*
657 * wake up an asynchronous call
658 */
David Howellsd0016482016-08-30 20:42:14 +0100659static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
660 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700661{
David Howellsd0016482016-08-30 20:42:14 +0100662 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000663 int u;
David Howellsd0016482016-08-30 20:42:14 +0100664
David Howells8e8d7f12017-01-05 10:38:34 +0000665 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100666 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000667
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100668 u = atomic_fetch_add_unless(&call->usage, 1, 0);
David Howells341f7412017-01-05 10:38:36 +0000669 if (u != 0) {
670 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000671 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000672 __builtin_return_address(0));
673
674 if (!queue_work(afs_async_calls, &call->async_work))
675 afs_put_call(call);
676 }
David Howells08e0e7c2007-04-26 15:55:03 -0700677}
678
679/*
David Howells341f7412017-01-05 10:38:36 +0000680 * Delete an asynchronous call. The work item carries a ref to the call struct
681 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700682 */
David Howellsd0016482016-08-30 20:42:14 +0100683static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700684{
David Howellsd0016482016-08-30 20:42:14 +0100685 struct afs_call *call = container_of(work, struct afs_call, async_work);
686
David Howells08e0e7c2007-04-26 15:55:03 -0700687 _enter("");
688
David Howells341f7412017-01-05 10:38:36 +0000689 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700690
691 _leave("");
692}
693
694/*
David Howells341f7412017-01-05 10:38:36 +0000695 * Perform I/O processing on an asynchronous call. The work item carries a ref
696 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700697 */
David Howellsd0016482016-08-30 20:42:14 +0100698static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700699{
David Howellsd0016482016-08-30 20:42:14 +0100700 struct afs_call *call = container_of(work, struct afs_call, async_work);
701
David Howells08e0e7c2007-04-26 15:55:03 -0700702 _enter("");
703
David Howellsd0016482016-08-30 20:42:14 +0100704 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
705 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700706 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100707 }
David Howells08e0e7c2007-04-26 15:55:03 -0700708
David Howells56ff9c82017-01-05 10:38:36 +0000709 if (call->state == AFS_CALL_COMPLETE) {
David Howells341f7412017-01-05 10:38:36 +0000710 /* We have two refs to release - one from the alloc and one
711 * queued with the work item - and we can't just deallocate the
712 * call because the work item may be queued again.
713 */
David Howellsd0016482016-08-30 20:42:14 +0100714 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000715 if (!queue_work(afs_async_calls, &call->async_work))
716 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700717 }
718
David Howells341f7412017-01-05 10:38:36 +0000719 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700720 _leave("");
721}
722
David Howells00e90712016-09-08 11:10:12 +0100723static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
724{
725 struct afs_call *call = (struct afs_call *)user_call_ID;
726
727 call->rxcall = rxcall;
728}
729
730/*
731 * Charge the incoming call preallocation.
732 */
David Howellsf044c882017-11-02 15:27:45 +0000733void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100734{
David Howellsf044c882017-11-02 15:27:45 +0000735 struct afs_net *net =
736 container_of(work, struct afs_net, charge_preallocation_work);
737 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100738
739 for (;;) {
740 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000741 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100742 if (!call)
743 break;
744
David Howells56ff9c82017-01-05 10:38:36 +0000745 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000746 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000747 init_waitqueue_head(&call->waitq);
David Howells12bdcf32018-10-20 00:57:56 +0100748 afs_extract_to_tmp(call);
David Howells00e90712016-09-08 11:10:12 +0100749 }
750
David Howellsf044c882017-11-02 15:27:45 +0000751 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100752 afs_wake_up_async_call,
753 afs_rx_attach,
754 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100755 GFP_KERNEL,
756 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100757 break;
758 call = NULL;
759 }
David Howellsf044c882017-11-02 15:27:45 +0000760 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100761}
762
763/*
764 * Discard a preallocated call when a socket is shut down.
765 */
766static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
767 unsigned long user_call_ID)
768{
769 struct afs_call *call = (struct afs_call *)user_call_ID;
770
David Howells00e90712016-09-08 11:10:12 +0100771 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000772 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100773}
774
David Howells08e0e7c2007-04-26 15:55:03 -0700775/*
David Howellsd0016482016-08-30 20:42:14 +0100776 * Notification of an incoming call.
777 */
David Howells00e90712016-09-08 11:10:12 +0100778static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
779 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100780{
David Howellsf044c882017-11-02 15:27:45 +0000781 struct afs_net *net = afs_sock2net(sk);
782
783 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100784}
785
786/*
David Howells372ee162016-08-03 14:11:40 +0100787 * Grab the operation ID from an incoming cache manager call. The socket
788 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700789 */
David Howellsd0016482016-08-30 20:42:14 +0100790static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700791{
David Howellsd0016482016-08-30 20:42:14 +0100792 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700793
David Howells12bdcf32018-10-20 00:57:56 +0100794 _enter("{%zu}", iov_iter_count(call->_iter));
David Howells08e0e7c2007-04-26 15:55:03 -0700795
796 /* the operation ID forms the first four bytes of the request data */
David Howells12bdcf32018-10-20 00:57:56 +0100797 ret = afs_extract_data(call, true);
David Howellsd0016482016-08-30 20:42:14 +0100798 if (ret < 0)
799 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700800
David Howells50a2c952016-10-13 08:27:10 +0100801 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000802 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howells08e0e7c2007-04-26 15:55:03 -0700803
804 /* ask the cache manager to route the call (it'll change the call type
805 * if successful) */
806 if (!afs_cm_incoming_call(call))
807 return -ENOTSUPP;
808
David Howells8e8d7f12017-01-05 10:38:34 +0000809 trace_afs_cb_call(call);
810
David Howells08e0e7c2007-04-26 15:55:03 -0700811 /* pass responsibility for the remainer of this message off to the
812 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100813 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700814}
815
816/*
David Howellse8332512017-08-29 10:18:56 +0100817 * Advance the AFS call state when an RxRPC service call ends the transmit
818 * phase.
819 */
820static void afs_notify_end_reply_tx(struct sock *sock,
821 struct rxrpc_call *rxcall,
822 unsigned long call_user_ID)
823{
824 struct afs_call *call = (struct afs_call *)call_user_ID;
825
David Howells98bf40c2017-11-02 15:27:53 +0000826 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100827}
828
829/*
David Howells08e0e7c2007-04-26 15:55:03 -0700830 * send an empty reply
831 */
832void afs_send_empty_reply(struct afs_call *call)
833{
David Howellsf044c882017-11-02 15:27:45 +0000834 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700835 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700836
837 _enter("");
838
David Howellsf044c882017-11-02 15:27:45 +0000839 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100840
David Howells08e0e7c2007-04-26 15:55:03 -0700841 msg.msg_name = NULL;
842 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100843 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700844 msg.msg_control = NULL;
845 msg.msg_controllen = 0;
846 msg.msg_flags = 0;
847
David Howellsf044c882017-11-02 15:27:45 +0000848 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100849 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700850 case 0:
851 _leave(" [replied]");
852 return;
853
854 case -ENOMEM:
855 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000856 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100857 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700858 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700859 _leave(" [error]");
860 return;
861 }
862}
863
864/*
David Howellsb908fe62007-04-26 15:58:17 -0700865 * send a simple reply
866 */
867void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
868{
David Howellsf044c882017-11-02 15:27:45 +0000869 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700870 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500871 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100872 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700873
874 _enter("");
875
David Howellsf044c882017-11-02 15:27:45 +0000876 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100877
David Howellsb908fe62007-04-26 15:58:17 -0700878 iov[0].iov_base = (void *) buf;
879 iov[0].iov_len = len;
880 msg.msg_name = NULL;
881 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100882 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700883 msg.msg_control = NULL;
884 msg.msg_controllen = 0;
885 msg.msg_flags = 0;
886
David Howellsf044c882017-11-02 15:27:45 +0000887 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100888 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100889 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100890 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700891 _leave(" [replied]");
892 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100893 }
David Howells6c67c7c2014-05-21 14:48:05 +0100894
David Howellsbd6dc742007-07-20 10:59:41 +0100895 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700896 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000897 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100898 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700899 }
David Howellsbd6dc742007-07-20 10:59:41 +0100900 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700901}
902
903/*
David Howells372ee162016-08-03 14:11:40 +0100904 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700905 */
David Howells12bdcf32018-10-20 00:57:56 +0100906int afs_extract_data(struct afs_call *call, bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700907{
David Howellsf044c882017-11-02 15:27:45 +0000908 struct afs_net *net = call->net;
David Howells12bdcf32018-10-20 00:57:56 +0100909 struct iov_iter *iter = call->_iter;
David Howells98bf40c2017-11-02 15:27:53 +0000910 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000911 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100912 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700913
David Howells12bdcf32018-10-20 00:57:56 +0100914 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700915
David Howells12bdcf32018-10-20 00:57:56 +0100916 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
David Howells98bf40c2017-11-02 15:27:53 +0000917 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100918 &call->service_id);
David Howellsd0016482016-08-30 20:42:14 +0100919 if (ret == 0 || ret == -EAGAIN)
920 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700921
David Howells98bf40c2017-11-02 15:27:53 +0000922 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100923 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000924 switch (state) {
925 case AFS_CALL_CL_AWAIT_REPLY:
926 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100927 break;
David Howells98bf40c2017-11-02 15:27:53 +0000928 case AFS_CALL_SV_AWAIT_REQUEST:
929 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100930 break;
David Howells98bf40c2017-11-02 15:27:53 +0000931 case AFS_CALL_COMPLETE:
932 kdebug("prem complete %d", call->error);
David Howellsf51375c2018-10-20 00:57:57 +0100933 return afs_io_error(call, afs_io_error_extract);
David Howellsd0016482016-08-30 20:42:14 +0100934 default:
935 break;
936 }
937 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700938 }
David Howellsd0016482016-08-30 20:42:14 +0100939
David Howells98bf40c2017-11-02 15:27:53 +0000940 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100941 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700942}
David Howells5f702c82018-04-06 14:17:25 +0100943
944/*
945 * Log protocol error production.
946 */
David Howells160cb952018-10-20 00:57:56 +0100947noinline int afs_protocol_error(struct afs_call *call, int error,
948 enum afs_eproto_cause cause)
David Howells5f702c82018-04-06 14:17:25 +0100949{
David Howells160cb952018-10-20 00:57:56 +0100950 trace_afs_protocol_error(call, error, cause);
David Howells5f702c82018-04-06 14:17:25 +0100951 return error;
952}