blob: b86195e4dc6cd657485eb01f62d103b4e06f7008 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsec268152007-04-26 15:49:28 -07002/* AFS Cache Manager Service
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/module.h>
9#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/sched.h>
David Howells08e0e7c2007-04-26 15:55:03 -070012#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "internal.h"
David Howells08e0e7c2007-04-26 15:55:03 -070014#include "afs_cm.h"
David Howells35dbfba2018-10-20 00:57:58 +010015#include "protocol_yfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
David Howellsd0016482016-08-30 20:42:14 +010017static int afs_deliver_cb_init_call_back_state(struct afs_call *);
18static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
19static int afs_deliver_cb_probe(struct afs_call *);
20static int afs_deliver_cb_callback(struct afs_call *);
21static int afs_deliver_cb_probe_uuid(struct afs_call *);
22static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
David Howells08e0e7c2007-04-26 15:55:03 -070023static void afs_cm_destructor(struct afs_call *);
David Howells341f7412017-01-05 10:38:36 +000024static void SRXAFSCB_CallBack(struct work_struct *);
25static void SRXAFSCB_InitCallBackState(struct work_struct *);
26static void SRXAFSCB_Probe(struct work_struct *);
27static void SRXAFSCB_ProbeUuid(struct work_struct *);
28static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David Howells35dbfba2018-10-20 00:57:58 +010030static int afs_deliver_yfs_cb_callback(struct afs_call *);
31
David Howells8e8d7f12017-01-05 10:38:34 +000032#define CM_NAME(name) \
Andi Kleend2abfa862019-04-13 08:37:36 +010033 char afs_SRXCB##name##_name[] __tracepoint_string = \
David Howells8e8d7f12017-01-05 10:38:34 +000034 "CB." #name
35
David Howells08e0e7c2007-04-26 15:55:03 -070036/*
37 * CB.CallBack operation type
38 */
David Howells8e8d7f12017-01-05 10:38:34 +000039static CM_NAME(CallBack);
David Howells08e0e7c2007-04-26 15:55:03 -070040static const struct afs_call_type afs_SRXCBCallBack = {
David Howells8e8d7f12017-01-05 10:38:34 +000041 .name = afs_SRXCBCallBack_name,
David Howells08e0e7c2007-04-26 15:55:03 -070042 .deliver = afs_deliver_cb_callback,
David Howells08e0e7c2007-04-26 15:55:03 -070043 .destructor = afs_cm_destructor,
David Howells341f7412017-01-05 10:38:36 +000044 .work = SRXAFSCB_CallBack,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
David Howells08e0e7c2007-04-26 15:55:03 -070047/*
48 * CB.InitCallBackState operation type
49 */
David Howells8e8d7f12017-01-05 10:38:34 +000050static CM_NAME(InitCallBackState);
David Howells08e0e7c2007-04-26 15:55:03 -070051static const struct afs_call_type afs_SRXCBInitCallBackState = {
David Howells8e8d7f12017-01-05 10:38:34 +000052 .name = afs_SRXCBInitCallBackState_name,
David Howells08e0e7c2007-04-26 15:55:03 -070053 .deliver = afs_deliver_cb_init_call_back_state,
David Howells08e0e7c2007-04-26 15:55:03 -070054 .destructor = afs_cm_destructor,
David Howells341f7412017-01-05 10:38:36 +000055 .work = SRXAFSCB_InitCallBackState,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
David Howells08e0e7c2007-04-26 15:55:03 -070058/*
David Howellsc35eccb2007-04-26 15:58:49 -070059 * CB.InitCallBackState3 operation type
60 */
David Howells8e8d7f12017-01-05 10:38:34 +000061static CM_NAME(InitCallBackState3);
David Howellsc35eccb2007-04-26 15:58:49 -070062static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
David Howells8e8d7f12017-01-05 10:38:34 +000063 .name = afs_SRXCBInitCallBackState3_name,
David Howellsc35eccb2007-04-26 15:58:49 -070064 .deliver = afs_deliver_cb_init_call_back_state3,
David Howellsc35eccb2007-04-26 15:58:49 -070065 .destructor = afs_cm_destructor,
David Howells341f7412017-01-05 10:38:36 +000066 .work = SRXAFSCB_InitCallBackState,
David Howellsc35eccb2007-04-26 15:58:49 -070067};
68
69/*
David Howells08e0e7c2007-04-26 15:55:03 -070070 * CB.Probe operation type
71 */
David Howells8e8d7f12017-01-05 10:38:34 +000072static CM_NAME(Probe);
David Howells08e0e7c2007-04-26 15:55:03 -070073static const struct afs_call_type afs_SRXCBProbe = {
David Howells8e8d7f12017-01-05 10:38:34 +000074 .name = afs_SRXCBProbe_name,
David Howells08e0e7c2007-04-26 15:55:03 -070075 .deliver = afs_deliver_cb_probe,
David Howells08e0e7c2007-04-26 15:55:03 -070076 .destructor = afs_cm_destructor,
David Howells341f7412017-01-05 10:38:36 +000077 .work = SRXAFSCB_Probe,
David Howells08e0e7c2007-04-26 15:55:03 -070078};
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
David Howells9396d492008-04-29 01:03:22 -070081 * CB.ProbeUuid operation type
82 */
David Howells8e8d7f12017-01-05 10:38:34 +000083static CM_NAME(ProbeUuid);
David Howells9396d492008-04-29 01:03:22 -070084static const struct afs_call_type afs_SRXCBProbeUuid = {
David Howells8e8d7f12017-01-05 10:38:34 +000085 .name = afs_SRXCBProbeUuid_name,
David Howells9396d492008-04-29 01:03:22 -070086 .deliver = afs_deliver_cb_probe_uuid,
David Howells9396d492008-04-29 01:03:22 -070087 .destructor = afs_cm_destructor,
David Howells341f7412017-01-05 10:38:36 +000088 .work = SRXAFSCB_ProbeUuid,
David Howells9396d492008-04-29 01:03:22 -070089};
90
91/*
David Howells7c80bcc2008-04-29 01:03:21 -070092 * CB.TellMeAboutYourself operation type
David Howellsb908fe62007-04-26 15:58:17 -070093 */
David Howells8e8d7f12017-01-05 10:38:34 +000094static CM_NAME(TellMeAboutYourself);
David Howells7c80bcc2008-04-29 01:03:21 -070095static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
David Howells8e8d7f12017-01-05 10:38:34 +000096 .name = afs_SRXCBTellMeAboutYourself_name,
David Howells7c80bcc2008-04-29 01:03:21 -070097 .deliver = afs_deliver_cb_tell_me_about_yourself,
David Howellsb908fe62007-04-26 15:58:17 -070098 .destructor = afs_cm_destructor,
David Howells341f7412017-01-05 10:38:36 +000099 .work = SRXAFSCB_TellMeAboutYourself,
David Howellsb908fe62007-04-26 15:58:17 -0700100};
101
102/*
David Howells35dbfba2018-10-20 00:57:58 +0100103 * YFS CB.CallBack operation type
104 */
105static CM_NAME(YFS_CallBack);
106static const struct afs_call_type afs_SRXYFSCB_CallBack = {
107 .name = afs_SRXCBYFS_CallBack_name,
108 .deliver = afs_deliver_yfs_cb_callback,
109 .destructor = afs_cm_destructor,
110 .work = SRXAFSCB_CallBack,
111};
112
113/*
David Howells08e0e7c2007-04-26 15:55:03 -0700114 * route an incoming cache manager call
115 * - return T if supported, F if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
David Howells08e0e7c2007-04-26 15:55:03 -0700117bool afs_cm_incoming_call(struct afs_call *call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
David Howells35dbfba2018-10-20 00:57:58 +0100119 _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
David Howells3bf0fb62018-10-20 00:57:59 +0100121 call->epoch = rxrpc_kernel_get_epoch(call->net->socket, call->rxcall);
122
David Howells50a2c952016-10-13 08:27:10 +0100123 switch (call->operation_ID) {
David Howells08e0e7c2007-04-26 15:55:03 -0700124 case CBCallBack:
125 call->type = &afs_SRXCBCallBack;
126 return true;
127 case CBInitCallBackState:
128 call->type = &afs_SRXCBInitCallBackState;
129 return true;
David Howellsc35eccb2007-04-26 15:58:49 -0700130 case CBInitCallBackState3:
131 call->type = &afs_SRXCBInitCallBackState3;
132 return true;
David Howells08e0e7c2007-04-26 15:55:03 -0700133 case CBProbe:
134 call->type = &afs_SRXCBProbe;
135 return true;
David Howellsf4b35262017-11-02 15:27:48 +0000136 case CBProbeUuid:
137 call->type = &afs_SRXCBProbeUuid;
138 return true;
David Howells7c80bcc2008-04-29 01:03:21 -0700139 case CBTellMeAboutYourself:
140 call->type = &afs_SRXCBTellMeAboutYourself;
David Howellsb908fe62007-04-26 15:58:17 -0700141 return true;
David Howells35dbfba2018-10-20 00:57:58 +0100142 case YFSCBCallBack:
143 if (call->service_id != YFS_CM_SERVICE)
144 return false;
145 call->type = &afs_SRXYFSCB_CallBack;
146 return true;
David Howells08e0e7c2007-04-26 15:55:03 -0700147 default:
148 return false;
149 }
David Howellsec268152007-04-26 15:49:28 -0700150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
David Howells3bf0fb62018-10-20 00:57:59 +0100153 * Record a probe to the cache manager from a server.
154 */
155static int afs_record_cm_probe(struct afs_call *call, struct afs_server *server)
156{
157 _enter("");
158
159 if (test_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags) &&
160 !test_bit(AFS_SERVER_FL_PROBING, &server->flags)) {
161 if (server->cm_epoch == call->epoch)
162 return 0;
163
164 if (!server->probe.said_rebooted) {
165 pr_notice("kAFS: FS rebooted %pU\n", &server->uuid);
166 server->probe.said_rebooted = true;
167 }
168 }
169
170 spin_lock(&server->probe_lock);
171
172 if (!test_bit(AFS_SERVER_FL_HAVE_EPOCH, &server->flags)) {
173 server->cm_epoch = call->epoch;
174 server->probe.cm_epoch = call->epoch;
175 goto out;
176 }
177
178 if (server->probe.cm_probed &&
179 call->epoch != server->probe.cm_epoch &&
180 !server->probe.said_inconsistent) {
181 pr_notice("kAFS: FS endpoints inconsistent %pU\n",
182 &server->uuid);
183 server->probe.said_inconsistent = true;
184 }
185
186 if (!server->probe.cm_probed || call->epoch == server->cm_epoch)
187 server->probe.cm_epoch = server->cm_epoch;
188
189out:
190 server->probe.cm_probed = true;
191 spin_unlock(&server->probe_lock);
192 return 0;
193}
194
195/*
196 * Find the server record by peer address and record a probe to the cache
197 * manager from a server.
198 */
199static int afs_find_cm_server_by_peer(struct afs_call *call)
200{
201 struct sockaddr_rxrpc srx;
202 struct afs_server *server;
203
204 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
205
206 server = afs_find_server(call->net, &srx);
207 if (!server) {
208 trace_afs_cm_no_server(call, &srx);
209 return 0;
210 }
211
David Howellsffba7182019-05-09 22:22:50 +0100212 call->server = server;
David Howells3bf0fb62018-10-20 00:57:59 +0100213 return afs_record_cm_probe(call, server);
214}
215
216/*
217 * Find the server record by server UUID and record a probe to the cache
218 * manager from a server.
219 */
220static int afs_find_cm_server_by_uuid(struct afs_call *call,
221 struct afs_uuid *uuid)
222{
223 struct afs_server *server;
224
225 rcu_read_lock();
226 server = afs_find_server_by_uuid(call->net, call->request);
227 rcu_read_unlock();
228 if (!server) {
229 trace_afs_cm_no_server_u(call, call->request);
230 return 0;
231 }
232
David Howellsffba7182019-05-09 22:22:50 +0100233 call->server = server;
David Howells3bf0fb62018-10-20 00:57:59 +0100234 return afs_record_cm_probe(call, server);
235}
236
237/*
David Howells428edad2018-05-12 00:28:58 +0100238 * Clean up a cache manager call.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
David Howells08e0e7c2007-04-26 15:55:03 -0700240static void afs_cm_destructor(struct afs_call *call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
David Howells08e0e7c2007-04-26 15:55:03 -0700242 kfree(call->buffer);
243 call->buffer = NULL;
David Howellsec268152007-04-26 15:49:28 -0700244}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
David Howellsc435ee32017-11-02 15:27:49 +0000247 * The server supplied a list of callbacks that it wanted to break.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
David Howells08e0e7c2007-04-26 15:55:03 -0700249static void SRXAFSCB_CallBack(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
David Howells08e0e7c2007-04-26 15:55:03 -0700251 struct afs_call *call = container_of(work, struct afs_call, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David Howells08e0e7c2007-04-26 15:55:03 -0700253 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David Howells428edad2018-05-12 00:28:58 +0100255 /* We need to break the callbacks before sending the reply as the
256 * server holds up change visibility till it receives our reply so as
257 * to maintain cache coherency.
258 */
David Howells45218192019-06-20 18:12:17 +0100259 if (call->server) {
260 trace_afs_server(call->server, atomic_read(&call->server->usage),
261 afs_server_trace_callback);
David Howellsffba7182019-05-09 22:22:50 +0100262 afs_break_callbacks(call->server, call->count, call->request);
David Howells45218192019-06-20 18:12:17 +0100263 }
David Howells428edad2018-05-12 00:28:58 +0100264
265 afs_send_empty_reply(call);
David Howells341f7412017-01-05 10:38:36 +0000266 afs_put_call(call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700268}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270/*
David Howells08e0e7c2007-04-26 15:55:03 -0700271 * deliver request data to a CB.CallBack call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
David Howellsd0016482016-08-30 20:42:14 +0100273static int afs_deliver_cb_callback(struct afs_call *call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
David Howells5cf9dd52018-04-09 21:12:31 +0100275 struct afs_callback_break *cb;
David Howells08e0e7c2007-04-26 15:55:03 -0700276 __be32 *bp;
David Howells08e0e7c2007-04-26 15:55:03 -0700277 int ret, loop;
278
David Howellsd0016482016-08-30 20:42:14 +0100279 _enter("{%u}", call->unmarshall);
David Howells08e0e7c2007-04-26 15:55:03 -0700280
281 switch (call->unmarshall) {
282 case 0:
David Howells12bdcf32018-10-20 00:57:56 +0100283 afs_extract_to_tmp(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700284 call->unmarshall++;
285
286 /* extract the FID array and its count in two steps */
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600287 /* fall through */
David Howells08e0e7c2007-04-26 15:55:03 -0700288 case 1:
289 _debug("extract FID count");
David Howells12bdcf32018-10-20 00:57:56 +0100290 ret = afs_extract_data(call, true);
David Howells372ee162016-08-03 14:11:40 +0100291 if (ret < 0)
292 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700293
294 call->count = ntohl(call->tmp);
295 _debug("FID count: %u", call->count);
296 if (call->count > AFSCBMAX)
David Howells160cb952018-10-20 00:57:56 +0100297 return afs_protocol_error(call, -EBADMSG,
298 afs_eproto_cb_fid_count);
David Howells08e0e7c2007-04-26 15:55:03 -0700299
Kees Cook6da2ec52018-06-12 13:55:00 -0700300 call->buffer = kmalloc(array3_size(call->count, 3, 4),
301 GFP_KERNEL);
David Howells08e0e7c2007-04-26 15:55:03 -0700302 if (!call->buffer)
303 return -ENOMEM;
David Howells12bdcf32018-10-20 00:57:56 +0100304 afs_extract_to_buf(call, call->count * 3 * 4);
David Howells08e0e7c2007-04-26 15:55:03 -0700305 call->unmarshall++;
306
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600307 /* Fall through */
David Howells08e0e7c2007-04-26 15:55:03 -0700308 case 2:
309 _debug("extract FID array");
David Howells12bdcf32018-10-20 00:57:56 +0100310 ret = afs_extract_data(call, true);
David Howells372ee162016-08-03 14:11:40 +0100311 if (ret < 0)
312 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700313
314 _debug("unmarshall FID array");
315 call->request = kcalloc(call->count,
David Howells5cf9dd52018-04-09 21:12:31 +0100316 sizeof(struct afs_callback_break),
David Howells08e0e7c2007-04-26 15:55:03 -0700317 GFP_KERNEL);
318 if (!call->request)
319 return -ENOMEM;
320
321 cb = call->request;
322 bp = call->buffer;
323 for (loop = call->count; loop > 0; loop--, cb++) {
324 cb->fid.vid = ntohl(*bp++);
325 cb->fid.vnode = ntohl(*bp++);
326 cb->fid.unique = ntohl(*bp++);
David Howells08e0e7c2007-04-26 15:55:03 -0700327 }
328
David Howells12bdcf32018-10-20 00:57:56 +0100329 afs_extract_to_tmp(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700330 call->unmarshall++;
331
332 /* extract the callback array and its count in two steps */
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600333 /* fall through */
David Howells08e0e7c2007-04-26 15:55:03 -0700334 case 3:
335 _debug("extract CB count");
David Howells12bdcf32018-10-20 00:57:56 +0100336 ret = afs_extract_data(call, true);
David Howells372ee162016-08-03 14:11:40 +0100337 if (ret < 0)
338 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700339
Marc Dionnebcd89272017-03-16 16:27:44 +0000340 call->count2 = ntohl(call->tmp);
341 _debug("CB count: %u", call->count2);
342 if (call->count2 != call->count && call->count2 != 0)
David Howells160cb952018-10-20 00:57:56 +0100343 return afs_protocol_error(call, -EBADMSG,
344 afs_eproto_cb_count);
David Howells06aeb292018-10-20 00:57:58 +0100345 call->_iter = &call->iter;
346 iov_iter_discard(&call->iter, READ, call->count2 * 3 * 4);
David Howells08e0e7c2007-04-26 15:55:03 -0700347 call->unmarshall++;
David Howells08e0e7c2007-04-26 15:55:03 -0700348
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600349 /* Fall through */
David Howells08e0e7c2007-04-26 15:55:03 -0700350 case 4:
David Howells06aeb292018-10-20 00:57:58 +0100351 _debug("extract discard %zu/%u",
352 iov_iter_count(&call->iter), call->count2 * 3 * 4);
353
David Howells12bdcf32018-10-20 00:57:56 +0100354 ret = afs_extract_data(call, false);
David Howells372ee162016-08-03 14:11:40 +0100355 if (ret < 0)
356 return ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700357
David Howells08e0e7c2007-04-26 15:55:03 -0700358 call->unmarshall++;
David Howellsd0016482016-08-30 20:42:14 +0100359 case 5:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Howells98bf40c2017-11-02 15:27:53 +0000363 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
David Howellsf51375c2018-10-20 00:57:57 +0100364 return afs_io_error(call, afs_io_error_cm_reply);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
David Howells08e0e7c2007-04-26 15:55:03 -0700366 /* we'll need the file server record as that tells us which set of
367 * vnodes to operate upon */
David Howells3bf0fb62018-10-20 00:57:59 +0100368 return afs_find_cm_server_by_peer(call);
David Howellsec268152007-04-26 15:49:28 -0700369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*
David Howells08e0e7c2007-04-26 15:55:03 -0700372 * allow the fileserver to request callback state (re-)initialisation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
David Howells08e0e7c2007-04-26 15:55:03 -0700374static void SRXAFSCB_InitCallBackState(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
David Howells08e0e7c2007-04-26 15:55:03 -0700376 struct afs_call *call = container_of(work, struct afs_call, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David Howellsffba7182019-05-09 22:22:50 +0100378 _enter("{%p}", call->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Howellsffba7182019-05-09 22:22:50 +0100380 if (call->server)
381 afs_init_callback_state(call->server);
David Howells08e0e7c2007-04-26 15:55:03 -0700382 afs_send_empty_reply(call);
David Howells341f7412017-01-05 10:38:36 +0000383 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700384 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
David Howells08e0e7c2007-04-26 15:55:03 -0700388 * deliver request data to a CB.InitCallBackState call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
David Howellsd0016482016-08-30 20:42:14 +0100390static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
David Howells372ee162016-08-03 14:11:40 +0100392 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David Howellsd0016482016-08-30 20:42:14 +0100394 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
David Howells12bdcf32018-10-20 00:57:56 +0100396 afs_extract_discard(call, 0);
397 ret = afs_extract_data(call, false);
David Howells372ee162016-08-03 14:11:40 +0100398 if (ret < 0)
399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Howells08e0e7c2007-04-26 15:55:03 -0700401 /* we'll need the file server record as that tells us which set of
402 * vnodes to operate upon */
David Howells3bf0fb62018-10-20 00:57:59 +0100403 return afs_find_cm_server_by_peer(call);
David Howellsec268152007-04-26 15:49:28 -0700404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
David Howellsc35eccb2007-04-26 15:58:49 -0700407 * deliver request data to a CB.InitCallBackState3 call
408 */
David Howellsd0016482016-08-30 20:42:14 +0100409static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
David Howellsc35eccb2007-04-26 15:58:49 -0700410{
Christoph Hellwig41bb26f2017-05-28 08:56:46 +0300411 struct afs_uuid *r;
David Howellsd0016482016-08-30 20:42:14 +0100412 unsigned loop;
413 __be32 *b;
414 int ret;
David Howellsc35eccb2007-04-26 15:58:49 -0700415
David Howellsd0016482016-08-30 20:42:14 +0100416 _enter("");
David Howellsc35eccb2007-04-26 15:58:49 -0700417
David Howellsd0016482016-08-30 20:42:14 +0100418 _enter("{%u}", call->unmarshall);
419
420 switch (call->unmarshall) {
421 case 0:
Kees Cook6da2ec52018-06-12 13:55:00 -0700422 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
David Howellsd0016482016-08-30 20:42:14 +0100423 if (!call->buffer)
424 return -ENOMEM;
David Howells12bdcf32018-10-20 00:57:56 +0100425 afs_extract_to_buf(call, 11 * sizeof(__be32));
David Howellsd0016482016-08-30 20:42:14 +0100426 call->unmarshall++;
427
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600428 /* Fall through */
David Howellsd0016482016-08-30 20:42:14 +0100429 case 1:
430 _debug("extract UUID");
David Howells12bdcf32018-10-20 00:57:56 +0100431 ret = afs_extract_data(call, false);
David Howellsd0016482016-08-30 20:42:14 +0100432 switch (ret) {
433 case 0: break;
434 case -EAGAIN: return 0;
435 default: return ret;
436 }
437
438 _debug("unmarshall UUID");
Christoph Hellwig41bb26f2017-05-28 08:56:46 +0300439 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
David Howellsd0016482016-08-30 20:42:14 +0100440 if (!call->request)
441 return -ENOMEM;
442
443 b = call->buffer;
444 r = call->request;
David Howellsff548772017-02-10 16:34:07 +0000445 r->time_low = b[0];
446 r->time_mid = htons(ntohl(b[1]));
447 r->time_hi_and_version = htons(ntohl(b[2]));
David Howellsd0016482016-08-30 20:42:14 +0100448 r->clock_seq_hi_and_reserved = ntohl(b[3]);
449 r->clock_seq_low = ntohl(b[4]);
450
451 for (loop = 0; loop < 6; loop++)
452 r->node[loop] = ntohl(b[loop + 5]);
453
David Howellsd0016482016-08-30 20:42:14 +0100454 call->unmarshall++;
455
456 case 2:
457 break;
458 }
David Howellsc35eccb2007-04-26 15:58:49 -0700459
David Howells98bf40c2017-11-02 15:27:53 +0000460 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
David Howellsf51375c2018-10-20 00:57:57 +0100461 return afs_io_error(call, afs_io_error_cm_reply);
David Howellsc35eccb2007-04-26 15:58:49 -0700462
463 /* we'll need the file server record as that tells us which set of
464 * vnodes to operate upon */
David Howells3bf0fb62018-10-20 00:57:59 +0100465 return afs_find_cm_server_by_uuid(call, call->request);
David Howellsc35eccb2007-04-26 15:58:49 -0700466}
467
468/*
David Howells08e0e7c2007-04-26 15:55:03 -0700469 * allow the fileserver to see if the cache manager is still alive
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
David Howells08e0e7c2007-04-26 15:55:03 -0700471static void SRXAFSCB_Probe(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David Howells08e0e7c2007-04-26 15:55:03 -0700473 struct afs_call *call = container_of(work, struct afs_call, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
David Howells08e0e7c2007-04-26 15:55:03 -0700475 _enter("");
476 afs_send_empty_reply(call);
David Howells341f7412017-01-05 10:38:36 +0000477 afs_put_call(call);
David Howells08e0e7c2007-04-26 15:55:03 -0700478 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700479}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
David Howells08e0e7c2007-04-26 15:55:03 -0700482 * deliver request data to a CB.Probe call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
David Howellsd0016482016-08-30 20:42:14 +0100484static int afs_deliver_cb_probe(struct afs_call *call)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
David Howells372ee162016-08-03 14:11:40 +0100486 int ret;
487
David Howellsd0016482016-08-30 20:42:14 +0100488 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
David Howells12bdcf32018-10-20 00:57:56 +0100490 afs_extract_discard(call, 0);
491 ret = afs_extract_data(call, false);
David Howells372ee162016-08-03 14:11:40 +0100492 if (ret < 0)
493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells98bf40c2017-11-02 15:27:53 +0000495 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
David Howellsf51375c2018-10-20 00:57:57 +0100496 return afs_io_error(call, afs_io_error_cm_reply);
David Howells3bf0fb62018-10-20 00:57:59 +0100497 return afs_find_cm_server_by_peer(call);
David Howellsec268152007-04-26 15:49:28 -0700498}
David Howellsb908fe62007-04-26 15:58:17 -0700499
500/*
David Howells9396d492008-04-29 01:03:22 -0700501 * allow the fileserver to quickly find out if the fileserver has been rebooted
502 */
503static void SRXAFSCB_ProbeUuid(struct work_struct *work)
504{
505 struct afs_call *call = container_of(work, struct afs_call, work);
Christoph Hellwig41bb26f2017-05-28 08:56:46 +0300506 struct afs_uuid *r = call->request;
David Howells9396d492008-04-29 01:03:22 -0700507
David Howells9396d492008-04-29 01:03:22 -0700508 _enter("");
509
David Howellsf044c882017-11-02 15:27:45 +0000510 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
David Howells2067b2b2019-07-30 14:38:51 +0100511 afs_send_empty_reply(call);
David Howells9396d492008-04-29 01:03:22 -0700512 else
David Howells2067b2b2019-07-30 14:38:51 +0100513 rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
514 1, 1, "K-1");
David Howells9396d492008-04-29 01:03:22 -0700515
David Howells341f7412017-01-05 10:38:36 +0000516 afs_put_call(call);
David Howells9396d492008-04-29 01:03:22 -0700517 _leave("");
518}
519
520/*
521 * deliver request data to a CB.ProbeUuid call
522 */
David Howellsd0016482016-08-30 20:42:14 +0100523static int afs_deliver_cb_probe_uuid(struct afs_call *call)
David Howells9396d492008-04-29 01:03:22 -0700524{
Christoph Hellwig41bb26f2017-05-28 08:56:46 +0300525 struct afs_uuid *r;
David Howells9396d492008-04-29 01:03:22 -0700526 unsigned loop;
527 __be32 *b;
528 int ret;
529
David Howellsd0016482016-08-30 20:42:14 +0100530 _enter("{%u}", call->unmarshall);
David Howells9396d492008-04-29 01:03:22 -0700531
532 switch (call->unmarshall) {
533 case 0:
Kees Cook6da2ec52018-06-12 13:55:00 -0700534 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
David Howells9396d492008-04-29 01:03:22 -0700535 if (!call->buffer)
536 return -ENOMEM;
David Howells12bdcf32018-10-20 00:57:56 +0100537 afs_extract_to_buf(call, 11 * sizeof(__be32));
David Howells9396d492008-04-29 01:03:22 -0700538 call->unmarshall++;
539
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600540 /* Fall through */
David Howells9396d492008-04-29 01:03:22 -0700541 case 1:
542 _debug("extract UUID");
David Howells12bdcf32018-10-20 00:57:56 +0100543 ret = afs_extract_data(call, false);
David Howells9396d492008-04-29 01:03:22 -0700544 switch (ret) {
545 case 0: break;
546 case -EAGAIN: return 0;
547 default: return ret;
548 }
549
550 _debug("unmarshall UUID");
Christoph Hellwig41bb26f2017-05-28 08:56:46 +0300551 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
David Howells9396d492008-04-29 01:03:22 -0700552 if (!call->request)
553 return -ENOMEM;
554
555 b = call->buffer;
556 r = call->request;
David Howellsfe342cf2018-04-09 21:12:31 +0100557 r->time_low = b[0];
558 r->time_mid = htons(ntohl(b[1]));
559 r->time_hi_and_version = htons(ntohl(b[2]));
David Howells9396d492008-04-29 01:03:22 -0700560 r->clock_seq_hi_and_reserved = ntohl(b[3]);
561 r->clock_seq_low = ntohl(b[4]);
562
563 for (loop = 0; loop < 6; loop++)
564 r->node[loop] = ntohl(b[loop + 5]);
565
David Howells9396d492008-04-29 01:03:22 -0700566 call->unmarshall++;
567
568 case 2:
David Howells9396d492008-04-29 01:03:22 -0700569 break;
570 }
571
David Howells98bf40c2017-11-02 15:27:53 +0000572 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
David Howellsf51375c2018-10-20 00:57:57 +0100573 return afs_io_error(call, afs_io_error_cm_reply);
David Howells3bf0fb62018-10-20 00:57:59 +0100574 return afs_find_cm_server_by_uuid(call, call->request);
David Howells9396d492008-04-29 01:03:22 -0700575}
576
577/*
David Howellsb908fe62007-04-26 15:58:17 -0700578 * allow the fileserver to ask about the cache manager's capabilities
579 */
David Howells7c80bcc2008-04-29 01:03:21 -0700580static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
David Howellsb908fe62007-04-26 15:58:17 -0700581{
David Howellsb908fe62007-04-26 15:58:17 -0700582 struct afs_call *call = container_of(work, struct afs_call, work);
Florian Westphal35ebfc22019-05-31 18:27:03 +0200583 int loop;
David Howellsb908fe62007-04-26 15:58:17 -0700584
585 struct {
586 struct /* InterfaceAddr */ {
587 __be32 nifs;
588 __be32 uuid[11];
589 __be32 ifaddr[32];
590 __be32 netmask[32];
591 __be32 mtu[32];
592 } ia;
593 struct /* Capabilities */ {
594 __be32 capcount;
595 __be32 caps[1];
596 } cap;
597 } reply;
598
599 _enter("");
600
David Howellsb908fe62007-04-26 15:58:17 -0700601 memset(&reply, 0, sizeof(reply));
David Howellsb908fe62007-04-26 15:58:17 -0700602
David Howellsf044c882017-11-02 15:27:45 +0000603 reply.ia.uuid[0] = call->net->uuid.time_low;
604 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
605 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
606 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
607 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
David Howellsb908fe62007-04-26 15:58:17 -0700608 for (loop = 0; loop < 6; loop++)
David Howellsf044c882017-11-02 15:27:45 +0000609 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
David Howellsb908fe62007-04-26 15:58:17 -0700610
David Howellsb908fe62007-04-26 15:58:17 -0700611 reply.cap.capcount = htonl(1);
612 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
613 afs_send_simple_reply(call, &reply, sizeof(reply));
David Howells341f7412017-01-05 10:38:36 +0000614 afs_put_call(call);
David Howellsb908fe62007-04-26 15:58:17 -0700615 _leave("");
616}
617
618/*
David Howells7c80bcc2008-04-29 01:03:21 -0700619 * deliver request data to a CB.TellMeAboutYourself call
David Howellsb908fe62007-04-26 15:58:17 -0700620 */
David Howellsd0016482016-08-30 20:42:14 +0100621static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
David Howellsb908fe62007-04-26 15:58:17 -0700622{
David Howells372ee162016-08-03 14:11:40 +0100623 int ret;
624
David Howellsd0016482016-08-30 20:42:14 +0100625 _enter("");
David Howellsb908fe62007-04-26 15:58:17 -0700626
David Howells12bdcf32018-10-20 00:57:56 +0100627 afs_extract_discard(call, 0);
628 ret = afs_extract_data(call, false);
David Howells372ee162016-08-03 14:11:40 +0100629 if (ret < 0)
630 return ret;
David Howellsb908fe62007-04-26 15:58:17 -0700631
David Howells98bf40c2017-11-02 15:27:53 +0000632 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
David Howellsf51375c2018-10-20 00:57:57 +0100633 return afs_io_error(call, afs_io_error_cm_reply);
David Howells3bf0fb62018-10-20 00:57:59 +0100634 return afs_find_cm_server_by_peer(call);
David Howellsb908fe62007-04-26 15:58:17 -0700635}
David Howells35dbfba2018-10-20 00:57:58 +0100636
637/*
638 * deliver request data to a YFS CB.CallBack call
639 */
640static int afs_deliver_yfs_cb_callback(struct afs_call *call)
641{
642 struct afs_callback_break *cb;
David Howells35dbfba2018-10-20 00:57:58 +0100643 struct yfs_xdr_YFSFid *bp;
644 size_t size;
645 int ret, loop;
646
647 _enter("{%u}", call->unmarshall);
648
649 switch (call->unmarshall) {
650 case 0:
651 afs_extract_to_tmp(call);
652 call->unmarshall++;
653
654 /* extract the FID array and its count in two steps */
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600655 /* Fall through */
David Howells35dbfba2018-10-20 00:57:58 +0100656 case 1:
657 _debug("extract FID count");
658 ret = afs_extract_data(call, true);
659 if (ret < 0)
660 return ret;
661
662 call->count = ntohl(call->tmp);
663 _debug("FID count: %u", call->count);
664 if (call->count > YFSCBMAX)
665 return afs_protocol_error(call, -EBADMSG,
666 afs_eproto_cb_fid_count);
667
668 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid));
669 call->buffer = kmalloc(size, GFP_KERNEL);
670 if (!call->buffer)
671 return -ENOMEM;
672 afs_extract_to_buf(call, size);
673 call->unmarshall++;
674
Gustavo A. R. Silvae690c9e2019-01-10 15:52:25 -0600675 /* Fall through */
David Howells35dbfba2018-10-20 00:57:58 +0100676 case 2:
677 _debug("extract FID array");
678 ret = afs_extract_data(call, false);
679 if (ret < 0)
680 return ret;
681
682 _debug("unmarshall FID array");
683 call->request = kcalloc(call->count,
684 sizeof(struct afs_callback_break),
685 GFP_KERNEL);
686 if (!call->request)
687 return -ENOMEM;
688
689 cb = call->request;
690 bp = call->buffer;
691 for (loop = call->count; loop > 0; loop--, cb++) {
692 cb->fid.vid = xdr_to_u64(bp->volume);
693 cb->fid.vnode = xdr_to_u64(bp->vnode.lo);
694 cb->fid.vnode_hi = ntohl(bp->vnode.hi);
695 cb->fid.unique = ntohl(bp->vnode.unique);
696 bp++;
697 }
698
699 afs_extract_to_tmp(call);
700 call->unmarshall++;
701
702 case 3:
703 break;
704 }
705
706 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
707 return afs_io_error(call, afs_io_error_cm_reply);
708
709 /* We'll need the file server record as that tells us which set of
710 * vnodes to operate upon.
711 */
David Howells3bf0fb62018-10-20 00:57:59 +0100712 return afs_find_cm_server_by_peer(call);
David Howells35dbfba2018-10-20 00:57:58 +0100713}