Simplified distributed block storage with strong consistency, like in Ceph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
2.5 KiB

  1. // Copyright (c) Vitaliy Filippov, 2019+
  2. // License: VNPL-1.0 or GNU GPL-2.0+ (see README.md for details)
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include "rw_blocking.h"
  7. int read_blocking(int fd, void *read_buf, size_t remaining)
  8. {
  9. size_t done = 0;
  10. while (done < remaining)
  11. {
  12. size_t r = read(fd, read_buf, remaining-done);
  13. if (r <= 0)
  14. {
  15. if (!errno)
  16. {
  17. // EOF
  18. return done;
  19. }
  20. else if (errno != EAGAIN && errno != EPIPE)
  21. {
  22. perror("read");
  23. exit(1);
  24. }
  25. continue;
  26. }
  27. done += r;
  28. read_buf += r;
  29. }
  30. return done;
  31. }
  32. int write_blocking(int fd, void *write_buf, size_t remaining)
  33. {
  34. size_t done = 0;
  35. while (done < remaining)
  36. {
  37. size_t r = write(fd, write_buf, remaining-done);
  38. if (r < 0)
  39. {
  40. if (errno != EAGAIN && errno != EPIPE)
  41. {
  42. perror("write");
  43. exit(1);
  44. }
  45. continue;
  46. }
  47. done += r;
  48. write_buf += r;
  49. }
  50. return done;
  51. }
  52. int readv_blocking(int fd, iovec *iov, int iovcnt)
  53. {
  54. int v = 0;
  55. int done = 0;
  56. while (v < iovcnt)
  57. {
  58. ssize_t r = readv(fd, iov, iovcnt);
  59. if (r < 0)
  60. {
  61. if (errno != EAGAIN && errno != EPIPE)
  62. {
  63. perror("writev");
  64. exit(1);
  65. }
  66. continue;
  67. }
  68. while (v < iovcnt)
  69. {
  70. if (iov[v].iov_len > r)
  71. {
  72. iov[v].iov_len -= r;
  73. iov[v].iov_base += r;
  74. break;
  75. }
  76. else
  77. {
  78. v++;
  79. }
  80. }
  81. done += r;
  82. }
  83. return done;
  84. }
  85. int writev_blocking(int fd, iovec *iov, int iovcnt)
  86. {
  87. int v = 0;
  88. int done = 0;
  89. while (v < iovcnt)
  90. {
  91. ssize_t r = writev(fd, iov, iovcnt);
  92. if (r < 0)
  93. {
  94. if (errno != EAGAIN && errno != EPIPE)
  95. {
  96. perror("writev");
  97. exit(1);
  98. }
  99. continue;
  100. }
  101. while (v < iovcnt)
  102. {
  103. if (iov[v].iov_len > r)
  104. {
  105. iov[v].iov_len -= r;
  106. iov[v].iov_base += r;
  107. break;
  108. }
  109. else
  110. {
  111. v++;
  112. }
  113. }
  114. done += r;
  115. }
  116. return done;
  117. }