midcomms.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2008 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. * midcomms.c
  15. *
  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 interested
  20. * part of the locking mechanism.
  21. *
  22. * It also takes messages from the locking layer, formats them
  23. * into packets and sends them to the comms layer.
  24. */
  25. #include "dlm_internal.h"
  26. #include "lowcomms.h"
  27. #include "config.h"
  28. #include "lock.h"
  29. #include "midcomms.h"
  30. static void copy_from_cb(void *dst, const void *base, unsigned offset,
  31. unsigned len, unsigned limit)
  32. {
  33. unsigned copy = len;
  34. if ((copy + offset) > limit)
  35. copy = limit - offset;
  36. memcpy(dst, base + offset, copy);
  37. len -= copy;
  38. if (len)
  39. memcpy(dst + copy, base, len);
  40. }
  41. /*
  42. * Called from the low-level comms layer to process a buffer of
  43. * commands.
  44. *
  45. * Only complete messages are processed here, any "spare" bytes from
  46. * the end of a buffer are saved and tacked onto the front of the next
  47. * message that comes in. I doubt this will happen very often but we
  48. * need to be able to cope with it and I don't want the task to be waiting
  49. * for packets to come in when there is useful work to be done.
  50. */
  51. int dlm_process_incoming_buffer(int nodeid, const void *base,
  52. unsigned offset, unsigned len, unsigned limit)
  53. {
  54. union {
  55. unsigned char __buf[DLM_INBUF_LEN];
  56. /* this is to force proper alignment on some arches */
  57. union dlm_packet p;
  58. } __tmp;
  59. union dlm_packet *p = &__tmp.p;
  60. int ret = 0;
  61. int err = 0;
  62. uint16_t msglen;
  63. uint32_t lockspace;
  64. while (len > sizeof(struct dlm_header)) {
  65. /* Copy just the header to check the total length. The
  66. message may wrap around the end of the buffer back to the
  67. start, so we need to use a temp buffer and copy_from_cb. */
  68. copy_from_cb(p, base, offset, sizeof(struct dlm_header),
  69. limit);
  70. msglen = le16_to_cpu(p->header.h_length);
  71. lockspace = p->header.h_lockspace;
  72. err = -EINVAL;
  73. if (msglen < sizeof(struct dlm_header))
  74. break;
  75. if (p->header.h_cmd == DLM_MSG) {
  76. if (msglen < sizeof(struct dlm_message))
  77. break;
  78. } else {
  79. if (msglen < sizeof(struct dlm_rcom))
  80. break;
  81. }
  82. err = -E2BIG;
  83. if (msglen > dlm_config.ci_buffer_size) {
  84. log_print("message size %d from %d too big, buf len %d",
  85. msglen, nodeid, len);
  86. break;
  87. }
  88. err = 0;
  89. /* If only part of the full message is contained in this
  90. buffer, then do nothing and wait for lowcomms to call
  91. us again later with more data. We return 0 meaning
  92. we've consumed none of the input buffer. */
  93. if (msglen > len)
  94. break;
  95. /* Allocate a larger temp buffer if the full message won't fit
  96. in the buffer on the stack (which should work for most
  97. ordinary messages). */
  98. if (msglen > sizeof(__tmp) && p == &__tmp.p) {
  99. p = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS);
  100. if (p == NULL)
  101. return ret;
  102. }
  103. copy_from_cb(p, base, offset, msglen, limit);
  104. BUG_ON(lockspace != p->header.h_lockspace);
  105. ret += msglen;
  106. offset += msglen;
  107. offset &= (limit - 1);
  108. len -= msglen;
  109. dlm_receive_buffer(p, nodeid);
  110. }
  111. if (p != &__tmp.p)
  112. kfree(p);
  113. return err ? err : ret;
  114. }