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