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