midcomms.c 3.8 KB

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