]>
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; | |
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); |
d2ddc776 | 165 | afs_put_cb_interest(call->net, call->cbi); |
341f741f DH |
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 | ||
025db80c | 222 | call->operation_ID = type->op; |
08e0e7c8 | 223 | init_waitqueue_head(&call->waitq); |
08e0e7c8 DH |
224 | return call; |
225 | ||
00d3b7a4 | 226 | nomem_free: |
341f741f | 227 | afs_put_call(call); |
08e0e7c8 DH |
228 | nomem_call: |
229 | return NULL; | |
230 | } | |
231 | ||
232 | /* | |
233 | * clean up a call with flat buffer | |
234 | */ | |
235 | void afs_flat_call_destructor(struct afs_call *call) | |
236 | { | |
237 | _enter(""); | |
238 | ||
239 | kfree(call->request); | |
240 | call->request = NULL; | |
241 | kfree(call->buffer); | |
242 | call->buffer = NULL; | |
243 | } | |
244 | ||
2f5705a5 DH |
245 | #define AFS_BVEC_MAX 8 |
246 | ||
247 | /* | |
248 | * Load the given bvec with the next few pages. | |
249 | */ | |
250 | static void afs_load_bvec(struct afs_call *call, struct msghdr *msg, | |
251 | struct bio_vec *bv, pgoff_t first, pgoff_t last, | |
252 | unsigned offset) | |
253 | { | |
254 | struct page *pages[AFS_BVEC_MAX]; | |
255 | unsigned int nr, n, i, to, bytes = 0; | |
256 | ||
257 | nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX); | |
258 | n = find_get_pages_contig(call->mapping, first, nr, pages); | |
259 | ASSERTCMP(n, ==, nr); | |
260 | ||
261 | msg->msg_flags |= MSG_MORE; | |
262 | for (i = 0; i < nr; i++) { | |
263 | to = PAGE_SIZE; | |
264 | if (first + i >= last) { | |
265 | to = call->last_to; | |
266 | msg->msg_flags &= ~MSG_MORE; | |
267 | } | |
268 | bv[i].bv_page = pages[i]; | |
269 | bv[i].bv_len = to - offset; | |
270 | bv[i].bv_offset = offset; | |
271 | bytes += to - offset; | |
272 | offset = 0; | |
273 | } | |
274 | ||
275 | iov_iter_bvec(&msg->msg_iter, WRITE | ITER_BVEC, bv, nr, bytes); | |
276 | } | |
277 | ||
e833251a DH |
278 | /* |
279 | * Advance the AFS call state when the RxRPC call ends the transmit phase. | |
280 | */ | |
281 | static void afs_notify_end_request_tx(struct sock *sock, | |
282 | struct rxrpc_call *rxcall, | |
283 | unsigned long call_user_ID) | |
284 | { | |
285 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
286 | ||
287 | if (call->state == AFS_CALL_REQUESTING) | |
288 | call->state = AFS_CALL_AWAIT_REPLY; | |
289 | } | |
290 | ||
31143d5d DH |
291 | /* |
292 | * attach the data from a bunch of pages on an inode to a call | |
293 | */ | |
39c6acea | 294 | static int afs_send_pages(struct afs_call *call, struct msghdr *msg) |
31143d5d | 295 | { |
2f5705a5 DH |
296 | struct bio_vec bv[AFS_BVEC_MAX]; |
297 | unsigned int bytes, nr, loop, offset; | |
31143d5d DH |
298 | pgoff_t first = call->first, last = call->last; |
299 | int ret; | |
300 | ||
31143d5d DH |
301 | offset = call->first_offset; |
302 | call->first_offset = 0; | |
303 | ||
304 | do { | |
2f5705a5 | 305 | afs_load_bvec(call, msg, bv, first, last, offset); |
2c099014 DH |
306 | trace_afs_send_pages(call, msg, first, last, offset); |
307 | ||
2f5705a5 DH |
308 | offset = 0; |
309 | bytes = msg->msg_iter.count; | |
310 | nr = msg->msg_iter.nr_segs; | |
311 | ||
f044c884 | 312 | ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg, |
e833251a | 313 | bytes, afs_notify_end_request_tx); |
2f5705a5 DH |
314 | for (loop = 0; loop < nr; loop++) |
315 | put_page(bv[loop].bv_page); | |
31143d5d DH |
316 | if (ret < 0) |
317 | break; | |
2f5705a5 DH |
318 | |
319 | first += nr; | |
5bbf5d39 | 320 | } while (first <= last); |
31143d5d | 321 | |
2c099014 | 322 | trace_afs_sent_pages(call, call->first, last, first, ret); |
31143d5d DH |
323 | return ret; |
324 | } | |
325 | ||
08e0e7c8 DH |
326 | /* |
327 | * initiate a call | |
328 | */ | |
8b2a464c | 329 | long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, |
33cd7f2b | 330 | gfp_t gfp, bool async) |
08e0e7c8 | 331 | { |
8b2a464c | 332 | struct sockaddr_rxrpc *srx = ac->addr; |
08e0e7c8 DH |
333 | struct rxrpc_call *rxcall; |
334 | struct msghdr msg; | |
335 | struct kvec iov[1]; | |
70af0e3b | 336 | size_t offset; |
e754eba6 | 337 | s64 tx_total_len; |
08e0e7c8 DH |
338 | int ret; |
339 | ||
4d9df986 | 340 | _enter(",{%pISp},", &srx->transport); |
08e0e7c8 | 341 | |
00d3b7a4 DH |
342 | ASSERT(call->type != NULL); |
343 | ASSERT(call->type->name != NULL); | |
344 | ||
31143d5d DH |
345 | _debug("____MAKE %p{%s,%x} [%d]____", |
346 | call, call->type->name, key_serial(call->key), | |
f044c884 | 347 | atomic_read(&call->net->nr_outstanding_calls)); |
00d3b7a4 | 348 | |
56ff9c83 | 349 | call->async = async; |
08e0e7c8 | 350 | |
e754eba6 DH |
351 | /* Work out the length we're going to transmit. This is awkward for |
352 | * calls such as FS.StoreData where there's an extra injection of data | |
353 | * after the initial fixed part. | |
354 | */ | |
355 | tx_total_len = call->request_size; | |
356 | if (call->send_pages) { | |
1199db60 DH |
357 | if (call->last == call->first) { |
358 | tx_total_len += call->last_to - call->first_offset; | |
359 | } else { | |
360 | /* It looks mathematically like you should be able to | |
361 | * combine the following lines with the ones above, but | |
362 | * unsigned arithmetic is fun when it wraps... | |
363 | */ | |
364 | tx_total_len += PAGE_SIZE - call->first_offset; | |
365 | tx_total_len += call->last_to; | |
366 | tx_total_len += (call->last - call->first - 1) * PAGE_SIZE; | |
367 | } | |
e754eba6 DH |
368 | } |
369 | ||
08e0e7c8 | 370 | /* create a call */ |
4d9df986 | 371 | rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key, |
e754eba6 DH |
372 | (unsigned long)call, |
373 | tx_total_len, gfp, | |
56ff9c83 DH |
374 | (async ? |
375 | afs_wake_up_async_call : | |
a68f4a27 DH |
376 | afs_wake_up_call_waiter), |
377 | call->upgrade); | |
08e0e7c8 DH |
378 | if (IS_ERR(rxcall)) { |
379 | ret = PTR_ERR(rxcall); | |
380 | goto error_kill_call; | |
381 | } | |
382 | ||
383 | call->rxcall = rxcall; | |
384 | ||
385 | /* send the request */ | |
386 | iov[0].iov_base = call->request; | |
387 | iov[0].iov_len = call->request_size; | |
388 | ||
389 | msg.msg_name = NULL; | |
390 | msg.msg_namelen = 0; | |
2e90b1c4 | 391 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, |
c0371da6 | 392 | call->request_size); |
08e0e7c8 DH |
393 | msg.msg_control = NULL; |
394 | msg.msg_controllen = 0; | |
bc5e3a54 | 395 | msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0); |
08e0e7c8 | 396 | |
f044c884 | 397 | ret = rxrpc_kernel_send_data(call->net->socket, rxcall, |
e833251a DH |
398 | &msg, call->request_size, |
399 | afs_notify_end_request_tx); | |
08e0e7c8 DH |
400 | if (ret < 0) |
401 | goto error_do_abort; | |
402 | ||
31143d5d | 403 | if (call->send_pages) { |
39c6acea | 404 | ret = afs_send_pages(call, &msg); |
31143d5d DH |
405 | if (ret < 0) |
406 | goto error_do_abort; | |
407 | } | |
408 | ||
08e0e7c8 DH |
409 | /* at this point, an async call may no longer exist as it may have |
410 | * already completed */ | |
56ff9c83 DH |
411 | if (call->async) |
412 | return -EINPROGRESS; | |
413 | ||
d2ddc776 | 414 | return afs_wait_for_call_to_complete(call, ac); |
08e0e7c8 DH |
415 | |
416 | error_do_abort: | |
70af0e3b DH |
417 | call->state = AFS_CALL_COMPLETE; |
418 | if (ret != -ECONNABORTED) { | |
f044c884 DH |
419 | rxrpc_kernel_abort_call(call->net->socket, rxcall, |
420 | RX_USER_ABORT, ret, "KSD"); | |
70af0e3b | 421 | } else { |
70af0e3b | 422 | offset = 0; |
f044c884 | 423 | rxrpc_kernel_recv_data(call->net->socket, rxcall, NULL, |
33cd7f2b | 424 | 0, &offset, false, &call->abort_code, |
f044c884 | 425 | &call->service_id); |
d2ddc776 DH |
426 | ac->abort_code = call->abort_code; |
427 | ac->responded = true; | |
70af0e3b | 428 | } |
025db80c DH |
429 | call->error = ret; |
430 | trace_afs_call_done(call); | |
08e0e7c8 | 431 | error_kill_call: |
341f741f | 432 | afs_put_call(call); |
d2ddc776 | 433 | ac->error = ret; |
08e0e7c8 DH |
434 | _leave(" = %d", ret); |
435 | return ret; | |
436 | } | |
437 | ||
08e0e7c8 DH |
438 | /* |
439 | * deliver messages to a call | |
440 | */ | |
441 | static void afs_deliver_to_call(struct afs_call *call) | |
442 | { | |
08e0e7c8 DH |
443 | u32 abort_code; |
444 | int ret; | |
445 | ||
d001648e DH |
446 | _enter("%s", call->type->name); |
447 | ||
448 | while (call->state == AFS_CALL_AWAIT_REPLY || | |
449 | call->state == AFS_CALL_AWAIT_OP_ID || | |
450 | call->state == AFS_CALL_AWAIT_REQUEST || | |
451 | call->state == AFS_CALL_AWAIT_ACK | |
452 | ) { | |
453 | if (call->state == AFS_CALL_AWAIT_ACK) { | |
454 | size_t offset = 0; | |
f044c884 DH |
455 | ret = rxrpc_kernel_recv_data(call->net->socket, |
456 | call->rxcall, | |
d001648e | 457 | NULL, 0, &offset, false, |
a68f4a27 DH |
458 | &call->abort_code, |
459 | &call->service_id); | |
8e8d7f13 DH |
460 | trace_afs_recv_data(call, 0, offset, false, ret); |
461 | ||
d001648e DH |
462 | if (ret == -EINPROGRESS || ret == -EAGAIN) |
463 | return; | |
025db80c DH |
464 | if (ret < 0) |
465 | call->error = ret; | |
466 | if (ret < 0 || ret == 1) | |
467 | goto call_complete; | |
d001648e | 468 | return; |
08e0e7c8 DH |
469 | } |
470 | ||
d001648e DH |
471 | ret = call->type->deliver(call); |
472 | switch (ret) { | |
473 | case 0: | |
474 | if (call->state == AFS_CALL_AWAIT_REPLY) | |
025db80c | 475 | goto call_complete; |
d001648e DH |
476 | goto done; |
477 | case -EINPROGRESS: | |
478 | case -EAGAIN: | |
479 | goto out; | |
70af0e3b | 480 | case -ECONNABORTED: |
33cd7f2b | 481 | goto save_error; |
d001648e DH |
482 | case -ENOTCONN: |
483 | abort_code = RX_CALL_DEAD; | |
f044c884 | 484 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
3a92789a | 485 | abort_code, ret, "KNC"); |
70af0e3b | 486 | goto save_error; |
d001648e | 487 | case -ENOTSUPP: |
1157f153 | 488 | abort_code = RXGEN_OPCODE; |
f044c884 | 489 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
3a92789a | 490 | abort_code, ret, "KIV"); |
70af0e3b | 491 | goto save_error; |
d001648e DH |
492 | case -ENODATA: |
493 | case -EBADMSG: | |
494 | case -EMSGSIZE: | |
495 | default: | |
496 | abort_code = RXGEN_CC_UNMARSHAL; | |
497 | if (call->state != AFS_CALL_AWAIT_REPLY) | |
498 | abort_code = RXGEN_SS_UNMARSHAL; | |
f044c884 | 499 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
3a92789a | 500 | abort_code, -EBADMSG, "KUM"); |
70af0e3b | 501 | goto save_error; |
d001648e | 502 | } |
08e0e7c8 DH |
503 | } |
504 | ||
d001648e DH |
505 | done: |
506 | if (call->state == AFS_CALL_COMPLETE && call->incoming) | |
341f741f | 507 | afs_put_call(call); |
d001648e | 508 | out: |
08e0e7c8 | 509 | _leave(""); |
d001648e DH |
510 | return; |
511 | ||
70af0e3b | 512 | save_error: |
d001648e | 513 | call->error = ret; |
025db80c DH |
514 | call_complete: |
515 | if (call->state != AFS_CALL_COMPLETE) { | |
516 | call->state = AFS_CALL_COMPLETE; | |
517 | trace_afs_call_done(call); | |
518 | } | |
d001648e | 519 | goto done; |
08e0e7c8 DH |
520 | } |
521 | ||
522 | /* | |
523 | * wait synchronously for a call to complete | |
524 | */ | |
d2ddc776 DH |
525 | static long afs_wait_for_call_to_complete(struct afs_call *call, |
526 | struct afs_addr_cursor *ac) | |
08e0e7c8 | 527 | { |
bc5e3a54 | 528 | signed long rtt2, timeout; |
33cd7f2b | 529 | long ret; |
bc5e3a54 DH |
530 | u64 rtt; |
531 | u32 life, last_life; | |
08e0e7c8 DH |
532 | |
533 | DECLARE_WAITQUEUE(myself, current); | |
534 | ||
535 | _enter(""); | |
536 | ||
f044c884 | 537 | rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall); |
bc5e3a54 DH |
538 | rtt2 = nsecs_to_jiffies64(rtt) * 2; |
539 | if (rtt2 < 2) | |
540 | rtt2 = 2; | |
541 | ||
542 | timeout = rtt2; | |
f044c884 | 543 | last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
bc5e3a54 | 544 | |
08e0e7c8 DH |
545 | add_wait_queue(&call->waitq, &myself); |
546 | for (;;) { | |
bc5e3a54 | 547 | set_current_state(TASK_UNINTERRUPTIBLE); |
08e0e7c8 DH |
548 | |
549 | /* deliver any messages that are in the queue */ | |
d001648e DH |
550 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
551 | call->need_attention = false; | |
08e0e7c8 DH |
552 | __set_current_state(TASK_RUNNING); |
553 | afs_deliver_to_call(call); | |
554 | continue; | |
555 | } | |
556 | ||
bc5e3a54 | 557 | if (call->state == AFS_CALL_COMPLETE) |
08e0e7c8 | 558 | break; |
bc5e3a54 | 559 | |
f044c884 | 560 | life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
bc5e3a54 DH |
561 | if (timeout == 0 && |
562 | life == last_life && signal_pending(current)) | |
563 | break; | |
564 | ||
565 | if (life != last_life) { | |
566 | timeout = rtt2; | |
567 | last_life = life; | |
568 | } | |
569 | ||
570 | timeout = schedule_timeout(timeout); | |
08e0e7c8 DH |
571 | } |
572 | ||
573 | remove_wait_queue(&call->waitq, &myself); | |
574 | __set_current_state(TASK_RUNNING); | |
575 | ||
954cd6dc | 576 | /* Kill off the call if it's still live. */ |
08e0e7c8 | 577 | if (call->state < AFS_CALL_COMPLETE) { |
954cd6dc | 578 | _debug("call interrupted"); |
d2ddc776 | 579 | if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
025db80c | 580 | RX_USER_ABORT, -EINTR, "KWI")) { |
d2ddc776 | 581 | call->error = -ERESTARTSYS; |
025db80c DH |
582 | trace_afs_call_done(call); |
583 | } | |
08e0e7c8 DH |
584 | } |
585 | ||
d2ddc776 DH |
586 | ac->abort_code = call->abort_code; |
587 | ac->error = call->error; | |
588 | ||
589 | ret = ac->error; | |
590 | switch (ret) { | |
591 | case 0: | |
592 | if (call->ret_reply0) { | |
593 | ret = (long)call->reply[0]; | |
594 | call->reply[0] = NULL; | |
595 | } | |
596 | /* Fall through */ | |
597 | case -ECONNABORTED: | |
598 | ac->responded = true; | |
599 | break; | |
33cd7f2b DH |
600 | } |
601 | ||
08e0e7c8 | 602 | _debug("call complete"); |
341f741f | 603 | afs_put_call(call); |
33cd7f2b | 604 | _leave(" = %p", (void *)ret); |
08e0e7c8 DH |
605 | return ret; |
606 | } | |
607 | ||
608 | /* | |
609 | * wake up a waiting call | |
610 | */ | |
d001648e DH |
611 | static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, |
612 | unsigned long call_user_ID) | |
08e0e7c8 | 613 | { |
d001648e DH |
614 | struct afs_call *call = (struct afs_call *)call_user_ID; |
615 | ||
616 | call->need_attention = true; | |
08e0e7c8 DH |
617 | wake_up(&call->waitq); |
618 | } | |
619 | ||
620 | /* | |
621 | * wake up an asynchronous call | |
622 | */ | |
d001648e DH |
623 | static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, |
624 | unsigned long call_user_ID) | |
08e0e7c8 | 625 | { |
d001648e | 626 | struct afs_call *call = (struct afs_call *)call_user_ID; |
341f741f | 627 | int u; |
d001648e | 628 | |
8e8d7f13 | 629 | trace_afs_notify_call(rxcall, call); |
d001648e | 630 | call->need_attention = true; |
341f741f DH |
631 | |
632 | u = __atomic_add_unless(&call->usage, 1, 0); | |
633 | if (u != 0) { | |
634 | trace_afs_call(call, afs_call_trace_wake, u, | |
f044c884 | 635 | atomic_read(&call->net->nr_outstanding_calls), |
341f741f DH |
636 | __builtin_return_address(0)); |
637 | ||
638 | if (!queue_work(afs_async_calls, &call->async_work)) | |
639 | afs_put_call(call); | |
640 | } | |
08e0e7c8 DH |
641 | } |
642 | ||
08e0e7c8 | 643 | /* |
341f741f DH |
644 | * Delete an asynchronous call. The work item carries a ref to the call struct |
645 | * that we need to release. | |
08e0e7c8 | 646 | */ |
d001648e | 647 | static void afs_delete_async_call(struct work_struct *work) |
08e0e7c8 | 648 | { |
d001648e DH |
649 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
650 | ||
08e0e7c8 DH |
651 | _enter(""); |
652 | ||
341f741f | 653 | afs_put_call(call); |
08e0e7c8 DH |
654 | |
655 | _leave(""); | |
656 | } | |
657 | ||
658 | /* | |
341f741f DH |
659 | * Perform I/O processing on an asynchronous call. The work item carries a ref |
660 | * to the call struct that we either need to release or to pass on. | |
08e0e7c8 | 661 | */ |
d001648e | 662 | static void afs_process_async_call(struct work_struct *work) |
08e0e7c8 | 663 | { |
d001648e DH |
664 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
665 | ||
08e0e7c8 DH |
666 | _enter(""); |
667 | ||
d001648e DH |
668 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
669 | call->need_attention = false; | |
08e0e7c8 | 670 | afs_deliver_to_call(call); |
d001648e | 671 | } |
08e0e7c8 | 672 | |
56ff9c83 | 673 | if (call->state == AFS_CALL_COMPLETE) { |
97e3043a | 674 | call->reply[0] = NULL; |
08e0e7c8 | 675 | |
341f741f DH |
676 | /* We have two refs to release - one from the alloc and one |
677 | * queued with the work item - and we can't just deallocate the | |
678 | * call because the work item may be queued again. | |
679 | */ | |
d001648e | 680 | call->async_work.func = afs_delete_async_call; |
341f741f DH |
681 | if (!queue_work(afs_async_calls, &call->async_work)) |
682 | afs_put_call(call); | |
08e0e7c8 DH |
683 | } |
684 | ||
341f741f | 685 | afs_put_call(call); |
08e0e7c8 DH |
686 | _leave(""); |
687 | } | |
688 | ||
00e90712 DH |
689 | static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) |
690 | { | |
691 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
692 | ||
693 | call->rxcall = rxcall; | |
694 | } | |
695 | ||
696 | /* | |
697 | * Charge the incoming call preallocation. | |
698 | */ | |
f044c884 | 699 | void afs_charge_preallocation(struct work_struct *work) |
00e90712 | 700 | { |
f044c884 DH |
701 | struct afs_net *net = |
702 | container_of(work, struct afs_net, charge_preallocation_work); | |
703 | struct afs_call *call = net->spare_incoming_call; | |
00e90712 DH |
704 | |
705 | for (;;) { | |
706 | if (!call) { | |
f044c884 | 707 | call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); |
00e90712 DH |
708 | if (!call) |
709 | break; | |
710 | ||
56ff9c83 | 711 | call->async = true; |
00e90712 | 712 | call->state = AFS_CALL_AWAIT_OP_ID; |
56ff9c83 | 713 | init_waitqueue_head(&call->waitq); |
00e90712 DH |
714 | } |
715 | ||
f044c884 | 716 | if (rxrpc_kernel_charge_accept(net->socket, |
00e90712 DH |
717 | afs_wake_up_async_call, |
718 | afs_rx_attach, | |
719 | (unsigned long)call, | |
720 | GFP_KERNEL) < 0) | |
721 | break; | |
722 | call = NULL; | |
723 | } | |
f044c884 | 724 | net->spare_incoming_call = call; |
00e90712 DH |
725 | } |
726 | ||
727 | /* | |
728 | * Discard a preallocated call when a socket is shut down. | |
729 | */ | |
730 | static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, | |
731 | unsigned long user_call_ID) | |
732 | { | |
733 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
734 | ||
00e90712 | 735 | call->rxcall = NULL; |
341f741f | 736 | afs_put_call(call); |
00e90712 DH |
737 | } |
738 | ||
d001648e DH |
739 | /* |
740 | * Notification of an incoming call. | |
741 | */ | |
00e90712 DH |
742 | static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, |
743 | unsigned long user_call_ID) | |
d001648e | 744 | { |
f044c884 DH |
745 | struct afs_net *net = afs_sock2net(sk); |
746 | ||
747 | queue_work(afs_wq, &net->charge_preallocation_work); | |
d001648e DH |
748 | } |
749 | ||
08e0e7c8 | 750 | /* |
372ee163 DH |
751 | * Grab the operation ID from an incoming cache manager call. The socket |
752 | * buffer is discarded on error or if we don't yet have sufficient data. | |
08e0e7c8 | 753 | */ |
d001648e | 754 | static int afs_deliver_cm_op_id(struct afs_call *call) |
08e0e7c8 | 755 | { |
d001648e | 756 | int ret; |
08e0e7c8 | 757 | |
d001648e | 758 | _enter("{%zu}", call->offset); |
08e0e7c8 DH |
759 | |
760 | ASSERTCMP(call->offset, <, 4); | |
761 | ||
762 | /* the operation ID forms the first four bytes of the request data */ | |
50a2c953 | 763 | ret = afs_extract_data(call, &call->tmp, 4, true); |
d001648e DH |
764 | if (ret < 0) |
765 | return ret; | |
08e0e7c8 | 766 | |
50a2c953 | 767 | call->operation_ID = ntohl(call->tmp); |
08e0e7c8 | 768 | call->state = AFS_CALL_AWAIT_REQUEST; |
d001648e | 769 | call->offset = 0; |
08e0e7c8 DH |
770 | |
771 | /* ask the cache manager to route the call (it'll change the call type | |
772 | * if successful) */ | |
773 | if (!afs_cm_incoming_call(call)) | |
774 | return -ENOTSUPP; | |
775 | ||
8e8d7f13 DH |
776 | trace_afs_cb_call(call); |
777 | ||
08e0e7c8 DH |
778 | /* pass responsibility for the remainer of this message off to the |
779 | * cache manager op */ | |
d001648e | 780 | return call->type->deliver(call); |
08e0e7c8 DH |
781 | } |
782 | ||
e833251a DH |
783 | /* |
784 | * Advance the AFS call state when an RxRPC service call ends the transmit | |
785 | * phase. | |
786 | */ | |
787 | static void afs_notify_end_reply_tx(struct sock *sock, | |
788 | struct rxrpc_call *rxcall, | |
789 | unsigned long call_user_ID) | |
790 | { | |
791 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
792 | ||
793 | if (call->state == AFS_CALL_REPLYING) | |
794 | call->state = AFS_CALL_AWAIT_ACK; | |
795 | } | |
796 | ||
08e0e7c8 DH |
797 | /* |
798 | * send an empty reply | |
799 | */ | |
800 | void afs_send_empty_reply(struct afs_call *call) | |
801 | { | |
f044c884 | 802 | struct afs_net *net = call->net; |
08e0e7c8 | 803 | struct msghdr msg; |
08e0e7c8 DH |
804 | |
805 | _enter(""); | |
806 | ||
f044c884 | 807 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); |
e754eba6 | 808 | |
08e0e7c8 DH |
809 | msg.msg_name = NULL; |
810 | msg.msg_namelen = 0; | |
bfd4e956 | 811 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, NULL, 0, 0); |
08e0e7c8 DH |
812 | msg.msg_control = NULL; |
813 | msg.msg_controllen = 0; | |
814 | msg.msg_flags = 0; | |
815 | ||
f044c884 | 816 | switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, |
e833251a | 817 | afs_notify_end_reply_tx)) { |
08e0e7c8 DH |
818 | case 0: |
819 | _leave(" [replied]"); | |
820 | return; | |
821 | ||
822 | case -ENOMEM: | |
823 | _debug("oom"); | |
f044c884 | 824 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 825 | RX_USER_ABORT, -ENOMEM, "KOO"); |
08e0e7c8 | 826 | default: |
08e0e7c8 DH |
827 | _leave(" [error]"); |
828 | return; | |
829 | } | |
830 | } | |
831 | ||
b908fe6b DH |
832 | /* |
833 | * send a simple reply | |
834 | */ | |
835 | void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) | |
836 | { | |
f044c884 | 837 | struct afs_net *net = call->net; |
b908fe6b | 838 | struct msghdr msg; |
2e90b1c4 | 839 | struct kvec iov[1]; |
bd6dc742 | 840 | int n; |
b908fe6b DH |
841 | |
842 | _enter(""); | |
843 | ||
f044c884 | 844 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); |
e754eba6 | 845 | |
b908fe6b DH |
846 | iov[0].iov_base = (void *) buf; |
847 | iov[0].iov_len = len; | |
848 | msg.msg_name = NULL; | |
849 | msg.msg_namelen = 0; | |
2e90b1c4 | 850 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iov, 1, len); |
b908fe6b DH |
851 | msg.msg_control = NULL; |
852 | msg.msg_controllen = 0; | |
853 | msg.msg_flags = 0; | |
854 | ||
f044c884 | 855 | n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, |
e833251a | 856 | afs_notify_end_reply_tx); |
bd6dc742 | 857 | if (n >= 0) { |
6c67c7c3 | 858 | /* Success */ |
b908fe6b DH |
859 | _leave(" [replied]"); |
860 | return; | |
bd6dc742 | 861 | } |
6c67c7c3 | 862 | |
bd6dc742 | 863 | if (n == -ENOMEM) { |
b908fe6b | 864 | _debug("oom"); |
f044c884 | 865 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 866 | RX_USER_ABORT, -ENOMEM, "KOO"); |
b908fe6b | 867 | } |
bd6dc742 | 868 | _leave(" [error]"); |
b908fe6b DH |
869 | } |
870 | ||
08e0e7c8 | 871 | /* |
372ee163 | 872 | * Extract a piece of data from the received data socket buffers. |
08e0e7c8 | 873 | */ |
d001648e DH |
874 | int afs_extract_data(struct afs_call *call, void *buf, size_t count, |
875 | bool want_more) | |
08e0e7c8 | 876 | { |
f044c884 | 877 | struct afs_net *net = call->net; |
d001648e | 878 | int ret; |
08e0e7c8 | 879 | |
d001648e DH |
880 | _enter("{%s,%zu},,%zu,%d", |
881 | call->type->name, call->offset, count, want_more); | |
08e0e7c8 | 882 | |
d001648e | 883 | ASSERTCMP(call->offset, <=, count); |
08e0e7c8 | 884 | |
f044c884 | 885 | ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, |
d001648e | 886 | buf, count, &call->offset, |
a68f4a27 DH |
887 | want_more, &call->abort_code, |
888 | &call->service_id); | |
8e8d7f13 | 889 | trace_afs_recv_data(call, count, call->offset, want_more, ret); |
d001648e DH |
890 | if (ret == 0 || ret == -EAGAIN) |
891 | return ret; | |
08e0e7c8 | 892 | |
d001648e DH |
893 | if (ret == 1) { |
894 | switch (call->state) { | |
895 | case AFS_CALL_AWAIT_REPLY: | |
896 | call->state = AFS_CALL_COMPLETE; | |
025db80c | 897 | trace_afs_call_done(call); |
d001648e DH |
898 | break; |
899 | case AFS_CALL_AWAIT_REQUEST: | |
900 | call->state = AFS_CALL_REPLYING; | |
901 | break; | |
902 | default: | |
903 | break; | |
904 | } | |
905 | return 0; | |
08e0e7c8 | 906 | } |
d001648e | 907 | |
d2ddc776 | 908 | call->error = ret; |
d001648e | 909 | call->state = AFS_CALL_COMPLETE; |
025db80c | 910 | trace_afs_call_done(call); |
d001648e | 911 | return ret; |
08e0e7c8 | 912 | } |