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