blob: a7b44863d502e95cbb28a1f7ed2f2a17d7ba1043 [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* Maintain an RxRPC server socket to do AFS communications through
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
14
David Howells08e0e7c2007-04-26 15:55:03 -070015#include <net/sock.h>
16#include <net/af_rxrpc.h>
David Howells08e0e7c2007-04-26 15:55:03 -070017#include "internal.h"
18#include "afs_cm.h"
David Howells35dbfba2018-10-20 00:57:58 +010019#include "protocol_yfs.h"
David Howells08e0e7c2007-04-26 15:55:03 -070020
David Howellsf044c882017-11-02 15:27:45 +000021struct workqueue_struct *afs_async_calls;
David Howells08e0e7c2007-04-26 15:55:03 -070022
David Howellsd0016482016-08-30 20:42:14 +010023static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd2ddc772017-11-02 15:27:50 +000024static long afs_wait_for_call_to_complete(struct afs_call *, struct afs_addr_cursor *);
David Howellsd0016482016-08-30 20:42:14 +010025static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010026static void afs_process_async_call(struct work_struct *);
David Howells00e90712016-09-08 11:10:12 +010027static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
28static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
David Howellsd0016482016-08-30 20:42:14 +010029static int afs_deliver_cm_op_id(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070030
David Howells08e0e7c2007-04-26 15:55:03 -070031/* asynchronous incoming call initial processing */
32static const struct afs_call_type afs_RXCMxxxx = {
David Howells00d3b7a2007-04-26 15:57:07 -070033 .name = "CB.xxxx",
David Howells08e0e7c2007-04-26 15:55:03 -070034 .deliver = afs_deliver_cm_op_id,
David Howells08e0e7c2007-04-26 15:55:03 -070035};
36
David Howells08e0e7c2007-04-26 15:55:03 -070037/*
38 * open an RxRPC socket and bind it to be a server for callback notifications
39 * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
40 */
David Howellsf044c882017-11-02 15:27:45 +000041int afs_open_socket(struct afs_net *net)
David Howells08e0e7c2007-04-26 15:55:03 -070042{
43 struct sockaddr_rxrpc srx;
44 struct socket *socket;
David Howells4776cab2018-05-10 23:10:40 +010045 unsigned int min_level;
David 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
206/*
David Howells3bf0fb62018-10-20 00:57:59 +0100207 * Queue the call for actual work.
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100208 */
David Howells3bf0fb62018-10-20 00:57:59 +0100209static void afs_queue_call_work(struct afs_call *call)
Nathaniel Wesley Filardo6cf12862014-05-21 16:04:11 +0100210{
David Howells3bf0fb62018-10-20 00:57:59 +0100211 if (call->type->work) {
212 int u = atomic_inc_return(&call->usage);
David Howells341f7412017-01-05 10:38:36 +0000213
David Howells3bf0fb62018-10-20 00:57:59 +0100214 trace_afs_call(call, afs_call_trace_work, u,
215 atomic_read(&call->net->nr_outstanding_calls),
216 __builtin_return_address(0));
David Howells341f7412017-01-05 10:38:36 +0000217
David Howells3bf0fb62018-10-20 00:57:59 +0100218 INIT_WORK(&call->work, call->type->work);
David Howells341f7412017-01-05 10:38:36 +0000219
David Howells3bf0fb62018-10-20 00:57:59 +0100220 if (!queue_work(afs_wq, &call->work))
221 afs_put_call(call);
222 }
David Howells6c67c7c2014-05-21 14:48:05 +0100223}
224
225/*
David Howells08e0e7c2007-04-26 15:55:03 -0700226 * allocate a call with flat request and reply buffers
227 */
David Howellsf044c882017-11-02 15:27:45 +0000228struct afs_call *afs_alloc_flat_call(struct afs_net *net,
229 const struct afs_call_type *type,
David Howellsd0016482016-08-30 20:42:14 +0100230 size_t request_size, size_t reply_max)
David Howells08e0e7c2007-04-26 15:55:03 -0700231{
232 struct afs_call *call;
233
David Howellsf044c882017-11-02 15:27:45 +0000234 call = afs_alloc_call(net, type, GFP_NOFS);
David Howells08e0e7c2007-04-26 15:55:03 -0700235 if (!call)
236 goto nomem_call;
237
David Howells00d3b7a2007-04-26 15:57:07 -0700238 if (request_size) {
David Howells341f7412017-01-05 10:38:36 +0000239 call->request_size = request_size;
David Howells00d3b7a2007-04-26 15:57:07 -0700240 call->request = kmalloc(request_size, GFP_NOFS);
241 if (!call->request)
242 goto nomem_free;
243 }
244
David Howellsd0016482016-08-30 20:42:14 +0100245 if (reply_max) {
David Howells341f7412017-01-05 10:38:36 +0000246 call->reply_max = reply_max;
David Howellsd0016482016-08-30 20:42:14 +0100247 call->buffer = kmalloc(reply_max, GFP_NOFS);
David Howells00d3b7a2007-04-26 15:57:07 -0700248 if (!call->buffer)
249 goto nomem_free;
250 }
251
David Howells12bdcf32018-10-20 00:57:56 +0100252 afs_extract_to_buf(call, call->reply_max);
David Howells025db802017-11-02 15:27:51 +0000253 call->operation_ID = type->op;
David Howells08e0e7c2007-04-26 15:55:03 -0700254 init_waitqueue_head(&call->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700255 return call;
256
David Howells00d3b7a2007-04-26 15:57:07 -0700257nomem_free:
David Howells341f7412017-01-05 10:38:36 +0000258 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700259nomem_call:
260 return NULL;
261}
262
263/*
264 * clean up a call with flat buffer
265 */
266void afs_flat_call_destructor(struct afs_call *call)
267{
268 _enter("");
269
270 kfree(call->request);
271 call->request = NULL;
272 kfree(call->buffer);
273 call->buffer = NULL;
274}
275
David Howells2f5705a2017-03-16 16:27:46 +0000276#define AFS_BVEC_MAX 8
277
278/*
279 * Load the given bvec with the next few pages.
280 */
281static void afs_load_bvec(struct afs_call *call, struct msghdr *msg,
282 struct bio_vec *bv, pgoff_t first, pgoff_t last,
283 unsigned offset)
284{
285 struct page *pages[AFS_BVEC_MAX];
286 unsigned int nr, n, i, to, bytes = 0;
287
288 nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX);
289 n = find_get_pages_contig(call->mapping, first, nr, pages);
290 ASSERTCMP(n, ==, nr);
291
292 msg->msg_flags |= MSG_MORE;
293 for (i = 0; i < nr; i++) {
294 to = PAGE_SIZE;
295 if (first + i >= last) {
296 to = call->last_to;
297 msg->msg_flags &= ~MSG_MORE;
298 }
299 bv[i].bv_page = pages[i];
300 bv[i].bv_len = to - offset;
301 bv[i].bv_offset = offset;
302 bytes += to - offset;
303 offset = 0;
304 }
305
David Howellsaa563d72018-10-20 00:57:56 +0100306 iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes);
David Howells2f5705a2017-03-16 16:27:46 +0000307}
308
David Howells08e0e7c2007-04-26 15:55:03 -0700309/*
David Howellse8332512017-08-29 10:18:56 +0100310 * Advance the AFS call state when the RxRPC call ends the transmit phase.
311 */
312static void afs_notify_end_request_tx(struct sock *sock,
313 struct rxrpc_call *rxcall,
314 unsigned long call_user_ID)
315{
316 struct afs_call *call = (struct afs_call *)call_user_ID;
317
David Howells98bf40c2017-11-02 15:27:53 +0000318 afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
David Howellse8332512017-08-29 10:18:56 +0100319}
320
321/*
David Howells31143d52007-05-09 02:33:46 -0700322 * attach the data from a bunch of pages on an inode to a call
323 */
Al Viro39c6ace2016-01-09 20:36:51 -0500324static int afs_send_pages(struct afs_call *call, struct msghdr *msg)
David Howells31143d52007-05-09 02:33:46 -0700325{
David Howells2f5705a2017-03-16 16:27:46 +0000326 struct bio_vec bv[AFS_BVEC_MAX];
327 unsigned int bytes, nr, loop, offset;
David Howells31143d52007-05-09 02:33:46 -0700328 pgoff_t first = call->first, last = call->last;
329 int ret;
330
David Howells31143d52007-05-09 02:33:46 -0700331 offset = call->first_offset;
332 call->first_offset = 0;
333
334 do {
David Howells2f5705a2017-03-16 16:27:46 +0000335 afs_load_bvec(call, msg, bv, first, last, offset);
David Howells2c099012017-11-02 15:27:51 +0000336 trace_afs_send_pages(call, msg, first, last, offset);
337
David Howells2f5705a2017-03-16 16:27:46 +0000338 offset = 0;
339 bytes = msg->msg_iter.count;
340 nr = msg->msg_iter.nr_segs;
David Howells31143d52007-05-09 02:33:46 -0700341
David Howellsf044c882017-11-02 15:27:45 +0000342 ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg,
David Howellse8332512017-08-29 10:18:56 +0100343 bytes, afs_notify_end_request_tx);
David Howells2f5705a2017-03-16 16:27:46 +0000344 for (loop = 0; loop < nr; loop++)
345 put_page(bv[loop].bv_page);
David Howells31143d52007-05-09 02:33:46 -0700346 if (ret < 0)
347 break;
David Howells2f5705a2017-03-16 16:27:46 +0000348
349 first += nr;
David Howells5bbf5d32007-05-10 03:15:23 -0700350 } while (first <= last);
David Howells31143d52007-05-09 02:33:46 -0700351
David Howells2c099012017-11-02 15:27:51 +0000352 trace_afs_sent_pages(call, call->first, last, first, ret);
David Howells31143d52007-05-09 02:33:46 -0700353 return ret;
354}
355
356/*
David Howells08e0e7c2007-04-26 15:55:03 -0700357 * initiate a call
358 */
David Howells8b2a4642017-11-02 15:27:50 +0000359long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call,
David Howells33cd7f22017-11-02 15:27:48 +0000360 gfp_t gfp, bool async)
David Howells08e0e7c2007-04-26 15:55:03 -0700361{
David Howells2feeaf82018-10-20 00:57:59 +0100362 struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
David Howells08e0e7c2007-04-26 15:55:03 -0700363 struct rxrpc_call *rxcall;
364 struct msghdr msg;
365 struct kvec iov[1];
David Howellse754eba2017-06-07 12:40:03 +0100366 s64 tx_total_len;
David Howells08e0e7c2007-04-26 15:55:03 -0700367 int ret;
368
David Howells4d9df982017-11-02 15:27:47 +0000369 _enter(",{%pISp},", &srx->transport);
David Howells08e0e7c2007-04-26 15:55:03 -0700370
David Howells00d3b7a2007-04-26 15:57:07 -0700371 ASSERT(call->type != NULL);
372 ASSERT(call->type->name != NULL);
373
David Howells31143d52007-05-09 02:33:46 -0700374 _debug("____MAKE %p{%s,%x} [%d]____",
375 call, call->type->name, key_serial(call->key),
David Howellsf044c882017-11-02 15:27:45 +0000376 atomic_read(&call->net->nr_outstanding_calls));
David Howells00d3b7a2007-04-26 15:57:07 -0700377
David Howells56ff9c82017-01-05 10:38:36 +0000378 call->async = async;
David Howells3bf0fb62018-10-20 00:57:59 +0100379 call->addr_ix = ac->index;
380 call->alist = afs_get_addrlist(ac->alist);
David Howells08e0e7c2007-04-26 15:55:03 -0700381
David Howellse754eba2017-06-07 12:40:03 +0100382 /* Work out the length we're going to transmit. This is awkward for
383 * calls such as FS.StoreData where there's an extra injection of data
384 * after the initial fixed part.
385 */
386 tx_total_len = call->request_size;
387 if (call->send_pages) {
David Howells1199db62017-11-02 15:27:51 +0000388 if (call->last == call->first) {
389 tx_total_len += call->last_to - call->first_offset;
390 } else {
391 /* It looks mathematically like you should be able to
392 * combine the following lines with the ones above, but
393 * unsigned arithmetic is fun when it wraps...
394 */
395 tx_total_len += PAGE_SIZE - call->first_offset;
396 tx_total_len += call->last_to;
397 tx_total_len += (call->last - call->first - 1) * PAGE_SIZE;
398 }
David Howellse754eba2017-06-07 12:40:03 +0100399 }
400
David Howells08e0e7c2007-04-26 15:55:03 -0700401 /* create a call */
David Howells4d9df982017-11-02 15:27:47 +0000402 rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
David Howellse754eba2017-06-07 12:40:03 +0100403 (unsigned long)call,
404 tx_total_len, gfp,
David Howells56ff9c82017-01-05 10:38:36 +0000405 (async ?
406 afs_wake_up_async_call :
David Howellsa68f4a22017-10-18 11:36:39 +0100407 afs_wake_up_call_waiter),
David Howellsa25e21f2018-03-27 23:03:00 +0100408 call->upgrade,
409 call->debug_id);
David Howells08e0e7c2007-04-26 15:55:03 -0700410 if (IS_ERR(rxcall)) {
411 ret = PTR_ERR(rxcall);
David Howells3bf0fb62018-10-20 00:57:59 +0100412 call->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700413 goto error_kill_call;
414 }
415
416 call->rxcall = rxcall;
417
418 /* send the request */
419 iov[0].iov_base = call->request;
420 iov[0].iov_len = call->request_size;
421
422 msg.msg_name = NULL;
423 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100424 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
David Howells08e0e7c2007-04-26 15:55:03 -0700425 msg.msg_control = NULL;
426 msg.msg_controllen = 0;
David Howellsbc5e3a52017-10-18 11:07:31 +0100427 msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700428
David Howellsf044c882017-11-02 15:27:45 +0000429 ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
David Howellse8332512017-08-29 10:18:56 +0100430 &msg, call->request_size,
431 afs_notify_end_request_tx);
David Howells08e0e7c2007-04-26 15:55:03 -0700432 if (ret < 0)
433 goto error_do_abort;
434
David Howells31143d52007-05-09 02:33:46 -0700435 if (call->send_pages) {
Al Viro39c6ace2016-01-09 20:36:51 -0500436 ret = afs_send_pages(call, &msg);
David Howells31143d52007-05-09 02:33:46 -0700437 if (ret < 0)
438 goto error_do_abort;
439 }
440
David Howells08e0e7c2007-04-26 15:55:03 -0700441 /* at this point, an async call may no longer exist as it may have
442 * already completed */
David Howells56ff9c82017-01-05 10:38:36 +0000443 if (call->async)
444 return -EINPROGRESS;
445
David Howellsd2ddc772017-11-02 15:27:50 +0000446 return afs_wait_for_call_to_complete(call, ac);
David Howells08e0e7c2007-04-26 15:55:03 -0700447
448error_do_abort:
David Howells70af0e32017-03-16 16:27:47 +0000449 call->state = AFS_CALL_COMPLETE;
450 if (ret != -ECONNABORTED) {
David Howellsf044c882017-11-02 15:27:45 +0000451 rxrpc_kernel_abort_call(call->net->socket, rxcall,
452 RX_USER_ABORT, ret, "KSD");
David Howells70af0e32017-03-16 16:27:47 +0000453 } else {
David Howellsaa563d72018-10-20 00:57:56 +0100454 iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
David Howellseb9950e2018-08-03 17:06:56 +0100455 rxrpc_kernel_recv_data(call->net->socket, rxcall,
456 &msg.msg_iter, false,
457 &call->abort_code, &call->service_id);
David Howellsd2ddc772017-11-02 15:27:50 +0000458 ac->abort_code = call->abort_code;
459 ac->responded = true;
David Howells70af0e32017-03-16 16:27:47 +0000460 }
David Howells025db802017-11-02 15:27:51 +0000461 call->error = ret;
462 trace_afs_call_done(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700463error_kill_call:
David Howells3bf0fb62018-10-20 00:57:59 +0100464 if (call->type->done)
465 call->type->done(call);
David Howells341f7412017-01-05 10:38:36 +0000466 afs_put_call(call);
David Howellsd2ddc772017-11-02 15:27:50 +0000467 ac->error = ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700468 _leave(" = %d", ret);
469 return ret;
470}
471
472/*
David Howells08e0e7c2007-04-26 15:55:03 -0700473 * deliver messages to a call
474 */
475static void afs_deliver_to_call(struct afs_call *call)
476{
David Howells98bf40c2017-11-02 15:27:53 +0000477 enum afs_call_state state;
478 u32 abort_code, remote_abort = 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700479 int ret;
480
David Howellsd0016482016-08-30 20:42:14 +0100481 _enter("%s", call->type->name);
David Howells08e0e7c2007-04-26 15:55:03 -0700482
David Howells98bf40c2017-11-02 15:27:53 +0000483 while (state = READ_ONCE(call->state),
484 state == AFS_CALL_CL_AWAIT_REPLY ||
485 state == AFS_CALL_SV_AWAIT_OP_ID ||
486 state == AFS_CALL_SV_AWAIT_REQUEST ||
487 state == AFS_CALL_SV_AWAIT_ACK
David Howellsd0016482016-08-30 20:42:14 +0100488 ) {
David Howells98bf40c2017-11-02 15:27:53 +0000489 if (state == AFS_CALL_SV_AWAIT_ACK) {
David Howells12bdcf32018-10-20 00:57:56 +0100490 iov_iter_kvec(&call->iter, READ, NULL, 0, 0);
David Howellsf044c882017-11-02 15:27:45 +0000491 ret = rxrpc_kernel_recv_data(call->net->socket,
David Howells12bdcf32018-10-20 00:57:56 +0100492 call->rxcall, &call->iter,
493 false, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100494 &call->service_id);
David Howells12bdcf32018-10-20 00:57:56 +0100495 trace_afs_receive_data(call, &call->iter, false, ret);
David Howells8e8d7f12017-01-05 10:38:34 +0000496
David Howellsd0016482016-08-30 20:42:14 +0100497 if (ret == -EINPROGRESS || ret == -EAGAIN)
498 return;
David Howells98bf40c2017-11-02 15:27:53 +0000499 if (ret < 0 || ret == 1) {
500 if (ret == 1)
501 ret = 0;
David Howells025db802017-11-02 15:27:51 +0000502 goto call_complete;
David Howells98bf40c2017-11-02 15:27:53 +0000503 }
David Howellsd0016482016-08-30 20:42:14 +0100504 return;
David Howells08e0e7c2007-04-26 15:55:03 -0700505 }
506
David Howells12d8e952018-10-20 00:57:58 +0100507 if (call->want_reply_time &&
508 rxrpc_kernel_get_reply_time(call->net->socket,
509 call->rxcall,
510 &call->reply_time))
511 call->want_reply_time = false;
512
David Howellsd0016482016-08-30 20:42:14 +0100513 ret = call->type->deliver(call);
David Howells98bf40c2017-11-02 15:27:53 +0000514 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100515 switch (ret) {
516 case 0:
David Howells3bf0fb62018-10-20 00:57:59 +0100517 afs_queue_call_work(call);
David Howellsf2686b02018-05-10 14:12:50 +0100518 if (state == AFS_CALL_CL_PROC_REPLY) {
519 if (call->cbi)
520 set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
521 &call->cbi->server->flags);
David Howells025db802017-11-02 15:27:51 +0000522 goto call_complete;
David Howellsf2686b02018-05-10 14:12:50 +0100523 }
David Howells98bf40c2017-11-02 15:27:53 +0000524 ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100525 goto done;
526 case -EINPROGRESS:
527 case -EAGAIN:
528 goto out;
David Howells70af0e32017-03-16 16:27:47 +0000529 case -ECONNABORTED:
David Howells98bf40c2017-11-02 15:27:53 +0000530 ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
531 goto done;
David Howellsd0016482016-08-30 20:42:14 +0100532 case -ENOTSUPP:
David Howells1157f152017-03-16 16:27:47 +0000533 abort_code = RXGEN_OPCODE;
David Howellsf044c882017-11-02 15:27:45 +0000534 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100535 abort_code, ret, "KIV");
David Howells98bf40c2017-11-02 15:27:53 +0000536 goto local_abort;
David Howells4ac15ea2018-10-20 00:57:57 +0100537 case -EIO:
538 pr_err("kAFS: Call %u in bad state %u\n",
539 call->debug_id, state);
540 /* Fall through */
David Howellsd0016482016-08-30 20:42:14 +0100541 case -ENODATA:
542 case -EBADMSG:
543 case -EMSGSIZE:
544 default:
545 abort_code = RXGEN_CC_UNMARSHAL;
David Howells98bf40c2017-11-02 15:27:53 +0000546 if (state != AFS_CALL_CL_AWAIT_REPLY)
David Howellsd0016482016-08-30 20:42:14 +0100547 abort_code = RXGEN_SS_UNMARSHAL;
David Howellsf044c882017-11-02 15:27:45 +0000548 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells12bdcf32018-10-20 00:57:56 +0100549 abort_code, ret, "KUM");
David Howells98bf40c2017-11-02 15:27:53 +0000550 goto local_abort;
David Howellsd0016482016-08-30 20:42:14 +0100551 }
David Howells08e0e7c2007-04-26 15:55:03 -0700552 }
553
David Howellsd0016482016-08-30 20:42:14 +0100554done:
David Howells3bf0fb62018-10-20 00:57:59 +0100555 if (call->type->done)
556 call->type->done(call);
David Howells98bf40c2017-11-02 15:27:53 +0000557 if (state == AFS_CALL_COMPLETE && call->incoming)
David Howells341f7412017-01-05 10:38:36 +0000558 afs_put_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100559out:
David Howells08e0e7c2007-04-26 15:55:03 -0700560 _leave("");
David Howellsd0016482016-08-30 20:42:14 +0100561 return;
562
David Howells98bf40c2017-11-02 15:27:53 +0000563local_abort:
564 abort_code = 0;
David Howells025db802017-11-02 15:27:51 +0000565call_complete:
David Howells98bf40c2017-11-02 15:27:53 +0000566 afs_set_call_complete(call, ret, remote_abort);
567 state = AFS_CALL_COMPLETE;
David Howellsd0016482016-08-30 20:42:14 +0100568 goto done;
David Howells08e0e7c2007-04-26 15:55:03 -0700569}
570
571/*
572 * wait synchronously for a call to complete
573 */
David Howellsd2ddc772017-11-02 15:27:50 +0000574static long afs_wait_for_call_to_complete(struct afs_call *call,
575 struct afs_addr_cursor *ac)
David Howells08e0e7c2007-04-26 15:55:03 -0700576{
David Howellsbc5e3a52017-10-18 11:07:31 +0100577 signed long rtt2, timeout;
David Howells33cd7f22017-11-02 15:27:48 +0000578 long ret;
David Howells7150cea2018-11-12 22:33:22 +0000579 bool stalled = false;
David Howellsbc5e3a52017-10-18 11:07:31 +0100580 u64 rtt;
581 u32 life, last_life;
David Howells08e0e7c2007-04-26 15:55:03 -0700582
583 DECLARE_WAITQUEUE(myself, current);
584
585 _enter("");
586
David Howellsf044c882017-11-02 15:27:45 +0000587 rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100588 rtt2 = nsecs_to_jiffies64(rtt) * 2;
589 if (rtt2 < 2)
590 rtt2 = 2;
591
592 timeout = rtt2;
David Howellsf044c882017-11-02 15:27:45 +0000593 last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100594
David Howells08e0e7c2007-04-26 15:55:03 -0700595 add_wait_queue(&call->waitq, &myself);
596 for (;;) {
David Howellsbc5e3a52017-10-18 11:07:31 +0100597 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700598
599 /* deliver any messages that are in the queue */
David Howells98bf40c2017-11-02 15:27:53 +0000600 if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
601 call->need_attention) {
David Howellsd0016482016-08-30 20:42:14 +0100602 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700603 __set_current_state(TASK_RUNNING);
604 afs_deliver_to_call(call);
605 continue;
606 }
607
David Howells98bf40c2017-11-02 15:27:53 +0000608 if (afs_check_call_state(call, AFS_CALL_COMPLETE))
David Howells08e0e7c2007-04-26 15:55:03 -0700609 break;
David Howellsbc5e3a52017-10-18 11:07:31 +0100610
David Howellsf044c882017-11-02 15:27:45 +0000611 life = rxrpc_kernel_check_life(call->net->socket, call->rxcall);
David Howellsbc5e3a52017-10-18 11:07:31 +0100612 if (timeout == 0 &&
David Howells7150cea2018-11-12 22:33:22 +0000613 life == last_life && signal_pending(current)) {
614 if (stalled)
David Howellsbc5e3a52017-10-18 11:07:31 +0100615 break;
David Howells7150cea2018-11-12 22:33:22 +0000616 __set_current_state(TASK_RUNNING);
617 rxrpc_kernel_probe_life(call->net->socket, call->rxcall);
618 timeout = rtt2;
619 stalled = true;
620 continue;
621 }
David Howellsbc5e3a52017-10-18 11:07:31 +0100622
623 if (life != last_life) {
624 timeout = rtt2;
625 last_life = life;
David Howells7150cea2018-11-12 22:33:22 +0000626 stalled = false;
David Howellsbc5e3a52017-10-18 11:07:31 +0100627 }
628
629 timeout = schedule_timeout(timeout);
David Howells08e0e7c2007-04-26 15:55:03 -0700630 }
631
632 remove_wait_queue(&call->waitq, &myself);
633 __set_current_state(TASK_RUNNING);
634
David Howells954cd6d2017-03-16 16:27:49 +0000635 /* Kill off the call if it's still live. */
David Howells98bf40c2017-11-02 15:27:53 +0000636 if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
David Howells954cd6d2017-03-16 16:27:49 +0000637 _debug("call interrupted");
David Howellsd2ddc772017-11-02 15:27:50 +0000638 if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
David Howells98bf40c2017-11-02 15:27:53 +0000639 RX_USER_ABORT, -EINTR, "KWI"))
640 afs_set_call_complete(call, -EINTR, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700641 }
642
David Howells98bf40c2017-11-02 15:27:53 +0000643 spin_lock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000644 ac->abort_code = call->abort_code;
645 ac->error = call->error;
David Howells98bf40c2017-11-02 15:27:53 +0000646 spin_unlock_bh(&call->state_lock);
David Howellsd2ddc772017-11-02 15:27:50 +0000647
648 ret = ac->error;
649 switch (ret) {
650 case 0:
651 if (call->ret_reply0) {
652 ret = (long)call->reply[0];
653 call->reply[0] = NULL;
654 }
655 /* Fall through */
656 case -ECONNABORTED:
657 ac->responded = true;
658 break;
David Howells33cd7f22017-11-02 15:27:48 +0000659 }
660
David Howells08e0e7c2007-04-26 15:55:03 -0700661 _debug("call complete");
David Howells341f7412017-01-05 10:38:36 +0000662 afs_put_call(call);
David Howells33cd7f22017-11-02 15:27:48 +0000663 _leave(" = %p", (void *)ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700664 return ret;
665}
666
667/*
668 * wake up a waiting call
669 */
David Howellsd0016482016-08-30 20:42:14 +0100670static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
671 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700672{
David Howellsd0016482016-08-30 20:42:14 +0100673 struct afs_call *call = (struct afs_call *)call_user_ID;
674
675 call->need_attention = true;
David Howells08e0e7c2007-04-26 15:55:03 -0700676 wake_up(&call->waitq);
677}
678
679/*
680 * wake up an asynchronous call
681 */
David Howellsd0016482016-08-30 20:42:14 +0100682static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
683 unsigned long call_user_ID)
David Howells08e0e7c2007-04-26 15:55:03 -0700684{
David Howellsd0016482016-08-30 20:42:14 +0100685 struct afs_call *call = (struct afs_call *)call_user_ID;
David Howells341f7412017-01-05 10:38:36 +0000686 int u;
David Howellsd0016482016-08-30 20:42:14 +0100687
David Howells8e8d7f12017-01-05 10:38:34 +0000688 trace_afs_notify_call(rxcall, call);
David Howellsd0016482016-08-30 20:42:14 +0100689 call->need_attention = true;
David Howells341f7412017-01-05 10:38:36 +0000690
Mark Rutlandbfc18e32018-06-21 13:13:04 +0100691 u = atomic_fetch_add_unless(&call->usage, 1, 0);
David Howells341f7412017-01-05 10:38:36 +0000692 if (u != 0) {
693 trace_afs_call(call, afs_call_trace_wake, u,
David Howellsf044c882017-11-02 15:27:45 +0000694 atomic_read(&call->net->nr_outstanding_calls),
David Howells341f7412017-01-05 10:38:36 +0000695 __builtin_return_address(0));
696
697 if (!queue_work(afs_async_calls, &call->async_work))
698 afs_put_call(call);
699 }
David Howells08e0e7c2007-04-26 15:55:03 -0700700}
701
702/*
David Howells341f7412017-01-05 10:38:36 +0000703 * Delete an asynchronous call. The work item carries a ref to the call struct
704 * that we need to release.
David Howells08e0e7c2007-04-26 15:55:03 -0700705 */
David Howellsd0016482016-08-30 20:42:14 +0100706static void afs_delete_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700707{
David Howellsd0016482016-08-30 20:42:14 +0100708 struct afs_call *call = container_of(work, struct afs_call, async_work);
709
David Howells08e0e7c2007-04-26 15:55:03 -0700710 _enter("");
711
David Howells341f7412017-01-05 10:38:36 +0000712 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700713
714 _leave("");
715}
716
717/*
David Howells341f7412017-01-05 10:38:36 +0000718 * Perform I/O processing on an asynchronous call. The work item carries a ref
719 * to the call struct that we either need to release or to pass on.
David Howells08e0e7c2007-04-26 15:55:03 -0700720 */
David Howellsd0016482016-08-30 20:42:14 +0100721static void afs_process_async_call(struct work_struct *work)
David Howells08e0e7c2007-04-26 15:55:03 -0700722{
David Howellsd0016482016-08-30 20:42:14 +0100723 struct afs_call *call = container_of(work, struct afs_call, async_work);
724
David Howells08e0e7c2007-04-26 15:55:03 -0700725 _enter("");
726
David Howellsd0016482016-08-30 20:42:14 +0100727 if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
728 call->need_attention = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700729 afs_deliver_to_call(call);
David Howellsd0016482016-08-30 20:42:14 +0100730 }
David Howells08e0e7c2007-04-26 15:55:03 -0700731
David Howells56ff9c82017-01-05 10:38:36 +0000732 if (call->state == AFS_CALL_COMPLETE) {
David Howells341f7412017-01-05 10:38:36 +0000733 /* We have two refs to release - one from the alloc and one
734 * queued with the work item - and we can't just deallocate the
735 * call because the work item may be queued again.
736 */
David Howellsd0016482016-08-30 20:42:14 +0100737 call->async_work.func = afs_delete_async_call;
David Howells341f7412017-01-05 10:38:36 +0000738 if (!queue_work(afs_async_calls, &call->async_work))
739 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700740 }
741
David Howells341f7412017-01-05 10:38:36 +0000742 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700743 _leave("");
744}
745
David Howells00e90712016-09-08 11:10:12 +0100746static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
747{
748 struct afs_call *call = (struct afs_call *)user_call_ID;
749
750 call->rxcall = rxcall;
751}
752
753/*
754 * Charge the incoming call preallocation.
755 */
David Howellsf044c882017-11-02 15:27:45 +0000756void afs_charge_preallocation(struct work_struct *work)
David Howells00e90712016-09-08 11:10:12 +0100757{
David Howellsf044c882017-11-02 15:27:45 +0000758 struct afs_net *net =
759 container_of(work, struct afs_net, charge_preallocation_work);
760 struct afs_call *call = net->spare_incoming_call;
David Howells00e90712016-09-08 11:10:12 +0100761
762 for (;;) {
763 if (!call) {
David Howellsf044c882017-11-02 15:27:45 +0000764 call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
David Howells00e90712016-09-08 11:10:12 +0100765 if (!call)
766 break;
767
David Howells56ff9c82017-01-05 10:38:36 +0000768 call->async = true;
David Howells98bf40c2017-11-02 15:27:53 +0000769 call->state = AFS_CALL_SV_AWAIT_OP_ID;
David Howells56ff9c82017-01-05 10:38:36 +0000770 init_waitqueue_head(&call->waitq);
David Howells12bdcf32018-10-20 00:57:56 +0100771 afs_extract_to_tmp(call);
David Howells00e90712016-09-08 11:10:12 +0100772 }
773
David Howellsf044c882017-11-02 15:27:45 +0000774 if (rxrpc_kernel_charge_accept(net->socket,
David Howells00e90712016-09-08 11:10:12 +0100775 afs_wake_up_async_call,
776 afs_rx_attach,
777 (unsigned long)call,
David Howellsa25e21f2018-03-27 23:03:00 +0100778 GFP_KERNEL,
779 call->debug_id) < 0)
David Howells00e90712016-09-08 11:10:12 +0100780 break;
781 call = NULL;
782 }
David Howellsf044c882017-11-02 15:27:45 +0000783 net->spare_incoming_call = call;
David Howells00e90712016-09-08 11:10:12 +0100784}
785
786/*
787 * Discard a preallocated call when a socket is shut down.
788 */
789static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
790 unsigned long user_call_ID)
791{
792 struct afs_call *call = (struct afs_call *)user_call_ID;
793
David Howells00e90712016-09-08 11:10:12 +0100794 call->rxcall = NULL;
David Howells341f7412017-01-05 10:38:36 +0000795 afs_put_call(call);
David Howells00e90712016-09-08 11:10:12 +0100796}
797
David Howells08e0e7c2007-04-26 15:55:03 -0700798/*
David Howellsd0016482016-08-30 20:42:14 +0100799 * Notification of an incoming call.
800 */
David Howells00e90712016-09-08 11:10:12 +0100801static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
802 unsigned long user_call_ID)
David Howellsd0016482016-08-30 20:42:14 +0100803{
David Howellsf044c882017-11-02 15:27:45 +0000804 struct afs_net *net = afs_sock2net(sk);
805
806 queue_work(afs_wq, &net->charge_preallocation_work);
David Howellsd0016482016-08-30 20:42:14 +0100807}
808
809/*
David Howells372ee162016-08-03 14:11:40 +0100810 * Grab the operation ID from an incoming cache manager call. The socket
811 * buffer is discarded on error or if we don't yet have sufficient data.
David Howells08e0e7c2007-04-26 15:55:03 -0700812 */
David Howellsd0016482016-08-30 20:42:14 +0100813static int afs_deliver_cm_op_id(struct afs_call *call)
David Howells08e0e7c2007-04-26 15:55:03 -0700814{
David Howellsd0016482016-08-30 20:42:14 +0100815 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700816
David Howells12bdcf32018-10-20 00:57:56 +0100817 _enter("{%zu}", iov_iter_count(call->_iter));
David Howells08e0e7c2007-04-26 15:55:03 -0700818
819 /* the operation ID forms the first four bytes of the request data */
David Howells12bdcf32018-10-20 00:57:56 +0100820 ret = afs_extract_data(call, true);
David Howellsd0016482016-08-30 20:42:14 +0100821 if (ret < 0)
822 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700823
David Howells50a2c952016-10-13 08:27:10 +0100824 call->operation_ID = ntohl(call->tmp);
David Howells98bf40c2017-11-02 15:27:53 +0000825 afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
David Howells08e0e7c2007-04-26 15:55:03 -0700826
827 /* ask the cache manager to route the call (it'll change the call type
828 * if successful) */
829 if (!afs_cm_incoming_call(call))
830 return -ENOTSUPP;
831
David Howells8e8d7f12017-01-05 10:38:34 +0000832 trace_afs_cb_call(call);
833
David Howells08e0e7c2007-04-26 15:55:03 -0700834 /* pass responsibility for the remainer of this message off to the
835 * cache manager op */
David Howellsd0016482016-08-30 20:42:14 +0100836 return call->type->deliver(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700837}
838
839/*
David Howellse8332512017-08-29 10:18:56 +0100840 * Advance the AFS call state when an RxRPC service call ends the transmit
841 * phase.
842 */
843static void afs_notify_end_reply_tx(struct sock *sock,
844 struct rxrpc_call *rxcall,
845 unsigned long call_user_ID)
846{
847 struct afs_call *call = (struct afs_call *)call_user_ID;
848
David Howells98bf40c2017-11-02 15:27:53 +0000849 afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
David Howellse8332512017-08-29 10:18:56 +0100850}
851
852/*
David Howells08e0e7c2007-04-26 15:55:03 -0700853 * send an empty reply
854 */
855void afs_send_empty_reply(struct afs_call *call)
856{
David Howellsf044c882017-11-02 15:27:45 +0000857 struct afs_net *net = call->net;
David Howells08e0e7c2007-04-26 15:55:03 -0700858 struct msghdr msg;
David Howells08e0e7c2007-04-26 15:55:03 -0700859
860 _enter("");
861
David Howellsf044c882017-11-02 15:27:45 +0000862 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
David Howellse754eba2017-06-07 12:40:03 +0100863
David Howells08e0e7c2007-04-26 15:55:03 -0700864 msg.msg_name = NULL;
865 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100866 iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700867 msg.msg_control = NULL;
868 msg.msg_controllen = 0;
869 msg.msg_flags = 0;
870
David Howellsf044c882017-11-02 15:27:45 +0000871 switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
David Howellse8332512017-08-29 10:18:56 +0100872 afs_notify_end_reply_tx)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700873 case 0:
874 _leave(" [replied]");
875 return;
876
877 case -ENOMEM:
878 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000879 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100880 RX_USER_ABORT, -ENOMEM, "KOO");
David Howells08e0e7c2007-04-26 15:55:03 -0700881 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700882 _leave(" [error]");
883 return;
884 }
885}
886
887/*
David Howellsb908fe62007-04-26 15:58:17 -0700888 * send a simple reply
889 */
890void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
891{
David Howellsf044c882017-11-02 15:27:45 +0000892 struct afs_net *net = call->net;
David Howellsb908fe62007-04-26 15:58:17 -0700893 struct msghdr msg;
Al Viro2e90b1c2014-11-27 21:50:31 -0500894 struct kvec iov[1];
David Howellsbd6dc742007-07-20 10:59:41 +0100895 int n;
David Howellsb908fe62007-04-26 15:58:17 -0700896
897 _enter("");
898
David Howellsf044c882017-11-02 15:27:45 +0000899 rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
David Howellse754eba2017-06-07 12:40:03 +0100900
David Howellsb908fe62007-04-26 15:58:17 -0700901 iov[0].iov_base = (void *) buf;
902 iov[0].iov_len = len;
903 msg.msg_name = NULL;
904 msg.msg_namelen = 0;
David Howellsaa563d72018-10-20 00:57:56 +0100905 iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
David Howellsb908fe62007-04-26 15:58:17 -0700906 msg.msg_control = NULL;
907 msg.msg_controllen = 0;
908 msg.msg_flags = 0;
909
David Howellsf044c882017-11-02 15:27:45 +0000910 n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
David Howellse8332512017-08-29 10:18:56 +0100911 afs_notify_end_reply_tx);
David Howellsbd6dc742007-07-20 10:59:41 +0100912 if (n >= 0) {
David Howells6c67c7c2014-05-21 14:48:05 +0100913 /* Success */
David Howellsb908fe62007-04-26 15:58:17 -0700914 _leave(" [replied]");
915 return;
David Howellsbd6dc742007-07-20 10:59:41 +0100916 }
David Howells6c67c7c2014-05-21 14:48:05 +0100917
David Howellsbd6dc742007-07-20 10:59:41 +0100918 if (n == -ENOMEM) {
David Howellsb908fe62007-04-26 15:58:17 -0700919 _debug("oom");
David Howellsf044c882017-11-02 15:27:45 +0000920 rxrpc_kernel_abort_call(net->socket, call->rxcall,
David Howells3a927892017-04-06 10:11:56 +0100921 RX_USER_ABORT, -ENOMEM, "KOO");
David Howellsb908fe62007-04-26 15:58:17 -0700922 }
David Howellsbd6dc742007-07-20 10:59:41 +0100923 _leave(" [error]");
David Howellsb908fe62007-04-26 15:58:17 -0700924}
925
926/*
David Howells372ee162016-08-03 14:11:40 +0100927 * Extract a piece of data from the received data socket buffers.
David Howells08e0e7c2007-04-26 15:55:03 -0700928 */
David Howells12bdcf32018-10-20 00:57:56 +0100929int afs_extract_data(struct afs_call *call, bool want_more)
David Howells08e0e7c2007-04-26 15:55:03 -0700930{
David Howellsf044c882017-11-02 15:27:45 +0000931 struct afs_net *net = call->net;
David Howells12bdcf32018-10-20 00:57:56 +0100932 struct iov_iter *iter = call->_iter;
David Howells98bf40c2017-11-02 15:27:53 +0000933 enum afs_call_state state;
Dan Carpenter7888da92018-01-02 10:02:19 +0000934 u32 remote_abort = 0;
David Howellsd0016482016-08-30 20:42:14 +0100935 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700936
David Howells12bdcf32018-10-20 00:57:56 +0100937 _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more);
David Howells08e0e7c2007-04-26 15:55:03 -0700938
David Howells12bdcf32018-10-20 00:57:56 +0100939 ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
David Howells98bf40c2017-11-02 15:27:53 +0000940 want_more, &remote_abort,
David Howellsa68f4a22017-10-18 11:36:39 +0100941 &call->service_id);
David Howellsd0016482016-08-30 20:42:14 +0100942 if (ret == 0 || ret == -EAGAIN)
943 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700944
David Howells98bf40c2017-11-02 15:27:53 +0000945 state = READ_ONCE(call->state);
David Howellsd0016482016-08-30 20:42:14 +0100946 if (ret == 1) {
David Howells98bf40c2017-11-02 15:27:53 +0000947 switch (state) {
948 case AFS_CALL_CL_AWAIT_REPLY:
949 afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
David Howellsd0016482016-08-30 20:42:14 +0100950 break;
David Howells98bf40c2017-11-02 15:27:53 +0000951 case AFS_CALL_SV_AWAIT_REQUEST:
952 afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
David Howellsd0016482016-08-30 20:42:14 +0100953 break;
David Howells98bf40c2017-11-02 15:27:53 +0000954 case AFS_CALL_COMPLETE:
955 kdebug("prem complete %d", call->error);
David Howellsf51375c2018-10-20 00:57:57 +0100956 return afs_io_error(call, afs_io_error_extract);
David Howellsd0016482016-08-30 20:42:14 +0100957 default:
958 break;
959 }
960 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -0700961 }
David Howellsd0016482016-08-30 20:42:14 +0100962
David Howells98bf40c2017-11-02 15:27:53 +0000963 afs_set_call_complete(call, ret, remote_abort);
David Howellsd0016482016-08-30 20:42:14 +0100964 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700965}
David Howells5f702c82018-04-06 14:17:25 +0100966
967/*
968 * Log protocol error production.
969 */
David Howells160cb952018-10-20 00:57:56 +0100970noinline int afs_protocol_error(struct afs_call *call, int error,
971 enum afs_eproto_cause cause)
David Howells5f702c82018-04-06 14:17:25 +0100972{
David Howells160cb952018-10-20 00:57:56 +0100973 trace_afs_protocol_error(call, error, cause);
David Howells5f702c82018-04-06 14:17:25 +0100974 return error;
975}