midcomms.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. struct dlm_header dlm;
  58. } __tmp;
  59. struct dlm_header *msg = &__tmp.dlm;
  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(msg, base, offset, sizeof(struct dlm_header),
  69. limit);
  70. msglen = le16_to_cpu(msg->h_length);
  71. lockspace = msg->h_lockspace;
  72. err = -EINVAL;
  73. if (msglen < sizeof(struct dlm_header))
  74. break;
  75. err = -E2BIG;
  76. if (msglen > dlm_config.ci_buffer_size) {
  77. log_print("message size %d from %d too big, buf len %d",
  78. msglen, nodeid, len);
  79. break;
  80. }
  81. err = 0;
  82. /* If only part of the full message is contained in this
  83. buffer, then do nothing and wait for lowcomms to call
  84. us again later with more data. We return 0 meaning
  85. we've consumed none of the input buffer. */
  86. if (msglen > len)
  87. break;
  88. /* Allocate a larger temp buffer if the full message won't fit
  89. in the buffer on the stack (which should work for most
  90. ordinary messages). */
  91. if (msglen > DLM_INBUF_LEN && msg == &__tmp.dlm) {
  92. msg = kmalloc(dlm_config.ci_buffer_size, GFP_KERNEL);
  93. if (msg == NULL)
  94. return ret;
  95. }
  96. copy_from_cb(msg, base, offset, msglen, limit);
  97. BUG_ON(lockspace != msg->h_lockspace);
  98. ret += msglen;
  99. offset += msglen;
  100. offset &= (limit - 1);
  101. len -= msglen;
  102. dlm_receive_buffer(msg, nodeid);
  103. }
  104. if (msg != &__tmp.dlm)
  105. kfree(msg);
  106. return err ? err : ret;
  107. }