]>
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; |
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 | ||
7a75b007 DH |
206 | static struct afs_call *afs_get_call(struct afs_call *call, |
207 | enum afs_call_trace why) | |
208 | { | |
209 | int u = atomic_inc_return(&call->usage); | |
210 | ||
211 | trace_afs_call(call, why, u, | |
212 | atomic_read(&call->net->nr_outstanding_calls), | |
213 | __builtin_return_address(0)); | |
214 | return call; | |
215 | } | |
216 | ||
6cf12869 | 217 | /* |
3bf0fb6f | 218 | * Queue the call for actual work. |
6cf12869 | 219 | */ |
3bf0fb6f | 220 | static void afs_queue_call_work(struct afs_call *call) |
6cf12869 | 221 | { |
3bf0fb6f | 222 | if (call->type->work) { |
3bf0fb6f | 223 | INIT_WORK(&call->work, call->type->work); |
341f741f | 224 | |
7a75b007 | 225 | afs_get_call(call, afs_call_trace_work); |
3bf0fb6f DH |
226 | if (!queue_work(afs_wq, &call->work)) |
227 | afs_put_call(call); | |
228 | } | |
6c67c7c3 DH |
229 | } |
230 | ||
08e0e7c8 DH |
231 | /* |
232 | * allocate a call with flat request and reply buffers | |
233 | */ | |
f044c884 DH |
234 | struct afs_call *afs_alloc_flat_call(struct afs_net *net, |
235 | const struct afs_call_type *type, | |
d001648e | 236 | size_t request_size, size_t reply_max) |
08e0e7c8 DH |
237 | { |
238 | struct afs_call *call; | |
239 | ||
f044c884 | 240 | call = afs_alloc_call(net, type, GFP_NOFS); |
08e0e7c8 DH |
241 | if (!call) |
242 | goto nomem_call; | |
243 | ||
244 | if (request_size) { | |
341f741f | 245 | call->request_size = request_size; |
08e0e7c8 DH |
246 | call->request = kmalloc(request_size, GFP_NOFS); |
247 | if (!call->request) | |
00d3b7a4 | 248 | goto nomem_free; |
08e0e7c8 DH |
249 | } |
250 | ||
d001648e | 251 | if (reply_max) { |
341f741f | 252 | call->reply_max = reply_max; |
d001648e | 253 | call->buffer = kmalloc(reply_max, GFP_NOFS); |
08e0e7c8 | 254 | if (!call->buffer) |
00d3b7a4 | 255 | goto nomem_free; |
08e0e7c8 DH |
256 | } |
257 | ||
12bdcf33 | 258 | afs_extract_to_buf(call, call->reply_max); |
025db80c | 259 | call->operation_ID = type->op; |
08e0e7c8 | 260 | init_waitqueue_head(&call->waitq); |
08e0e7c8 DH |
261 | return call; |
262 | ||
00d3b7a4 | 263 | nomem_free: |
341f741f | 264 | afs_put_call(call); |
08e0e7c8 DH |
265 | nomem_call: |
266 | return NULL; | |
267 | } | |
268 | ||
269 | /* | |
270 | * clean up a call with flat buffer | |
271 | */ | |
272 | void afs_flat_call_destructor(struct afs_call *call) | |
273 | { | |
274 | _enter(""); | |
275 | ||
276 | kfree(call->request); | |
277 | call->request = NULL; | |
278 | kfree(call->buffer); | |
279 | call->buffer = NULL; | |
280 | } | |
281 | ||
2f5705a5 DH |
282 | #define AFS_BVEC_MAX 8 |
283 | ||
284 | /* | |
285 | * Load the given bvec with the next few pages. | |
286 | */ | |
287 | static void afs_load_bvec(struct afs_call *call, struct msghdr *msg, | |
288 | struct bio_vec *bv, pgoff_t first, pgoff_t last, | |
289 | unsigned offset) | |
290 | { | |
291 | struct page *pages[AFS_BVEC_MAX]; | |
292 | unsigned int nr, n, i, to, bytes = 0; | |
293 | ||
294 | nr = min_t(pgoff_t, last - first + 1, AFS_BVEC_MAX); | |
295 | n = find_get_pages_contig(call->mapping, first, nr, pages); | |
296 | ASSERTCMP(n, ==, nr); | |
297 | ||
298 | msg->msg_flags |= MSG_MORE; | |
299 | for (i = 0; i < nr; i++) { | |
300 | to = PAGE_SIZE; | |
301 | if (first + i >= last) { | |
302 | to = call->last_to; | |
303 | msg->msg_flags &= ~MSG_MORE; | |
304 | } | |
305 | bv[i].bv_page = pages[i]; | |
306 | bv[i].bv_len = to - offset; | |
307 | bv[i].bv_offset = offset; | |
308 | bytes += to - offset; | |
309 | offset = 0; | |
310 | } | |
311 | ||
aa563d7b | 312 | iov_iter_bvec(&msg->msg_iter, WRITE, bv, nr, bytes); |
2f5705a5 DH |
313 | } |
314 | ||
e833251a DH |
315 | /* |
316 | * Advance the AFS call state when the RxRPC call ends the transmit phase. | |
317 | */ | |
318 | static void afs_notify_end_request_tx(struct sock *sock, | |
319 | struct rxrpc_call *rxcall, | |
320 | unsigned long call_user_ID) | |
321 | { | |
322 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
323 | ||
98bf40cd | 324 | afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY); |
e833251a DH |
325 | } |
326 | ||
31143d5d DH |
327 | /* |
328 | * attach the data from a bunch of pages on an inode to a call | |
329 | */ | |
39c6acea | 330 | static int afs_send_pages(struct afs_call *call, struct msghdr *msg) |
31143d5d | 331 | { |
2f5705a5 DH |
332 | struct bio_vec bv[AFS_BVEC_MAX]; |
333 | unsigned int bytes, nr, loop, offset; | |
31143d5d DH |
334 | pgoff_t first = call->first, last = call->last; |
335 | int ret; | |
336 | ||
31143d5d DH |
337 | offset = call->first_offset; |
338 | call->first_offset = 0; | |
339 | ||
340 | do { | |
2f5705a5 | 341 | afs_load_bvec(call, msg, bv, first, last, offset); |
2c099014 DH |
342 | trace_afs_send_pages(call, msg, first, last, offset); |
343 | ||
2f5705a5 DH |
344 | offset = 0; |
345 | bytes = msg->msg_iter.count; | |
346 | nr = msg->msg_iter.nr_segs; | |
347 | ||
f044c884 | 348 | ret = rxrpc_kernel_send_data(call->net->socket, call->rxcall, msg, |
e833251a | 349 | bytes, afs_notify_end_request_tx); |
2f5705a5 DH |
350 | for (loop = 0; loop < nr; loop++) |
351 | put_page(bv[loop].bv_page); | |
31143d5d DH |
352 | if (ret < 0) |
353 | break; | |
2f5705a5 DH |
354 | |
355 | first += nr; | |
5bbf5d39 | 356 | } while (first <= last); |
31143d5d | 357 | |
2c099014 | 358 | trace_afs_sent_pages(call, call->first, last, first, ret); |
31143d5d DH |
359 | return ret; |
360 | } | |
361 | ||
08e0e7c8 DH |
362 | /* |
363 | * initiate a call | |
364 | */ | |
8b2a464c | 365 | long afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, |
33cd7f2b | 366 | gfp_t gfp, bool async) |
08e0e7c8 | 367 | { |
2feeaf84 | 368 | struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index]; |
08e0e7c8 DH |
369 | struct rxrpc_call *rxcall; |
370 | struct msghdr msg; | |
371 | struct kvec iov[1]; | |
e754eba6 | 372 | s64 tx_total_len; |
08e0e7c8 DH |
373 | int ret; |
374 | ||
4d9df986 | 375 | _enter(",{%pISp},", &srx->transport); |
08e0e7c8 | 376 | |
00d3b7a4 DH |
377 | ASSERT(call->type != NULL); |
378 | ASSERT(call->type->name != NULL); | |
379 | ||
31143d5d DH |
380 | _debug("____MAKE %p{%s,%x} [%d]____", |
381 | call, call->type->name, key_serial(call->key), | |
f044c884 | 382 | atomic_read(&call->net->nr_outstanding_calls)); |
00d3b7a4 | 383 | |
56ff9c83 | 384 | call->async = async; |
3bf0fb6f DH |
385 | call->addr_ix = ac->index; |
386 | call->alist = afs_get_addrlist(ac->alist); | |
08e0e7c8 | 387 | |
e754eba6 DH |
388 | /* Work out the length we're going to transmit. This is awkward for |
389 | * calls such as FS.StoreData where there's an extra injection of data | |
390 | * after the initial fixed part. | |
391 | */ | |
392 | tx_total_len = call->request_size; | |
393 | if (call->send_pages) { | |
1199db60 DH |
394 | if (call->last == call->first) { |
395 | tx_total_len += call->last_to - call->first_offset; | |
396 | } else { | |
397 | /* It looks mathematically like you should be able to | |
398 | * combine the following lines with the ones above, but | |
399 | * unsigned arithmetic is fun when it wraps... | |
400 | */ | |
401 | tx_total_len += PAGE_SIZE - call->first_offset; | |
402 | tx_total_len += call->last_to; | |
403 | tx_total_len += (call->last - call->first - 1) * PAGE_SIZE; | |
404 | } | |
e754eba6 DH |
405 | } |
406 | ||
08e0e7c8 | 407 | /* create a call */ |
4d9df986 | 408 | rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key, |
e754eba6 DH |
409 | (unsigned long)call, |
410 | tx_total_len, gfp, | |
56ff9c83 DH |
411 | (async ? |
412 | afs_wake_up_async_call : | |
a68f4a27 | 413 | afs_wake_up_call_waiter), |
a25e21f0 DH |
414 | call->upgrade, |
415 | call->debug_id); | |
08e0e7c8 DH |
416 | if (IS_ERR(rxcall)) { |
417 | ret = PTR_ERR(rxcall); | |
3bf0fb6f | 418 | call->error = ret; |
08e0e7c8 DH |
419 | goto error_kill_call; |
420 | } | |
421 | ||
422 | call->rxcall = rxcall; | |
423 | ||
424 | /* send the request */ | |
425 | iov[0].iov_base = call->request; | |
426 | iov[0].iov_len = call->request_size; | |
427 | ||
428 | msg.msg_name = NULL; | |
429 | msg.msg_namelen = 0; | |
aa563d7b | 430 | iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size); |
08e0e7c8 DH |
431 | msg.msg_control = NULL; |
432 | msg.msg_controllen = 0; | |
bc5e3a54 | 433 | msg.msg_flags = MSG_WAITALL | (call->send_pages ? MSG_MORE : 0); |
08e0e7c8 | 434 | |
f044c884 | 435 | ret = rxrpc_kernel_send_data(call->net->socket, rxcall, |
e833251a DH |
436 | &msg, call->request_size, |
437 | afs_notify_end_request_tx); | |
08e0e7c8 DH |
438 | if (ret < 0) |
439 | goto error_do_abort; | |
440 | ||
31143d5d | 441 | if (call->send_pages) { |
39c6acea | 442 | ret = afs_send_pages(call, &msg); |
31143d5d DH |
443 | if (ret < 0) |
444 | goto error_do_abort; | |
445 | } | |
446 | ||
08e0e7c8 DH |
447 | /* at this point, an async call may no longer exist as it may have |
448 | * already completed */ | |
56ff9c83 DH |
449 | if (call->async) |
450 | return -EINPROGRESS; | |
451 | ||
d2ddc776 | 452 | return afs_wait_for_call_to_complete(call, ac); |
08e0e7c8 DH |
453 | |
454 | error_do_abort: | |
70af0e3b DH |
455 | call->state = AFS_CALL_COMPLETE; |
456 | if (ret != -ECONNABORTED) { | |
f044c884 DH |
457 | rxrpc_kernel_abort_call(call->net->socket, rxcall, |
458 | RX_USER_ABORT, ret, "KSD"); | |
70af0e3b | 459 | } else { |
aa563d7b | 460 | iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0); |
eb9950eb DH |
461 | rxrpc_kernel_recv_data(call->net->socket, rxcall, |
462 | &msg.msg_iter, false, | |
463 | &call->abort_code, &call->service_id); | |
d2ddc776 DH |
464 | ac->abort_code = call->abort_code; |
465 | ac->responded = true; | |
70af0e3b | 466 | } |
025db80c DH |
467 | call->error = ret; |
468 | trace_afs_call_done(call); | |
08e0e7c8 | 469 | error_kill_call: |
3bf0fb6f DH |
470 | if (call->type->done) |
471 | call->type->done(call); | |
341f741f | 472 | afs_put_call(call); |
d2ddc776 | 473 | ac->error = ret; |
08e0e7c8 DH |
474 | _leave(" = %d", ret); |
475 | return ret; | |
476 | } | |
477 | ||
08e0e7c8 DH |
478 | /* |
479 | * deliver messages to a call | |
480 | */ | |
481 | static void afs_deliver_to_call(struct afs_call *call) | |
482 | { | |
98bf40cd DH |
483 | enum afs_call_state state; |
484 | u32 abort_code, remote_abort = 0; | |
08e0e7c8 DH |
485 | int ret; |
486 | ||
d001648e DH |
487 | _enter("%s", call->type->name); |
488 | ||
98bf40cd DH |
489 | while (state = READ_ONCE(call->state), |
490 | state == AFS_CALL_CL_AWAIT_REPLY || | |
491 | state == AFS_CALL_SV_AWAIT_OP_ID || | |
492 | state == AFS_CALL_SV_AWAIT_REQUEST || | |
493 | state == AFS_CALL_SV_AWAIT_ACK | |
d001648e | 494 | ) { |
98bf40cd | 495 | if (state == AFS_CALL_SV_AWAIT_ACK) { |
12bdcf33 | 496 | iov_iter_kvec(&call->iter, READ, NULL, 0, 0); |
f044c884 | 497 | ret = rxrpc_kernel_recv_data(call->net->socket, |
12bdcf33 DH |
498 | call->rxcall, &call->iter, |
499 | false, &remote_abort, | |
a68f4a27 | 500 | &call->service_id); |
12bdcf33 | 501 | trace_afs_receive_data(call, &call->iter, false, ret); |
8e8d7f13 | 502 | |
d001648e DH |
503 | if (ret == -EINPROGRESS || ret == -EAGAIN) |
504 | return; | |
98bf40cd DH |
505 | if (ret < 0 || ret == 1) { |
506 | if (ret == 1) | |
507 | ret = 0; | |
025db80c | 508 | goto call_complete; |
98bf40cd | 509 | } |
d001648e | 510 | return; |
08e0e7c8 DH |
511 | } |
512 | ||
12d8e95a DH |
513 | if (call->want_reply_time && |
514 | rxrpc_kernel_get_reply_time(call->net->socket, | |
515 | call->rxcall, | |
516 | &call->reply_time)) | |
517 | call->want_reply_time = false; | |
518 | ||
d001648e | 519 | ret = call->type->deliver(call); |
98bf40cd | 520 | state = READ_ONCE(call->state); |
d001648e DH |
521 | switch (ret) { |
522 | case 0: | |
3bf0fb6f | 523 | afs_queue_call_work(call); |
f2686b09 DH |
524 | if (state == AFS_CALL_CL_PROC_REPLY) { |
525 | if (call->cbi) | |
526 | set_bit(AFS_SERVER_FL_MAY_HAVE_CB, | |
527 | &call->cbi->server->flags); | |
025db80c | 528 | goto call_complete; |
f2686b09 | 529 | } |
98bf40cd | 530 | ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY); |
d001648e DH |
531 | goto done; |
532 | case -EINPROGRESS: | |
533 | case -EAGAIN: | |
534 | goto out; | |
70af0e3b | 535 | case -ECONNABORTED: |
98bf40cd DH |
536 | ASSERTCMP(state, ==, AFS_CALL_COMPLETE); |
537 | goto done; | |
d001648e | 538 | case -ENOTSUPP: |
1157f153 | 539 | abort_code = RXGEN_OPCODE; |
f044c884 | 540 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
3a92789a | 541 | abort_code, ret, "KIV"); |
98bf40cd | 542 | goto local_abort; |
4ac15ea5 DH |
543 | case -EIO: |
544 | pr_err("kAFS: Call %u in bad state %u\n", | |
545 | call->debug_id, state); | |
546 | /* Fall through */ | |
d001648e DH |
547 | case -ENODATA: |
548 | case -EBADMSG: | |
549 | case -EMSGSIZE: | |
550 | default: | |
551 | abort_code = RXGEN_CC_UNMARSHAL; | |
98bf40cd | 552 | if (state != AFS_CALL_CL_AWAIT_REPLY) |
d001648e | 553 | abort_code = RXGEN_SS_UNMARSHAL; |
f044c884 | 554 | rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
12bdcf33 | 555 | abort_code, ret, "KUM"); |
98bf40cd | 556 | goto local_abort; |
d001648e | 557 | } |
08e0e7c8 DH |
558 | } |
559 | ||
d001648e | 560 | done: |
3bf0fb6f DH |
561 | if (call->type->done) |
562 | call->type->done(call); | |
98bf40cd | 563 | if (state == AFS_CALL_COMPLETE && call->incoming) |
341f741f | 564 | afs_put_call(call); |
d001648e | 565 | out: |
08e0e7c8 | 566 | _leave(""); |
d001648e DH |
567 | return; |
568 | ||
98bf40cd DH |
569 | local_abort: |
570 | abort_code = 0; | |
025db80c | 571 | call_complete: |
98bf40cd DH |
572 | afs_set_call_complete(call, ret, remote_abort); |
573 | state = AFS_CALL_COMPLETE; | |
d001648e | 574 | goto done; |
08e0e7c8 DH |
575 | } |
576 | ||
577 | /* | |
578 | * wait synchronously for a call to complete | |
579 | */ | |
d2ddc776 DH |
580 | static long afs_wait_for_call_to_complete(struct afs_call *call, |
581 | struct afs_addr_cursor *ac) | |
08e0e7c8 | 582 | { |
bc5e3a54 | 583 | signed long rtt2, timeout; |
33cd7f2b | 584 | long ret; |
7150ceaa | 585 | bool stalled = false; |
bc5e3a54 DH |
586 | u64 rtt; |
587 | u32 life, last_life; | |
08e0e7c8 DH |
588 | |
589 | DECLARE_WAITQUEUE(myself, current); | |
590 | ||
591 | _enter(""); | |
592 | ||
f044c884 | 593 | rtt = rxrpc_kernel_get_rtt(call->net->socket, call->rxcall); |
bc5e3a54 DH |
594 | rtt2 = nsecs_to_jiffies64(rtt) * 2; |
595 | if (rtt2 < 2) | |
596 | rtt2 = 2; | |
597 | ||
598 | timeout = rtt2; | |
f044c884 | 599 | last_life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
bc5e3a54 | 600 | |
08e0e7c8 DH |
601 | add_wait_queue(&call->waitq, &myself); |
602 | for (;;) { | |
bc5e3a54 | 603 | set_current_state(TASK_UNINTERRUPTIBLE); |
08e0e7c8 DH |
604 | |
605 | /* deliver any messages that are in the queue */ | |
98bf40cd DH |
606 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE) && |
607 | call->need_attention) { | |
d001648e | 608 | call->need_attention = false; |
08e0e7c8 DH |
609 | __set_current_state(TASK_RUNNING); |
610 | afs_deliver_to_call(call); | |
611 | continue; | |
612 | } | |
613 | ||
98bf40cd | 614 | if (afs_check_call_state(call, AFS_CALL_COMPLETE)) |
08e0e7c8 | 615 | break; |
bc5e3a54 | 616 | |
f044c884 | 617 | life = rxrpc_kernel_check_life(call->net->socket, call->rxcall); |
bc5e3a54 | 618 | if (timeout == 0 && |
7150ceaa DH |
619 | life == last_life && signal_pending(current)) { |
620 | if (stalled) | |
bc5e3a54 | 621 | break; |
7150ceaa DH |
622 | __set_current_state(TASK_RUNNING); |
623 | rxrpc_kernel_probe_life(call->net->socket, call->rxcall); | |
624 | timeout = rtt2; | |
625 | stalled = true; | |
626 | continue; | |
627 | } | |
bc5e3a54 DH |
628 | |
629 | if (life != last_life) { | |
630 | timeout = rtt2; | |
631 | last_life = life; | |
7150ceaa | 632 | stalled = false; |
bc5e3a54 DH |
633 | } |
634 | ||
635 | timeout = schedule_timeout(timeout); | |
08e0e7c8 DH |
636 | } |
637 | ||
638 | remove_wait_queue(&call->waitq, &myself); | |
639 | __set_current_state(TASK_RUNNING); | |
640 | ||
954cd6dc | 641 | /* Kill off the call if it's still live. */ |
98bf40cd | 642 | if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) { |
954cd6dc | 643 | _debug("call interrupted"); |
d2ddc776 | 644 | if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall, |
98bf40cd DH |
645 | RX_USER_ABORT, -EINTR, "KWI")) |
646 | afs_set_call_complete(call, -EINTR, 0); | |
08e0e7c8 DH |
647 | } |
648 | ||
98bf40cd | 649 | spin_lock_bh(&call->state_lock); |
d2ddc776 DH |
650 | ac->abort_code = call->abort_code; |
651 | ac->error = call->error; | |
98bf40cd | 652 | spin_unlock_bh(&call->state_lock); |
d2ddc776 DH |
653 | |
654 | ret = ac->error; | |
655 | switch (ret) { | |
656 | case 0: | |
657 | if (call->ret_reply0) { | |
658 | ret = (long)call->reply[0]; | |
659 | call->reply[0] = NULL; | |
660 | } | |
661 | /* Fall through */ | |
662 | case -ECONNABORTED: | |
663 | ac->responded = true; | |
664 | break; | |
33cd7f2b DH |
665 | } |
666 | ||
08e0e7c8 | 667 | _debug("call complete"); |
341f741f | 668 | afs_put_call(call); |
33cd7f2b | 669 | _leave(" = %p", (void *)ret); |
08e0e7c8 DH |
670 | return ret; |
671 | } | |
672 | ||
673 | /* | |
674 | * wake up a waiting call | |
675 | */ | |
d001648e DH |
676 | static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall, |
677 | unsigned long call_user_ID) | |
08e0e7c8 | 678 | { |
d001648e DH |
679 | struct afs_call *call = (struct afs_call *)call_user_ID; |
680 | ||
681 | call->need_attention = true; | |
08e0e7c8 DH |
682 | wake_up(&call->waitq); |
683 | } | |
684 | ||
685 | /* | |
686 | * wake up an asynchronous call | |
687 | */ | |
d001648e DH |
688 | static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall, |
689 | unsigned long call_user_ID) | |
08e0e7c8 | 690 | { |
d001648e | 691 | struct afs_call *call = (struct afs_call *)call_user_ID; |
341f741f | 692 | int u; |
d001648e | 693 | |
8e8d7f13 | 694 | trace_afs_notify_call(rxcall, call); |
d001648e | 695 | call->need_attention = true; |
341f741f | 696 | |
bfc18e38 | 697 | u = atomic_fetch_add_unless(&call->usage, 1, 0); |
341f741f DH |
698 | if (u != 0) { |
699 | trace_afs_call(call, afs_call_trace_wake, u, | |
f044c884 | 700 | atomic_read(&call->net->nr_outstanding_calls), |
341f741f DH |
701 | __builtin_return_address(0)); |
702 | ||
703 | if (!queue_work(afs_async_calls, &call->async_work)) | |
704 | afs_put_call(call); | |
705 | } | |
08e0e7c8 DH |
706 | } |
707 | ||
08e0e7c8 | 708 | /* |
341f741f DH |
709 | * Delete an asynchronous call. The work item carries a ref to the call struct |
710 | * that we need to release. | |
08e0e7c8 | 711 | */ |
d001648e | 712 | static void afs_delete_async_call(struct work_struct *work) |
08e0e7c8 | 713 | { |
d001648e DH |
714 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
715 | ||
08e0e7c8 DH |
716 | _enter(""); |
717 | ||
341f741f | 718 | afs_put_call(call); |
08e0e7c8 DH |
719 | |
720 | _leave(""); | |
721 | } | |
722 | ||
723 | /* | |
341f741f DH |
724 | * Perform I/O processing on an asynchronous call. The work item carries a ref |
725 | * to the call struct that we either need to release or to pass on. | |
08e0e7c8 | 726 | */ |
d001648e | 727 | static void afs_process_async_call(struct work_struct *work) |
08e0e7c8 | 728 | { |
d001648e DH |
729 | struct afs_call *call = container_of(work, struct afs_call, async_work); |
730 | ||
08e0e7c8 DH |
731 | _enter(""); |
732 | ||
d001648e DH |
733 | if (call->state < AFS_CALL_COMPLETE && call->need_attention) { |
734 | call->need_attention = false; | |
08e0e7c8 | 735 | afs_deliver_to_call(call); |
d001648e | 736 | } |
08e0e7c8 | 737 | |
56ff9c83 | 738 | if (call->state == AFS_CALL_COMPLETE) { |
341f741f DH |
739 | /* We have two refs to release - one from the alloc and one |
740 | * queued with the work item - and we can't just deallocate the | |
741 | * call because the work item may be queued again. | |
742 | */ | |
d001648e | 743 | call->async_work.func = afs_delete_async_call; |
341f741f DH |
744 | if (!queue_work(afs_async_calls, &call->async_work)) |
745 | afs_put_call(call); | |
08e0e7c8 DH |
746 | } |
747 | ||
341f741f | 748 | afs_put_call(call); |
08e0e7c8 DH |
749 | _leave(""); |
750 | } | |
751 | ||
00e90712 DH |
752 | static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID) |
753 | { | |
754 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
755 | ||
756 | call->rxcall = rxcall; | |
757 | } | |
758 | ||
759 | /* | |
760 | * Charge the incoming call preallocation. | |
761 | */ | |
f044c884 | 762 | void afs_charge_preallocation(struct work_struct *work) |
00e90712 | 763 | { |
f044c884 DH |
764 | struct afs_net *net = |
765 | container_of(work, struct afs_net, charge_preallocation_work); | |
766 | struct afs_call *call = net->spare_incoming_call; | |
00e90712 DH |
767 | |
768 | for (;;) { | |
769 | if (!call) { | |
f044c884 | 770 | call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL); |
00e90712 DH |
771 | if (!call) |
772 | break; | |
773 | ||
56ff9c83 | 774 | call->async = true; |
98bf40cd | 775 | call->state = AFS_CALL_SV_AWAIT_OP_ID; |
56ff9c83 | 776 | init_waitqueue_head(&call->waitq); |
12bdcf33 | 777 | afs_extract_to_tmp(call); |
00e90712 DH |
778 | } |
779 | ||
f044c884 | 780 | if (rxrpc_kernel_charge_accept(net->socket, |
00e90712 DH |
781 | afs_wake_up_async_call, |
782 | afs_rx_attach, | |
783 | (unsigned long)call, | |
a25e21f0 DH |
784 | GFP_KERNEL, |
785 | call->debug_id) < 0) | |
00e90712 DH |
786 | break; |
787 | call = NULL; | |
788 | } | |
f044c884 | 789 | net->spare_incoming_call = call; |
00e90712 DH |
790 | } |
791 | ||
792 | /* | |
793 | * Discard a preallocated call when a socket is shut down. | |
794 | */ | |
795 | static void afs_rx_discard_new_call(struct rxrpc_call *rxcall, | |
796 | unsigned long user_call_ID) | |
797 | { | |
798 | struct afs_call *call = (struct afs_call *)user_call_ID; | |
799 | ||
00e90712 | 800 | call->rxcall = NULL; |
341f741f | 801 | afs_put_call(call); |
00e90712 DH |
802 | } |
803 | ||
d001648e DH |
804 | /* |
805 | * Notification of an incoming call. | |
806 | */ | |
00e90712 DH |
807 | static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall, |
808 | unsigned long user_call_ID) | |
d001648e | 809 | { |
f044c884 DH |
810 | struct afs_net *net = afs_sock2net(sk); |
811 | ||
812 | queue_work(afs_wq, &net->charge_preallocation_work); | |
d001648e DH |
813 | } |
814 | ||
08e0e7c8 | 815 | /* |
372ee163 DH |
816 | * Grab the operation ID from an incoming cache manager call. The socket |
817 | * buffer is discarded on error or if we don't yet have sufficient data. | |
08e0e7c8 | 818 | */ |
d001648e | 819 | static int afs_deliver_cm_op_id(struct afs_call *call) |
08e0e7c8 | 820 | { |
d001648e | 821 | int ret; |
08e0e7c8 | 822 | |
12bdcf33 | 823 | _enter("{%zu}", iov_iter_count(call->_iter)); |
08e0e7c8 DH |
824 | |
825 | /* the operation ID forms the first four bytes of the request data */ | |
12bdcf33 | 826 | ret = afs_extract_data(call, true); |
d001648e DH |
827 | if (ret < 0) |
828 | return ret; | |
08e0e7c8 | 829 | |
50a2c953 | 830 | call->operation_ID = ntohl(call->tmp); |
98bf40cd | 831 | afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST); |
08e0e7c8 DH |
832 | |
833 | /* ask the cache manager to route the call (it'll change the call type | |
834 | * if successful) */ | |
835 | if (!afs_cm_incoming_call(call)) | |
836 | return -ENOTSUPP; | |
837 | ||
8e8d7f13 DH |
838 | trace_afs_cb_call(call); |
839 | ||
08e0e7c8 DH |
840 | /* pass responsibility for the remainer of this message off to the |
841 | * cache manager op */ | |
d001648e | 842 | return call->type->deliver(call); |
08e0e7c8 DH |
843 | } |
844 | ||
e833251a DH |
845 | /* |
846 | * Advance the AFS call state when an RxRPC service call ends the transmit | |
847 | * phase. | |
848 | */ | |
849 | static void afs_notify_end_reply_tx(struct sock *sock, | |
850 | struct rxrpc_call *rxcall, | |
851 | unsigned long call_user_ID) | |
852 | { | |
853 | struct afs_call *call = (struct afs_call *)call_user_ID; | |
854 | ||
98bf40cd | 855 | afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK); |
e833251a DH |
856 | } |
857 | ||
08e0e7c8 DH |
858 | /* |
859 | * send an empty reply | |
860 | */ | |
861 | void afs_send_empty_reply(struct afs_call *call) | |
862 | { | |
f044c884 | 863 | struct afs_net *net = call->net; |
08e0e7c8 | 864 | struct msghdr msg; |
08e0e7c8 DH |
865 | |
866 | _enter(""); | |
867 | ||
f044c884 | 868 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0); |
e754eba6 | 869 | |
08e0e7c8 DH |
870 | msg.msg_name = NULL; |
871 | msg.msg_namelen = 0; | |
aa563d7b | 872 | iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0); |
08e0e7c8 DH |
873 | msg.msg_control = NULL; |
874 | msg.msg_controllen = 0; | |
875 | msg.msg_flags = 0; | |
876 | ||
f044c884 | 877 | switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0, |
e833251a | 878 | afs_notify_end_reply_tx)) { |
08e0e7c8 DH |
879 | case 0: |
880 | _leave(" [replied]"); | |
881 | return; | |
882 | ||
883 | case -ENOMEM: | |
884 | _debug("oom"); | |
f044c884 | 885 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 886 | RX_USER_ABORT, -ENOMEM, "KOO"); |
08e0e7c8 | 887 | default: |
08e0e7c8 DH |
888 | _leave(" [error]"); |
889 | return; | |
890 | } | |
891 | } | |
892 | ||
b908fe6b DH |
893 | /* |
894 | * send a simple reply | |
895 | */ | |
896 | void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len) | |
897 | { | |
f044c884 | 898 | struct afs_net *net = call->net; |
b908fe6b | 899 | struct msghdr msg; |
2e90b1c4 | 900 | struct kvec iov[1]; |
bd6dc742 | 901 | int n; |
b908fe6b DH |
902 | |
903 | _enter(""); | |
904 | ||
f044c884 | 905 | rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len); |
e754eba6 | 906 | |
b908fe6b DH |
907 | iov[0].iov_base = (void *) buf; |
908 | iov[0].iov_len = len; | |
909 | msg.msg_name = NULL; | |
910 | msg.msg_namelen = 0; | |
aa563d7b | 911 | iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len); |
b908fe6b DH |
912 | msg.msg_control = NULL; |
913 | msg.msg_controllen = 0; | |
914 | msg.msg_flags = 0; | |
915 | ||
f044c884 | 916 | n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len, |
e833251a | 917 | afs_notify_end_reply_tx); |
bd6dc742 | 918 | if (n >= 0) { |
6c67c7c3 | 919 | /* Success */ |
b908fe6b DH |
920 | _leave(" [replied]"); |
921 | return; | |
bd6dc742 | 922 | } |
6c67c7c3 | 923 | |
bd6dc742 | 924 | if (n == -ENOMEM) { |
b908fe6b | 925 | _debug("oom"); |
f044c884 | 926 | rxrpc_kernel_abort_call(net->socket, call->rxcall, |
3a92789a | 927 | RX_USER_ABORT, -ENOMEM, "KOO"); |
b908fe6b | 928 | } |
bd6dc742 | 929 | _leave(" [error]"); |
b908fe6b DH |
930 | } |
931 | ||
08e0e7c8 | 932 | /* |
372ee163 | 933 | * Extract a piece of data from the received data socket buffers. |
08e0e7c8 | 934 | */ |
12bdcf33 | 935 | int afs_extract_data(struct afs_call *call, bool want_more) |
08e0e7c8 | 936 | { |
f044c884 | 937 | struct afs_net *net = call->net; |
12bdcf33 | 938 | struct iov_iter *iter = call->_iter; |
98bf40cd | 939 | enum afs_call_state state; |
7888da95 | 940 | u32 remote_abort = 0; |
d001648e | 941 | int ret; |
08e0e7c8 | 942 | |
12bdcf33 | 943 | _enter("{%s,%zu},%d", call->type->name, iov_iter_count(iter), want_more); |
eb9950eb | 944 | |
12bdcf33 | 945 | ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter, |
98bf40cd | 946 | want_more, &remote_abort, |
a68f4a27 | 947 | &call->service_id); |
d001648e DH |
948 | if (ret == 0 || ret == -EAGAIN) |
949 | return ret; | |
08e0e7c8 | 950 | |
98bf40cd | 951 | state = READ_ONCE(call->state); |
d001648e | 952 | if (ret == 1) { |
98bf40cd DH |
953 | switch (state) { |
954 | case AFS_CALL_CL_AWAIT_REPLY: | |
955 | afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY); | |
d001648e | 956 | break; |
98bf40cd DH |
957 | case AFS_CALL_SV_AWAIT_REQUEST: |
958 | afs_set_call_state(call, state, AFS_CALL_SV_REPLYING); | |
d001648e | 959 | break; |
98bf40cd DH |
960 | case AFS_CALL_COMPLETE: |
961 | kdebug("prem complete %d", call->error); | |
f51375cd | 962 | return afs_io_error(call, afs_io_error_extract); |
d001648e DH |
963 | default: |
964 | break; | |
965 | } | |
966 | return 0; | |
08e0e7c8 | 967 | } |
d001648e | 968 | |
98bf40cd | 969 | afs_set_call_complete(call, ret, remote_abort); |
d001648e | 970 | return ret; |
08e0e7c8 | 971 | } |
5f702c8e DH |
972 | |
973 | /* | |
974 | * Log protocol error production. | |
975 | */ | |
160cb957 DH |
976 | noinline int afs_protocol_error(struct afs_call *call, int error, |
977 | enum afs_eproto_cause cause) | |
5f702c8e | 978 | { |
160cb957 | 979 | trace_afs_protocol_error(call, error, cause); |
5f702c8e DH |
980 | return error; |
981 | } |