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