xref: /openbmc/linux/fs/afs/cmservice.c (revision bc5aa3a0)
1 /* AFS Cache Manager Service
2  *
3  * Copyright (C) 2002 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 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/ip.h>
17 #include "internal.h"
18 #include "afs_cm.h"
19 
20 #if 0
21 struct workqueue_struct *afs_cm_workqueue;
22 #endif  /*  0  */
23 
24 static int afs_deliver_cb_init_call_back_state(struct afs_call *,
25 					       struct sk_buff *, bool);
26 static int afs_deliver_cb_init_call_back_state3(struct afs_call *,
27 						struct sk_buff *, bool);
28 static int afs_deliver_cb_probe(struct afs_call *, struct sk_buff *, bool);
29 static int afs_deliver_cb_callback(struct afs_call *, struct sk_buff *, bool);
30 static int afs_deliver_cb_probe_uuid(struct afs_call *, struct sk_buff *, bool);
31 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *,
32 						 struct sk_buff *, bool);
33 static void afs_cm_destructor(struct afs_call *);
34 
35 /*
36  * CB.CallBack operation type
37  */
38 static const struct afs_call_type afs_SRXCBCallBack = {
39 	.name		= "CB.CallBack",
40 	.deliver	= afs_deliver_cb_callback,
41 	.abort_to_error	= afs_abort_to_error,
42 	.destructor	= afs_cm_destructor,
43 };
44 
45 /*
46  * CB.InitCallBackState operation type
47  */
48 static const struct afs_call_type afs_SRXCBInitCallBackState = {
49 	.name		= "CB.InitCallBackState",
50 	.deliver	= afs_deliver_cb_init_call_back_state,
51 	.abort_to_error	= afs_abort_to_error,
52 	.destructor	= afs_cm_destructor,
53 };
54 
55 /*
56  * CB.InitCallBackState3 operation type
57  */
58 static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
59 	.name		= "CB.InitCallBackState3",
60 	.deliver	= afs_deliver_cb_init_call_back_state3,
61 	.abort_to_error	= afs_abort_to_error,
62 	.destructor	= afs_cm_destructor,
63 };
64 
65 /*
66  * CB.Probe operation type
67  */
68 static const struct afs_call_type afs_SRXCBProbe = {
69 	.name		= "CB.Probe",
70 	.deliver	= afs_deliver_cb_probe,
71 	.abort_to_error	= afs_abort_to_error,
72 	.destructor	= afs_cm_destructor,
73 };
74 
75 /*
76  * CB.ProbeUuid operation type
77  */
78 static const struct afs_call_type afs_SRXCBProbeUuid = {
79 	.name		= "CB.ProbeUuid",
80 	.deliver	= afs_deliver_cb_probe_uuid,
81 	.abort_to_error	= afs_abort_to_error,
82 	.destructor	= afs_cm_destructor,
83 };
84 
85 /*
86  * CB.TellMeAboutYourself operation type
87  */
88 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
89 	.name		= "CB.TellMeAboutYourself",
90 	.deliver	= afs_deliver_cb_tell_me_about_yourself,
91 	.abort_to_error	= afs_abort_to_error,
92 	.destructor	= afs_cm_destructor,
93 };
94 
95 /*
96  * route an incoming cache manager call
97  * - return T if supported, F if not
98  */
99 bool afs_cm_incoming_call(struct afs_call *call)
100 {
101 	u32 operation_id = ntohl(call->operation_ID);
102 
103 	_enter("{CB.OP %u}", operation_id);
104 
105 	switch (operation_id) {
106 	case CBCallBack:
107 		call->type = &afs_SRXCBCallBack;
108 		return true;
109 	case CBInitCallBackState:
110 		call->type = &afs_SRXCBInitCallBackState;
111 		return true;
112 	case CBInitCallBackState3:
113 		call->type = &afs_SRXCBInitCallBackState3;
114 		return true;
115 	case CBProbe:
116 		call->type = &afs_SRXCBProbe;
117 		return true;
118 	case CBTellMeAboutYourself:
119 		call->type = &afs_SRXCBTellMeAboutYourself;
120 		return true;
121 	default:
122 		return false;
123 	}
124 }
125 
126 /*
127  * clean up a cache manager call
128  */
129 static void afs_cm_destructor(struct afs_call *call)
130 {
131 	_enter("");
132 
133 	/* Break the callbacks here so that we do it after the final ACK is
134 	 * received.  The step number here must match the final number in
135 	 * afs_deliver_cb_callback().
136 	 */
137 	if (call->unmarshall == 6) {
138 		ASSERT(call->server && call->count && call->request);
139 		afs_break_callbacks(call->server, call->count, call->request);
140 	}
141 
142 	afs_put_server(call->server);
143 	call->server = NULL;
144 	kfree(call->buffer);
145 	call->buffer = NULL;
146 }
147 
148 /*
149  * allow the fileserver to see if the cache manager is still alive
150  */
151 static void SRXAFSCB_CallBack(struct work_struct *work)
152 {
153 	struct afs_call *call = container_of(work, struct afs_call, work);
154 
155 	_enter("");
156 
157 	/* be sure to send the reply *before* attempting to spam the AFS server
158 	 * with FSFetchStatus requests on the vnodes with broken callbacks lest
159 	 * the AFS server get into a vicious cycle of trying to break further
160 	 * callbacks because it hadn't received completion of the CBCallBack op
161 	 * yet */
162 	afs_send_empty_reply(call);
163 
164 	afs_break_callbacks(call->server, call->count, call->request);
165 	_leave("");
166 }
167 
168 /*
169  * deliver request data to a CB.CallBack call
170  */
171 static int afs_deliver_cb_callback(struct afs_call *call, struct sk_buff *skb,
172 				   bool last)
173 {
174 	struct afs_callback *cb;
175 	struct afs_server *server;
176 	struct in_addr addr;
177 	__be32 *bp;
178 	u32 tmp;
179 	int ret, loop;
180 
181 	_enter("{%u},{%u},%d", call->unmarshall, skb->len, last);
182 
183 	switch (call->unmarshall) {
184 	case 0:
185 		call->offset = 0;
186 		call->unmarshall++;
187 
188 		/* extract the FID array and its count in two steps */
189 	case 1:
190 		_debug("extract FID count");
191 		ret = afs_extract_data(call, skb, last, &call->tmp, 4);
192 		if (ret < 0)
193 			return ret;
194 
195 		call->count = ntohl(call->tmp);
196 		_debug("FID count: %u", call->count);
197 		if (call->count > AFSCBMAX)
198 			return -EBADMSG;
199 
200 		call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL);
201 		if (!call->buffer)
202 			return -ENOMEM;
203 		call->offset = 0;
204 		call->unmarshall++;
205 
206 	case 2:
207 		_debug("extract FID array");
208 		ret = afs_extract_data(call, skb, last, call->buffer,
209 				       call->count * 3 * 4);
210 		if (ret < 0)
211 			return ret;
212 
213 		_debug("unmarshall FID array");
214 		call->request = kcalloc(call->count,
215 					sizeof(struct afs_callback),
216 					GFP_KERNEL);
217 		if (!call->request)
218 			return -ENOMEM;
219 
220 		cb = call->request;
221 		bp = call->buffer;
222 		for (loop = call->count; loop > 0; loop--, cb++) {
223 			cb->fid.vid	= ntohl(*bp++);
224 			cb->fid.vnode	= ntohl(*bp++);
225 			cb->fid.unique	= ntohl(*bp++);
226 			cb->type	= AFSCM_CB_UNTYPED;
227 		}
228 
229 		call->offset = 0;
230 		call->unmarshall++;
231 
232 		/* extract the callback array and its count in two steps */
233 	case 3:
234 		_debug("extract CB count");
235 		ret = afs_extract_data(call, skb, last, &call->tmp, 4);
236 		if (ret < 0)
237 			return ret;
238 
239 		tmp = ntohl(call->tmp);
240 		_debug("CB count: %u", tmp);
241 		if (tmp != call->count && tmp != 0)
242 			return -EBADMSG;
243 		call->offset = 0;
244 		call->unmarshall++;
245 		if (tmp == 0)
246 			goto empty_cb_array;
247 
248 	case 4:
249 		_debug("extract CB array");
250 		ret = afs_extract_data(call, skb, last, call->request,
251 				       call->count * 3 * 4);
252 		if (ret < 0)
253 			return ret;
254 
255 		_debug("unmarshall CB array");
256 		cb = call->request;
257 		bp = call->buffer;
258 		for (loop = call->count; loop > 0; loop--, cb++) {
259 			cb->version	= ntohl(*bp++);
260 			cb->expiry	= ntohl(*bp++);
261 			cb->type	= ntohl(*bp++);
262 		}
263 
264 	empty_cb_array:
265 		call->offset = 0;
266 		call->unmarshall++;
267 
268 	case 5:
269 		ret = afs_data_complete(call, skb, last);
270 		if (ret < 0)
271 			return ret;
272 
273 		/* Record that the message was unmarshalled successfully so
274 		 * that the call destructor can know do the callback breaking
275 		 * work, even if the final ACK isn't received.
276 		 *
277 		 * If the step number changes, then afs_cm_destructor() must be
278 		 * updated also.
279 		 */
280 		call->unmarshall++;
281 	case 6:
282 		break;
283 	}
284 
285 
286 	call->state = AFS_CALL_REPLYING;
287 
288 	/* we'll need the file server record as that tells us which set of
289 	 * vnodes to operate upon */
290 	memcpy(&addr, &ip_hdr(skb)->saddr, 4);
291 	server = afs_find_server(&addr);
292 	if (!server)
293 		return -ENOTCONN;
294 	call->server = server;
295 
296 	INIT_WORK(&call->work, SRXAFSCB_CallBack);
297 	queue_work(afs_wq, &call->work);
298 	return 0;
299 }
300 
301 /*
302  * allow the fileserver to request callback state (re-)initialisation
303  */
304 static void SRXAFSCB_InitCallBackState(struct work_struct *work)
305 {
306 	struct afs_call *call = container_of(work, struct afs_call, work);
307 
308 	_enter("{%p}", call->server);
309 
310 	afs_init_callback_state(call->server);
311 	afs_send_empty_reply(call);
312 	_leave("");
313 }
314 
315 /*
316  * deliver request data to a CB.InitCallBackState call
317  */
318 static int afs_deliver_cb_init_call_back_state(struct afs_call *call,
319 					       struct sk_buff *skb,
320 					       bool last)
321 {
322 	struct afs_server *server;
323 	struct in_addr addr;
324 	int ret;
325 
326 	_enter(",{%u},%d", skb->len, last);
327 
328 	ret = afs_data_complete(call, skb, last);
329 	if (ret < 0)
330 		return ret;
331 
332 	/* no unmarshalling required */
333 	call->state = AFS_CALL_REPLYING;
334 
335 	/* we'll need the file server record as that tells us which set of
336 	 * vnodes to operate upon */
337 	memcpy(&addr, &ip_hdr(skb)->saddr, 4);
338 	server = afs_find_server(&addr);
339 	if (!server)
340 		return -ENOTCONN;
341 	call->server = server;
342 
343 	INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
344 	queue_work(afs_wq, &call->work);
345 	return 0;
346 }
347 
348 /*
349  * deliver request data to a CB.InitCallBackState3 call
350  */
351 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call,
352 						struct sk_buff *skb,
353 						bool last)
354 {
355 	struct afs_server *server;
356 	struct in_addr addr;
357 
358 	_enter(",{%u},%d", skb->len, last);
359 
360 	/* There are some arguments that we ignore */
361 	afs_data_consumed(call, skb);
362 	if (!last)
363 		return -EAGAIN;
364 
365 	/* no unmarshalling required */
366 	call->state = AFS_CALL_REPLYING;
367 
368 	/* we'll need the file server record as that tells us which set of
369 	 * vnodes to operate upon */
370 	memcpy(&addr, &ip_hdr(skb)->saddr, 4);
371 	server = afs_find_server(&addr);
372 	if (!server)
373 		return -ENOTCONN;
374 	call->server = server;
375 
376 	INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
377 	queue_work(afs_wq, &call->work);
378 	return 0;
379 }
380 
381 /*
382  * allow the fileserver to see if the cache manager is still alive
383  */
384 static void SRXAFSCB_Probe(struct work_struct *work)
385 {
386 	struct afs_call *call = container_of(work, struct afs_call, work);
387 
388 	_enter("");
389 	afs_send_empty_reply(call);
390 	_leave("");
391 }
392 
393 /*
394  * deliver request data to a CB.Probe call
395  */
396 static int afs_deliver_cb_probe(struct afs_call *call, struct sk_buff *skb,
397 				bool last)
398 {
399 	int ret;
400 
401 	_enter(",{%u},%d", skb->len, last);
402 
403 	ret = afs_data_complete(call, skb, last);
404 	if (ret < 0)
405 		return ret;
406 
407 	/* no unmarshalling required */
408 	call->state = AFS_CALL_REPLYING;
409 
410 	INIT_WORK(&call->work, SRXAFSCB_Probe);
411 	queue_work(afs_wq, &call->work);
412 	return 0;
413 }
414 
415 /*
416  * allow the fileserver to quickly find out if the fileserver has been rebooted
417  */
418 static void SRXAFSCB_ProbeUuid(struct work_struct *work)
419 {
420 	struct afs_call *call = container_of(work, struct afs_call, work);
421 	struct afs_uuid *r = call->request;
422 
423 	struct {
424 		__be32	match;
425 	} reply;
426 
427 	_enter("");
428 
429 
430 	if (memcmp(r, &afs_uuid, sizeof(afs_uuid)) == 0)
431 		reply.match = htonl(0);
432 	else
433 		reply.match = htonl(1);
434 
435 	afs_send_simple_reply(call, &reply, sizeof(reply));
436 	_leave("");
437 }
438 
439 /*
440  * deliver request data to a CB.ProbeUuid call
441  */
442 static int afs_deliver_cb_probe_uuid(struct afs_call *call, struct sk_buff *skb,
443 				     bool last)
444 {
445 	struct afs_uuid *r;
446 	unsigned loop;
447 	__be32 *b;
448 	int ret;
449 
450 	_enter("{%u},{%u},%d", call->unmarshall, skb->len, last);
451 
452 	ret = afs_data_complete(call, skb, last);
453 	if (ret < 0)
454 		return ret;
455 
456 	switch (call->unmarshall) {
457 	case 0:
458 		call->offset = 0;
459 		call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL);
460 		if (!call->buffer)
461 			return -ENOMEM;
462 		call->unmarshall++;
463 
464 	case 1:
465 		_debug("extract UUID");
466 		ret = afs_extract_data(call, skb, last, call->buffer,
467 				       11 * sizeof(__be32));
468 		switch (ret) {
469 		case 0:		break;
470 		case -EAGAIN:	return 0;
471 		default:	return ret;
472 		}
473 
474 		_debug("unmarshall UUID");
475 		call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
476 		if (!call->request)
477 			return -ENOMEM;
478 
479 		b = call->buffer;
480 		r = call->request;
481 		r->time_low			= ntohl(b[0]);
482 		r->time_mid			= ntohl(b[1]);
483 		r->time_hi_and_version		= ntohl(b[2]);
484 		r->clock_seq_hi_and_reserved 	= ntohl(b[3]);
485 		r->clock_seq_low		= ntohl(b[4]);
486 
487 		for (loop = 0; loop < 6; loop++)
488 			r->node[loop] = ntohl(b[loop + 5]);
489 
490 		call->offset = 0;
491 		call->unmarshall++;
492 
493 	case 2:
494 		_debug("trailer");
495 		if (skb->len != 0)
496 			return -EBADMSG;
497 		break;
498 	}
499 
500 	ret = afs_data_complete(call, skb, last);
501 	if (ret < 0)
502 		return ret;
503 
504 	call->state = AFS_CALL_REPLYING;
505 
506 	INIT_WORK(&call->work, SRXAFSCB_ProbeUuid);
507 	queue_work(afs_wq, &call->work);
508 	return 0;
509 }
510 
511 /*
512  * allow the fileserver to ask about the cache manager's capabilities
513  */
514 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
515 {
516 	struct afs_interface *ifs;
517 	struct afs_call *call = container_of(work, struct afs_call, work);
518 	int loop, nifs;
519 
520 	struct {
521 		struct /* InterfaceAddr */ {
522 			__be32 nifs;
523 			__be32 uuid[11];
524 			__be32 ifaddr[32];
525 			__be32 netmask[32];
526 			__be32 mtu[32];
527 		} ia;
528 		struct /* Capabilities */ {
529 			__be32 capcount;
530 			__be32 caps[1];
531 		} cap;
532 	} reply;
533 
534 	_enter("");
535 
536 	nifs = 0;
537 	ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
538 	if (ifs) {
539 		nifs = afs_get_ipv4_interfaces(ifs, 32, false);
540 		if (nifs < 0) {
541 			kfree(ifs);
542 			ifs = NULL;
543 			nifs = 0;
544 		}
545 	}
546 
547 	memset(&reply, 0, sizeof(reply));
548 	reply.ia.nifs = htonl(nifs);
549 
550 	reply.ia.uuid[0] = htonl(afs_uuid.time_low);
551 	reply.ia.uuid[1] = htonl(afs_uuid.time_mid);
552 	reply.ia.uuid[2] = htonl(afs_uuid.time_hi_and_version);
553 	reply.ia.uuid[3] = htonl((s8) afs_uuid.clock_seq_hi_and_reserved);
554 	reply.ia.uuid[4] = htonl((s8) afs_uuid.clock_seq_low);
555 	for (loop = 0; loop < 6; loop++)
556 		reply.ia.uuid[loop + 5] = htonl((s8) afs_uuid.node[loop]);
557 
558 	if (ifs) {
559 		for (loop = 0; loop < nifs; loop++) {
560 			reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
561 			reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
562 			reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
563 		}
564 		kfree(ifs);
565 	}
566 
567 	reply.cap.capcount = htonl(1);
568 	reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
569 	afs_send_simple_reply(call, &reply, sizeof(reply));
570 
571 	_leave("");
572 }
573 
574 /*
575  * deliver request data to a CB.TellMeAboutYourself call
576  */
577 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call,
578 						 struct sk_buff *skb, bool last)
579 {
580 	int ret;
581 
582 	_enter(",{%u},%d", skb->len, last);
583 
584 	ret = afs_data_complete(call, skb, last);
585 	if (ret < 0)
586 		return ret;
587 
588 	/* no unmarshalling required */
589 	call->state = AFS_CALL_REPLYING;
590 
591 	INIT_WORK(&call->work, SRXAFSCB_TellMeAboutYourself);
592 	queue_work(afs_wq, &call->work);
593 	return 0;
594 }
595