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