]>
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); |
33cd7f2b | 23 | static long 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 | 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; | |
44 | int ret; | |
45 | ||
46 | _enter(""); | |
47 | ||
3838d3ec | 48 | ret = sock_create_kern(&init_net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket); |
0e119b41 DH |
49 | if (ret < 0) |
50 | goto error_1; | |
08e0e7c8 DH |
51 | |
52 | socket->sk->sk_allocation = GFP_NOFS; | |
53 | ||
54 | /* bind the callback manager's address to make this a server socket */ | |
3838d3ec | 55 | memset(&srx, 0, sizeof(srx)); |
08e0e7c8 DH |
56 | srx.srx_family = AF_RXRPC; |
57 | srx.srx_service = CM_SERVICE; | |
58 | srx.transport_type = SOCK_DGRAM; | |
3838d3ec DH |
59 | srx.transport_len = sizeof(srx.transport.sin6); |
60 | srx.transport.sin6.sin6_family = AF_INET6; | |
61 | srx.transport.sin6.sin6_port = htons(AFS_CM_PORT); | |
08e0e7c8 DH |
62 | |
63 | ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx)); | |
0e119b41 DH |
64 | if (ret < 0) |
65 | goto error_2; | |
66 | ||
00e90712 DH |
67 | rxrpc_kernel_new_call_notification(socket, afs_rx_new_call, |
68 | afs_rx_discard_new_call); | |
d001648e | 69 | |
0e119b41 DH |
70 | ret = kernel_listen(socket, INT_MAX); |
71 | if (ret < 0) | |
72 | goto error_2; | |
08e0e7c8 | 73 | |
f044c884 DH |
74 | net->socket = socket; |
75 | afs_charge_preallocation(&net->charge_preallocation_work); | |
08e0e7c8 DH |
76 | _leave(" = 0"); |
77 | return 0; | |
0e119b41 DH |
78 | |
79 | error_2: | |
80 | sock_release(socket); | |
81 | error_1: | |
0e119b41 DH |
82 | _leave(" = %d", ret); |
83 | return ret; | |
08e0e7c8 DH |
84 | } |
85 | ||
86 | /* | |
87 | * close the RxRPC socket AFS was using | |
88 | */ | |
f044c884 | 89 | void afs_close_socket(struct afs_net *net) |
08e0e7c8 DH |
90 | { |
91 | _enter(""); | |
92 | ||
f044c884 | 93 | kernel_listen(net->socket, 0); |
341f741f DH |
94 | flush_workqueue(afs_async_calls); |
95 | ||
f044c884 DH |
96 | if (net->spare_incoming_call) { |
97 | afs_put_call(net->spare_incoming_call); | |
98 | net->spare_incoming_call = NULL; | |
00e90712 DH |
99 | } |
100 | ||
f044c884 DH |
101 | _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls)); |
102 | wait_on_atomic_t(&net->nr_outstanding_calls, atomic_t_wait, | |
2f02f7ae DH |
103 | TASK_UNINTERRUPTIBLE); |
104 | _debug("no outstanding calls"); | |
105 | ||
f044c884 | 106 | kernel_sock_shutdown(net->socket, SHUT_RDWR); |
d001648e | 107 | flush_workqueue(afs_async_calls); |
f044c884 | 108 | sock_release(net->socket); |
08e0e7c8 DH |
109 | |
110 | _debug("dework"); | |
08e0e7c8 DH |
111 | _leave(""); |
112 | } | |
113 | ||
00d3b7a4 | 114 | /* |
341f741f | 115 | * Allocate a call. |
00d3b7a4 | 116 | */ |
f044c884 DH |
117 | static struct afs_call *afs_alloc_call(struct afs_net *net, |
118 | const struct afs_call_type *type, | |
341f741f | 119 | gfp_t gfp) |
00d3b7a4 | 120 | { |
341f741f DH |
121 | struct afs_call *call; |
122 | int o; | |
00d3b7a4 | 123 | |
341f741f DH |
124 | call = kzalloc(sizeof(*call), gfp); |
125 | if (!call) | |
126 | return NULL; | |
00d3b7a4 | 127 | |
341f741f | 128 | call->type = type; |
f044c884 | 129 | call->net = net; |
341f741f DH |
130 | atomic_set(&call->usage, 1); |
131 | INIT_WORK(&call->async_work, afs_process_async_call); | |
132 | init_waitqueue_head(&call->waitq); | |
2f02f7ae | 133 | |
f044c884 | 134 | o = atomic_inc_return(&net->nr_outstanding_calls); |
341f741f DH |
135 | trace_afs_call(call, afs_call_trace_alloc, 1, o, |
136 | __builtin_return_address(0)); | |
137 | return call; | |
00d3b7a4 DH |
138 | } |
139 | ||
6c67c7c3 | 140 | /* |
341f741f | 141 | * Dispose of a reference on a call. |
6c67c7c3 | 142 | */ |
341f741f | 143 | void afs_put_call(struct afs_call *call) |
6c67c7c3 | 144 | { |
f044c884 | 145 | struct afs_net *net = call->net; |
341f741f | 146 | int n = atomic_dec_return(&call->usage); |
f044c884 | 147 | int o = atomic_read(&net->nr_outstanding_calls); |
341f741f DH |
148 | |
149 | trace_afs_call(call, afs_call_trace_put, n + 1, o, | |
150 | __builtin_return_address(0)); | |
151 | ||
152 | ASSERTCMP(n, >=, 0); | |
153 | if (n == 0) { | |
154 | ASSERT(!work_pending(&call->async_work)); | |
155 | ASSERT(call->type->name != NULL); | |
156 | ||
157 | if (call->rxcall) { | |
f044c884 | 158 | rxrpc_kernel_end_call(net->socket, call->rxcall); |
341f741f DH |
159 | call->rxcall = NULL; |
160 | } | |
161 | if (call->type->destructor) | |
162 | call->type->destructor(call); | |
163 | ||
d0676a16 | 164 | afs_put_server(call->net, call->cm_server); |
341f741f DH |
165 | kfree(call->request); |
166 | kfree(call); | |
167 | ||
f044c884 | 168 | o = atomic_dec_return(&net->nr_outstanding_calls); |
341f741f DH |
169 | trace_afs_call(call, afs_call_trace_free, 0, o, |
170 | __builtin_return_address(0)); | |
171 | if (o == 0) | |
f044c884 | 172 | wake_up_atomic_t(&net->nr_outstanding_calls); |
6c67c7c3 | 173 | } |
6cf12869 NWF |
174 | } |
175 | ||
176 | /* | |
341f741f | 177 | * Queue the call for actual work. Returns 0 unconditionally for convenience. |
6cf12869 | 178 | */ |
341f741f | 179 | int afs_queue_call_work(struct afs_call *call) |
6cf12869 | 180 | { |
341f741f DH |
181 | int u = atomic_inc_return(&call->usage); |
182 | ||
183 | trace_afs_call(call, afs_call_trace_work, u, | |
f044c884 | 184 | atomic_read(&call->net->nr_outstanding_calls), |
341f741f DH |
185 | __builtin_return_address(0)); |
186 | ||
187 | INIT_WORK(&call->work, call->type->work); | |
188 | ||
189 | if (!queue_work(afs_wq, &call->work)) | |
190 | afs_put_call(call); | |
191 | return 0; | |
6c67c7c3 DH |
192 | } |
193 | ||
08e0e7c8 DH |
194 | /* |
195 | * allocate a call with flat request and reply buffers | |
196 | */ | |
f044c884 DH |
197 | struct afs_call *afs_alloc_flat_call(struct afs_net *net, |
198 | const struct afs_call_type *type, | |
d001648e | 199 | size_t request_size, size_t reply_max) |
08e0e7c8 DH |
200 | { |
201 | struct afs_call *call; | |
202 | ||
f044c884 | 203 | call = afs_alloc_call(net, type, GFP_NOFS); |
08e0e7c8 DH |
204 | if (!call) |
205 | goto nomem_call; | |
206 | ||
207 | if (request_size) { | |
341f741f | 208 | call->request_size = request_size; |
08e0e7c8 DH |
209 | call->request = kmalloc(request_size, GFP_NOFS); |
210 | if (!call->request) | |
00d3b7a4 | 211 | goto nomem_free; |
08e0e7c8 DH |
212 | } |
213 | ||
d001648e | 214 | if (reply_max) { |
341f741f | 215 | call->reply_max = reply_max; |
d001648e | 216 | call->buffer = kmalloc(reply_max, GFP_NOFS); |
08e0e7c8 | 217 | if (!call->buffer) |
00d3b7a4 | 218 | goto nomem_free; |
08e0e7c8 DH |
219 | } |
220 | ||
08e0e7c8 | 221 | init_waitqueue_head(&call->waitq); |
08e0e7c8 DH |
222 | return call; |
223 | ||
00d3b7a4 | 224 | nomem_free: |
341f741f | 225 | afs_put_call(call); |
08e0e7c8 DH |
226 | nomem_call: |
227 | return NULL; | |
228 | } | |
229 | ||
230 | /* | |
231 | * clean up a call with flat buffer | |
232 | */ | |
233 | void afs_flat_call_destructor(struct afs_call *call) | |
234 | { | |
235 | _enter(""); | |
236 | ||
237 | kfree(call->request); | |
238 | call->request = NULL; | |
239 | kfree(call->buffer); | |
240 | call->buffer = NULL; | |
241 | } | |
242 | ||
2f5705a5 DH |
243 | #define AFS_BVEC_MAX 8 |
244 | ||
245 | /* | |
246 | * Load the given bvec with the next few pages. | |
247 | */ | |
248 | static void afs_load_bvec(struct afs_call *call, struct msghdr *msg, | |
249 | struct bio_vec *bv, pgoff_t first, pgoff_t last, | |
250 | unsigned offset) | |
251 | { | |
252 | struct page *pages[AFS_BVEC_MAX]; | |
253 | unsigned int nr, n, i, to, bytes = 0; | |
254 | ||
255 | nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX); | |
256 | n = find_get_pages_contig(call->mapping, first, nr, pages); | |
257 | ASSERTCMP(n, ==, nr); | |
258 | ||
259 | msg->msg_flags |= MSG_MORE; | |
260 | for (i = 0; i < nr; i++) { | |
261 | to = PAGE_SIZE; | |
262 | if (first + i >= last) { | |
263 | to = call->last_to; | |
264 | msg->msg_flags &= ~MSG_MORE; | |
265 | } | |
266 | bv[i].bv_page = pages[i]; | |
267 | bv[i].bv_len = to - offset; | |
268 | bv[i].bv_offset = offset; | |
269 | bytes += to - offset; | |
270 | offset = 0; | |
271 | } | |
272 | ||
273 | iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes); | |
274 | } | |
275 | ||
e833251a DH |
276 | /* |
277 | * Advance the AFS call state when the RxRPC call ends the transmit phase. | |
278 | */ | |
279 | static void afs_notify_end_request_tx(struct sock *sock, | |
280 | struct rxrpc_call *rxcall, | |
281 | unsigned long call_user_ID) | |
282 | { | |
283 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
284 | ||
285 | if (call->state == AFS_CALL_REQUESTING) | |
286 | call->state = AFS_CALL_AWAIT_REPLY; | |
287 | } | |
288 | ||
31143d5d DH |
289 | /* |
290 | * attach the data from a bunch of pages on an inode to a call | |
291 | */ | |
39c6acea | 292 | static int afs_send_pages(struct afs_call *call, struct msghdr *msg) |
31143d5d | 293 | { |
2f5705a5 DH |
294 | struct bio_vec bv[AFS_BVEC_MAX]; |
295 | unsigned int bytes, nr, loop, offset; | |
31143d5d DH |
296 | pgoff_t first = call->first, last = call->last; |
297 | int ret; | |
298 | ||
31143d5d DH |
299 | offset = call->first_offset; |
300 | call->first_offset = 0; | |
301 | ||
302 | do { | |
2f5705a5 DH |
303 | afs_load_bvec(call, msg, bv, first, last, offset); |
304 | offset = 0; | |
305 | bytes = msg->msg_iter.count; | |
306 | nr = msg->msg_iter.nr_segs; | |
307 | ||
f044c884 | 308 | ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg, |
e833251a | 309 | bytes, afs_notify_end_request_tx); |
2f5705a5 DH |
310 | for (loop = 0; loop < nr; loop++) |
311 | put_page(bv[loop].bv_page); | |
31143d5d DH |
312 | if (ret < 0) |
313 | break; | |
2f5705a5 DH |
314 | |
315 | first += nr; | |
5bbf5d39 | 316 | } while (first <= last); |
31143d5d | 317 | |
31143d5d DH |
318 | return ret; |
319 | } | |
320 | ||
08e0e7c8 DH |
321 | /* |
322 | * initiate a call | |
323 | */ | |
8b2a464c | 324 | long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, |
33cd7f2b | 325 | gfp_t gfp, bool async) |
08e0e7c8 | 326 | { |
8b2a464c | 327 | struct sockaddr_rxrpc *srx = ac->addr; |
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 | 419 | rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL, |
33cd7f2b | 420 | 0, &offset, false, &call->abort_code, |
f044c884 | 421 | &call->service_id); |
33cd7f2b | 422 | ret = afs_abort_to_error(call->abort_code); |
70af0e3b | 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 | 472 | case -ECONNABORTED: |
33cd7f2b | 473 | goto save_error; |
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 DH |
505 | call->error = ret; |
506 | call->state = AFS_CALL_COMPLETE; | |
507 | goto done; | |
08e0e7c8 DH |
508 | } |
509 | ||
510 | /* | |
511 | * wait synchronously for a call to complete | |
512 | */ | |
33cd7f2b | 513 | static long afs_wait_for_call_to_complete(struct afs_call *call) |
08e0e7c8 | 514 | { |
bc5e3a54 | 515 | signed long rtt2, timeout; |
33cd7f2b | 516 | long ret; |
bc5e3a54 DH |
517 | u64 rtt; |
518 | u32 life, last_life; | |
08e0e7c8 DH |
519 | |
520 | DECLARE_WAITQUEUE(myself, current); | |
521 | ||
522 | _enter(""); | |
523 | ||
f044c884 | 524 | rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall); |
bc5e3a54 DH |
525 | rtt2 = nsecs_to_jiffies64(rtt) * 2; |
526 | if (rtt2 < 2) | |
527 | rtt2 = 2; | |
528 | ||
529 | timeout = rtt2; | |
f044c884 | 530 | last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
bc5e3a54 | 531 | |
08e0e7c8 DH |
532 | add_wait_queue(&call->waitq, &myself); |
533 | for (;;) { | |
bc5e3a54 | 534 | set_current_state(TASK_UNINTERRUPTIBLE); |
08e0e7c8 DH |
535 | |
536 | /* deliver any messages that are in the queue */ | |
d001648e DH |
537 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
538 | call->need_attention = false; | |
08e0e7c8 DH |
539 | __set_current_state(TASK_RUNNING); |
540 | afs_deliver_to_call(call); | |
541 | continue; | |
542 | } | |
543 | ||
bc5e3a54 | 544 | if (call->state == AFS_CALL_COMPLETE) |
08e0e7c8 | 545 | break; |
bc5e3a54 | 546 | |
f044c884 | 547 | life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
bc5e3a54 DH |
548 | if (timeout == 0 && |
549 | life == last_life && signal_pending(current)) | |
550 | break; | |
551 | ||
552 | if (life != last_life) { | |
553 | timeout = rtt2; | |
554 | last_life = life; | |
555 | } | |
556 | ||
557 | timeout = schedule_timeout(timeout); | |
08e0e7c8 DH |
558 | } |
559 | ||
560 | remove_wait_queue(&call->waitq, &myself); | |
561 | __set_current_state(TASK_RUNNING); | |
562 | ||
954cd6dc | 563 | /* Kill off the call if it's still live. */ |
08e0e7c8 | 564 | if (call->state < AFS_CALL_COMPLETE) { |
954cd6dc | 565 | _debug("call interrupted"); |
f044c884 | 566 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
954cd6dc | 567 | RX_USER_ABORT, -EINTR, "KWI"); |
08e0e7c8 DH |
568 | } |
569 | ||
954cd6dc | 570 | ret = call->error; |
33cd7f2b DH |
571 | if (ret < 0) { |
572 | ret = afs_abort_to_error(call->abort_code); | |
573 | } else if (ret == 0 && call->ret_reply0) { | |
574 | ret = (long)call->reply[0]; | |
575 | call->reply[0] = NULL; | |
576 | } | |
577 | ||
08e0e7c8 | 578 | _debug("call complete"); |
341f741f | 579 | afs_put_call(call); |
33cd7f2b | 580 | _leave(" = %p", (void *)ret); |
08e0e7c8 DH |
581 | return ret; |
582 | } | |
583 | ||
584 | /* | |
585 | * wake up a waiting call | |
586 | */ | |
d001648e DH |
587 | static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, |
588 | unsigned long call_user_ID) | |
08e0e7c8 | 589 | { |
d001648e DH |
590 | struct afs_call *call = (struct afs_call *)call_user_ID; |
591 | ||
592 | call->need_attention = true; | |
08e0e7c8 DH |
593 | wake_up(&call->waitq); |
594 | } | |
595 | ||
596 | /* | |
597 | * wake up an asynchronous call | |
598 | */ | |
d001648e DH |
599 | static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, |
600 | unsigned long call_user_ID) | |
08e0e7c8 | 601 | { |
d001648e | 602 | struct afs_call *call = (struct afs_call *)call_user_ID; |
341f741f | 603 | int u; |
d001648e | 604 | |
8e8d7f13 | 605 | trace_afs_notify_call(rxcall, call); |
d001648e | 606 | call->need_attention = true; |
341f741f DH |
607 | |
608 | u = __atomic_add_unless(&call->usage, 1, 0); | |
609 | if (u != 0) { | |
610 | trace_afs_call(call, afs_call_trace_wake, u, | |
f044c884 | 611 | atomic_read(&call->net->nr_outstanding_calls), |
341f741f DH |
612 | __builtin_return_address(0)); |
613 | ||
614 | if (!queue_work(afs_async_calls, &call->async_work)) | |
615 | afs_put_call(call); | |
616 | } | |
08e0e7c8 DH |
617 | } |
618 | ||
08e0e7c8 | 619 | /* |
341f741f DH |
620 | * Delete an asynchronous call. The work item carries a ref to the call struct |
621 | * that we need to release. | |
08e0e7c8 | 622 | */ |
d001648e | 623 | static void afs_delete_async_call(struct work_struct *work) |
08e0e7c8 | 624 | { |
d001648e DH |
625 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
626 | ||
08e0e7c8 DH |
627 | _enter(""); |
628 | ||
341f741f | 629 | afs_put_call(call); |
08e0e7c8 DH |
630 | |
631 | _leave(""); | |
632 | } | |
633 | ||
634 | /* | |
341f741f DH |
635 | * Perform I/O processing on an asynchronous call. The work item carries a ref |
636 | * to the call struct that we either need to release or to pass on. | |
08e0e7c8 | 637 | */ |
d001648e | 638 | static void afs_process_async_call(struct work_struct *work) |
08e0e7c8 | 639 | { |
d001648e DH |
640 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
641 | ||
08e0e7c8 DH |
642 | _enter(""); |
643 | ||
d001648e DH |
644 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
645 | call->need_attention = false; | |
08e0e7c8 | 646 | afs_deliver_to_call(call); |
d001648e | 647 | } |
08e0e7c8 | 648 | |
56ff9c83 | 649 | if (call->state == AFS_CALL_COMPLETE) { |
97e3043a | 650 | call->reply[0] = NULL; |
08e0e7c8 | 651 | |
341f741f DH |
652 | /* We have two refs to release - one from the alloc and one |
653 | * queued with the work item - and we can't just deallocate the | |
654 | * call because the work item may be queued again. | |
655 | */ | |
d001648e | 656 | call->async_work.func = afs_delete_async_call; |
341f741f DH |
657 | if (!queue_work(afs_async_calls, &call->async_work)) |
658 | afs_put_call(call); | |
08e0e7c8 DH |
659 | } |
660 | ||
341f741f | 661 | afs_put_call(call); |
08e0e7c8 DH |
662 | _leave(""); |
663 | } | |
664 | ||
00e90712 DH |
665 | static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) |
666 | { | |
667 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
668 | ||
669 | call->rxcall = rxcall; | |
670 | } | |
671 | ||
672 | /* | |
673 | * Charge the incoming call preallocation. | |
674 | */ | |
f044c884 | 675 | void afs_charge_preallocation(struct work_struct *work) |
00e90712 | 676 | { |
f044c884 DH |
677 | struct afs_net *net = |
678 | container_of(work, struct afs_net, charge_preallocation_work); | |
679 | struct afs_call *call = net->spare_incoming_call; | |
00e90712 DH |
680 | |
681 | for (;;) { | |
682 | if (!call) { | |
f044c884 | 683 | call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); |
00e90712 DH |
684 | if (!call) |
685 | break; | |
686 | ||
56ff9c83 | 687 | call->async = true; |
00e90712 | 688 | call->state = AFS_CALL_AWAIT_OP_ID; |
56ff9c83 | 689 | init_waitqueue_head(&call->waitq); |
00e90712 DH |
690 | } |
691 | ||
f044c884 | 692 | if (rxrpc_kernel_charge_accept(net->socket, |
00e90712 DH |
693 | afs_wake_up_async_call, |
694 | afs_rx_attach, | |
695 | (unsigned long)call, | |
696 | GFP_KERNEL) < 0) | |
697 | break; | |
698 | call = NULL; | |
699 | } | |
f044c884 | 700 | net->spare_incoming_call = call; |
00e90712 DH |
701 | } |
702 | ||
703 | /* | |
704 | * Discard a preallocated call when a socket is shut down. | |
705 | */ | |
706 | static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, | |
707 | unsigned long user_call_ID) | |
708 | { | |
709 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
710 | ||
00e90712 | 711 | call->rxcall = NULL; |
341f741f | 712 | afs_put_call(call); |
00e90712 DH |
713 | } |
714 | ||
d001648e DH |
715 | /* |
716 | * Notification of an incoming call. | |
717 | */ | |
00e90712 DH |
718 | static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, |
719 | unsigned long user_call_ID) | |
d001648e | 720 | { |
f044c884 DH |
721 | struct afs_net *net = afs_sock2net(sk); |
722 | ||
723 | queue_work(afs_wq, &net->charge_preallocation_work); | |
d001648e DH |
724 | } |
725 | ||
08e0e7c8 | 726 | /* |
372ee163 DH |
727 | * Grab the operation ID from an incoming cache manager call. The socket |
728 | * buffer is discarded on error or if we don't yet have sufficient data. | |
08e0e7c8 | 729 | */ |
d001648e | 730 | static int afs_deliver_cm_op_id(struct afs_call *call) |
08e0e7c8 | 731 | { |
d001648e | 732 | int ret; |
08e0e7c8 | 733 | |
d001648e | 734 | _enter("{%zu}", call->offset); |
08e0e7c8 DH |
735 | |
736 | ASSERTCMP(call->offset, <, 4); | |
737 | ||
738 | /* the operation ID forms the first four bytes of the request data */ | |
50a2c953 | 739 | ret = afs_extract_data(call, &call->tmp, 4, true); |
d001648e DH |
740 | if (ret < 0) |
741 | return ret; | |
08e0e7c8 | 742 | |
50a2c953 | 743 | call->operation_ID = ntohl(call->tmp); |
08e0e7c8 | 744 | call->state = AFS_CALL_AWAIT_REQUEST; |
d001648e | 745 | call->offset = 0; |
08e0e7c8 DH |
746 | |
747 | /* ask the cache manager to route the call (it'll change the call type | |
748 | * if successful) */ | |
749 | if (!afs_cm_incoming_call(call)) | |
750 | return -ENOTSUPP; | |
751 | ||
8e8d7f13 DH |
752 | trace_afs_cb_call(call); |
753 | ||
08e0e7c8 DH |
754 | /* pass responsibility for the remainer of this message off to the |
755 | * cache manager op */ | |
d001648e | 756 | return call->type->deliver(call); |
08e0e7c8 DH |
757 | } |
758 | ||
e833251a DH |
759 | /* |
760 | * Advance the AFS call state when an RxRPC service call ends the transmit | |
761 | * phase. | |
762 | */ | |
763 | static void afs_notify_end_reply_tx(struct sock *sock, | |
764 | struct rxrpc_call *rxcall, | |
765 | unsigned long call_user_ID) | |
766 | { | |
767 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
768 | ||
769 | if (call->state == AFS_CALL_REPLYING) | |
770 | call->state = AFS_CALL_AWAIT_ACK; | |
771 | } | |
772 | ||
08e0e7c8 DH |
773 | /* |
774 | * send an empty reply | |
775 | */ | |
776 | void afs_send_empty_reply(struct afs_call *call) | |
777 | { | |
f044c884 | 778 | struct afs_net *net = call->net; |
08e0e7c8 | 779 | struct msghdr msg; |
08e0e7c8 DH |
780 | |
781 | _enter(""); | |
782 | ||
f044c884 | 783 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); |
e754eba6 | 784 | |
08e0e7c8 DH |
785 | msg.msg_name = NULL; |
786 | msg.msg_namelen = 0; | |
bfd4e956 | 787 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0); |
08e0e7c8 DH |
788 | msg.msg_control = NULL; |
789 | msg.msg_controllen = 0; | |
790 | msg.msg_flags = 0; | |
791 | ||
792 | call->state = AFS_CALL_AWAIT_ACK; | |
f044c884 | 793 | switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, |
e833251a | 794 | afs_notify_end_reply_tx)) { |
08e0e7c8 DH |
795 | case 0: |
796 | _leave(" [replied]"); | |
797 | return; | |
798 | ||
799 | case -ENOMEM: | |
800 | _debug("oom"); | |
f044c884 | 801 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 802 | RX_USER_ABORT, -ENOMEM, "KOO"); |
08e0e7c8 | 803 | default: |
08e0e7c8 DH |
804 | _leave(" [error]"); |
805 | return; | |
806 | } | |
807 | } | |
808 | ||
b908fe6b DH |
809 | /* |
810 | * send a simple reply | |
811 | */ | |
812 | void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) | |
813 | { | |
f044c884 | 814 | struct afs_net *net = call->net; |
b908fe6b | 815 | struct msghdr msg; |
2e90b1c4 | 816 | struct kvec iov[1]; |
bd6dc742 | 817 | int n; |
b908fe6b DH |
818 | |
819 | _enter(""); | |
820 | ||
f044c884 | 821 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); |
e754eba6 | 822 | |
b908fe6b DH |
823 | iov[0].iov_base = (void *) buf; |
824 | iov[0].iov_len = len; | |
825 | msg.msg_name = NULL; | |
826 | msg.msg_namelen = 0; | |
2e90b1c4 | 827 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len); |
b908fe6b DH |
828 | msg.msg_control = NULL; |
829 | msg.msg_controllen = 0; | |
830 | msg.msg_flags = 0; | |
831 | ||
832 | call->state = AFS_CALL_AWAIT_ACK; | |
f044c884 | 833 | n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, |
e833251a | 834 | afs_notify_end_reply_tx); |
bd6dc742 | 835 | if (n >= 0) { |
6c67c7c3 | 836 | /* Success */ |
b908fe6b DH |
837 | _leave(" [replied]"); |
838 | return; | |
bd6dc742 | 839 | } |
6c67c7c3 | 840 | |
bd6dc742 | 841 | if (n == -ENOMEM) { |
b908fe6b | 842 | _debug("oom"); |
f044c884 | 843 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 844 | RX_USER_ABORT, -ENOMEM, "KOO"); |
b908fe6b | 845 | } |
bd6dc742 | 846 | _leave(" [error]"); |
b908fe6b DH |
847 | } |
848 | ||
08e0e7c8 | 849 | /* |
372ee163 | 850 | * Extract a piece of data from the received data socket buffers. |
08e0e7c8 | 851 | */ |
d001648e DH |
852 | int afs_extract_data(struct afs_call *call, void *buf, size_t count, |
853 | bool want_more) | |
08e0e7c8 | 854 | { |
f044c884 | 855 | struct afs_net *net = call->net; |
d001648e | 856 | int ret; |
08e0e7c8 | 857 | |
d001648e DH |
858 | _enter("{%s,%zu},,%zu,%d", |
859 | call->type->name, call->offset, count, want_more); | |
08e0e7c8 | 860 | |
d001648e | 861 | ASSERTCMP(call->offset, <=, count); |
08e0e7c8 | 862 | |
f044c884 | 863 | ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, |
d001648e | 864 | buf, count, &call->offset, |
a68f4a27 DH |
865 | want_more, &call->abort_code, |
866 | &call->service_id); | |
8e8d7f13 | 867 | trace_afs_recv_data(call, count, call->offset, want_more, ret); |
d001648e DH |
868 | if (ret == 0 || ret == -EAGAIN) |
869 | return ret; | |
08e0e7c8 | 870 | |
d001648e DH |
871 | if (ret == 1) { |
872 | switch (call->state) { | |
873 | case AFS_CALL_AWAIT_REPLY: | |
874 | call->state = AFS_CALL_COMPLETE; | |
875 | break; | |
876 | case AFS_CALL_AWAIT_REQUEST: | |
877 | call->state = AFS_CALL_REPLYING; | |
878 | break; | |
879 | default: | |
880 | break; | |
881 | } | |
882 | return 0; | |
08e0e7c8 | 883 | } |
d001648e DH |
884 | |
885 | if (ret == -ECONNABORTED) | |
f780c8ea | 886 | call->error = afs_abort_to_error(call->abort_code); |
d001648e DH |
887 | else |
888 | call->error = ret; | |
889 | call->state = AFS_CALL_COMPLETE; | |
890 | return ret; | |
08e0e7c8 | 891 | } |