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.

188 lines
4.6 KiB

  1. From 4c6e7c0fcc6da66cf81c0714bf907762194eedf2 Mon Sep 17 00:00:00 2001
  2. From: Chris Leech <cleech@redhat.com>
  3. Date: Tue, 13 Aug 2013 11:34:31 -0700
  4. Subject: [PATCH] idbm_rec_write, seperate old and new style writes
  5. Duplicates a small bit of code, but easier to understand and extened.
  6. ---
  7. usr/idbm.c | 129 +++++++++++++++++++++++++++++++++++------------------
  8. 1 file changed, 86 insertions(+), 43 deletions(-)
  9. --- a/usr/idbm.c
  10. +++ b/usr/idbm.c
  11. @@ -2130,12 +2130,7 @@ mkdir_portal:
  12. return f;
  13. }
  14. -/*
  15. - * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
  16. - * to be holt by the caller, this will avoid overwriting each other in
  17. - * case of updating(read-modify-write) the recs in parallel.
  18. - */
  19. -static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
  20. +static int idbm_rec_write_new(node_rec_t *rec)
  21. {
  22. struct stat statb;
  23. FILE *f;
  24. @@ -2148,39 +2143,8 @@ static int idbm_rec_write(node_rec_t *re
  25. return ISCSI_ERR_NOMEM;
  26. }
  27. - snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
  28. - if (access(portal, F_OK) != 0) {
  29. - if (mkdir(portal, 0660) != 0) {
  30. - log_error("Could not make %s: %s", portal,
  31. - strerror(errno));
  32. - rc = ISCSI_ERR_IDBM;
  33. - goto free_portal;
  34. - }
  35. - }
  36. -
  37. - snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
  38. - if (access(portal, F_OK) != 0) {
  39. - if (mkdir(portal, 0660) != 0) {
  40. - log_error("Could not make %s: %s", portal,
  41. - strerror(errno));
  42. - rc = ISCSI_ERR_IDBM;
  43. - goto free_portal;
  44. - }
  45. - }
  46. -
  47. snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
  48. rec->name, rec->conn[0].address, rec->conn[0].port);
  49. - log_debug(5, "Looking for config file %s", portal);
  50. -
  51. - if (!disable_lock) {
  52. - rc = idbm_lock();
  53. - if (rc)
  54. - goto free_portal;
  55. - }
  56. -
  57. - if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
  58. - /* drop down to old style portal as config */
  59. - goto open_conf;
  60. rc = stat(portal, &statb);
  61. if (rc) {
  62. @@ -2201,11 +2165,11 @@ static int idbm_rec_write(node_rec_t *re
  63. log_error("Could not convert %s: %s", portal,
  64. strerror(errno));
  65. rc = ISCSI_ERR_IDBM;
  66. - goto unlock;
  67. + goto free_portal;
  68. }
  69. } else {
  70. rc = ISCSI_ERR_INVAL;
  71. - goto unlock;
  72. + goto free_portal;
  73. }
  74. mkdir_portal:
  75. @@ -2216,24 +2180,103 @@ mkdir_portal:
  76. log_error("Could not make dir %s: %s",
  77. portal, strerror(errno));
  78. rc = ISCSI_ERR_IDBM;
  79. - goto unlock;
  80. + goto free_portal;
  81. }
  82. }
  83. snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
  84. rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
  85. rec->iface.name);
  86. -open_conf:
  87. +
  88. f = fopen(portal, "w");
  89. if (!f) {
  90. log_error("Could not open %s: %s", portal, strerror(errno));
  91. rc = ISCSI_ERR_IDBM;
  92. - goto unlock;
  93. + goto free_portal;
  94. + }
  95. +
  96. + idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
  97. + fclose(f);
  98. +free_portal:
  99. + free(portal);
  100. + return rc;
  101. +}
  102. +
  103. +static int idbm_rec_write_old(node_rec_t *rec)
  104. +{
  105. + FILE *f;
  106. + char *portal;
  107. + int rc = 0;
  108. +
  109. + portal = malloc(PATH_MAX);
  110. + if (!portal) {
  111. + log_error("Could not alloc portal");
  112. + return ISCSI_ERR_NOMEM;
  113. }
  114. + snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
  115. + rec->name, rec->conn[0].address, rec->conn[0].port);
  116. + f = fopen(portal, "w");
  117. + if (!f) {
  118. + log_error("Could not open %s: %sd", portal, strerror(errno));
  119. + rc = ISCSI_ERR_IDBM;
  120. + goto free_portal;
  121. + }
  122. idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
  123. fclose(f);
  124. -unlock:
  125. +free_portal:
  126. + free(portal);
  127. + return rc;
  128. +}
  129. +
  130. +/*
  131. + * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
  132. + * to be holt by the caller, this will avoid overwriting each other in
  133. + * case of updating(read-modify-write) the recs in parallel.
  134. + */
  135. +static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
  136. +{
  137. + char *portal;
  138. + int rc = 0;
  139. +
  140. + portal = malloc(PATH_MAX);
  141. + if (!portal) {
  142. + log_error("Could not alloc portal");
  143. + return ISCSI_ERR_NOMEM;
  144. + }
  145. +
  146. + snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
  147. + if (access(portal, F_OK) != 0) {
  148. + if (mkdir(portal, 0660) != 0) {
  149. + log_error("Could not make %s: %s", portal,
  150. + strerror(errno));
  151. + rc = ISCSI_ERR_IDBM;
  152. + goto free_portal;
  153. + }
  154. + }
  155. +
  156. + snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
  157. + if (access(portal, F_OK) != 0) {
  158. + if (mkdir(portal, 0660) != 0) {
  159. + log_error("Could not make %s: %s", portal,
  160. + strerror(errno));
  161. + rc = ISCSI_ERR_IDBM;
  162. + goto free_portal;
  163. + }
  164. + }
  165. +
  166. + if (!disable_lock) {
  167. + rc = idbm_lock();
  168. + if (rc)
  169. + goto free_portal;
  170. + }
  171. +
  172. + if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
  173. + /* old style portal as config */
  174. + rc = idbm_rec_write_old(rec);
  175. + else
  176. + rc = idbm_rec_write_new(rec);
  177. +
  178. if (!disable_lock)
  179. idbm_unlock();
  180. free_portal: