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