xref: /openbmc/linux/fs/dlm/midcomms.c (revision 04c71976)
1 /******************************************************************************
2 *******************************************************************************
3 **
4 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5 **  Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
6 **
7 **  This copyrighted material is made available to anyone wishing to use,
8 **  modify, copy, or redistribute it subject to the terms and conditions
9 **  of the GNU General Public License v.2.
10 **
11 *******************************************************************************
12 ******************************************************************************/
13 
14 /*
15  * midcomms.c
16  *
17  * This is the appallingly named "mid-level" comms layer.
18  *
19  * Its purpose is to take packets from the "real" comms layer,
20  * split them up into packets and pass them to the interested
21  * part of the locking mechanism.
22  *
23  * It also takes messages from the locking layer, formats them
24  * into packets and sends them to the comms layer.
25  */
26 
27 #include "dlm_internal.h"
28 #include "lowcomms.h"
29 #include "config.h"
30 #include "lock.h"
31 #include "midcomms.h"
32 
33 
34 static void copy_from_cb(void *dst, const void *base, unsigned offset,
35 			 unsigned len, unsigned limit)
36 {
37 	unsigned copy = len;
38 
39 	if ((copy + offset) > limit)
40 		copy = limit - offset;
41 	memcpy(dst, base + offset, copy);
42 	len -= copy;
43 	if (len)
44 		memcpy(dst + copy, base, len);
45 }
46 
47 /*
48  * Called from the low-level comms layer to process a buffer of
49  * commands.
50  *
51  * Only complete messages are processed here, any "spare" bytes from
52  * the end of a buffer are saved and tacked onto the front of the next
53  * message that comes in. I doubt this will happen very often but we
54  * need to be able to cope with it and I don't want the task to be waiting
55  * for packets to come in when there is useful work to be done.
56  */
57 
58 int dlm_process_incoming_buffer(int nodeid, const void *base,
59 				unsigned offset, unsigned len, unsigned limit)
60 {
61 	unsigned char __tmp[DLM_INBUF_LEN];
62 	struct dlm_header *msg = (struct dlm_header *) __tmp;
63 	int ret = 0;
64 	int err = 0;
65 	uint16_t msglen;
66 	uint32_t lockspace;
67 
68 	while (len > sizeof(struct dlm_header)) {
69 
70 		/* Copy just the header to check the total length.  The
71 		   message may wrap around the end of the buffer back to the
72 		   start, so we need to use a temp buffer and copy_from_cb. */
73 
74 		copy_from_cb(msg, base, offset, sizeof(struct dlm_header),
75 			     limit);
76 
77 		msglen = le16_to_cpu(msg->h_length);
78 		lockspace = msg->h_lockspace;
79 
80 		err = -EINVAL;
81 		if (msglen < sizeof(struct dlm_header))
82 			break;
83 		err = -E2BIG;
84 		if (msglen > dlm_config.ci_buffer_size) {
85 			log_print("message size %d from %d too big, buf len %d",
86 				  msglen, nodeid, len);
87 			break;
88 		}
89 		err = 0;
90 
91 		/* If only part of the full message is contained in this
92 		   buffer, then do nothing and wait for lowcomms to call
93 		   us again later with more data.  We return 0 meaning
94 		   we've consumed none of the input buffer. */
95 
96 		if (msglen > len)
97 			break;
98 
99 		/* Allocate a larger temp buffer if the full message won't fit
100 		   in the buffer on the stack (which should work for most
101 		   ordinary messages). */
102 
103 		if (msglen > sizeof(__tmp) &&
104 		    msg == (struct dlm_header *) __tmp) {
105 			msg = kmalloc(dlm_config.ci_buffer_size, GFP_KERNEL);
106 			if (msg == NULL)
107 				return ret;
108 		}
109 
110 		copy_from_cb(msg, base, offset, msglen, limit);
111 
112 		BUG_ON(lockspace != msg->h_lockspace);
113 
114 		ret += msglen;
115 		offset += msglen;
116 		offset &= (limit - 1);
117 		len -= msglen;
118 
119 		dlm_receive_buffer(msg, nodeid);
120 	}
121 
122 	if (msg != (struct dlm_header *) __tmp)
123 		kfree(msg);
124 
125 	return err ? err : ret;
126 }
127 
128