/******************************************************************************1*******************************************************************************2**3** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.4** Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.5**6** This copyrighted material is made available to anyone wishing to use,7** modify, copy, or redistribute it subject to the terms and conditions8** of the GNU General Public License v.2.9**10*******************************************************************************11******************************************************************************/1213/*14* midcomms.c15*16* This is the appallingly named "mid-level" comms layer.17*18* Its purpose is to take packets from the "real" comms layer,19* split them up into packets and pass them to the interested20* part of the locking mechanism.21*22* It also takes messages from the locking layer, formats them23* into packets and sends them to the comms layer.24*/2526#include "dlm_internal.h"27#include "lowcomms.h"28#include "config.h"29#include "lock.h"30#include "midcomms.h"313233static void copy_from_cb(void *dst, const void *base, unsigned offset,34unsigned len, unsigned limit)35{36unsigned copy = len;3738if ((copy + offset) > limit)39copy = limit - offset;40memcpy(dst, base + offset, copy);41len -= copy;42if (len)43memcpy(dst + copy, base, len);44}4546/*47* Called from the low-level comms layer to process a buffer of48* commands.49*50* Only complete messages are processed here, any "spare" bytes from51* the end of a buffer are saved and tacked onto the front of the next52* message that comes in. I doubt this will happen very often but we53* need to be able to cope with it and I don't want the task to be waiting54* for packets to come in when there is useful work to be done.55*/5657int dlm_process_incoming_buffer(int nodeid, const void *base,58unsigned offset, unsigned len, unsigned limit)59{60union {61unsigned char __buf[DLM_INBUF_LEN];62/* this is to force proper alignment on some arches */63union dlm_packet p;64} __tmp;65union dlm_packet *p = &__tmp.p;66int ret = 0;67int err = 0;68uint16_t msglen;69uint32_t lockspace;7071while (len > sizeof(struct dlm_header)) {7273/* Copy just the header to check the total length. The74message may wrap around the end of the buffer back to the75start, so we need to use a temp buffer and copy_from_cb. */7677copy_from_cb(p, base, offset, sizeof(struct dlm_header),78limit);7980msglen = le16_to_cpu(p->header.h_length);81lockspace = p->header.h_lockspace;8283err = -EINVAL;84if (msglen < sizeof(struct dlm_header))85break;86if (p->header.h_cmd == DLM_MSG) {87if (msglen < sizeof(struct dlm_message))88break;89} else {90if (msglen < sizeof(struct dlm_rcom))91break;92}93err = -E2BIG;94if (msglen > dlm_config.ci_buffer_size) {95log_print("message size %d from %d too big, buf len %d",96msglen, nodeid, len);97break;98}99err = 0;100101/* If only part of the full message is contained in this102buffer, then do nothing and wait for lowcomms to call103us again later with more data. We return 0 meaning104we've consumed none of the input buffer. */105106if (msglen > len)107break;108109/* Allocate a larger temp buffer if the full message won't fit110in the buffer on the stack (which should work for most111ordinary messages). */112113if (msglen > sizeof(__tmp) && p == &__tmp.p) {114p = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS);115if (p == NULL)116return ret;117}118119copy_from_cb(p, base, offset, msglen, limit);120121BUG_ON(lockspace != p->header.h_lockspace);122123ret += msglen;124offset += msglen;125offset &= (limit - 1);126len -= msglen;127128dlm_receive_buffer(p, nodeid);129}130131if (p != &__tmp.p)132kfree(p);133134return err ? err : ret;135}136137138139