blob: 4974defb45924b0c726ede65c8ec7bff01130e1e [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 Howellsd0016482016-08-30 20:42:14 +010024static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howells34fa4762019-01-10 15:40:50 +000025static void afs_delete_async_call(struct work_struct *);
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 Howells08e0e7c2007-04-26 15:55:03 -070046 int ret;
47
48 _enter("");
49
David Howells5b86d4f2018-05-18 11:46:15 +010050 ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
David Howells0e119b42016-06-10 22:30:37 +010051 if (ret < 0)
52 goto error_1;
David Howells08e0e7c2007-04-26 15:55:03 -070053
54 socket->sk->sk_allocation = GFP_NOFS;
55
56 /* bind the callback manager's address to make this a server socket */
David Howells3838d3e2017-11-02 15:27:47 +000057 memset(&srx, 0, sizeof(srx));
David Howells08e0e7c2007-04-26 15:55:03 -070058 srx.srx_family = AF_RXRPC;
59 srx.srx_service = CM_SERVICE;
60 srx.transport_type = SOCK_DGRAM;
David Howells3838d3e2017-11-02 15:27:47 +000061 srx.transport_len = sizeof(srx.transport.sin6);
62 srx.transport.sin6.sin6_family = AF_INET6;
63 srx.transport.sin6.sin6_port = htons(AFS_CM_PORT);
David Howells08e0e7c2007-04-26 15:55:03 -070064
David Howells4776cab2018-05-10 23:10:40 +010065 min_level = RXRPC_SECURITY_ENCRYPT;
66 ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
67 (void *)&min_level, sizeof(min_level));
68 if (ret < 0)
69 goto error_2;
70
David Howells08e0e7c2007-04-26 15:55:03 -070071 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
Marc Dionne83732ec2017-11-02 15:27:52 +000072 if (ret == -EADDRINUSE) {
73 srx.transport.sin6.sin6_port = 0;
74 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
75 }
David Howells0e119b42016-06-10 22:30:37 +010076 if (ret < 0)
77 goto error_2;
78
David Howells35dbfba2018-10-20 00:57:58 +010079 srx.srx_service = YFS_CM_SERVICE;
80 ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
81 if (ret < 0)
82 goto error_2;
83
David Howells3bf0fb62018-10-20 00:57:59 +010084 /* Ideally, we'd turn on service upgrade here, but we can't because
85 * OpenAFS is buggy and leaks the userStatus field from packet to
86 * packet and between FS packets and CB packets - so if we try to do an
87 * upgrade on an FS packet, OpenAFS will leak that into the CB packet
88 * it sends back to us.
89 */
David Howells35dbfba2018-10-20 00:57:58 +010090
David Howells00e90712016-09-08 11:10:12 +010091 rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
92 afs_rx_discard_new_call);
David Howellsd0016482016-08-30 20:42:14 +010093
David Howells0e119b42016-06-10 22:30:37 +010094 ret = kernel_listen(socket, INT_MAX);
95 if (ret < 0)
96 goto error_2;
David Howells08e0e7c2007-04-26 15:55:03 -070097
David Howellsf044c882017-11-02 15:27:45 +000098 net->socket = socket;
99 afs_charge_preallocation(&net->charge_preallocation_work);
David Howells08e0e7c2007-04-26 15:55:03 -0700100 _leave(" = 0");
101 return 0;
David Howells0e119b42016-06-10 22:30:37 +0100102
103error_2:
104 sock_release(socket);
105error_1:
David Howells0e119b42016-06-10 22:30:37 +0100106 _leave(" = %d", ret);
107 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700108}
109
110/*
111 * close the RxRPC socket AFS was using
112 */
David Howellsf044c882017-11-02 15:27:45 +0000113void afs_close_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -0700114{
115 _enter("");
116
David Howellsf044c882017-11-02 15:27:45 +0000117 kernel_listen(net->socket, 0);
David Howells341f7412017-01-05 10:38:36 +0000118 flush_workqueue(afs_async_calls);
119
David Howellsf044c882017-11-02 15:27:45 +0000120 if (net->spare_incoming_call) {
121 afs_put_call(net->spare_incoming_call);
122 net->spare_incoming_call = NULL;
David Howells00e90712016-09-08 11:10:12 +0100123 }
124
David Howellsf044c882017-11-02 15:27:45 +0000125 _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100126 wait_var_event(&net->nr_outstanding_calls,
127 !atomic_read(&net->nr_outstanding_calls));
David Howells2f02f7a2016-04-07 17:23:03 +0100128 _debug("no outstanding calls");
129
David Howellsf044c882017-11-02 15:27:45 +0000130 kernel_sock_shutdown(net->socket, SHUT_RDWR);
David Howells248f2192016-09-08 11:10:12 +0100131 flush_workqueue(afs_async_calls);
David Howellsf044c882017-11-02 15:27:45 +0000132 sock_release(net->socket);
David Howells08e0e7c2007-04-26 15:55:03 -0700133
134 _debug("dework");
David Howells08e0e7c2007-04-26 15:55:03 -0700135 _leave("");
136}
137
138/*
David Howells341f7412017-01-05 10:38:36 +0000139 * Allocate a call.
David Howells00d3b7a2007-04-26 15:57:07 -0700140 */
David Howellsf044c882017-11-02 15:27:45 +0000141static struct afs_call *afs_alloc_call(struct afs_net *net,
142 const struct afs_call_type *type,
David Howells341f7412017-01-05 10:38:36 +0000143 gfp_t gfp)
David Howells00d3b7a2007-04-26 15:57:07 -0700144{
David Howells341f7412017-01-05 10:38:36 +0000145 struct afs_call *call;
146 int o;
David Howells00d3b7a2007-04-26 15:57:07 -0700147
David Howells341f7412017-01-05 10:38:36 +0000148 call = kzalloc(sizeof(*call), gfp);
149 if (!call)
150 return NULL;
David Howells00d3b7a2007-04-26 15:57:07 -0700151
David Howells341f7412017-01-05 10:38:36 +0000152 call->type = type;
David Howellsf044c882017-11-02 15:27:45 +0000153 call->net = net;
David Howellsa25e21f2018-03-27 23:03:00 +0100154 call->debug_id = atomic_inc_return(&rxrpc_debug_id);
David Howells341f7412017-01-05 10:38:36 +0000155 atomic_set(&call->usage, 1);
156 INIT_WORK(&call->async_work, afs_process_async_call);
157 init_waitqueue_head(&call->waitq);
David Howells98bf40c2017-11-02 15:27:53 +0000158 spin_lock_init(&call->state_lock);
David Howells12bdcf32018-10-20 00:57:56 +0100159 call->_iter = &call->iter;
David Howells2f02f7a2016-04-07 17:23:03 +0100160
David Howellsf044c882017-11-02 15:27:45 +0000161 o = atomic_inc_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000162 trace_afs_call(call, afs_call_trace_alloc, 1, o,
163 __builtin_return_address(0));
164 return call;
David Howells00d3b7a2007-04-26 15:57:07 -0700165}
166
167/*
David Howells341f7412017-01-05 10:38:36 +0000168 * Dispose of a reference on a call.
David Howells6c67c7c2014-05-21 14:48:05 +0100169 */
David Howells341f7412017-01-05 10:38:36 +0000170void afs_put_call(struct afs_call *call)
David Howells6c67c7c2014-05-21 14:48:05 +0100171{
David Howellsf044c882017-11-02 15:27:45 +0000172 struct afs_net *net = call->net;
David Howells341f7412017-01-05 10:38:36 +0000173 int n = atomic_dec_return(&call->usage);
David Howellsf044c882017-11-02 15:27:45 +0000174 int o = atomic_read(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000175
176 trace_afs_call(call, afs_call_trace_put, n + 1, o,
177 __builtin_return_address(0));
178
179 ASSERTCMP(n, >=, 0);
180 if (n == 0) {
181 ASSERT(!work_pending(&call->async_work));
182 ASSERT(call->type->name != NULL);
183
184 if (call->rxcall) {
David Howellsf044c882017-11-02 15:27:45 +0000185 rxrpc_kernel_end_call(net->socket, call->rxcall);
David Howells341f7412017-01-05 10:38:36 +0000186 call->rxcall = NULL;
187 }
188 if (call->type->destructor)
189 call->type->destructor(call);
190
David Howellsd0676a12017-11-02 15:27:49 +0000191 afs_put_server(call->net, call->cm_server);
David Howellsd2ddc772017-11-02 15:27:50 +0000192 afs_put_cb_interest(call->net, call->cbi);
David Howells3bf0fb62018-10-20 00:57:59 +0100193 afs_put_addrlist(call->alist);
David Howells341f7412017-01-05 10:38:36 +0000194 kfree(call->request);
David Howellsa25e21f2018-03-27 23:03:00 +0100195
196 trace_afs_call(call, afs_call_trace_free, 0, o,
197 __builtin_return_address(0));
David Howells341f7412017-01-05 10:38:36 +0000198 kfree(call);
199
David Howellsf044c882017-11-02 15:27:45 +0000200 o = atomic_dec_return(&net->nr_outstanding_calls);
David Howells341f7412017-01-05 10:38:36 +0000201 if (o == 0)
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100202 wake_up_var(&net->nr_outstanding_calls);
David Howells6c67c7c2014-05-21 14:48:05 +0100203 }
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100204}
205
David Howells7a75b002019-01-10 15:14:29 +0000206static struct afs_call *afs_get_call(struct afs_call *call,
207 enum afs_call_trace why)
208{
209 int u = atomic_inc_return(&call->usage);
210
211 trace_afs_call(call, why, u,
212 atomic_read(&call->net->nr_outstanding_calls),
213 __builtin_return_address(0));
214 return call;
215}
216
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100217/*
David Howells3bf0fb62018-10-20 00:57:59 +0100218 * Queue the call for actual work.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100219 */
David Howells3bf0fb62018-10-20 00:57:59 +0100220static void afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100221{
David Howells3bf0fb62018-10-20 00:57:59 +0100222 if (call->type->work) {
David Howells3bf0fb62018-10-20 00:57:59 +0100223 INIT_WORK(&call->work, call->type->work);
David Howells341f7412017-01-05 10:38:36 +0000224
David Howells7a75b002019-01-10 15:14:29 +0000225 afs_get_call(call, afs_call_trace_work);
David Howells3bf0fb62018-10-20 00:57:59 +0100226 if (!queue_work(afs_wq, &call->work))
227 afs_put_call(call);
228 }
David Howells6c67c7c2014-05-21 14:48:05 +0100229}
230
231/*
David Howells08e0e7c2007-04-26 15:55:03 -0700232 * allocate a call with flat request and reply buffers
233 */
David Howellsf044c882017-11-02 15:27:45 +0000234struct afs_call *afs_alloc_flat_call(struct afs_net *net,
235 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100236 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700237{
238 struct afs_call *call;
239
David Howellsf044c882017-11-02 15:27:45 +0000240 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700241 if (!call)
242 goto nomem_call;
243
David Howells00d3b7a2007-04-26 15:57:07 -0700244 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000245 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700246 call->request = kmalloc(request_size, GFP_NOFS);
247 if (!call->request)
248 goto nomem_free;
249 }
250
David Howellsd0016482016-08-30 20:42:14 +0100251 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000252 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100253 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700254 if (!call->buffer)
255 goto nomem_free;
256 }
257
David Howells12bdcf32018-10-20 00:57:56 +0100258 afs_extract_to_buf(call, call->reply_max);
David Howells025db802017-11-02 15:27:51 +0000259 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700260 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700261 return call;
262
David Howells00d3b7a2007-04-26 15:57:07 -0700263nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000264 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700265nomem_call:
266 return NULL;
267}
268
269/*
270 * clean up a call with flat buffer
271 */
272void afs_flat_call_destructor(struct afs_call *call)
273{
274 _enter("");
275
276 kfree(call->request);
277 call->request = NULL;
278 kfree(call->buffer);
279 call->buffer = NULL;
280}
281
David Howells2f5705a2017-03-16 16:27:46 +0000282#define AFS_BVEC_MAX 8
283
284/*
285 * Load the given bvec with the next few pages.
286 */
287static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
288 struct bio_vec *bv, pgoff_t first, pgoff_t last,
289 unsigned offset)
290{
291 struct page *pages[AFS_BVEC_MAX];
292 unsigned int nr, n, i, to, bytes = 0;
293
294 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
295 n = find_get_pages_contig(call->mapping, first, nr, pages);
296 ASSERTCMP(n, ==, nr);
297
298 msg->msg_flags |= MSG_MORE;
299 for (i = 0; i < nr; i++) {
300 to = PAGE_SIZE;
301 if (first + i >= last) {
302 to = call->last_to;
303 msg->msg_flags &= ~MSG_MORE;
304 }
305 bv[i].bv_page = pages[i];
306 bv[i].bv_len = to - offset;
307 bv[i].bv_offset = offset;
308 bytes += to - offset;
309 offset = 0;
310 }
311
David Howellsaa563d72018-10-20 00:57:56 +0100312 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
David Howells2f5705a2017-03-16 16:27:46 +0000313}
314
David Howells08e0e7c2007-04-26 15:55:03 -0700315/*
David Howellse8332512017-08-29 10:18:56 +0100316 * Advance the AFS call state when the RxRPC call ends the transmit phase.
317 */
318static void afs_notify_end_request_tx(struct sock *sock,
319 struct rxrpc_call *rxcall,
320 unsigned long call_user_ID)
321{
322 struct afs_call *call = (struct afs_call *)call_user_ID;
323
David Howells98bf40c2017-11-02 15:27:53 +0000324 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100325}
326
327/*
David Howells31143d52007-05-09 02:33:46 -0700328 * attach the data from a bunch of pages on an inode to a call
329 */
Al Viro39c6ace2016-01-09 20:36:51 -0500330static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700331{
David Howells2f5705a2017-03-16 16:27:46 +0000332 struct bio_vec bv[AFS_BVEC_MAX];
333 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700334 pgoff_t first = call->first, last = call->last;
335 int ret;
336
David Howells31143d52007-05-09 02:33:46 -0700337 offset = call->first_offset;
338 call->first_offset = 0;
339
340 do {
David Howells2f5705a2017-03-16 16:27:46 +0000341 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000342 trace_afs_send_pages(call, msg, first, last, offset);
343
David Howells2f5705a2017-03-16 16:27:46 +0000344 offset = 0;
345 bytes = msg->msg_iter.count;
346 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700347
David Howellsf044c882017-11-02 15:27:45 +0000348 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100349 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000350 for (loop = 0; loop < nr; loop++)
351 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700352 if (ret < 0)
353 break;
David Howells2f5705a2017-03-16 16:27:46 +0000354
355 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700356 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700357
David Howells2c099012017-11-02 15:27:51 +0000358 trace_afs_sent_pages(call, call->first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700359 return ret;
360}
361
362/*
David Howells0b9bf382019-04-25 14:26:50 +0100363 * Initiate a call and synchronously queue up the parameters for dispatch. Any
364 * error is stored into the call struct, which the caller must check for.
David Howells08e0e7c2007-04-26 15:55:03 -0700365 */
David Howells0b9bf382019-04-25 14:26:50 +0100366void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
David Howells08e0e7c2007-04-26 15:55:03 -0700367{
David Howells2feeaf82018-10-20 00:57:59 +0100368 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
David Howells08e0e7c2007-04-26 15:55:03 -0700369 struct rxrpc_call *rxcall;
370 struct msghdr msg;
371 struct kvec iov[1];
David Howellse754eba2017-06-07 12:40:03 +0100372 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700373 int ret;
374
David Howells4d9df982017-11-02 15:27:47 +0000375 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700376
David Howells00d3b7a2007-04-26 15:57:07 -0700377 ASSERT(call->type != NULL);
378 ASSERT(call->type->name != NULL);
379
David Howells31143d52007-05-09 02:33:46 -0700380 _debug("____MAKE %p{%s,%x} [%d]____",
381 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000382 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700383
David Howells3bf0fb62018-10-20 00:57:59 +0100384 call->addr_ix = ac->index;
385 call->alist = afs_get_addrlist(ac->alist);
David Howells08e0e7c2007-04-26 15:55:03 -0700386
David Howellse754eba2017-06-07 12:40:03 +0100387 /* Work out the length we're going to transmit. This is awkward for
388 * calls such as FS.StoreData where there's an extra injection of data
389 * after the initial fixed part.
390 */
391 tx_total_len = call->request_size;
392 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000393 if (call->last == call->first) {
394 tx_total_len += call->last_to - call->first_offset;
395 } else {
396 /* It looks mathematically like you should be able to
397 * combine the following lines with the ones above, but
398 * unsigned arithmetic is fun when it wraps...
399 */
400 tx_total_len += PAGE_SIZE - call->first_offset;
401 tx_total_len += call->last_to;
402 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
403 }
David Howellse754eba2017-06-07 12:40:03 +0100404 }
405
David Howells34fa4762019-01-10 15:40:50 +0000406 /* If the call is going to be asynchronous, we need an extra ref for
407 * the call to hold itself so the caller need not hang on to its ref.
408 */
409 if (call->async)
410 afs_get_call(call, afs_call_trace_get);
411
David Howells08e0e7c2007-04-26 15:55:03 -0700412 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000413 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100414 (unsigned long)call,
415 tx_total_len, gfp,
David Howells0b9bf382019-04-25 14:26:50 +0100416 (call->async ?
David Howells56ff9c82017-01-05 10:38:36 +0000417 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100418 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100419 call->upgrade,
420 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700421 if (IS_ERR(rxcall)) {
422 ret = PTR_ERR(rxcall);
David Howells3bf0fb62018-10-20 00:57:59 +0100423 call->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700424 goto error_kill_call;
425 }
426
427 call->rxcall = rxcall;
428
David Howells94f699c2019-05-16 13:21:59 +0100429 if (call->max_lifespan)
430 rxrpc_kernel_set_max_life(call->net->socket, rxcall,
431 call->max_lifespan);
432
David Howells08e0e7c2007-04-26 15:55:03 -0700433 /* send the request */
434 iov[0].iov_base = call->request;
435 iov[0].iov_len = call->request_size;
436
437 msg.msg_name = NULL;
438 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100439 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700440 msg.msg_control = NULL;
441 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100442 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700443
David Howellsf044c882017-11-02 15:27:45 +0000444 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100445 &msg, call->request_size,
446 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700447 if (ret < 0)
448 goto error_do_abort;
449
David Howells31143d52007-05-09 02:33:46 -0700450 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500451 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700452 if (ret < 0)
453 goto error_do_abort;
454 }
455
David Howells34fa4762019-01-10 15:40:50 +0000456 /* Note that at this point, we may have received the reply or an abort
457 * - and an asynchronous call may already have completed.
David Howells0b9bf382019-04-25 14:26:50 +0100458 *
459 * afs_wait_for_call_to_complete(call, ac)
460 * must be called to synchronously clean up.
David Howells34fa4762019-01-10 15:40:50 +0000461 */
David Howells0b9bf382019-04-25 14:26:50 +0100462 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700463
464error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000465 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000466 rxrpc_kernel_abort_call(call->net->socket, rxcall,
467 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000468 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100469 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
David Howellseb9950e2018-08-03 17:06:56 +0100470 rxrpc_kernel_recv_data(call->net->socket, rxcall,
471 &msg.msg_iter, false,
472 &call->abort_code, &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000473 ac->abort_code = call->abort_code;
474 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000475 }
David Howells025db802017-11-02 15:27:51 +0000476 call->error = ret;
477 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700478error_kill_call:
David Howells3bf0fb62018-10-20 00:57:59 +0100479 if (call->type->done)
480 call->type->done(call);
David Howells34fa4762019-01-10 15:40:50 +0000481
482 /* We need to dispose of the extra ref we grabbed for an async call.
483 * The call, however, might be queued on afs_async_calls and we need to
484 * make sure we don't get any more notifications that might requeue it.
485 */
486 if (call->rxcall) {
487 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
488 call->rxcall = NULL;
489 }
490 if (call->async) {
491 if (cancel_work_sync(&call->async_work))
492 afs_put_call(call);
493 afs_put_call(call);
494 }
495
David Howellsd2ddc772017-11-02 15:27:50 +0000496 ac->error = ret;
David Howells34fa4762019-01-10 15:40:50 +0000497 call->state = AFS_CALL_COMPLETE;
David Howells08e0e7c2007-04-26 15:55:03 -0700498 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700499}
500
501/*
David Howells08e0e7c2007-04-26 15:55:03 -0700502 * deliver messages to a call
503 */
504static void afs_deliver_to_call(struct afs_call *call)
505{
David Howells98bf40c2017-11-02 15:27:53 +0000506 enum afs_call_state state;
507 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700508 int ret;
509
David Howellsd0016482016-08-30 20:42:14 +0100510 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700511
David Howells98bf40c2017-11-02 15:27:53 +0000512 while (state = READ_ONCE(call->state),
513 state == AFS_CALL_CL_AWAIT_REPLY ||
514 state == AFS_CALL_SV_AWAIT_OP_ID ||
515 state == AFS_CALL_SV_AWAIT_REQUEST ||
516 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100517 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000518 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howells12bdcf32018-10-20 00:57:56 +0100519 iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
David Howellsf044c882017-11-02 15:27:45 +0000520 ret = rxrpc_kernel_recv_data(call->net->socket,
David Howells12bdcf32018-10-20 00:57:56 +0100521 call->rxcall, &call->iter,
522 false, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100523 &call->service_id);
David Howells12bdcf32018-10-20 00:57:56 +0100524 trace_afs_receive_data(call, &call->iter, false, ret);
David Howells8e8d7f12017-01-05 10:38:34 +0000525
David Howellsd0016482016-08-30 20:42:14 +0100526 if (ret == -EINPROGRESS || ret == -EAGAIN)
527 return;
David Howells98bf40c2017-11-02 15:27:53 +0000528 if (ret < 0 || ret == 1) {
529 if (ret == 1)
530 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000531 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000532 }
David Howellsd0016482016-08-30 20:42:14 +0100533 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700534 }
535
David Howells12d8e952018-10-20 00:57:58 +0100536 if (call->want_reply_time &&
537 rxrpc_kernel_get_reply_time(call->net->socket,
538 call->rxcall,
539 &call->reply_time))
540 call->want_reply_time = false;
541
David Howellsd0016482016-08-30 20:42:14 +0100542 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000543 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100544 switch (ret) {
545 case 0:
David Howells3bf0fb62018-10-20 00:57:59 +0100546 afs_queue_call_work(call);
David Howellsf2686b02018-05-10 14:12:50 +0100547 if (state == AFS_CALL_CL_PROC_REPLY) {
548 if (call->cbi)
549 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
550 &call->cbi->server->flags);
David Howells025db802017-11-02 15:27:51 +0000551 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100552 }
David Howells98bf40c2017-11-02 15:27:53 +0000553 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100554 goto done;
555 case -EINPROGRESS:
556 case -EAGAIN:
557 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000558 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000559 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
560 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100561 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000562 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000563 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100564 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000565 goto local_abort;
David Howells4ac15ea2018-10-20 00:57:57 +0100566 case -EIO:
567 pr_err("kAFS: Call %u in bad state %u\n",
568 call->debug_id, state);
569 /* Fall through */
David Howellsd0016482016-08-30 20:42:14 +0100570 case -ENODATA:
571 case -EBADMSG:
572 case -EMSGSIZE:
David Howellsd0016482016-08-30 20:42:14 +0100573 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000574 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100575 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000576 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells12bdcf32018-10-20 00:57:56 +0100577 abort_code, ret, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000578 goto local_abort;
David Howells8022c4b2019-04-13 08:37:37 +0100579 default:
580 abort_code = RX_USER_ABORT;
581 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
582 abort_code, ret, "KER");
583 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100584 }
David Howells08e0e7c2007-04-26 15:55:03 -0700585 }
586
David Howellsd0016482016-08-30 20:42:14 +0100587done:
David Howells3bf0fb62018-10-20 00:57:59 +0100588 if (call->type->done)
589 call->type->done(call);
David Howells98bf40c2017-11-02 15:27:53 +0000590 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000591 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100592out:
David Howells08e0e7c2007-04-26 15:55:03 -0700593 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100594 return;
595
David Howells98bf40c2017-11-02 15:27:53 +0000596local_abort:
597 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000598call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000599 afs_set_call_complete(call, ret, remote_abort);
600 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100601 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700602}
603
604/*
David Howells0b9bf382019-04-25 14:26:50 +0100605 * Wait synchronously for a call to complete and clean up the call struct.
David Howells08e0e7c2007-04-26 15:55:03 -0700606 */
David Howells0b9bf382019-04-25 14:26:50 +0100607long afs_wait_for_call_to_complete(struct afs_call *call,
608 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700609{
David Howellsbc5e3a52017-10-18 11:07:31 +0100610 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000611 long ret;
David Howells7150cea2018-11-12 22:33:22 +0000612 bool stalled = false;
David Howellsbc5e3a52017-10-18 11:07:31 +0100613 u64 rtt;
614 u32 life, last_life;
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100615 bool rxrpc_complete = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700616
617 DECLARE_WAITQUEUE(myself, current);
618
619 _enter("");
620
David Howells0b9bf382019-04-25 14:26:50 +0100621 ret = call->error;
622 if (ret < 0)
623 goto out;
624
David Howellsf044c882017-11-02 15:27:45 +0000625 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100626 rtt2 = nsecs_to_jiffies64(rtt) * 2;
627 if (rtt2 < 2)
628 rtt2 = 2;
629
630 timeout = rtt2;
Marc Dionne4611da32019-04-12 16:33:47 +0100631 rxrpc_kernel_check_life(call->net->socket, call->rxcall, &last_life);
David Howellsbc5e3a52017-10-18 11:07:31 +0100632
David Howells08e0e7c2007-04-26 15:55:03 -0700633 add_wait_queue(&call->waitq, &myself);
634 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100635 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700636
637 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000638 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
639 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100640 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700641 __set_current_state(TASK_RUNNING);
642 afs_deliver_to_call(call);
643 continue;
644 }
645
David Howells98bf40c2017-11-02 15:27:53 +0000646 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700647 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100648
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100649 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall, &life)) {
650 /* rxrpc terminated the call. */
651 rxrpc_complete = true;
652 break;
653 }
654
David Howellsbc5e3a52017-10-18 11:07:31 +0100655 if (timeout == 0 &&
David Howells7150cea2018-11-12 22:33:22 +0000656 life == last_life && signal_pending(current)) {
657 if (stalled)
David Howellsbc5e3a52017-10-18 11:07:31 +0100658 break;
David Howells7150cea2018-11-12 22:33:22 +0000659 __set_current_state(TASK_RUNNING);
660 rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
661 timeout = rtt2;
662 stalled = true;
663 continue;
664 }
David Howellsbc5e3a52017-10-18 11:07:31 +0100665
666 if (life != last_life) {
667 timeout = rtt2;
668 last_life = life;
David Howells7150cea2018-11-12 22:33:22 +0000669 stalled = false;
David Howellsbc5e3a52017-10-18 11:07:31 +0100670 }
671
672 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700673 }
674
675 remove_wait_queue(&call->waitq, &myself);
676 __set_current_state(TASK_RUNNING);
677
David Howells98bf40c2017-11-02 15:27:53 +0000678 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100679 if (rxrpc_complete) {
680 afs_set_call_complete(call, call->error, call->abort_code);
681 } else {
682 /* Kill off the call if it's still live. */
683 _debug("call interrupted");
684 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
685 RX_USER_ABORT, -EINTR, "KWI"))
686 afs_set_call_complete(call, -EINTR, 0);
687 }
David Howells08e0e7c2007-04-26 15:55:03 -0700688 }
689
David Howells98bf40c2017-11-02 15:27:53 +0000690 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000691 ac->abort_code = call->abort_code;
692 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000693 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000694
695 ret = ac->error;
696 switch (ret) {
697 case 0:
698 if (call->ret_reply0) {
699 ret = (long)call->reply[0];
700 call->reply[0] = NULL;
701 }
702 /* Fall through */
703 case -ECONNABORTED:
704 ac->responded = true;
705 break;
David Howells33cd7f22017-11-02 15:27:48 +0000706 }
707
David Howells0b9bf382019-04-25 14:26:50 +0100708out:
David Howells08e0e7c2007-04-26 15:55:03 -0700709 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000710 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000711 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700712 return ret;
713}
714
715/*
716 * wake up a waiting call
717 */
David Howellsd0016482016-08-30 20:42:14 +0100718static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
719 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700720{
David Howellsd0016482016-08-30 20:42:14 +0100721 struct afs_call *call = (struct afs_call *)call_user_ID;
722
723 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700724 wake_up(&call->waitq);
725}
726
727/*
728 * wake up an asynchronous call
729 */
David Howellsd0016482016-08-30 20:42:14 +0100730static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
731 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700732{
David Howellsd0016482016-08-30 20:42:14 +0100733 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000734 int u;
David Howellsd0016482016-08-30 20:42:14 +0100735
David Howells8e8d7f12017-01-05 10:38:34 +0000736 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100737 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000738
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100739 u = atomic_fetch_add_unless(&call->usage, 1, 0);
David Howells341f7412017-01-05 10:38:36 +0000740 if (u != 0) {
741 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000742 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000743 __builtin_return_address(0));
744
745 if (!queue_work(afs_async_calls, &call->async_work))
746 afs_put_call(call);
747 }
David Howells08e0e7c2007-04-26 15:55:03 -0700748}
749
750/*
David Howells341f7412017-01-05 10:38:36 +0000751 * Delete an asynchronous call. The work item carries a ref to the call struct
752 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700753 */
David Howellsd0016482016-08-30 20:42:14 +0100754static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700755{
David Howellsd0016482016-08-30 20:42:14 +0100756 struct afs_call *call = container_of(work, struct afs_call, async_work);
757
David Howells08e0e7c2007-04-26 15:55:03 -0700758 _enter("");
759
David Howells341f7412017-01-05 10:38:36 +0000760 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700761
762 _leave("");
763}
764
765/*
David Howells341f7412017-01-05 10:38:36 +0000766 * Perform I/O processing on an asynchronous call. The work item carries a ref
767 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700768 */
David Howellsd0016482016-08-30 20:42:14 +0100769static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700770{
David Howellsd0016482016-08-30 20:42:14 +0100771 struct afs_call *call = container_of(work, struct afs_call, async_work);
772
David Howells08e0e7c2007-04-26 15:55:03 -0700773 _enter("");
774
David Howellsd0016482016-08-30 20:42:14 +0100775 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
776 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700777 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100778 }
David Howells08e0e7c2007-04-26 15:55:03 -0700779
David Howells56ff9c82017-01-05 10:38:36 +0000780 if (call->state == AFS_CALL_COMPLETE) {
David Howells341f7412017-01-05 10:38:36 +0000781 /* We have two refs to release - one from the alloc and one
782 * queued with the work item - and we can't just deallocate the
783 * call because the work item may be queued again.
784 */
David Howellsd0016482016-08-30 20:42:14 +0100785 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000786 if (!queue_work(afs_async_calls, &call->async_work))
787 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700788 }
789
David Howells341f7412017-01-05 10:38:36 +0000790 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700791 _leave("");
792}
793
David Howells00e90712016-09-08 11:10:12 +0100794static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
795{
796 struct afs_call *call = (struct afs_call *)user_call_ID;
797
798 call->rxcall = rxcall;
799}
800
801/*
802 * Charge the incoming call preallocation.
803 */
David Howellsf044c882017-11-02 15:27:45 +0000804void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100805{
David Howellsf044c882017-11-02 15:27:45 +0000806 struct afs_net *net =
807 container_of(work, struct afs_net, charge_preallocation_work);
808 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100809
810 for (;;) {
811 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000812 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100813 if (!call)
814 break;
815
David Howells56ff9c82017-01-05 10:38:36 +0000816 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000817 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000818 init_waitqueue_head(&call->waitq);
David Howells12bdcf32018-10-20 00:57:56 +0100819 afs_extract_to_tmp(call);
David Howells00e90712016-09-08 11:10:12 +0100820 }
821
David Howellsf044c882017-11-02 15:27:45 +0000822 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100823 afs_wake_up_async_call,
824 afs_rx_attach,
825 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100826 GFP_KERNEL,
827 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100828 break;
829 call = NULL;
830 }
David Howellsf044c882017-11-02 15:27:45 +0000831 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100832}
833
834/*
835 * Discard a preallocated call when a socket is shut down.
836 */
837static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
838 unsigned long user_call_ID)
839{
840 struct afs_call *call = (struct afs_call *)user_call_ID;
841
David Howells00e90712016-09-08 11:10:12 +0100842 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000843 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100844}
845
David Howells08e0e7c2007-04-26 15:55:03 -0700846/*
David Howellsd0016482016-08-30 20:42:14 +0100847 * Notification of an incoming call.
848 */
David Howells00e90712016-09-08 11:10:12 +0100849static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
850 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100851{
David Howellsf044c882017-11-02 15:27:45 +0000852 struct afs_net *net = afs_sock2net(sk);
853
854 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100855}
856
857/*
David Howells372ee162016-08-03 14:11:40 +0100858 * Grab the operation ID from an incoming cache manager call. The socket
859 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700860 */
David Howellsd0016482016-08-30 20:42:14 +0100861static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700862{
David Howellsd0016482016-08-30 20:42:14 +0100863 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700864
David Howells12bdcf32018-10-20 00:57:56 +0100865 _enter("{%zu}", iov_iter_count(call->_iter));
David Howells08e0e7c2007-04-26 15:55:03 -0700866
867 /* the operation ID forms the first four bytes of the request data */
David Howells12bdcf32018-10-20 00:57:56 +0100868 ret = afs_extract_data(call, true);
David Howellsd0016482016-08-30 20:42:14 +0100869 if (ret < 0)
870 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700871
David Howells50a2c952016-10-13 08:27:10 +0100872 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000873 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howells08e0e7c2007-04-26 15:55:03 -0700874
875 /* ask the cache manager to route the call (it'll change the call type
876 * if successful) */
877 if (!afs_cm_incoming_call(call))
878 return -ENOTSUPP;
879
David Howells8e8d7f12017-01-05 10:38:34 +0000880 trace_afs_cb_call(call);
881
David Howells08e0e7c2007-04-26 15:55:03 -0700882 /* pass responsibility for the remainer of this message off to the
883 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100884 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700885}
886
887/*
David Howellse8332512017-08-29 10:18:56 +0100888 * Advance the AFS call state when an RxRPC service call ends the transmit
889 * phase.
890 */
891static void afs_notify_end_reply_tx(struct sock *sock,
892 struct rxrpc_call *rxcall,
893 unsigned long call_user_ID)
894{
895 struct afs_call *call = (struct afs_call *)call_user_ID;
896
David Howells98bf40c2017-11-02 15:27:53 +0000897 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100898}
899
900/*
David Howells08e0e7c2007-04-26 15:55:03 -0700901 * send an empty reply
902 */
903void afs_send_empty_reply(struct afs_call *call)
904{
David Howellsf044c882017-11-02 15:27:45 +0000905 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700906 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700907
908 _enter("");
909
David Howellsf044c882017-11-02 15:27:45 +0000910 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100911
David Howells08e0e7c2007-04-26 15:55:03 -0700912 msg.msg_name = NULL;
913 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100914 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700915 msg.msg_control = NULL;
916 msg.msg_controllen = 0;
917 msg.msg_flags = 0;
918
David Howellsf044c882017-11-02 15:27:45 +0000919 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100920 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700921 case 0:
922 _leave(" [replied]");
923 return;
924
925 case -ENOMEM:
926 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000927 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100928 RX_USER_ABORT, -ENOMEM, "KOO");
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600929 /* Fall through */
David Howells08e0e7c2007-04-26 15:55:03 -0700930 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700931 _leave(" [error]");
932 return;
933 }
934}
935
936/*
David Howellsb908fe62007-04-26 15:58:17 -0700937 * send a simple reply
938 */
939void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
940{
David Howellsf044c882017-11-02 15:27:45 +0000941 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700942 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500943 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100944 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700945
946 _enter("");
947
David Howellsf044c882017-11-02 15:27:45 +0000948 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100949
David Howellsb908fe62007-04-26 15:58:17 -0700950 iov[0].iov_base = (void *) buf;
951 iov[0].iov_len = len;
952 msg.msg_name = NULL;
953 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100954 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700955 msg.msg_control = NULL;
956 msg.msg_controllen = 0;
957 msg.msg_flags = 0;
958
David Howellsf044c882017-11-02 15:27:45 +0000959 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100960 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100961 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100962 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700963 _leave(" [replied]");
964 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100965 }
David Howells6c67c7c2014-05-21 14:48:05 +0100966
David Howellsbd6dc742007-07-20 10:59:41 +0100967 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700968 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000969 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100970 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700971 }
David Howellsbd6dc742007-07-20 10:59:41 +0100972 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700973}
974
975/*
David Howells372ee162016-08-03 14:11:40 +0100976 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700977 */
David Howells12bdcf32018-10-20 00:57:56 +0100978int afs_extract_data(struct afs_call *call, bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700979{
David Howellsf044c882017-11-02 15:27:45 +0000980 struct afs_net *net = call->net;
David Howells12bdcf32018-10-20 00:57:56 +0100981 struct iov_iter *iter = call->_iter;
David Howells98bf40c2017-11-02 15:27:53 +0000982 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000983 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100984 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700985
David Howells12bdcf32018-10-20 00:57:56 +0100986 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700987
David Howells12bdcf32018-10-20 00:57:56 +0100988 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
David Howells98bf40c2017-11-02 15:27:53 +0000989 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100990 &call->service_id);
David Howellsd0016482016-08-30 20:42:14 +0100991 if (ret == 0 || ret == -EAGAIN)
992 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700993
David Howells98bf40c2017-11-02 15:27:53 +0000994 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100995 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000996 switch (state) {
997 case AFS_CALL_CL_AWAIT_REPLY:
998 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100999 break;
David Howells98bf40c2017-11-02 15:27:53 +00001000 case AFS_CALL_SV_AWAIT_REQUEST:
1001 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +01001002 break;
David Howells98bf40c2017-11-02 15:27:53 +00001003 case AFS_CALL_COMPLETE:
1004 kdebug("prem complete %d", call->error);
David Howellsf51375c2018-10-20 00:57:57 +01001005 return afs_io_error(call, afs_io_error_extract);
David Howellsd0016482016-08-30 20:42:14 +01001006 default:
1007 break;
1008 }
1009 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -07001010 }
David Howellsd0016482016-08-30 20:42:14 +01001011
David Howells98bf40c2017-11-02 15:27:53 +00001012 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +01001013 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -07001014}
David Howells5f702c82018-04-06 14:17:25 +01001015
1016/*
1017 * Log protocol error production.
1018 */
David Howells160cb952018-10-20 00:57:56 +01001019noinline int afs_protocol_error(struct afs_call *call, int error,
1020 enum afs_eproto_cause cause)
David Howells5f702c82018-04-06 14:17:25 +01001021{
David Howells160cb952018-10-20 00:57:56 +01001022 trace_afs_protocol_error(call, error, cause);
David Howells5f702c82018-04-06 14:17:25 +01001023 return error;
1024}