| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 1 | /****************************************************************************** | 
 | 2 | ******************************************************************************* | 
 | 3 | ** | 
 | 4 | **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved. | 
| Fabio M. Di Nitto | e7847d3 | 2008-01-30 10:56:42 -0600 | [diff] [blame] | 5 | **  Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved. | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 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" | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 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 | { | 
| Fabio M. Di Nitto | e7847d3 | 2008-01-30 10:56:42 -0600 | [diff] [blame] | 61 | 	union { | 
 | 62 | 		unsigned char __buf[DLM_INBUF_LEN]; | 
 | 63 | 		/* this is to force proper alignment on some arches */ | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 64 | 		union dlm_packet p; | 
| Fabio M. Di Nitto | e7847d3 | 2008-01-30 10:56:42 -0600 | [diff] [blame] | 65 | 	} __tmp; | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 66 | 	union dlm_packet *p = &__tmp.p; | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 67 | 	int ret = 0; | 
 | 68 | 	int err = 0; | 
 | 69 | 	uint16_t msglen; | 
 | 70 | 	uint32_t lockspace; | 
 | 71 |  | 
 | 72 | 	while (len > sizeof(struct dlm_header)) { | 
 | 73 |  | 
 | 74 | 		/* Copy just the header to check the total length.  The | 
 | 75 | 		   message may wrap around the end of the buffer back to the | 
 | 76 | 		   start, so we need to use a temp buffer and copy_from_cb. */ | 
 | 77 |  | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 78 | 		copy_from_cb(p, base, offset, sizeof(struct dlm_header), | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 79 | 			     limit); | 
 | 80 |  | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 81 | 		msglen = le16_to_cpu(p->header.h_length); | 
 | 82 | 		lockspace = p->header.h_lockspace; | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 83 |  | 
 | 84 | 		err = -EINVAL; | 
 | 85 | 		if (msglen < sizeof(struct dlm_header)) | 
 | 86 | 			break; | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 87 | 		if (p->header.h_cmd == DLM_MSG) { | 
 | 88 | 			if (msglen < sizeof(struct dlm_message)) | 
 | 89 | 				break; | 
 | 90 | 		} else { | 
 | 91 | 			if (msglen < sizeof(struct dlm_rcom)) | 
 | 92 | 				break; | 
 | 93 | 		} | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 94 | 		err = -E2BIG; | 
| David Teigland | 68c817a | 2007-01-09 09:41:48 -0600 | [diff] [blame] | 95 | 		if (msglen > dlm_config.ci_buffer_size) { | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 96 | 			log_print("message size %d from %d too big, buf len %d", | 
 | 97 | 				  msglen, nodeid, len); | 
 | 98 | 			break; | 
 | 99 | 		} | 
 | 100 | 		err = 0; | 
 | 101 |  | 
 | 102 | 		/* If only part of the full message is contained in this | 
 | 103 | 		   buffer, then do nothing and wait for lowcomms to call | 
 | 104 | 		   us again later with more data.  We return 0 meaning | 
 | 105 | 		   we've consumed none of the input buffer. */ | 
 | 106 |  | 
 | 107 | 		if (msglen > len) | 
 | 108 | 			break; | 
 | 109 |  | 
 | 110 | 		/* Allocate a larger temp buffer if the full message won't fit | 
 | 111 | 		   in the buffer on the stack (which should work for most | 
 | 112 | 		   ordinary messages). */ | 
 | 113 |  | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 114 | 		if (msglen > sizeof(__tmp) && p == &__tmp.p) { | 
| Steven Whitehouse | d6d7b70 | 2008-11-12 16:49:48 -0600 | [diff] [blame] | 115 | 			p = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS); | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 116 | 			if (p == NULL) | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 117 | 				return ret; | 
 | 118 | 		} | 
 | 119 |  | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 120 | 		copy_from_cb(p, base, offset, msglen, limit); | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 121 |  | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 122 | 		BUG_ON(lockspace != p->header.h_lockspace); | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 123 |  | 
 | 124 | 		ret += msglen; | 
 | 125 | 		offset += msglen; | 
 | 126 | 		offset &= (limit - 1); | 
 | 127 | 		len -= msglen; | 
 | 128 |  | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 129 | 		dlm_receive_buffer(p, nodeid); | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 130 | 	} | 
 | 131 |  | 
| Al Viro | eef7d73 | 2008-01-25 00:58:46 -0500 | [diff] [blame] | 132 | 	if (p != &__tmp.p) | 
 | 133 | 		kfree(p); | 
| David Teigland | e7fd417 | 2006-01-18 09:30:29 +0000 | [diff] [blame] | 134 |  | 
 | 135 | 	return err ? err : ret; | 
 | 136 | } | 
 | 137 |  |