xref: /OK3568_Linux_fs/kernel/fs/dlm/midcomms.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun *******************************************************************************
4*4882a593Smuzhiyun **
5*4882a593Smuzhiyun **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
6*4882a593Smuzhiyun **  Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
7*4882a593Smuzhiyun **
8*4882a593Smuzhiyun **
9*4882a593Smuzhiyun *******************************************************************************
10*4882a593Smuzhiyun ******************************************************************************/
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun /*
13*4882a593Smuzhiyun  * midcomms.c
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * This is the appallingly named "mid-level" comms layer.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * Its purpose is to take packets from the "real" comms layer,
18*4882a593Smuzhiyun  * split them up into packets and pass them to the interested
19*4882a593Smuzhiyun  * part of the locking mechanism.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * It also takes messages from the locking layer, formats them
22*4882a593Smuzhiyun  * into packets and sends them to the comms layer.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <asm/unaligned.h>
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include "dlm_internal.h"
28*4882a593Smuzhiyun #include "lowcomms.h"
29*4882a593Smuzhiyun #include "config.h"
30*4882a593Smuzhiyun #include "lock.h"
31*4882a593Smuzhiyun #include "midcomms.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun  * Called from the low-level comms layer to process a buffer of
35*4882a593Smuzhiyun  * commands.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun 
dlm_process_incoming_buffer(int nodeid,unsigned char * buf,int len)38*4882a593Smuzhiyun int dlm_process_incoming_buffer(int nodeid, unsigned char *buf, int len)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	const unsigned char *ptr = buf;
41*4882a593Smuzhiyun 	const struct dlm_header *hd;
42*4882a593Smuzhiyun 	uint16_t msglen;
43*4882a593Smuzhiyun 	int ret = 0;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	while (len >= sizeof(struct dlm_header)) {
46*4882a593Smuzhiyun 		hd = (struct dlm_header *)ptr;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 		/* no message should be more than this otherwise we
49*4882a593Smuzhiyun 		 * cannot deliver this message to upper layers
50*4882a593Smuzhiyun 		 */
51*4882a593Smuzhiyun 		msglen = get_unaligned_le16(&hd->h_length);
52*4882a593Smuzhiyun 		if (msglen > DEFAULT_BUFFER_SIZE ||
53*4882a593Smuzhiyun 		    msglen < sizeof(struct dlm_header)) {
54*4882a593Smuzhiyun 			log_print("received invalid length header: %u from node %d, will abort message parsing",
55*4882a593Smuzhiyun 				  msglen, nodeid);
56*4882a593Smuzhiyun 			return -EBADMSG;
57*4882a593Smuzhiyun 		}
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 		/* caller will take care that leftover
60*4882a593Smuzhiyun 		 * will be parsed next call with more data
61*4882a593Smuzhiyun 		 */
62*4882a593Smuzhiyun 		if (msglen > len)
63*4882a593Smuzhiyun 			break;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 		switch (hd->h_cmd) {
66*4882a593Smuzhiyun 		case DLM_MSG:
67*4882a593Smuzhiyun 			if (msglen < sizeof(struct dlm_message)) {
68*4882a593Smuzhiyun 				log_print("dlm msg too small: %u, will skip this message",
69*4882a593Smuzhiyun 					  msglen);
70*4882a593Smuzhiyun 				goto skip;
71*4882a593Smuzhiyun 			}
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 			break;
74*4882a593Smuzhiyun 		case DLM_RCOM:
75*4882a593Smuzhiyun 			if (msglen < sizeof(struct dlm_rcom)) {
76*4882a593Smuzhiyun 				log_print("dlm rcom msg too small: %u, will skip this message",
77*4882a593Smuzhiyun 					  msglen);
78*4882a593Smuzhiyun 				goto skip;
79*4882a593Smuzhiyun 			}
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 			break;
82*4882a593Smuzhiyun 		default:
83*4882a593Smuzhiyun 			log_print("unsupported h_cmd received: %u, will skip this message",
84*4882a593Smuzhiyun 				  hd->h_cmd);
85*4882a593Smuzhiyun 			goto skip;
86*4882a593Smuzhiyun 		}
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 		/* for aligned memory access, we just copy current message
89*4882a593Smuzhiyun 		 * to begin of the buffer which contains already parsed buffer
90*4882a593Smuzhiyun 		 * data and should provide align access for upper layers
91*4882a593Smuzhiyun 		 * because the start address of the buffer has a aligned
92*4882a593Smuzhiyun 		 * address. This memmove can be removed when the upperlayer
93*4882a593Smuzhiyun 		 * is capable of unaligned memory access.
94*4882a593Smuzhiyun 		 */
95*4882a593Smuzhiyun 		memmove(buf, ptr, msglen);
96*4882a593Smuzhiyun 		dlm_receive_buffer((union dlm_packet *)buf, nodeid);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun skip:
99*4882a593Smuzhiyun 		ret += msglen;
100*4882a593Smuzhiyun 		len -= msglen;
101*4882a593Smuzhiyun 		ptr += msglen;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	return ret;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107