blob: a34a89c75c6ac6e75195c0b9f5675aff10008bbf [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
429 /* send the request */
430 iov[0].iov_base = call->request;
431 iov[0].iov_len = call->request_size;
432
433 msg.msg_name = NULL;
434 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100435 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700436 msg.msg_control = NULL;
437 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100438 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700439
David Howellsf044c882017-11-02 15:27:45 +0000440 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100441 &msg, call->request_size,
442 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700443 if (ret < 0)
444 goto error_do_abort;
445
David Howells31143d52007-05-09 02:33:46 -0700446 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500447 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700448 if (ret < 0)
449 goto error_do_abort;
450 }
451
David Howells34fa4762019-01-10 15:40:50 +0000452 /* Note that at this point, we may have received the reply or an abort
453 * - and an asynchronous call may already have completed.
David Howells0b9bf382019-04-25 14:26:50 +0100454 *
455 * afs_wait_for_call_to_complete(call, ac)
456 * must be called to synchronously clean up.
David Howells34fa4762019-01-10 15:40:50 +0000457 */
David Howells0b9bf382019-04-25 14:26:50 +0100458 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700459
460error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000461 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000462 rxrpc_kernel_abort_call(call->net->socket, rxcall,
463 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000464 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100465 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
David Howellseb9950e2018-08-03 17:06:56 +0100466 rxrpc_kernel_recv_data(call->net->socket, rxcall,
467 &msg.msg_iter, false,
468 &call->abort_code, &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000469 ac->abort_code = call->abort_code;
470 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000471 }
David Howells025db802017-11-02 15:27:51 +0000472 call->error = ret;
473 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700474error_kill_call:
David Howells3bf0fb62018-10-20 00:57:59 +0100475 if (call->type->done)
476 call->type->done(call);
David Howells34fa4762019-01-10 15:40:50 +0000477
478 /* We need to dispose of the extra ref we grabbed for an async call.
479 * The call, however, might be queued on afs_async_calls and we need to
480 * make sure we don't get any more notifications that might requeue it.
481 */
482 if (call->rxcall) {
483 rxrpc_kernel_end_call(call->net->socket, call->rxcall);
484 call->rxcall = NULL;
485 }
486 if (call->async) {
487 if (cancel_work_sync(&call->async_work))
488 afs_put_call(call);
489 afs_put_call(call);
490 }
491
David Howellsd2ddc772017-11-02 15:27:50 +0000492 ac->error = ret;
David Howells34fa4762019-01-10 15:40:50 +0000493 call->state = AFS_CALL_COMPLETE;
David Howells08e0e7c2007-04-26 15:55:03 -0700494 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700495}
496
497/*
David Howells08e0e7c2007-04-26 15:55:03 -0700498 * deliver messages to a call
499 */
500static void afs_deliver_to_call(struct afs_call *call)
501{
David Howells98bf40c2017-11-02 15:27:53 +0000502 enum afs_call_state state;
503 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700504 int ret;
505
David Howellsd0016482016-08-30 20:42:14 +0100506 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700507
David Howells98bf40c2017-11-02 15:27:53 +0000508 while (state = READ_ONCE(call->state),
509 state == AFS_CALL_CL_AWAIT_REPLY ||
510 state == AFS_CALL_SV_AWAIT_OP_ID ||
511 state == AFS_CALL_SV_AWAIT_REQUEST ||
512 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100513 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000514 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howells12bdcf32018-10-20 00:57:56 +0100515 iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
David Howellsf044c882017-11-02 15:27:45 +0000516 ret = rxrpc_kernel_recv_data(call->net->socket,
David Howells12bdcf32018-10-20 00:57:56 +0100517 call->rxcall, &call->iter,
518 false, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100519 &call->service_id);
David Howells12bdcf32018-10-20 00:57:56 +0100520 trace_afs_receive_data(call, &call->iter, false, ret);
David Howells8e8d7f12017-01-05 10:38:34 +0000521
David Howellsd0016482016-08-30 20:42:14 +0100522 if (ret == -EINPROGRESS || ret == -EAGAIN)
523 return;
David Howells98bf40c2017-11-02 15:27:53 +0000524 if (ret < 0 || ret == 1) {
525 if (ret == 1)
526 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000527 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000528 }
David Howellsd0016482016-08-30 20:42:14 +0100529 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700530 }
531
David Howells12d8e952018-10-20 00:57:58 +0100532 if (call->want_reply_time &&
533 rxrpc_kernel_get_reply_time(call->net->socket,
534 call->rxcall,
535 &call->reply_time))
536 call->want_reply_time = false;
537
David Howellsd0016482016-08-30 20:42:14 +0100538 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000539 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100540 switch (ret) {
541 case 0:
David Howells3bf0fb62018-10-20 00:57:59 +0100542 afs_queue_call_work(call);
David Howellsf2686b02018-05-10 14:12:50 +0100543 if (state == AFS_CALL_CL_PROC_REPLY) {
544 if (call->cbi)
545 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
546 &call->cbi->server->flags);
David Howells025db802017-11-02 15:27:51 +0000547 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100548 }
David Howells98bf40c2017-11-02 15:27:53 +0000549 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100550 goto done;
551 case -EINPROGRESS:
552 case -EAGAIN:
553 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000554 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000555 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
556 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100557 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000558 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000559 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100560 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000561 goto local_abort;
David Howells4ac15ea2018-10-20 00:57:57 +0100562 case -EIO:
563 pr_err("kAFS: Call %u in bad state %u\n",
564 call->debug_id, state);
565 /* Fall through */
David Howellsd0016482016-08-30 20:42:14 +0100566 case -ENODATA:
567 case -EBADMSG:
568 case -EMSGSIZE:
David Howellsd0016482016-08-30 20:42:14 +0100569 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000570 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100571 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000572 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells12bdcf32018-10-20 00:57:56 +0100573 abort_code, ret, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000574 goto local_abort;
David Howells8022c4b2019-04-13 08:37:37 +0100575 default:
576 abort_code = RX_USER_ABORT;
577 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
578 abort_code, ret, "KER");
579 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100580 }
David Howells08e0e7c2007-04-26 15:55:03 -0700581 }
582
David Howellsd0016482016-08-30 20:42:14 +0100583done:
David Howells3bf0fb62018-10-20 00:57:59 +0100584 if (call->type->done)
585 call->type->done(call);
David Howells98bf40c2017-11-02 15:27:53 +0000586 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000587 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100588out:
David Howells08e0e7c2007-04-26 15:55:03 -0700589 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100590 return;
591
David Howells98bf40c2017-11-02 15:27:53 +0000592local_abort:
593 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000594call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000595 afs_set_call_complete(call, ret, remote_abort);
596 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100597 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700598}
599
600/*
David Howells0b9bf382019-04-25 14:26:50 +0100601 * Wait synchronously for a call to complete and clean up the call struct.
David Howells08e0e7c2007-04-26 15:55:03 -0700602 */
David Howells0b9bf382019-04-25 14:26:50 +0100603long afs_wait_for_call_to_complete(struct afs_call *call,
604 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700605{
David Howellsbc5e3a52017-10-18 11:07:31 +0100606 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000607 long ret;
David Howells7150cea2018-11-12 22:33:22 +0000608 bool stalled = false;
David Howellsbc5e3a52017-10-18 11:07:31 +0100609 u64 rtt;
610 u32 life, last_life;
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100611 bool rxrpc_complete = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700612
613 DECLARE_WAITQUEUE(myself, current);
614
615 _enter("");
616
David Howells0b9bf382019-04-25 14:26:50 +0100617 ret = call->error;
618 if (ret < 0)
619 goto out;
620
David Howellsf044c882017-11-02 15:27:45 +0000621 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100622 rtt2 = nsecs_to_jiffies64(rtt) * 2;
623 if (rtt2 < 2)
624 rtt2 = 2;
625
626 timeout = rtt2;
Marc Dionne4611da32019-04-12 16:33:47 +0100627 rxrpc_kernel_check_life(call->net->socket, call->rxcall, &last_life);
David Howellsbc5e3a52017-10-18 11:07:31 +0100628
David Howells08e0e7c2007-04-26 15:55:03 -0700629 add_wait_queue(&call->waitq, &myself);
630 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100631 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700632
633 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000634 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
635 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100636 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700637 __set_current_state(TASK_RUNNING);
638 afs_deliver_to_call(call);
639 continue;
640 }
641
David Howells98bf40c2017-11-02 15:27:53 +0000642 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700643 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100644
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100645 if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall, &life)) {
646 /* rxrpc terminated the call. */
647 rxrpc_complete = true;
648 break;
649 }
650
David Howellsbc5e3a52017-10-18 11:07:31 +0100651 if (timeout == 0 &&
David Howells7150cea2018-11-12 22:33:22 +0000652 life == last_life && signal_pending(current)) {
653 if (stalled)
David Howellsbc5e3a52017-10-18 11:07:31 +0100654 break;
David Howells7150cea2018-11-12 22:33:22 +0000655 __set_current_state(TASK_RUNNING);
656 rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
657 timeout = rtt2;
658 stalled = true;
659 continue;
660 }
David Howellsbc5e3a52017-10-18 11:07:31 +0100661
662 if (life != last_life) {
663 timeout = rtt2;
664 last_life = life;
David Howells7150cea2018-11-12 22:33:22 +0000665 stalled = false;
David Howellsbc5e3a52017-10-18 11:07:31 +0100666 }
667
668 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700669 }
670
671 remove_wait_queue(&call->waitq, &myself);
672 __set_current_state(TASK_RUNNING);
673
David Howells98bf40c2017-11-02 15:27:53 +0000674 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
Marc Dionnef7f1dd32019-04-12 16:34:02 +0100675 if (rxrpc_complete) {
676 afs_set_call_complete(call, call->error, call->abort_code);
677 } else {
678 /* Kill off the call if it's still live. */
679 _debug("call interrupted");
680 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
681 RX_USER_ABORT, -EINTR, "KWI"))
682 afs_set_call_complete(call, -EINTR, 0);
683 }
David Howells08e0e7c2007-04-26 15:55:03 -0700684 }
685
David Howells98bf40c2017-11-02 15:27:53 +0000686 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000687 ac->abort_code = call->abort_code;
688 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000689 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000690
691 ret = ac->error;
692 switch (ret) {
693 case 0:
694 if (call->ret_reply0) {
695 ret = (long)call->reply[0];
696 call->reply[0] = NULL;
697 }
698 /* Fall through */
699 case -ECONNABORTED:
700 ac->responded = true;
701 break;
David Howells33cd7f22017-11-02 15:27:48 +0000702 }
703
David Howells0b9bf382019-04-25 14:26:50 +0100704out:
David Howells08e0e7c2007-04-26 15:55:03 -0700705 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000706 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000707 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700708 return ret;
709}
710
711/*
712 * wake up a waiting call
713 */
David Howellsd0016482016-08-30 20:42:14 +0100714static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
715 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700716{
David Howellsd0016482016-08-30 20:42:14 +0100717 struct afs_call *call = (struct afs_call *)call_user_ID;
718
719 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700720 wake_up(&call->waitq);
721}
722
723/*
724 * wake up an asynchronous call
725 */
David Howellsd0016482016-08-30 20:42:14 +0100726static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
727 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700728{
David Howellsd0016482016-08-30 20:42:14 +0100729 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000730 int u;
David Howellsd0016482016-08-30 20:42:14 +0100731
David Howells8e8d7f12017-01-05 10:38:34 +0000732 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100733 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000734
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100735 u = atomic_fetch_add_unless(&call->usage, 1, 0);
David Howells341f7412017-01-05 10:38:36 +0000736 if (u != 0) {
737 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000738 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000739 __builtin_return_address(0));
740
741 if (!queue_work(afs_async_calls, &call->async_work))
742 afs_put_call(call);
743 }
David Howells08e0e7c2007-04-26 15:55:03 -0700744}
745
746/*
David Howells341f7412017-01-05 10:38:36 +0000747 * Delete an asynchronous call. The work item carries a ref to the call struct
748 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700749 */
David Howellsd0016482016-08-30 20:42:14 +0100750static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700751{
David Howellsd0016482016-08-30 20:42:14 +0100752 struct afs_call *call = container_of(work, struct afs_call, async_work);
753
David Howells08e0e7c2007-04-26 15:55:03 -0700754 _enter("");
755
David Howells341f7412017-01-05 10:38:36 +0000756 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700757
758 _leave("");
759}
760
761/*
David Howells341f7412017-01-05 10:38:36 +0000762 * Perform I/O processing on an asynchronous call. The work item carries a ref
763 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700764 */
David Howellsd0016482016-08-30 20:42:14 +0100765static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700766{
David Howellsd0016482016-08-30 20:42:14 +0100767 struct afs_call *call = container_of(work, struct afs_call, async_work);
768
David Howells08e0e7c2007-04-26 15:55:03 -0700769 _enter("");
770
David Howellsd0016482016-08-30 20:42:14 +0100771 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
772 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700773 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100774 }
David Howells08e0e7c2007-04-26 15:55:03 -0700775
David Howells56ff9c82017-01-05 10:38:36 +0000776 if (call->state == AFS_CALL_COMPLETE) {
David Howells341f7412017-01-05 10:38:36 +0000777 /* We have two refs to release - one from the alloc and one
778 * queued with the work item - and we can't just deallocate the
779 * call because the work item may be queued again.
780 */
David Howellsd0016482016-08-30 20:42:14 +0100781 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000782 if (!queue_work(afs_async_calls, &call->async_work))
783 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700784 }
785
David Howells341f7412017-01-05 10:38:36 +0000786 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700787 _leave("");
788}
789
David Howells00e90712016-09-08 11:10:12 +0100790static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
791{
792 struct afs_call *call = (struct afs_call *)user_call_ID;
793
794 call->rxcall = rxcall;
795}
796
797/*
798 * Charge the incoming call preallocation.
799 */
David Howellsf044c882017-11-02 15:27:45 +0000800void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100801{
David Howellsf044c882017-11-02 15:27:45 +0000802 struct afs_net *net =
803 container_of(work, struct afs_net, charge_preallocation_work);
804 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100805
806 for (;;) {
807 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000808 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100809 if (!call)
810 break;
811
David Howells56ff9c82017-01-05 10:38:36 +0000812 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000813 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000814 init_waitqueue_head(&call->waitq);
David Howells12bdcf32018-10-20 00:57:56 +0100815 afs_extract_to_tmp(call);
David Howells00e90712016-09-08 11:10:12 +0100816 }
817
David Howellsf044c882017-11-02 15:27:45 +0000818 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100819 afs_wake_up_async_call,
820 afs_rx_attach,
821 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100822 GFP_KERNEL,
823 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100824 break;
825 call = NULL;
826 }
David Howellsf044c882017-11-02 15:27:45 +0000827 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100828}
829
830/*
831 * Discard a preallocated call when a socket is shut down.
832 */
833static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
834 unsigned long user_call_ID)
835{
836 struct afs_call *call = (struct afs_call *)user_call_ID;
837
David Howells00e90712016-09-08 11:10:12 +0100838 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000839 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100840}
841
David Howells08e0e7c2007-04-26 15:55:03 -0700842/*
David Howellsd0016482016-08-30 20:42:14 +0100843 * Notification of an incoming call.
844 */
David Howells00e90712016-09-08 11:10:12 +0100845static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
846 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100847{
David Howellsf044c882017-11-02 15:27:45 +0000848 struct afs_net *net = afs_sock2net(sk);
849
850 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100851}
852
853/*
David Howells372ee162016-08-03 14:11:40 +0100854 * Grab the operation ID from an incoming cache manager call. The socket
855 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700856 */
David Howellsd0016482016-08-30 20:42:14 +0100857static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700858{
David Howellsd0016482016-08-30 20:42:14 +0100859 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700860
David Howells12bdcf32018-10-20 00:57:56 +0100861 _enter("{%zu}", iov_iter_count(call->_iter));
David Howells08e0e7c2007-04-26 15:55:03 -0700862
863 /* the operation ID forms the first four bytes of the request data */
David Howells12bdcf32018-10-20 00:57:56 +0100864 ret = afs_extract_data(call, true);
David Howellsd0016482016-08-30 20:42:14 +0100865 if (ret < 0)
866 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700867
David Howells50a2c952016-10-13 08:27:10 +0100868 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000869 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howells08e0e7c2007-04-26 15:55:03 -0700870
871 /* ask the cache manager to route the call (it'll change the call type
872 * if successful) */
873 if (!afs_cm_incoming_call(call))
874 return -ENOTSUPP;
875
David Howells8e8d7f12017-01-05 10:38:34 +0000876 trace_afs_cb_call(call);
877
David Howells08e0e7c2007-04-26 15:55:03 -0700878 /* pass responsibility for the remainer of this message off to the
879 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100880 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700881}
882
883/*
David Howellse8332512017-08-29 10:18:56 +0100884 * Advance the AFS call state when an RxRPC service call ends the transmit
885 * phase.
886 */
887static void afs_notify_end_reply_tx(struct sock *sock,
888 struct rxrpc_call *rxcall,
889 unsigned long call_user_ID)
890{
891 struct afs_call *call = (struct afs_call *)call_user_ID;
892
David Howells98bf40c2017-11-02 15:27:53 +0000893 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100894}
895
896/*
David Howells08e0e7c2007-04-26 15:55:03 -0700897 * send an empty reply
898 */
899void afs_send_empty_reply(struct afs_call *call)
900{
David Howellsf044c882017-11-02 15:27:45 +0000901 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700902 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700903
904 _enter("");
905
David Howellsf044c882017-11-02 15:27:45 +0000906 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100907
David Howells08e0e7c2007-04-26 15:55:03 -0700908 msg.msg_name = NULL;
909 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100910 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700911 msg.msg_control = NULL;
912 msg.msg_controllen = 0;
913 msg.msg_flags = 0;
914
David Howellsf044c882017-11-02 15:27:45 +0000915 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100916 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700917 case 0:
918 _leave(" [replied]");
919 return;
920
921 case -ENOMEM:
922 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000923 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100924 RX_USER_ABORT, -ENOMEM, "KOO");
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600925 /* Fall through */
David Howells08e0e7c2007-04-26 15:55:03 -0700926 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700927 _leave(" [error]");
928 return;
929 }
930}
931
932/*
David Howellsb908fe62007-04-26 15:58:17 -0700933 * send a simple reply
934 */
935void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
936{
David Howellsf044c882017-11-02 15:27:45 +0000937 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700938 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500939 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100940 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700941
942 _enter("");
943
David Howellsf044c882017-11-02 15:27:45 +0000944 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100945
David Howellsb908fe62007-04-26 15:58:17 -0700946 iov[0].iov_base = (void *) buf;
947 iov[0].iov_len = len;
948 msg.msg_name = NULL;
949 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100950 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700951 msg.msg_control = NULL;
952 msg.msg_controllen = 0;
953 msg.msg_flags = 0;
954
David Howellsf044c882017-11-02 15:27:45 +0000955 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100956 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100957 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100958 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700959 _leave(" [replied]");
960 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100961 }
David Howells6c67c7c2014-05-21 14:48:05 +0100962
David Howellsbd6dc742007-07-20 10:59:41 +0100963 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700964 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000965 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100966 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700967 }
David Howellsbd6dc742007-07-20 10:59:41 +0100968 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700969}
970
971/*
David Howells372ee162016-08-03 14:11:40 +0100972 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700973 */
David Howells12bdcf32018-10-20 00:57:56 +0100974int afs_extract_data(struct afs_call *call, bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700975{
David Howellsf044c882017-11-02 15:27:45 +0000976 struct afs_net *net = call->net;
David Howells12bdcf32018-10-20 00:57:56 +0100977 struct iov_iter *iter = call->_iter;
David Howells98bf40c2017-11-02 15:27:53 +0000978 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000979 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100980 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700981
David Howells12bdcf32018-10-20 00:57:56 +0100982 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700983
David Howells12bdcf32018-10-20 00:57:56 +0100984 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
David Howells98bf40c2017-11-02 15:27:53 +0000985 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100986 &call->service_id);
David Howellsd0016482016-08-30 20:42:14 +0100987 if (ret == 0 || ret == -EAGAIN)
988 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700989
David Howells98bf40c2017-11-02 15:27:53 +0000990 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100991 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000992 switch (state) {
993 case AFS_CALL_CL_AWAIT_REPLY:
994 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100995 break;
David Howells98bf40c2017-11-02 15:27:53 +0000996 case AFS_CALL_SV_AWAIT_REQUEST:
997 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100998 break;
David Howells98bf40c2017-11-02 15:27:53 +0000999 case AFS_CALL_COMPLETE:
1000 kdebug("prem complete %d", call->error);
David Howellsf51375c2018-10-20 00:57:57 +01001001 return afs_io_error(call, afs_io_error_extract);
David Howellsd0016482016-08-30 20:42:14 +01001002 default:
1003 break;
1004 }
1005 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -07001006 }
David Howellsd0016482016-08-30 20:42:14 +01001007
David Howells98bf40c2017-11-02 15:27:53 +00001008 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +01001009 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -07001010}
David Howells5f702c82018-04-06 14:17:25 +01001011
1012/*
1013 * Log protocol error production.
1014 */
David Howells160cb952018-10-20 00:57:56 +01001015noinline int afs_protocol_error(struct afs_call *call, int error,
1016 enum afs_eproto_cause cause)
David Howells5f702c82018-04-06 14:17:25 +01001017{
David Howells160cb952018-10-20 00:57:56 +01001018 trace_afs_protocol_error(call, error, cause);
David Howells5f702c82018-04-06 14:17:25 +01001019 return error;
1020}