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