midcomms.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2007 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. unsigned char __tmp[DLM_INBUF_LEN];
  55. struct dlm_header *msg = (struct dlm_header *) __tmp;
  56. int ret = 0;
  57. int err = 0;
  58. uint16_t msglen;
  59. uint32_t lockspace;
  60. while (len > sizeof(struct dlm_header)) {
  61. /* Copy just the header to check the total length. The
  62. message may wrap around the end of the buffer back to the
  63. start, so we need to use a temp buffer and copy_from_cb. */
  64. copy_from_cb(msg, base, offset, sizeof(struct dlm_header),
  65. limit);
  66. msglen = le16_to_cpu(msg->h_length);
  67. lockspace = msg->h_lockspace;
  68. err = -EINVAL;
  69. if (msglen < sizeof(struct dlm_header))
  70. break;
  71. err = -E2BIG;
  72. if (msglen > dlm_config.ci_buffer_size) {
  73. log_print("message size %d from %d too big, buf len %d",
  74. msglen, nodeid, len);
  75. break;
  76. }
  77. err = 0;
  78. /* If only part of the full message is contained in this
  79. buffer, then do nothing and wait for lowcomms to call
  80. us again later with more data. We return 0 meaning
  81. we've consumed none of the input buffer. */
  82. if (msglen > len)
  83. break;
  84. /* Allocate a larger temp buffer if the full message won't fit
  85. in the buffer on the stack (which should work for most
  86. ordinary messages). */
  87. if (msglen > sizeof(__tmp) &&
  88. msg == (struct dlm_header *) __tmp) {
  89. msg = kmalloc(dlm_config.ci_buffer_size, GFP_KERNEL);
  90. if (msg == NULL)
  91. return ret;
  92. }
  93. copy_from_cb(msg, base, offset, msglen, limit);
  94. BUG_ON(lockspace != msg->h_lockspace);
  95. ret += msglen;
  96. offset += msglen;
  97. offset &= (limit - 1);
  98. len -= msglen;
  99. dlm_receive_buffer(msg, nodeid);
  100. }
  101. if (msg != (struct dlm_header *) __tmp)
  102. kfree(msg);
  103. return err ? err : ret;
  104. }