]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
08e0e7c8 DH |
2 | /* Maintain an RxRPC server socket to do AFS communications through |
3 | * | |
4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells (dhowells@redhat.com) | |
08e0e7c8 DH |
6 | */ |
7 | ||
5a0e3ad6 | 8 | #include <linux/slab.h> |
174cd4b1 IM |
9 | #include <linux/sched/signal.h> |
10 | ||
08e0e7c8 DH |
11 | #include <net/sock.h> |
12 | #include <net/af_rxrpc.h> | |
08e0e7c8 DH |
13 | #include "internal.h" |
14 | #include "afs_cm.h" | |
35dbfba3 | 15 | #include "protocol_yfs.h" |
08e0e7c8 | 16 | |
f044c884 | 17 | struct workqueue_struct *afs_async_calls; |
08e0e7c8 | 18 | |
d001648e | 19 | static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long); |
d001648e | 20 | static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long); |
d001648e | 21 | static void afs_process_async_call(struct work_struct *); |
00e90712 DH |
22 | static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long); |
23 | static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long); | |
d001648e | 24 | static int afs_deliver_cm_op_id(struct afs_call *); |
08e0e7c8 | 25 | |
08e0e7c8 DH |
26 | /* asynchronous incoming call initial processing */ |
27 | static const struct afs_call_type afs_RXCMxxxx = { | |
00d3b7a4 | 28 | .name = "CB.xxxx", |
08e0e7c8 | 29 | .deliver = afs_deliver_cm_op_id, |
08e0e7c8 DH |
30 | }; |
31 | ||
08e0e7c8 DH |
32 | /* |
33 | * open an RxRPC socket and bind it to be a server for callback notifications | |
34 | * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT | |
35 | */ | |
f044c884 | 36 | int afs_open_socket(struct afs_net *net) |
08e0e7c8 DH |
37 | { |
38 | struct sockaddr_rxrpc srx; | |
39 | struct socket *socket; | |
4776cab4 | 40 | unsigned int min_level; |
08e0e7c8 DH |
41 | int ret; |
42 | ||
43 | _enter(""); | |
44 | ||
5b86d4ff | 45 | ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket); |
0e119b41 DH |
46 | if (ret < 0) |
47 | goto error_1; | |
08e0e7c8 DH |
48 | |
49 | socket->sk->sk_allocation = GFP_NOFS; | |
50 | ||
51 | /* bind the callback manager's address to make this a server socket */ | |
3838d3ec | 52 | memset(&srx, 0, sizeof(srx)); |
08e0e7c8 DH |
53 | srx.srx_family = AF_RXRPC; |
54 | srx.srx_service = CM_SERVICE; | |
55 | srx.transport_type = SOCK_DGRAM; | |
3838d3ec DH |
56 | srx.transport_len = sizeof(srx.transport.sin6); |
57 | srx.transport.sin6.sin6_family = AF_INET6; | |
58 | srx.transport.sin6.sin6_port = htons(AFS_CM_PORT); | |
08e0e7c8 | 59 | |
4776cab4 DH |
60 | min_level = RXRPC_SECURITY_ENCRYPT; |
61 | ret = kernel_setsockopt(socket, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL, | |
62 | (void *)&min_level, sizeof(min_level)); | |
63 | if (ret < 0) | |
64 | goto error_2; | |
65 | ||
08e0e7c8 | 66 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); |
83732ec5 MD |
67 | if (ret == -EADDRINUSE) { |
68 | srx.transport.sin6.sin6_port = 0; | |
69 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); | |
70 | } | |
0e119b41 DH |
71 | if (ret < 0) |
72 | goto error_2; | |
73 | ||
35dbfba3 DH |
74 | srx.srx_service = YFS_CM_SERVICE; |
75 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); | |
76 | if (ret < 0) | |
77 | goto error_2; | |
78 | ||
3bf0fb6f DH |
79 | /* Ideally, we'd turn on service upgrade here, but we can't because |
80 | * OpenAFS is buggy and leaks the userStatus field from packet to | |
81 | * packet and between FS packets and CB packets - so if we try to do an | |
82 | * upgrade on an FS packet, OpenAFS will leak that into the CB packet | |
83 | * it sends back to us. | |
84 | */ | |
35dbfba3 | 85 | |
00e90712 DH |
86 | rxrpc_kernel_new_call_notification(socket, afs_rx_new_call, |
87 | afs_rx_discard_new_call); | |
d001648e | 88 | |
0e119b41 DH |
89 | ret = kernel_listen(socket, INT_MAX); |
90 | if (ret < 0) | |
91 | goto error_2; | |
08e0e7c8 | 92 | |
f044c884 DH |
93 | net->socket = socket; |
94 | afs_charge_preallocation(&net->charge_preallocation_work); | |
08e0e7c8 DH |
95 | _leave(" = 0"); |
96 | return 0; | |
0e119b41 DH |
97 | |
98 | error_2: | |
99 | sock_release(socket); | |
100 | error_1: | |
0e119b41 DH |
101 | _leave(" = %d", ret); |
102 | return ret; | |
08e0e7c8 DH |
103 | } |
104 | ||
105 | /* | |
106 | * close the RxRPC socket AFS was using | |
107 | */ | |
f044c884 | 108 | void afs_close_socket(struct afs_net *net) |
08e0e7c8 DH |
109 | { |
110 | _enter(""); | |
111 | ||
f044c884 | 112 | kernel_listen(net->socket, 0); |
341f741f DH |
113 | flush_workqueue(afs_async_calls); |
114 | ||
f044c884 DH |
115 | if (net->spare_incoming_call) { |
116 | afs_put_call(net->spare_incoming_call); | |
117 | net->spare_incoming_call = NULL; | |
00e90712 DH |
118 | } |
119 | ||
f044c884 | 120 | _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls)); |
ab1fbe32 PZ |
121 | wait_var_event(&net->nr_outstanding_calls, |
122 | !atomic_read(&net->nr_outstanding_calls)); | |
2f02f7ae DH |
123 | _debug("no outstanding calls"); |
124 | ||
f044c884 | 125 | kernel_sock_shutdown(net->socket, SHUT_RDWR); |
d001648e | 126 | flush_workqueue(afs_async_calls); |
f044c884 | 127 | sock_release(net->socket); |
08e0e7c8 DH |
128 | |
129 | _debug("dework"); | |
08e0e7c8 DH |
130 | _leave(""); |
131 | } | |
132 | ||
00d3b7a4 | 133 | /* |
341f741f | 134 | * Allocate a call. |
00d3b7a4 | 135 | */ |
f044c884 DH |
136 | static struct afs_call *afs_alloc_call(struct afs_net *net, |
137 | const struct afs_call_type *type, | |
341f741f | 138 | gfp_t gfp) |
00d3b7a4 | 139 | { |
341f741f DH |
140 | struct afs_call *call; |
141 | int o; | |
00d3b7a4 | 142 | |
341f741f DH |
143 | call = kzalloc(sizeof(*call), gfp); |
144 | if (!call) | |
145 | return NULL; | |
00d3b7a4 | 146 | |
341f741f | 147 | call->type = type; |
f044c884 | 148 | call->net = net; |
a25e21f0 | 149 | call->debug_id = atomic_inc_return(&rxrpc_debug_id); |
341f741f DH |
150 | atomic_set(&call->usage, 1); |
151 | INIT_WORK(&call->async_work, afs_process_async_call); | |
152 | init_waitqueue_head(&call->waitq); | |
98bf40cd | 153 | spin_lock_init(&call->state_lock); |
fc276122 | 154 | call->iter = &call->def_iter; |
2f02f7ae | 155 | |
f044c884 | 156 | o = atomic_inc_return(&net->nr_outstanding_calls); |
341f741f DH |
157 | trace_afs_call(call, afs_call_trace_alloc, 1, o, |
158 | __builtin_return_address(0)); | |
159 | return call; | |
00d3b7a4 DH |
160 | } |
161 | ||
6c67c7c3 | 162 | /* |
341f741f | 163 | * Dispose of a reference on a call. |
6c67c7c3 | 164 | */ |
341f741f | 165 | void afs_put_call(struct afs_call *call) |
6c67c7c3 | 166 | { |
f044c884 | 167 | struct afs_net *net = call->net; |
341f741f | 168 | int n = atomic_dec_return(&call->usage); |
f044c884 | 169 | int o = atomic_read(&net->nr_outstanding_calls); |
341f741f | 170 | |
4636cf18 | 171 | trace_afs_call(call, afs_call_trace_put, n, o, |
341f741f DH |
172 | __builtin_return_address(0)); |
173 | ||
174 | ASSERTCMP(n, >=, 0); | |
175 | if (n == 0) { | |
176 | ASSERT(!work_pending(&call->async_work)); | |
177 | ASSERT(call->type->name != NULL); | |
178 | ||
179 | if (call->rxcall) { | |
f044c884 | 180 | rxrpc_kernel_end_call(net->socket, call->rxcall); |
341f741f DH |
181 | call->rxcall = NULL; |
182 | } | |
183 | if (call->type->destructor) | |
184 | call->type->destructor(call); | |
185 | ||
977e5f8e | 186 | afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call); |
d2ddc776 | 187 | afs_put_cb_interest(call->net, call->cbi); |
3bf0fb6f | 188 | afs_put_addrlist(call->alist); |
341f741f | 189 | kfree(call->request); |
341f741f | 190 | |
341f741f DH |
191 | trace_afs_call(call, afs_call_trace_free, 0, o, |
192 | __builtin_return_address(0)); | |
a25e21f0 DH |
193 | kfree(call); |
194 | ||
195 | o = atomic_dec_return(&net->nr_outstanding_calls); | |
341f741f | 196 | if (o == 0) |
ab1fbe32 | 197 | wake_up_var(&net->nr_outstanding_calls); |
6c67c7c3 | 198 | } |
6cf12869 NWF |
199 | } |
200 | ||
7a75b007 DH |
201 | static struct afs_call *afs_get_call(struct afs_call *call, |
202 | enum afs_call_trace why) | |
203 | { | |
204 | int u = atomic_inc_return(&call->usage); | |
205 | ||
206 | trace_afs_call(call, why, u, | |
207 | atomic_read(&call->net->nr_outstanding_calls), | |
208 | __builtin_return_address(0)); | |
209 | return call; | |
210 | } | |
211 | ||
6cf12869 | 212 | /* |
3bf0fb6f | 213 | * Queue the call for actual work. |
6cf12869 | 214 | */ |
3bf0fb6f | 215 | static void afs_queue_call_work(struct afs_call *call) |
6cf12869 | 216 | { |
3bf0fb6f | 217 | if (call->type->work) { |
3bf0fb6f | 218 | INIT_WORK(&call->work, call->type->work); |
341f741f | 219 | |
7a75b007 | 220 | afs_get_call(call, afs_call_trace_work); |
3bf0fb6f DH |
221 | if (!queue_work(afs_wq, &call->work)) |
222 | afs_put_call(call); | |
223 | } | |
6c67c7c3 DH |
224 | } |
225 | ||
08e0e7c8 DH |
226 | /* |
227 | * allocate a call with flat request and reply buffers | |
228 | */ | |
f044c884 DH |
229 | struct afs_call *afs_alloc_flat_call(struct afs_net *net, |
230 | const struct afs_call_type *type, | |
d001648e | 231 | size_t request_size, size_t reply_max) |
08e0e7c8 DH |
232 | { |
233 | struct afs_call *call; | |
234 | ||
f044c884 | 235 | call = afs_alloc_call(net, type, GFP_NOFS); |
08e0e7c8 DH |
236 | if (!call) |
237 | goto nomem_call; | |
238 | ||
239 | if (request_size) { | |
341f741f | 240 | call->request_size = request_size; |
08e0e7c8 DH |
241 | call->request = kmalloc(request_size, GFP_NOFS); |
242 | if (!call->request) | |
00d3b7a4 | 243 | goto nomem_free; |
08e0e7c8 DH |
244 | } |
245 | ||
d001648e | 246 | if (reply_max) { |
341f741f | 247 | call->reply_max = reply_max; |
d001648e | 248 | call->buffer = kmalloc(reply_max, GFP_NOFS); |
08e0e7c8 | 249 | if (!call->buffer) |
00d3b7a4 | 250 | goto nomem_free; |
08e0e7c8 DH |
251 | } |
252 | ||
12bdcf33 | 253 | afs_extract_to_buf(call, call->reply_max); |
025db80c | 254 | call->operation_ID = type->op; |
08e0e7c8 | 255 | init_waitqueue_head(&call->waitq); |
08e0e7c8 DH |
256 | return call; |
257 | ||
00d3b7a4 | 258 | nomem_free: |
341f741f | 259 | afs_put_call(call); |
08e0e7c8 DH |
260 | nomem_call: |
261 | return NULL; | |
262 | } | |
263 | ||
264 | /* | |
265 | * clean up a call with flat buffer | |
266 | */ | |
267 | void afs_flat_call_destructor(struct afs_call *call) | |
268 | { | |
269 | _enter(""); | |
270 | ||
271 | kfree(call->request); | |
272 | call->request = NULL; | |
273 | kfree(call->buffer); | |
274 | call->buffer = NULL; | |
275 | } | |
276 | ||
2f5705a5 DH |
277 | #define AFS_BVEC_MAX 8 |
278 | ||
279 | /* | |
280 | * Load the given bvec with the next few pages. | |
281 | */ | |
282 | static void afs_load_bvec(struct afs_call *call, struct msghdr *msg, | |
283 | struct bio_vec *bv, pgoff_t first, pgoff_t last, | |
284 | unsigned offset) | |
285 | { | |
e49c7b2f | 286 | struct afs_operation *op = call->op; |
2f5705a5 DH |
287 | struct page *pages[AFS_BVEC_MAX]; |
288 | unsigned int nr, n, i, to, bytes = 0; | |
289 | ||
290 | nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX); | |
e49c7b2f | 291 | n = find_get_pages_contig(op->store.mapping, first, nr, pages); |
2f5705a5 DH |
292 | ASSERTCMP(n, ==, nr); |
293 | ||
294 | msg->msg_flags |= MSG_MORE; | |
295 | for (i = 0; i < nr; i++) { | |
296 | to = PAGE_SIZE; | |
297 | if (first + i >= last) { | |
e49c7b2f | 298 | to = op->store.last_to; |
2f5705a5 DH |
299 | msg->msg_flags &= ~MSG_MORE; |
300 | } | |
301 | bv[i].bv_page = pages[i]; | |
302 | bv[i].bv_len = to - offset; | |
303 | bv[i].bv_offset = offset; | |
304 | bytes += to - offset; | |
305 | offset = 0; | |
306 | } | |
307 | ||
aa563d7b | 308 | iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes); |
2f5705a5 DH |
309 | } |
310 | ||
e833251a DH |
311 | /* |
312 | * Advance the AFS call state when the RxRPC call ends the transmit phase. | |
313 | */ | |
314 | static void afs_notify_end_request_tx(struct sock *sock, | |
315 | struct rxrpc_call *rxcall, | |
316 | unsigned long call_user_ID) | |
317 | { | |
318 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
319 | ||
98bf40cd | 320 | afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY); |
e833251a DH |
321 | } |
322 | ||
31143d5d DH |
323 | /* |
324 | * attach the data from a bunch of pages on an inode to a call | |
325 | */ | |
39c6acea | 326 | static int afs_send_pages(struct afs_call *call, struct msghdr *msg) |
31143d5d | 327 | { |
e49c7b2f | 328 | struct afs_operation *op = call->op; |
2f5705a5 DH |
329 | struct bio_vec bv[AFS_BVEC_MAX]; |
330 | unsigned int bytes, nr, loop, offset; | |
e49c7b2f | 331 | pgoff_t first = op->store.first, last = op->store.last; |
31143d5d DH |
332 | int ret; |
333 | ||
e49c7b2f DH |
334 | offset = op->store.first_offset; |
335 | op->store.first_offset = 0; | |
31143d5d DH |
336 | |
337 | do { | |
2f5705a5 | 338 | afs_load_bvec(call, msg, bv, first, last, offset); |
2c099014 DH |
339 | trace_afs_send_pages(call, msg, first, last, offset); |
340 | ||
2f5705a5 DH |
341 | offset = 0; |
342 | bytes = msg->msg_iter.count; | |
343 | nr = msg->msg_iter.nr_segs; | |
344 | ||
e49c7b2f | 345 | ret = rxrpc_kernel_send_data(op->net->socket, call->rxcall, msg, |
e833251a | 346 | bytes, afs_notify_end_request_tx); |
2f5705a5 DH |
347 | for (loop = 0; loop < nr; loop++) |
348 | put_page(bv[loop].bv_page); | |
31143d5d DH |
349 | if (ret < 0) |
350 | break; | |
2f5705a5 DH |
351 | |
352 | first += nr; | |
5bbf5d39 | 353 | } while (first <= last); |
31143d5d | 354 | |
e49c7b2f | 355 | trace_afs_sent_pages(call, op->store.first, last, first, ret); |
31143d5d DH |
356 | return ret; |
357 | } | |
358 | ||
08e0e7c8 | 359 | /* |
0b9bf381 DH |
360 | * Initiate a call and synchronously queue up the parameters for dispatch. Any |
361 | * error is stored into the call struct, which the caller must check for. | |
08e0e7c8 | 362 | */ |
0b9bf381 | 363 | void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp) |
08e0e7c8 | 364 | { |
2feeaf84 | 365 | struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index]; |
08e0e7c8 DH |
366 | struct rxrpc_call *rxcall; |
367 | struct msghdr msg; | |
368 | struct kvec iov[1]; | |
e754eba6 | 369 | s64 tx_total_len; |
08e0e7c8 DH |
370 | int ret; |
371 | ||
4d9df986 | 372 | _enter(",{%pISp},", &srx->transport); |
08e0e7c8 | 373 | |
00d3b7a4 DH |
374 | ASSERT(call->type != NULL); |
375 | ASSERT(call->type->name != NULL); | |
376 | ||
31143d5d DH |
377 | _debug("____MAKE %p{%s,%x} [%d]____", |
378 | call, call->type->name, key_serial(call->key), | |
f044c884 | 379 | atomic_read(&call->net->nr_outstanding_calls)); |
00d3b7a4 | 380 | |
3bf0fb6f DH |
381 | call->addr_ix = ac->index; |
382 | call->alist = afs_get_addrlist(ac->alist); | |
08e0e7c8 | 383 | |
e754eba6 DH |
384 | /* Work out the length we're going to transmit. This is awkward for |
385 | * calls such as FS.StoreData where there's an extra injection of data | |
386 | * after the initial fixed part. | |
387 | */ | |
388 | tx_total_len = call->request_size; | |
389 | if (call->send_pages) { | |
e49c7b2f DH |
390 | struct afs_operation *op = call->op; |
391 | ||
392 | if (op->store.last == op->store.first) { | |
393 | tx_total_len += op->store.last_to - op->store.first_offset; | |
1199db60 DH |
394 | } else { |
395 | /* It looks mathematically like you should be able to | |
396 | * combine the following lines with the ones above, but | |
397 | * unsigned arithmetic is fun when it wraps... | |
398 | */ | |
e49c7b2f DH |
399 | tx_total_len += PAGE_SIZE - op->store.first_offset; |
400 | tx_total_len += op->store.last_to; | |
401 | tx_total_len += (op->store.last - op->store.first - 1) * PAGE_SIZE; | |
1199db60 | 402 | } |
e754eba6 DH |
403 | } |
404 | ||
34fa4761 DH |
405 | /* If the call is going to be asynchronous, we need an extra ref for |
406 | * the call to hold itself so the caller need not hang on to its ref. | |
407 | */ | |
dde9f095 | 408 | if (call->async) { |
34fa4761 | 409 | afs_get_call(call, afs_call_trace_get); |
dde9f095 DH |
410 | call->drop_ref = true; |
411 | } | |
34fa4761 | 412 | |
08e0e7c8 | 413 | /* create a call */ |
4d9df986 | 414 | rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key, |
e754eba6 DH |
415 | (unsigned long)call, |
416 | tx_total_len, gfp, | |
0b9bf381 | 417 | (call->async ? |
56ff9c83 | 418 | afs_wake_up_async_call : |
a68f4a27 | 419 | afs_wake_up_call_waiter), |
a25e21f0 | 420 | call->upgrade, |
e138aa7d DH |
421 | (call->intr ? RXRPC_PREINTERRUPTIBLE : |
422 | RXRPC_UNINTERRUPTIBLE), | |
a25e21f0 | 423 | call->debug_id); |
08e0e7c8 DH |
424 | if (IS_ERR(rxcall)) { |
425 | ret = PTR_ERR(rxcall); | |
3bf0fb6f | 426 | call->error = ret; |
08e0e7c8 DH |
427 | goto error_kill_call; |
428 | } | |
429 | ||
430 | call->rxcall = rxcall; | |
431 | ||
94f699c9 DH |
432 | if (call->max_lifespan) |
433 | rxrpc_kernel_set_max_life(call->net->socket, rxcall, | |
434 | call->max_lifespan); | |
435 | ||
08e0e7c8 DH |
436 | /* send the request */ |
437 | iov[0].iov_base = call->request; | |
438 | iov[0].iov_len = call->request_size; | |
439 | ||
440 | msg.msg_name = NULL; | |
441 | msg.msg_namelen = 0; | |
aa563d7b | 442 | iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size); |
08e0e7c8 DH |
443 | msg.msg_control = NULL; |
444 | msg.msg_controllen = 0; | |
bc5e3a54 | 445 | msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0); |
08e0e7c8 | 446 | |
f044c884 | 447 | ret = rxrpc_kernel_send_data(call->net->socket, rxcall, |
e833251a DH |
448 | &msg, call->request_size, |
449 | afs_notify_end_request_tx); | |
08e0e7c8 DH |
450 | if (ret < 0) |
451 | goto error_do_abort; | |
452 | ||
31143d5d | 453 | if (call->send_pages) { |
39c6acea | 454 | ret = afs_send_pages(call, &msg); |
31143d5d DH |
455 | if (ret < 0) |
456 | goto error_do_abort; | |
457 | } | |
458 | ||
34fa4761 DH |
459 | /* Note that at this point, we may have received the reply or an abort |
460 | * - and an asynchronous call may already have completed. | |
0b9bf381 DH |
461 | * |
462 | * afs_wait_for_call_to_complete(call, ac) | |
463 | * must be called to synchronously clean up. | |
34fa4761 | 464 | */ |
0b9bf381 | 465 | return; |
08e0e7c8 DH |
466 | |
467 | error_do_abort: | |
70af0e3b | 468 | if (ret != -ECONNABORTED) { |
f044c884 DH |
469 | rxrpc_kernel_abort_call(call->net->socket, rxcall, |
470 | RX_USER_ABORT, ret, "KSD"); | |
70af0e3b | 471 | } else { |
aa563d7b | 472 | iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0); |
eb9950eb DH |
473 | rxrpc_kernel_recv_data(call->net->socket, rxcall, |
474 | &msg.msg_iter, false, | |
475 | &call->abort_code, &call->service_id); | |
d2ddc776 DH |
476 | ac->abort_code = call->abort_code; |
477 | ac->responded = true; | |
70af0e3b | 478 | } |
025db80c DH |
479 | call->error = ret; |
480 | trace_afs_call_done(call); | |
08e0e7c8 | 481 | error_kill_call: |
3bf0fb6f DH |
482 | if (call->type->done) |
483 | call->type->done(call); | |
34fa4761 DH |
484 | |
485 | /* We need to dispose of the extra ref we grabbed for an async call. | |
486 | * The call, however, might be queued on afs_async_calls and we need to | |
487 | * make sure we don't get any more notifications that might requeue it. | |
488 | */ | |
489 | if (call->rxcall) { | |
490 | rxrpc_kernel_end_call(call->net->socket, call->rxcall); | |
491 | call->rxcall = NULL; | |
492 | } | |
493 | if (call->async) { | |
494 | if (cancel_work_sync(&call->async_work)) | |
495 | afs_put_call(call); | |
496 | afs_put_call(call); | |
497 | } | |
498 | ||
d2ddc776 | 499 | ac->error = ret; |
34fa4761 | 500 | call->state = AFS_CALL_COMPLETE; |
08e0e7c8 | 501 | _leave(" = %d", ret); |
08e0e7c8 DH |
502 | } |
503 | ||
08e0e7c8 DH |
504 | /* |
505 | * deliver messages to a call | |
506 | */ | |
507 | static void afs_deliver_to_call(struct afs_call *call) | |
508 | { | |
98bf40cd DH |
509 | enum afs_call_state state; |
510 | u32 abort_code, remote_abort = 0; | |
08e0e7c8 DH |
511 | int ret; |
512 | ||
d001648e DH |
513 | _enter("%s", call->type->name); |
514 | ||
98bf40cd DH |
515 | while (state = READ_ONCE(call->state), |
516 | state == AFS_CALL_CL_AWAIT_REPLY || | |
517 | state == AFS_CALL_SV_AWAIT_OP_ID || | |
518 | state == AFS_CALL_SV_AWAIT_REQUEST || | |
519 | state == AFS_CALL_SV_AWAIT_ACK | |
d001648e | 520 | ) { |
98bf40cd | 521 | if (state == AFS_CALL_SV_AWAIT_ACK) { |
fc276122 | 522 | iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0); |
f044c884 | 523 | ret = rxrpc_kernel_recv_data(call->net->socket, |
fc276122 | 524 | call->rxcall, &call->def_iter, |
12bdcf33 | 525 | false, &remote_abort, |
a68f4a27 | 526 | &call->service_id); |
fc276122 | 527 | trace_afs_receive_data(call, &call->def_iter, false, ret); |
8e8d7f13 | 528 | |
d001648e DH |
529 | if (ret == -EINPROGRESS || ret == -EAGAIN) |
530 | return; | |
98bf40cd DH |
531 | if (ret < 0 || ret == 1) { |
532 | if (ret == 1) | |
533 | ret = 0; | |
025db80c | 534 | goto call_complete; |
98bf40cd | 535 | } |
d001648e | 536 | return; |
08e0e7c8 DH |
537 | } |
538 | ||
4571577f | 539 | if (!call->have_reply_time && |
12d8e95a DH |
540 | rxrpc_kernel_get_reply_time(call->net->socket, |
541 | call->rxcall, | |
542 | &call->reply_time)) | |
4571577f | 543 | call->have_reply_time = true; |
12d8e95a | 544 | |
d001648e | 545 | ret = call->type->deliver(call); |
98bf40cd | 546 | state = READ_ONCE(call->state); |
38355eec DH |
547 | if (ret == 0 && call->unmarshalling_error) |
548 | ret = -EBADMSG; | |
d001648e DH |
549 | switch (ret) { |
550 | case 0: | |
3bf0fb6f | 551 | afs_queue_call_work(call); |
f2686b09 DH |
552 | if (state == AFS_CALL_CL_PROC_REPLY) { |
553 | if (call->cbi) | |
554 | set_bit(AFS_SERVER_FL_MAY_HAVE_CB, | |
555 | &call->cbi->server->flags); | |
025db80c | 556 | goto call_complete; |
f2686b09 | 557 | } |
98bf40cd | 558 | ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY); |
d001648e DH |
559 | goto done; |
560 | case -EINPROGRESS: | |
561 | case -EAGAIN: | |
562 | goto out; | |
70af0e3b | 563 | case -ECONNABORTED: |
98bf40cd DH |
564 | ASSERTCMP(state, ==, AFS_CALL_COMPLETE); |
565 | goto done; | |
d001648e | 566 | case -ENOTSUPP: |
1157f153 | 567 | abort_code = RXGEN_OPCODE; |
f044c884 | 568 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
3a92789a | 569 | abort_code, ret, "KIV"); |
98bf40cd | 570 | goto local_abort; |
4ac15ea5 DH |
571 | case -EIO: |
572 | pr_err("kAFS: Call %u in bad state %u\n", | |
573 | call->debug_id, state); | |
574 | /* Fall through */ | |
d001648e DH |
575 | case -ENODATA: |
576 | case -EBADMSG: | |
577 | case -EMSGSIZE: | |
d001648e | 578 | abort_code = RXGEN_CC_UNMARSHAL; |
98bf40cd | 579 | if (state != AFS_CALL_CL_AWAIT_REPLY) |
d001648e | 580 | abort_code = RXGEN_SS_UNMARSHAL; |
f044c884 | 581 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
12bdcf33 | 582 | abort_code, ret, "KUM"); |
98bf40cd | 583 | goto local_abort; |
8022c4b9 DH |
584 | default: |
585 | abort_code = RX_USER_ABORT; | |
586 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, | |
587 | abort_code, ret, "KER"); | |
588 | goto local_abort; | |
d001648e | 589 | } |
08e0e7c8 DH |
590 | } |
591 | ||
d001648e | 592 | done: |
3bf0fb6f DH |
593 | if (call->type->done) |
594 | call->type->done(call); | |
d001648e | 595 | out: |
08e0e7c8 | 596 | _leave(""); |
d001648e DH |
597 | return; |
598 | ||
98bf40cd DH |
599 | local_abort: |
600 | abort_code = 0; | |
025db80c | 601 | call_complete: |
98bf40cd DH |
602 | afs_set_call_complete(call, ret, remote_abort); |
603 | state = AFS_CALL_COMPLETE; | |
d001648e | 604 | goto done; |
08e0e7c8 DH |
605 | } |
606 | ||
607 | /* | |
0b9bf381 | 608 | * Wait synchronously for a call to complete and clean up the call struct. |
08e0e7c8 | 609 | */ |
0b9bf381 DH |
610 | long afs_wait_for_call_to_complete(struct afs_call *call, |
611 | struct afs_addr_cursor *ac) | |
08e0e7c8 | 612 | { |
33cd7f2b | 613 | long ret; |
f7f1dd31 | 614 | bool rxrpc_complete = false; |
08e0e7c8 DH |
615 | |
616 | DECLARE_WAITQUEUE(myself, current); | |
617 | ||
618 | _enter(""); | |
619 | ||
0b9bf381 DH |
620 | ret = call->error; |
621 | if (ret < 0) | |
622 | goto out; | |
623 | ||
08e0e7c8 DH |
624 | add_wait_queue(&call->waitq, &myself); |
625 | for (;;) { | |
bc5e3a54 | 626 | set_current_state(TASK_UNINTERRUPTIBLE); |
08e0e7c8 DH |
627 | |
628 | /* deliver any messages that are in the queue */ | |
98bf40cd DH |
629 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE) && |
630 | call->need_attention) { | |
d001648e | 631 | call->need_attention = false; |
08e0e7c8 DH |
632 | __set_current_state(TASK_RUNNING); |
633 | afs_deliver_to_call(call); | |
634 | continue; | |
635 | } | |
636 | ||
98bf40cd | 637 | if (afs_check_call_state(call, AFS_CALL_COMPLETE)) |
08e0e7c8 | 638 | break; |
bc5e3a54 | 639 | |
7d7587db | 640 | if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) { |
f7f1dd31 MD |
641 | /* rxrpc terminated the call. */ |
642 | rxrpc_complete = true; | |
643 | break; | |
644 | } | |
645 | ||
7d7587db | 646 | schedule(); |
08e0e7c8 DH |
647 | } |
648 | ||
649 | remove_wait_queue(&call->waitq, &myself); | |
650 | __set_current_state(TASK_RUNNING); | |
651 | ||
98bf40cd | 652 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { |
f7f1dd31 MD |
653 | if (rxrpc_complete) { |
654 | afs_set_call_complete(call, call->error, call->abort_code); | |
655 | } else { | |
656 | /* Kill off the call if it's still live. */ | |
657 | _debug("call interrupted"); | |
658 | if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall, | |
659 | RX_USER_ABORT, -EINTR, "KWI")) | |
660 | afs_set_call_complete(call, -EINTR, 0); | |
661 | } | |
08e0e7c8 DH |
662 | } |
663 | ||
98bf40cd | 664 | spin_lock_bh(&call->state_lock); |
d2ddc776 DH |
665 | ac->abort_code = call->abort_code; |
666 | ac->error = call->error; | |
98bf40cd | 667 | spin_unlock_bh(&call->state_lock); |
d2ddc776 DH |
668 | |
669 | ret = ac->error; | |
670 | switch (ret) { | |
671 | case 0: | |
ffba718e DH |
672 | ret = call->ret0; |
673 | call->ret0 = 0; | |
674 | ||
d2ddc776 DH |
675 | /* Fall through */ |
676 | case -ECONNABORTED: | |
677 | ac->responded = true; | |
678 | break; | |
33cd7f2b DH |
679 | } |
680 | ||
0b9bf381 | 681 | out: |
08e0e7c8 | 682 | _debug("call complete"); |
341f741f | 683 | afs_put_call(call); |
33cd7f2b | 684 | _leave(" = %p", (void *)ret); |
08e0e7c8 DH |
685 | return ret; |
686 | } | |
687 | ||
688 | /* | |
689 | * wake up a waiting call | |
690 | */ | |
d001648e DH |
691 | static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, |
692 | unsigned long call_user_ID) | |
08e0e7c8 | 693 | { |
d001648e DH |
694 | struct afs_call *call = (struct afs_call *)call_user_ID; |
695 | ||
696 | call->need_attention = true; | |
08e0e7c8 DH |
697 | wake_up(&call->waitq); |
698 | } | |
699 | ||
700 | /* | |
701 | * wake up an asynchronous call | |
702 | */ | |
d001648e DH |
703 | static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, |
704 | unsigned long call_user_ID) | |
08e0e7c8 | 705 | { |
d001648e | 706 | struct afs_call *call = (struct afs_call *)call_user_ID; |
341f741f | 707 | int u; |
d001648e | 708 | |
8e8d7f13 | 709 | trace_afs_notify_call(rxcall, call); |
d001648e | 710 | call->need_attention = true; |
341f741f | 711 | |
bfc18e38 | 712 | u = atomic_fetch_add_unless(&call->usage, 1, 0); |
341f741f | 713 | if (u != 0) { |
4636cf18 | 714 | trace_afs_call(call, afs_call_trace_wake, u + 1, |
f044c884 | 715 | atomic_read(&call->net->nr_outstanding_calls), |
341f741f DH |
716 | __builtin_return_address(0)); |
717 | ||
718 | if (!queue_work(afs_async_calls, &call->async_work)) | |
719 | afs_put_call(call); | |
720 | } | |
08e0e7c8 DH |
721 | } |
722 | ||
08e0e7c8 | 723 | /* |
341f741f DH |
724 | * Perform I/O processing on an asynchronous call. The work item carries a ref |
725 | * to the call struct that we either need to release or to pass on. | |
08e0e7c8 | 726 | */ |
d001648e | 727 | static void afs_process_async_call(struct work_struct *work) |
08e0e7c8 | 728 | { |
d001648e DH |
729 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
730 | ||
08e0e7c8 DH |
731 | _enter(""); |
732 | ||
d001648e DH |
733 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
734 | call->need_attention = false; | |
08e0e7c8 | 735 | afs_deliver_to_call(call); |
d001648e | 736 | } |
08e0e7c8 | 737 | |
341f741f | 738 | afs_put_call(call); |
08e0e7c8 DH |
739 | _leave(""); |
740 | } | |
741 | ||
00e90712 DH |
742 | static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) |
743 | { | |
744 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
745 | ||
746 | call->rxcall = rxcall; | |
747 | } | |
748 | ||
749 | /* | |
750 | * Charge the incoming call preallocation. | |
751 | */ | |
f044c884 | 752 | void afs_charge_preallocation(struct work_struct *work) |
00e90712 | 753 | { |
f044c884 DH |
754 | struct afs_net *net = |
755 | container_of(work, struct afs_net, charge_preallocation_work); | |
756 | struct afs_call *call = net->spare_incoming_call; | |
00e90712 DH |
757 | |
758 | for (;;) { | |
759 | if (!call) { | |
f044c884 | 760 | call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); |
00e90712 DH |
761 | if (!call) |
762 | break; | |
763 | ||
dde9f095 | 764 | call->drop_ref = true; |
56ff9c83 | 765 | call->async = true; |
98bf40cd | 766 | call->state = AFS_CALL_SV_AWAIT_OP_ID; |
56ff9c83 | 767 | init_waitqueue_head(&call->waitq); |
12bdcf33 | 768 | afs_extract_to_tmp(call); |
00e90712 DH |
769 | } |
770 | ||
f044c884 | 771 | if (rxrpc_kernel_charge_accept(net->socket, |
00e90712 DH |
772 | afs_wake_up_async_call, |
773 | afs_rx_attach, | |
774 | (unsigned long)call, | |
a25e21f0 DH |
775 | GFP_KERNEL, |
776 | call->debug_id) < 0) | |
00e90712 DH |
777 | break; |
778 | call = NULL; | |
779 | } | |
f044c884 | 780 | net->spare_incoming_call = call; |
00e90712 DH |
781 | } |
782 | ||
783 | /* | |
784 | * Discard a preallocated call when a socket is shut down. | |
785 | */ | |
786 | static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, | |
787 | unsigned long user_call_ID) | |
788 | { | |
789 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
790 | ||
00e90712 | 791 | call->rxcall = NULL; |
341f741f | 792 | afs_put_call(call); |
00e90712 DH |
793 | } |
794 | ||
d001648e DH |
795 | /* |
796 | * Notification of an incoming call. | |
797 | */ | |
00e90712 DH |
798 | static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, |
799 | unsigned long user_call_ID) | |
d001648e | 800 | { |
f044c884 DH |
801 | struct afs_net *net = afs_sock2net(sk); |
802 | ||
803 | queue_work(afs_wq, &net->charge_preallocation_work); | |
d001648e DH |
804 | } |
805 | ||
08e0e7c8 | 806 | /* |
372ee163 DH |
807 | * Grab the operation ID from an incoming cache manager call. The socket |
808 | * buffer is discarded on error or if we don't yet have sufficient data. | |
08e0e7c8 | 809 | */ |
d001648e | 810 | static int afs_deliver_cm_op_id(struct afs_call *call) |
08e0e7c8 | 811 | { |
d001648e | 812 | int ret; |
08e0e7c8 | 813 | |
fc276122 | 814 | _enter("{%zu}", iov_iter_count(call->iter)); |
08e0e7c8 DH |
815 | |
816 | /* the operation ID forms the first four bytes of the request data */ | |
12bdcf33 | 817 | ret = afs_extract_data(call, true); |
d001648e DH |
818 | if (ret < 0) |
819 | return ret; | |
08e0e7c8 | 820 | |
50a2c953 | 821 | call->operation_ID = ntohl(call->tmp); |
98bf40cd | 822 | afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST); |
08e0e7c8 DH |
823 | |
824 | /* ask the cache manager to route the call (it'll change the call type | |
825 | * if successful) */ | |
826 | if (!afs_cm_incoming_call(call)) | |
827 | return -ENOTSUPP; | |
828 | ||
8e8d7f13 DH |
829 | trace_afs_cb_call(call); |
830 | ||
08e0e7c8 DH |
831 | /* pass responsibility for the remainer of this message off to the |
832 | * cache manager op */ | |
d001648e | 833 | return call->type->deliver(call); |
08e0e7c8 DH |
834 | } |
835 | ||
e833251a DH |
836 | /* |
837 | * Advance the AFS call state when an RxRPC service call ends the transmit | |
838 | * phase. | |
839 | */ | |
840 | static void afs_notify_end_reply_tx(struct sock *sock, | |
841 | struct rxrpc_call *rxcall, | |
842 | unsigned long call_user_ID) | |
843 | { | |
844 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
845 | ||
98bf40cd | 846 | afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK); |
e833251a DH |
847 | } |
848 | ||
08e0e7c8 DH |
849 | /* |
850 | * send an empty reply | |
851 | */ | |
852 | void afs_send_empty_reply(struct afs_call *call) | |
853 | { | |
f044c884 | 854 | struct afs_net *net = call->net; |
08e0e7c8 | 855 | struct msghdr msg; |
08e0e7c8 DH |
856 | |
857 | _enter(""); | |
858 | ||
f044c884 | 859 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); |
e754eba6 | 860 | |
08e0e7c8 DH |
861 | msg.msg_name = NULL; |
862 | msg.msg_namelen = 0; | |
aa563d7b | 863 | iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0); |
08e0e7c8 DH |
864 | msg.msg_control = NULL; |
865 | msg.msg_controllen = 0; | |
866 | msg.msg_flags = 0; | |
867 | ||
f044c884 | 868 | switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, |
e833251a | 869 | afs_notify_end_reply_tx)) { |
08e0e7c8 DH |
870 | case 0: |
871 | _leave(" [replied]"); | |
872 | return; | |
873 | ||
874 | case -ENOMEM: | |
875 | _debug("oom"); | |
f044c884 | 876 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 877 | RX_USER_ABORT, -ENOMEM, "KOO"); |
e690c9e3 | 878 | /* Fall through */ |
08e0e7c8 | 879 | default: |
08e0e7c8 DH |
880 | _leave(" [error]"); |
881 | return; | |
882 | } | |
883 | } | |
884 | ||
b908fe6b DH |
885 | /* |
886 | * send a simple reply | |
887 | */ | |
888 | void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) | |
889 | { | |
f044c884 | 890 | struct afs_net *net = call->net; |
b908fe6b | 891 | struct msghdr msg; |
2e90b1c4 | 892 | struct kvec iov[1]; |
bd6dc742 | 893 | int n; |
b908fe6b DH |
894 | |
895 | _enter(""); | |
896 | ||
f044c884 | 897 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); |
e754eba6 | 898 | |
b908fe6b DH |
899 | iov[0].iov_base = (void *) buf; |
900 | iov[0].iov_len = len; | |
901 | msg.msg_name = NULL; | |
902 | msg.msg_namelen = 0; | |
aa563d7b | 903 | iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len); |
b908fe6b DH |
904 | msg.msg_control = NULL; |
905 | msg.msg_controllen = 0; | |
906 | msg.msg_flags = 0; | |
907 | ||
f044c884 | 908 | n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, |
e833251a | 909 | afs_notify_end_reply_tx); |
bd6dc742 | 910 | if (n >= 0) { |
6c67c7c3 | 911 | /* Success */ |
b908fe6b DH |
912 | _leave(" [replied]"); |
913 | return; | |
bd6dc742 | 914 | } |
6c67c7c3 | 915 | |
bd6dc742 | 916 | if (n == -ENOMEM) { |
b908fe6b | 917 | _debug("oom"); |
f044c884 | 918 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 919 | RX_USER_ABORT, -ENOMEM, "KOO"); |
b908fe6b | 920 | } |
bd6dc742 | 921 | _leave(" [error]"); |
b908fe6b DH |
922 | } |
923 | ||
08e0e7c8 | 924 | /* |
372ee163 | 925 | * Extract a piece of data from the received data socket buffers. |
08e0e7c8 | 926 | */ |
12bdcf33 | 927 | int afs_extract_data(struct afs_call *call, bool want_more) |
08e0e7c8 | 928 | { |
f044c884 | 929 | struct afs_net *net = call->net; |
fc276122 | 930 | struct iov_iter *iter = call->iter; |
98bf40cd | 931 | enum afs_call_state state; |
7888da95 | 932 | u32 remote_abort = 0; |
d001648e | 933 | int ret; |
08e0e7c8 | 934 | |
12bdcf33 | 935 | _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more); |
eb9950eb | 936 | |
12bdcf33 | 937 | ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter, |
98bf40cd | 938 | want_more, &remote_abort, |
a68f4a27 | 939 | &call->service_id); |
d001648e DH |
940 | if (ret == 0 || ret == -EAGAIN) |
941 | return ret; | |
08e0e7c8 | 942 | |
98bf40cd | 943 | state = READ_ONCE(call->state); |
d001648e | 944 | if (ret == 1) { |
98bf40cd DH |
945 | switch (state) { |
946 | case AFS_CALL_CL_AWAIT_REPLY: | |
947 | afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY); | |
d001648e | 948 | break; |
98bf40cd DH |
949 | case AFS_CALL_SV_AWAIT_REQUEST: |
950 | afs_set_call_state(call, state, AFS_CALL_SV_REPLYING); | |
d001648e | 951 | break; |
98bf40cd DH |
952 | case AFS_CALL_COMPLETE: |
953 | kdebug("prem complete %d", call->error); | |
f51375cd | 954 | return afs_io_error(call, afs_io_error_extract); |
d001648e DH |
955 | default: |
956 | break; | |
957 | } | |
958 | return 0; | |
08e0e7c8 | 959 | } |
d001648e | 960 | |
98bf40cd | 961 | afs_set_call_complete(call, ret, remote_abort); |
d001648e | 962 | return ret; |
08e0e7c8 | 963 | } |
5f702c8e DH |
964 | |
965 | /* | |
966 | * Log protocol error production. | |
967 | */ | |
7126ead9 | 968 | noinline int afs_protocol_error(struct afs_call *call, |
160cb957 | 969 | enum afs_eproto_cause cause) |
5f702c8e | 970 | { |
7126ead9 | 971 | trace_afs_protocol_error(call, cause); |
38355eec DH |
972 | if (call) |
973 | call->unmarshalling_error = true; | |
7126ead9 | 974 | return -EBADMSG; |
5f702c8e | 975 | } |